From 5648fffeaf625dde2b9207ceae87979082a68409 Mon Sep 17 00:00:00 2001 From: Elena Kuleshov Date: Wed, 19 Sep 2018 14:03:47 -0400 Subject: Workflow Activities Execution Environment Add a license header to ExecuteActivity.java Fix a JUnit by modifying the values for the expected variables. BBInput changes to support CM flows Implement JavaDelegate for activities execution Change-Id: I9cfc51241d414b85441d1a8443a950b97d14b65f Issue-ID: SO-827 Signed-off-by: Elena Kuleshov --- .../servicedecomposition/tasks/BBInputSetup.java | 44 +++++-- .../tasks/BBInputSetupTest.java | 29 +++++ .../GeneralBuildingBlockCMExpected.json | 29 +++++ .../infrastructure/activity/ExecuteActivity.java | 144 +++++++++++++++++++++ .../activity/ExecuteActivityTest.java | 76 +++++++++++ 5 files changed, 313 insertions(+), 9 deletions(-) create mode 100644 bpmn/MSOCommonBPMN/src/test/resources/__files/ExecuteBuildingBlock/GeneralBuildingBlockCMExpected.json create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivityTest.java (limited to 'bpmn') diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetup.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetup.java index 6d5fb2f825..eb4f4ca0d5 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetup.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetup.java @@ -190,14 +190,19 @@ public class BBInputSetup implements JavaDelegate { if(requestDetails == null) { requestDetails = bbInputSetupUtils.getRequestDetails(requestId); } - ModelType modelType = requestDetails.getModelInfo().getModelType(); - if (aLaCarte && modelType.equals(ModelType.service)) { - return this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId); - } else if (aLaCarte && !modelType.equals(ModelType.service)) { - return this.getGBBALaCarteNonService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId, - vnfType); - } else { - return this.getGBBMacro(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId, vnfType); + if (requestDetails.getModelInfo() == null) { + return this.getGBBCM(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId); + } + else { + ModelType modelType = requestDetails.getModelInfo().getModelType(); + if (aLaCarte && modelType.equals(ModelType.service)) { + return this.getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId); + } else if (aLaCarte && !modelType.equals(ModelType.service)) { + return this.getGBBALaCarteNonService(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId, + vnfType); + } else { + return this.getGBBMacro(executeBB, requestDetails, lookupKeyMap, requestAction, resourceId, vnfType); + } } } @@ -236,6 +241,25 @@ public class BBInputSetup implements JavaDelegate { throw new Exception("Could not find relevant information for related Service Instance"); } } + + protected GeneralBuildingBlock getGBBCM(ExecuteBuildingBlock executeBB, + RequestDetails requestDetails, Map lookupKeyMap, String requestAction, + String resourceId) throws Exception { + ServiceInstance serviceInstance = new ServiceInstance(); + String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID); + serviceInstance.setServiceInstanceId(serviceInstanceId); + + List genericVnfs = serviceInstance.getVnfs(); + + String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID); + org.onap.aai.domain.yang.GenericVnf aaiGenericVnf = bbInputSetupUtils.getAAIGenericVnf(vnfId); + + GenericVnf genericVnf = this.mapperLayer.mapAAIGenericVnfIntoGenericVnf(aaiGenericVnf); + genericVnfs.add(genericVnf); + + return this.populateGBBWithSIAndAdditionalInfo(requestDetails, serviceInstance, executeBB, requestAction, new Customer()); + + } protected void populateObjectsOnAssignAndCreateFlows(RequestDetails requestDetails, Service service, String bbName, ServiceInstance serviceInstance, Map lookupKeyMap, String resourceId, String vnfType) @@ -790,7 +814,9 @@ public class BBInputSetup implements JavaDelegate { customer = mapCustomer(globalCustomerId, subscriptionServiceType); } outputBB.setServiceInstance(serviceInstance); - customer.getServiceSubscription().getServiceInstances().add(serviceInstance); + if (customer.getServiceSubscription() != null) { + customer.getServiceSubscription().getServiceInstances().add(serviceInstance); + } outputBB.setCustomer(customer); return outputBB; } diff --git a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupTest.java b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupTest.java index 9897c04ad8..d0ecedf878 100644 --- a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupTest.java +++ b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupTest.java @@ -301,6 +301,35 @@ public class BBInputSetupTest { assertThat(actual, sameBeanAs(expected)); } + + @Test + public void testGetGBBCM() throws Exception { + GeneralBuildingBlock expected = mapper.readValue(new File(RESOURCE_PATH + "GeneralBuildingBlockCMExpected.json"), + GeneralBuildingBlock.class); + + ExecuteBuildingBlock executeBB = new ExecuteBuildingBlock(); + executeBB.setRequestId("requestId"); + RequestDetails requestDetails = new RequestDetails(); + requestDetails.setModelInfo(null); + RequestParameters requestParams = new RequestParameters(); + requestParams.setaLaCarte(true); + requestDetails.setRequestParameters(requestParams); + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setSuppressRollback(true); + requestDetails.setRequestInfo(requestInfo); + doReturn(requestDetails).when(SPY_bbInputSetupUtils).getRequestDetails(executeBB.getRequestId()); + Map lookupKeyMap = new HashMap<>(); + String resourceId = "123"; + String requestAction = "createInstance"; + doReturn(expected).when(SPY_bbInputSetup).getGBBALaCarteService(executeBB, requestDetails, lookupKeyMap, + requestAction, resourceId); + doNothing().when(SPY_bbInputSetup).populateLookupKeyMapWithIds(any(WorkflowResourceIds.class), any()); + doReturn(null).when(bbInputSetupMapperLayer).mapAAIGenericVnfIntoGenericVnf(any(org.onap.aai.domain.yang.GenericVnf.class)); + GeneralBuildingBlock actual = SPY_bbInputSetup.getGBBCM(executeBB, requestDetails, lookupKeyMap, requestAction, + resourceId); + + assertThat(actual, sameBeanAs(expected)); + } @Test public void testGetGBBALaCarteNonService() throws Exception { diff --git a/bpmn/MSOCommonBPMN/src/test/resources/__files/ExecuteBuildingBlock/GeneralBuildingBlockCMExpected.json b/bpmn/MSOCommonBPMN/src/test/resources/__files/ExecuteBuildingBlock/GeneralBuildingBlockCMExpected.json new file mode 100644 index 0000000000..8cd04fdd8e --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/test/resources/__files/ExecuteBuildingBlock/GeneralBuildingBlockCMExpected.json @@ -0,0 +1,29 @@ +{ + "requestContext": { + "source": "VID", + "mso-request-id": "requestId", + "action": "createInstance", + "requestParameters": { + "userParams": [], + "aLaCarte": true + }, + "configurationParameters": [] + }, + "orchContext": { + "is-rollback-enabled": true + }, + "cloudRegion": { + "cloud-owner": "att-aic" + }, + "userInput": null, + "customer": { + "vpn-bindings": [] + }, + "serviceInstance": { + "vnfs": [null], + "pnfs": [], + "allotted-resources": [], + "networks": [], + "configurations": [] + } +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java new file mode 100644 index 0000000000..94eead2d05 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java @@ -0,0 +1,144 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.activity; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables; +import org.camunda.bpm.engine.variable.VariableMap; +import org.onap.so.bpmn.core.WorkflowException; +import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.logger.MessageEnum; +import org.onap.so.logger.MsoLogger; +import org.onap.so.serviceinstancebeans.RequestDetails; +import org.onap.so.serviceinstancebeans.ServiceInstancesRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; + +@Component("ExecuteActivity") +public class ExecuteActivity implements JavaDelegate { + + private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExecuteActivity.class); + private static final String G_BPMN_REQUEST = "bpmnRequest"; + private static final String VNF_TYPE = "vnfType"; + private static final String G_ACTION = "requestAction"; + private static final String G_REQUEST_ID = "mso-request-id"; + private static final String VNF_ID = "vnfId"; + private static final String SERVICE_INSTANCE_ID = "serviceInstanceId"; + + private static final String SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE = "implementation"; + private static final String ACTIVITY_PREFIX = "activity:"; + + private ObjectMapper mapper = new ObjectMapper(); + + @Autowired + private RuntimeService runtimeService; + @Autowired + private ExceptionBuilder exceptionBuilder; + + @Override + public void execute(DelegateExecution execution) throws Exception { + final String requestId = (String) execution.getVariable(G_REQUEST_ID); + + try { + final String implementationString = execution.getBpmnModelElementInstance().getAttributeValue(SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE); + msoLogger.debug("activity implementation String: " + implementationString); + if (!implementationString.startsWith(ACTIVITY_PREFIX)) { + buildAndThrowException(execution, "Implementation attribute has a wrong format"); + } + String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, ""); + msoLogger.info("activityName is: " + activityName); + + BuildingBlock buildingBlock = buildBuildingBlock(activityName); + ExecuteBuildingBlock executeBuildingBlock = buildExecuteBuildingBlock(execution, requestId, buildingBlock); + + Map variables = new HashMap<>(); + variables.put("buildingBlock", executeBuildingBlock); + variables.put("mso-request-id", requestId); + variables.put("retryCount", 1); + + ProcessInstanceWithVariables buildingBlockResult = runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables).executeWithVariablesInReturn(); + VariableMap variableMap = buildingBlockResult.getVariables(); + + WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException"); + if (workflowException != null) { + msoLogger.error("Workflow exception is: " + workflowException.getErrorMessage()); + } + execution.setVariable("WorkflowException", workflowException); + } + catch (Exception e) { + buildAndThrowException(execution, e.getMessage()); + } + } + + protected BuildingBlock buildBuildingBlock(String activityName) { + BuildingBlock buildingBlock = new BuildingBlock(); + buildingBlock.setBpmnFlowName(activityName); + buildingBlock.setMsoId(UUID.randomUUID().toString()); + buildingBlock.setKey(""); + buildingBlock.setIsVirtualLink(false); + buildingBlock.setVirtualLinkKey(""); + return buildingBlock; + } + + protected ExecuteBuildingBlock buildExecuteBuildingBlock(DelegateExecution execution, String requestId, + BuildingBlock buildingBlock) throws Exception { + ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock(); + String bpmnRequest = (String) execution.getVariable(G_BPMN_REQUEST); + ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class); + RequestDetails requestDetails = sIRequest.getRequestDetails(); + executeBuildingBlock.setaLaCarte(true); + executeBuildingBlock.setRequestAction((String) execution.getVariable(G_ACTION)); + executeBuildingBlock.setResourceId((String) execution.getVariable(VNF_ID)); + executeBuildingBlock.setVnfType((String) execution.getVariable(VNF_TYPE)); + WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds(); + workflowResourceIds.setServiceInstanceId((String) execution.getVariable(SERVICE_INSTANCE_ID)); + workflowResourceIds.setVnfId((String) execution.getVariable(VNF_ID)); + executeBuildingBlock.setWorkflowResourceIds(workflowResourceIds); + executeBuildingBlock.setRequestId(requestId); + executeBuildingBlock.setBuildingBlock(buildingBlock); + executeBuildingBlock.setRequestDetails(requestDetails); + return executeBuildingBlock; + } + + protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) { + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, msg, ex); + execution.setVariable("ExecuteActivityErrorMessage", msg); + exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg); + } + + protected void buildAndThrowException(DelegateExecution execution, String msg) { + msoLogger.error(msg); + execution.setVariable("ExecuteActuvityErrorMessage", msg); + exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg); + } +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivityTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivityTest.java new file mode 100644 index 0000000000..d4956f9349 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivityTest.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.activity; + + +import static org.junit.Assert.assertEquals; + +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake; +import org.junit.Before; + +import org.junit.Test; +import org.onap.so.bpmn.BaseTaskTest; +import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock; +import org.springframework.beans.factory.annotation.Autowired; + +public class ExecuteActivityTest extends BaseTaskTest { + @Autowired + protected ExecuteActivity executeActivity; + + private DelegateExecution execution; + + @Before + public void before() throws Exception { + execution = new DelegateExecutionFake(); + execution.setVariable("vnfType", "testVnfType"); + execution.setVariable("requestAction", "testRequestAction"); + execution.setVariable("mso-request-id", "testMsoRequestId"); + execution.setVariable("vnfId", "testVnfId"); + execution.setVariable("serviceInstanceId", "testServiceInstanceId"); + String bpmnRequest = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/Macro/ServiceMacroAssign.json"))); + execution.setVariable("bpmnRequest", bpmnRequest); + } + + @Test + public void buildBuildingBlock_Test(){ + BuildingBlock bb = executeActivity.buildBuildingBlock("testActivityName"); + assertEquals(bb.getBpmnFlowName(), "testActivityName"); + assertEquals(bb.getKey(), ""); + } + + @Test + public void executeBuildingBlock_Test() throws Exception { + BuildingBlock bb = executeActivity.buildBuildingBlock("testActivityName"); + ExecuteBuildingBlock ebb = executeActivity.buildExecuteBuildingBlock(execution, "testMsoRequestId", bb); + assertEquals(ebb.getVnfType(), "testVnfType"); + assertEquals(ebb.getRequestAction(), "testRequestAction"); + assertEquals(ebb.getRequestId(), "testMsoRequestId"); + assertEquals(ebb.getWorkflowResourceIds().getVnfId(), "testVnfId"); + assertEquals(ebb.getWorkflowResourceIds().getServiceInstanceId(), "testServiceInstanceId"); + assertEquals(ebb.getBuildingBlock(), bb); + } + +} -- cgit 1.2.3-korg