Workflow Activities Execution Environment 27/67827/2
authorElena Kuleshov <EK1439@att.com>
Wed, 19 Sep 2018 18:03:47 +0000 (14:03 -0400)
committerElena Kuleshov <EK1439@att.com>
Wed, 19 Sep 2018 19:41:11 +0000 (15:41 -0400)
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 <EK1439@att.com>
bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetup.java
bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupTest.java
bpmn/MSOCommonBPMN/src/test/resources/__files/ExecuteBuildingBlock/GeneralBuildingBlockCMExpected.json [new file with mode: 0644]
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java [new file with mode: 0644]
bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivityTest.java [new file with mode: 0644]

index 6d5fb2f..eb4f4ca 100644 (file)
@@ -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<ResourceKey, String> lookupKeyMap, String requestAction,
+                       String resourceId) throws Exception {           
+               ServiceInstance serviceInstance = new ServiceInstance();
+               String serviceInstanceId = lookupKeyMap.get(ResourceKey.SERVICE_INSTANCE_ID);
+               serviceInstance.setServiceInstanceId(serviceInstanceId);
+               
+               List<GenericVnf> 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<ResourceKey, String> 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;
        }
index 9897c04..d0ecedf 100644 (file)
@@ -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<ResourceKey, String> 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 (file)
index 0000000..8cd04fd
--- /dev/null
@@ -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 (file)
index 0000000..94eead2
--- /dev/null
@@ -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<String, Object> 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 (file)
index 0000000..d4956f9
--- /dev/null
@@ -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);       
+       }
+       
+}