Commit b4d6d544 authored by Bhushan Rade's avatar Bhushan Rade
Browse files

system dag ibm impl

parent b1bac701
......@@ -19,7 +19,7 @@
<properties>
<!-- <spring-boot.repackage.skip>true</spring-boot.repackage.skip> -->
<osdu.ibmcore.version>0.9.0</osdu.ibmcore.version>
<osdu.ibmcore.version>0.12.0-SNAPSHOT</osdu.ibmcore.version>
<start-class>org.opengroup.osdu.workflow.provider.ibm.WorkflowIBMApplication</start-class>
</properties>
......
......@@ -41,7 +41,7 @@ public class IBMCouchDB {
public Database getDatabase(String tenant, String collectionName) {
String fullyColletionName = ibmCouchDBConfig.getDbNamePrefix() + "-" + tenant + "-" + collectionName;
if (databaseRepoCache.get(fullyColletionName) != null) {
log.info("Cache hit, Database configuration found in cache");
log.info(String.format("Cache hit, Database %s configuration found in cache", fullyColletionName));
return databaseRepoCache.get(fullyColletionName);
}
Database db = null;
......
......@@ -15,6 +15,7 @@ import javax.inject.Inject;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.workflow.exception.WorkflowNotFoundException;
import org.opengroup.osdu.workflow.model.WorkflowMetadata;
import org.opengroup.osdu.workflow.provider.ibm.config.IBMCouchDB;
import org.opengroup.osdu.workflow.provider.ibm.model.WorkflowMetadataDoc;
......@@ -84,7 +85,9 @@ public class WorkflowMetadataRepository implements IWorkflowMetadataRepository {
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unexpected error", "Workflow doesn't exist", e);
}
if(result != null && result.getDocs().isEmpty()) {
throw new AppException(HttpStatus.SC_NOT_FOUND, "workflow not found", String.format("Workflow: %s doesn't exist", workflowName));
final String errorMessage = String.format("Workflow: %s doesn't exist", workflowName);
log.error(errorMessage);
throw new WorkflowNotFoundException(errorMessage);
} else
return result.getDocs().get(0).getWorkflowMetadata();
}
......
......@@ -3,53 +3,139 @@ package org.opengroup.osdu.workflow.provider.ibm.repository;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.workflow.model.WorkflowMetadata;
import org.opengroup.osdu.workflow.provider.ibm.config.IBMCouchDB;
import org.opengroup.osdu.workflow.provider.ibm.model.WorkflowMetadataDoc;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowSystemMetadataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.model.Response;
import com.cloudant.client.api.query.Expression;
import com.cloudant.client.api.query.QueryBuilder;
import com.cloudant.client.api.query.QueryResult;
import com.cloudant.client.org.lightcouch.DocumentConflictException;
import static com.cloudant.client.api.query.Expression.eq;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
@Component
@Slf4j
public class WorkflowSystemMetadataRepository implements IWorkflowSystemMetadataRepository {
/**
* Returns workflow metadata based on workflowName
*
* @param workflowName Name of the workflow for which metadata should be retrieved.
* @return Workflow metadata
*/
@Override
public WorkflowMetadata getSystemWorkflow(String workflowName) {
throw new AppException(HttpStatus.SC_NOT_FOUND, "workflow not found", String.format("Workflow: %s doesn't exist", workflowName));
}
/**
* Creates workflow metadata record in persistence store.
*
* @param workflowMetadata Workflow metadata object to save in persistence store.
* @return Workflow metadata
*/
@Override
public WorkflowMetadata createSystemWorkflow(WorkflowMetadata workflowMetadata) {
return null;
}
/**
* Deletes workflow metadata based on workflowName
*
* @param workflowName Name of the workflow for which metadata should be deleted.
*/
@Override
public void deleteSystemWorkflow(String workflowName) { }
/**
* Get all system workflows metadata based on prefix
*
* @param prefix Name of the system workflow for which metadata should be deleted.
*/
@Override
public List<WorkflowMetadata> getAllSystemWorkflow(String prefix) {
return new ArrayList<>();
}
/**
* Returns workflow metadata based on workflowName
*
* @param workflowName Name of the workflow for which metadata should be retrieved.
* @return Workflow metadata
*/
@Autowired
IBMCouchDB ibmCouchDB;
final private String COLLECTION_NAME="WorkflowSystemMetadata";
@Override
public WorkflowMetadata getSystemWorkflow(String workflowName) {
//throw new AppException(HttpStatus.SC_NOT_FOUND, "workflow not found", String.format("Workflow: %s doesn't exist", workflowName));
Database db = getDatabase();
QueryResult<WorkflowMetadataDoc> result = null;
try {
result = db.query(new QueryBuilder(eq("workflowName", workflowName)).build(), WorkflowMetadataDoc.class);
} catch (Exception e) {
log.error(String.format("Workflow: %s doesn't exist", workflowName), e);
e.printStackTrace();
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unexpected error", "Workflow doesn't exist", e);
}
if(result != null && result.getDocs().isEmpty()) {
throw new AppException(HttpStatus.SC_NOT_FOUND, "workflow not found", String.format("Workflow: %s doesn't exist", workflowName));
} else {
log.info("System workflow found :"+workflowName);
return result.getDocs().get(0).getWorkflowMetadata();
}
}
/**
* Creates workflow metadata record in persistence store.
*
* @param workflowMetadata Workflow metadata object to save in persistence store.
* @return Workflow metadata
*/
@Override
public WorkflowMetadata createSystemWorkflow(WorkflowMetadata workflowMetadata) {
Response response = null;
Database db = getDatabase();
WorkflowMetadataDoc workflowMetadataDoc = new WorkflowMetadataDoc(workflowMetadata);
QueryResult<WorkflowMetadataDoc> result = db.query(new QueryBuilder(eq("workflowName", workflowMetadata.getWorkflowName())).build(), WorkflowMetadataDoc.class);
if(result.getDocs().size()>0) {
throw new AppException(HttpStatus.SC_CONFLICT, "Conflict", String.format("Workflow with name %s already exists", workflowMetadata.getWorkflowName()));
}
try {
response = db.save(workflowMetadataDoc);
} catch (DocumentConflictException e) {
throw new AppException(HttpStatus.SC_CONFLICT, "Conflict", String.format("Workflow with name %s already exists", workflowMetadata.getWorkflowName()));
} catch (Exception e) {
log.error(String.format("System Workflow %s creation failed",workflowMetadata.getWorkflowName()), e);
e.printStackTrace();
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed", "Workflow creation failed", e);
}
if (response.getStatusCode() == 201)
return workflowMetadata;
else
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed..", "Workflow creation failed");
}
/**
* Deletes workflow metadata based on workflowName
*
* @param workflowName Name of the workflow for which metadata should be deleted.
*/
@Override
public void deleteSystemWorkflow(String workflowName) {
Database db = getDatabase();
QueryResult<WorkflowMetadataDoc> result = null;
try {
result = db.query(new QueryBuilder(eq("workflowName", workflowName)).build(), WorkflowMetadataDoc.class);
if(result!=null && result.getDocs().size()>0) {
WorkflowMetadataDoc workflowMetadataDoc = result.getDocs().get(0);
db.remove(workflowMetadataDoc);
}
else {
throw new AppException(HttpStatus.SC_NOT_FOUND, String.format("Workflow: %s doesn't exist", workflowName), "Document deletion failed");
}
} catch (Exception e) {
log.error(String.format("Workflow: %s doesn't exist", workflowName), e);
e.printStackTrace();
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unexpected error", "Workflow doesn't exist", e);
}
}
/**
* Get all system workflows metadata based on prefix
*
* @param prefix Name of the system workflow for which metadata should be deleted.
*/
@Override
public List<WorkflowMetadata> getAllSystemWorkflow(String prefix) {
Database db = getDatabase();
QueryResult<WorkflowMetadataDoc> result = db.query(new QueryBuilder(Expression.regex("workflowName", prefix)).build(), WorkflowMetadataDoc.class);
List<WorkflowMetadata> workflowMetadataList = result.getDocs().stream().map(i -> i.getWorkflowMetadata()).collect(Collectors.toList());
return workflowMetadataList;
}
private Database getDatabase() {
return ibmCouchDB.getDatabase("Shared", COLLECTION_NAME);
}
}
package org.opengroup.osdu.workflow.provider.ibm.service;
import org.opengroup.osdu.workflow.provider.interfaces.IAdminAuthorizationService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class AdminAuthorizationServiceImpl implements IAdminAuthorizationService {
@Override
public boolean isDomainAdminServiceAccount() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Jwt principal = (Jwt) authentication.getPrincipal();
Boolean isRootUser = principal.getClaimAsBoolean("rootUser");
if(isRootUser != null && isRootUser) {
log.debug("logged in user is root user");
return true;
}
log.error("logged in user is not root user.");
return false;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment