From fd4d683baf4344b058090e0b0203929103cacedf Mon Sep 17 00:00:00 2001 From: Elena Kuleshov Date: Mon, 8 Apr 2019 22:08:29 -0400 Subject: Query CatalogDB for WorkflowSpecifications Query CatalogDB for WorkflowSpecifications fron APIH Change-Id: I5eef5ba9b49c0a7420ef93a5425121bfe7225b1b Issue-ID: SO-1727 Signed-off-by: Kuleshov, Elena --- .../WorkflowSpecificationsHandler.java | 148 ++++++++++++++++++++- 1 file changed, 141 insertions(+), 7 deletions(-) (limited to 'mso-api-handlers/mso-api-handler-infra/src/main/java/org') diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/WorkflowSpecificationsHandler.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/WorkflowSpecificationsHandler.java index bc4f389469..c480bdfbd3 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/WorkflowSpecificationsHandler.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/WorkflowSpecificationsHandler.java @@ -19,8 +19,11 @@ */ package org.onap.so.apihandlerinfra; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import javax.transaction.Transactional; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -32,7 +35,19 @@ import org.onap.so.apihandler.common.ErrorNumbers; import org.onap.so.apihandler.common.ResponseBuilder; import org.onap.so.apihandlerinfra.exceptions.ValidateException; import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.ActivitySequence; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.ArtifactInfo; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.Validation; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowInputParameter; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecification; +import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecificationList; import org.onap.so.apihandlerinfra.workflowspecificationbeans.WorkflowSpecifications; +import org.onap.so.db.catalog.beans.ActivitySpec; +import org.onap.so.db.catalog.beans.ActivitySpecUserParameters; +import org.onap.so.db.catalog.beans.UserParameters; +import org.onap.so.db.catalog.beans.Workflow; +import org.onap.so.db.catalog.beans.WorkflowActivitySpecSequence; +import org.onap.so.db.catalog.client.CatalogDbClient; import org.onap.so.logger.ErrorCode; import org.onap.so.logger.MessageEnum; import org.springframework.beans.factory.annotation.Autowired; @@ -51,11 +66,17 @@ public class WorkflowSpecificationsHandler { @Autowired private ResponseBuilder builder; + @Autowired + private CatalogDbClient catalogDbClient; + + private static final String ARTIFACT_TYPE_WORKFLOW = "workflow"; + @Path("/{version:[vV]1}/workflows") @GET @ApiOperation(value = "Finds Workflow Specifications", response = Response.class) @Transactional - public Response queryFilters(@QueryParam("vnfModelVersionId") String vnfModelVersionId, + + public Response queryWorkflowSpecifications(@QueryParam("vnfModelVersionId") String vnfModelVersionId, @PathParam("version") String version) throws Exception { String apiVersion = version.substring(1); @@ -63,10 +84,8 @@ public class WorkflowSpecificationsHandler { ObjectMapper mapper1 = new ObjectMapper(); mapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - // Replace with Catalog DB Query - WorkflowSpecifications workflowSpecifications = mapper1.readValue( - new String(Files.readAllBytes(Paths.get("src/test/resources/__files/WorkflowSpecifications.json"))), - WorkflowSpecifications.class); + List workflows = catalogDbClient.findWorkflowByModelUUID(vnfModelVersionId); + WorkflowSpecifications workflowSpecifications = mapWorkflowsToWorkflowSpecifications(workflows); String jsonResponse = null; try { @@ -85,4 +104,119 @@ public class WorkflowSpecificationsHandler { return builder.buildResponse(HttpStatus.SC_OK, "", jsonResponse, apiVersion); } + + protected WorkflowSpecifications mapWorkflowsToWorkflowSpecifications(List workflows) { + if (workflows == null || workflows.size() == 0) { + return null; + } + WorkflowSpecifications workflowSpecifications = new WorkflowSpecifications(); + List workflowSpecificationList = new ArrayList(); + + for (Workflow workflow : workflows) { + WorkflowSpecificationList workflowSpecificationListItem = new WorkflowSpecificationList(); + WorkflowSpecification workflowSpecification = new WorkflowSpecification(); + workflowSpecification.setArtifactInfo(buildArtifactInfo(workflow)); + workflowSpecification.setActivitySequence(buildActivitySequence(workflow)); + workflowSpecification.setWorkflowInputParameters(buildWorkflowInputParameters(workflow)); + workflowSpecificationListItem.setWorkflowSpecification(workflowSpecification); + workflowSpecificationList.add(workflowSpecificationListItem); + } + workflowSpecifications.setWorkflowSpecificationList(workflowSpecificationList); + return workflowSpecifications; + } + + private ArtifactInfo buildArtifactInfo(Workflow workflow) { + ArtifactInfo artifactInfo = new ArtifactInfo(); + artifactInfo.setArtifactType(ARTIFACT_TYPE_WORKFLOW); + artifactInfo.setArtifactUuid(workflow.getArtifactUUID()); + artifactInfo.setArtifactName(workflow.getArtifactName()); + if (workflow.getVersion() != null) { + artifactInfo.setArtifactVersion(workflow.getVersion().toString()); + } + artifactInfo.setArtifactDescription(workflow.getDescription()); + artifactInfo.setWorkflowName(workflow.getName()); + artifactInfo.setOperationName(workflow.getOperationName()); + artifactInfo.setWorkflowSource(workflow.getSource()); + artifactInfo.setWorkflowResourceTarget(workflow.getResourceTarget()); + return artifactInfo; + } + + private List buildActivitySequence(Workflow workflow) { + List workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence(); + if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) { + return null; + } + List activitySequences = new ArrayList(); + for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) { + if (workflowActivitySpecSequence != null) { + ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec(); + if (activitySpec != null) { + ActivitySequence activitySequence = new ActivitySequence(); + activitySequence.setName(activitySpec.getName()); + activitySequence.setDescription(activitySpec.getDescription()); + activitySequences.add(activitySequence); + } + } + } + return activitySequences; + } + + private List buildWorkflowInputParameters(Workflow workflow) { + List workflowActivitySpecSequences = workflow.getWorkflowActivitySpecSequence(); + if (workflowActivitySpecSequences == null || workflowActivitySpecSequences.size() == 0) { + return null; + } + Map workflowInputParameterMap = new HashMap(); + for (WorkflowActivitySpecSequence workflowActivitySpecSequence : workflowActivitySpecSequences) { + if (workflowActivitySpecSequence != null) { + ActivitySpec activitySpec = workflowActivitySpecSequence.getActivitySpec(); + if (activitySpec != null) { + List activitySpecUserParameters = + activitySpec.getActivitySpecUserParameters(); + if (activitySpecUserParameters != null && activitySpecUserParameters.size() != 0) { + for (ActivitySpecUserParameters activitySpecUserParameter : activitySpecUserParameters) { + UserParameters userParameter = activitySpecUserParameter.getUserParameters(); + if (userParameter != null) { + WorkflowInputParameter workflowInputParameter = + buildWorkflowInputParameter(userParameter); + workflowInputParameterMap.put(userParameter.getName(), workflowInputParameter); + } + } + } + } + } + } + + if (workflowInputParameterMap.size() == 0) { + return null; + } + List workflowInputParameterList = + workflowInputParameterMap.values().stream().collect(Collectors.toList()); + return workflowInputParameterList; + } + + private WorkflowInputParameter buildWorkflowInputParameter(UserParameters userParameter) { + WorkflowInputParameter workflowInputParameter = new WorkflowInputParameter(); + workflowInputParameter.setLabel(userParameter.getLabel()); + workflowInputParameter.setInputType(userParameter.getType()); + workflowInputParameter.setRequired(userParameter.getIsRequried()); + workflowInputParameter.setSoFieldName(userParameter.getName()); + workflowInputParameter.setSoPayloadLocation(userParameter.getPayloadLocation()); + workflowInputParameter.setValidation(buildValidationList(userParameter)); + return workflowInputParameter; + } + + private List buildValidationList(UserParameters userParameter) { + List validationList = null; + if (userParameter.getMaxLength() != null || userParameter.getAllowableChars() != null) { + validationList = new ArrayList(); + Validation validation = new Validation(); + if (userParameter.getMaxLength() != null) { + validation.setMaxLength(userParameter.getMaxLength().toString()); + } + validation.setAllowableChars(userParameter.getAllowableChars()); + validationList.add(validation); + } + return validationList; + } } -- cgit 1.2.3-korg