2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.activity;
23 import java.util.HashMap;
25 import java.util.UUID;
27 import org.camunda.bpm.engine.RuntimeService;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.camunda.bpm.engine.delegate.JavaDelegate;
30 import org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables;
31 import org.camunda.bpm.engine.variable.VariableMap;
32 import org.onap.so.bpmn.core.WorkflowException;
33 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.onap.so.logger.MessageEnum;
38 import org.onap.so.logger.MsoLogger;
39 import org.onap.so.serviceinstancebeans.RequestDetails;
40 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
44 import com.fasterxml.jackson.databind.ObjectMapper;
46 @Component("ExecuteActivity")
47 public class ExecuteActivity implements JavaDelegate {
49 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExecuteActivity.class);
50 private static final String G_BPMN_REQUEST = "bpmnRequest";
51 private static final String VNF_TYPE = "vnfType";
52 private static final String G_ACTION = "requestAction";
53 private static final String G_REQUEST_ID = "mso-request-id";
54 private static final String VNF_ID = "vnfId";
55 private static final String SERVICE_INSTANCE_ID = "serviceInstanceId";
57 private static final String SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE = "implementation";
58 private static final String ACTIVITY_PREFIX = "activity:";
60 private ObjectMapper mapper = new ObjectMapper();
63 private RuntimeService runtimeService;
65 private ExceptionBuilder exceptionBuilder;
68 public void execute(DelegateExecution execution) throws Exception {
69 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
72 final String implementationString = execution.getBpmnModelElementInstance().getAttributeValue(SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE);
73 msoLogger.debug("activity implementation String: " + implementationString);
74 if (!implementationString.startsWith(ACTIVITY_PREFIX)) {
75 buildAndThrowException(execution, "Implementation attribute has a wrong format");
77 String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, "");
78 msoLogger.info("activityName is: " + activityName);
80 BuildingBlock buildingBlock = buildBuildingBlock(activityName);
81 ExecuteBuildingBlock executeBuildingBlock = buildExecuteBuildingBlock(execution, requestId, buildingBlock);
83 Map<String, Object> variables = new HashMap<>();
84 variables.put("buildingBlock", executeBuildingBlock);
85 variables.put("mso-request-id", requestId);
86 variables.put("retryCount", 1);
87 variables.put("aLaCarte", true);
89 ProcessInstanceWithVariables buildingBlockResult = runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables).executeWithVariablesInReturn();
90 VariableMap variableMap = buildingBlockResult.getVariables();
92 WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException");
93 if (workflowException != null) {
94 msoLogger.error("Workflow exception is: " + workflowException.getErrorMessage());
96 execution.setVariable("WorkflowException", workflowException);
99 buildAndThrowException(execution, e.getMessage());
103 protected BuildingBlock buildBuildingBlock(String activityName) {
104 BuildingBlock buildingBlock = new BuildingBlock();
105 buildingBlock.setBpmnFlowName(activityName);
106 buildingBlock.setMsoId(UUID.randomUUID().toString());
107 buildingBlock.setKey("");
108 buildingBlock.setIsVirtualLink(false);
109 buildingBlock.setVirtualLinkKey("");
110 return buildingBlock;
113 protected ExecuteBuildingBlock buildExecuteBuildingBlock(DelegateExecution execution, String requestId,
114 BuildingBlock buildingBlock) throws Exception {
115 ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
116 String bpmnRequest = (String) execution.getVariable(G_BPMN_REQUEST);
117 ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
118 RequestDetails requestDetails = sIRequest.getRequestDetails();
119 executeBuildingBlock.setaLaCarte(true);
120 executeBuildingBlock.setRequestAction((String) execution.getVariable(G_ACTION));
121 executeBuildingBlock.setResourceId((String) execution.getVariable(VNF_ID));
122 executeBuildingBlock.setVnfType((String) execution.getVariable(VNF_TYPE));
123 WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
124 workflowResourceIds.setServiceInstanceId((String) execution.getVariable(SERVICE_INSTANCE_ID));
125 workflowResourceIds.setVnfId((String) execution.getVariable(VNF_ID));
126 executeBuildingBlock.setWorkflowResourceIds(workflowResourceIds);
127 executeBuildingBlock.setRequestId(requestId);
128 executeBuildingBlock.setBuildingBlock(buildingBlock);
129 executeBuildingBlock.setRequestDetails(requestDetails);
130 return executeBuildingBlock;
133 protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
134 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(),
135 MsoLogger.ErrorCode.UnknownError, msg, ex);
136 execution.setVariable("ExecuteActivityErrorMessage", msg);
137 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
140 protected void buildAndThrowException(DelegateExecution execution, String msg) {
141 msoLogger.error(msg);
142 execution.setVariable("ExecuteActuvityErrorMessage", msg);
143 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);