2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.activity;
25 import java.util.HashMap;
27 import java.util.UUID;
28 import com.google.common.base.Strings;
29 import org.camunda.bpm.engine.RuntimeService;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.JavaDelegate;
32 import org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables;
33 import org.camunda.bpm.engine.variable.VariableMap;
34 import org.onap.so.bpmn.core.WorkflowException;
35 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionBBTasks;
36 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
38 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.so.logger.ErrorCode;
41 import org.onap.so.logger.MessageEnum;
42 import org.onap.so.serviceinstancebeans.RequestDetails;
43 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Component;
48 import com.fasterxml.jackson.databind.ObjectMapper;
50 @Component("ExecuteActivity")
51 public class ExecuteActivity implements JavaDelegate {
53 private static final Logger logger = LoggerFactory.getLogger(ExecuteActivity.class);
54 private static final String G_BPMN_REQUEST = "bpmnRequest";
55 private static final String VNF_TYPE = "vnfType";
56 private static final String G_ACTION = "requestAction";
57 private static final String G_REQUEST_ID = "mso-request-id";
58 private static final String VNF_ID = "vnfId";
59 private static final String SERVICE_INSTANCE_ID = "serviceInstanceId";
60 private static final String WORKFLOW_SYNC_ACK_SENT = "workflowSyncAckSent";
62 private static final String SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE = "implementation";
63 private static final String ACTIVITY_PREFIX = "activity:";
65 private ObjectMapper mapper = new ObjectMapper();
68 private RuntimeService runtimeService;
70 private ExceptionBuilder exceptionBuilder;
72 private WorkflowActionBBTasks workflowActionBBTasks;
75 public void execute(DelegateExecution execution) throws Exception {
76 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
79 Boolean workflowSyncAckSent = (Boolean) execution.getVariable(WORKFLOW_SYNC_ACK_SENT);
80 if (workflowSyncAckSent == null || workflowSyncAckSent == false) {
81 workflowActionBBTasks.sendSyncAck(execution);
82 execution.setVariable(WORKFLOW_SYNC_ACK_SENT, Boolean.TRUE);
84 final String implementationString =
85 execution.getBpmnModelElementInstance().getAttributeValue(SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE);
86 logger.debug("activity implementation String: {}", implementationString);
87 if (!implementationString.startsWith(ACTIVITY_PREFIX)) {
88 buildAndThrowException(execution, "Implementation attribute has a wrong format");
90 String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, "");
91 logger.info("activityName is: {}", activityName);
93 BuildingBlock buildingBlock = buildBuildingBlock(activityName);
94 ExecuteBuildingBlock executeBuildingBlock = buildExecuteBuildingBlock(execution, requestId, buildingBlock);
96 Map<String, Object> variables = new HashMap<>();
97 variables.put("buildingBlock", executeBuildingBlock);
98 variables.put(G_REQUEST_ID, requestId);
99 variables.put("retryCount", 1);
100 variables.put("aLaCarte", true);
102 ProcessInstanceWithVariables buildingBlockResult =
103 runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables)
104 .executeWithVariablesInReturn();
105 VariableMap variableMap = buildingBlockResult.getVariables();
107 WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException");
108 if (workflowException != null) {
109 logger.error("Workflow exception is: {}", workflowException.getErrorMessage());
111 execution.setVariable("WorkflowException", workflowException);
112 } catch (Exception e) {
113 buildAndThrowException(execution, e.getMessage());
117 protected BuildingBlock buildBuildingBlock(String activityName) {
118 BuildingBlock buildingBlock = new BuildingBlock();
119 buildingBlock.setBpmnFlowName(activityName);
120 buildingBlock.setMsoId(UUID.randomUUID().toString());
121 buildingBlock.setKey("");
122 buildingBlock.setIsVirtualLink(false);
123 buildingBlock.setVirtualLinkKey("");
124 return buildingBlock;
127 protected ExecuteBuildingBlock buildExecuteBuildingBlock(DelegateExecution execution, String requestId,
128 BuildingBlock buildingBlock) throws Exception {
129 ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
130 String bpmnRequest = (String) execution.getVariable(G_BPMN_REQUEST);
131 ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
132 RequestDetails requestDetails = sIRequest.getRequestDetails();
133 executeBuildingBlock.setaLaCarte(true);
134 executeBuildingBlock.setRequestAction((String) execution.getVariable(G_ACTION));
135 executeBuildingBlock.setResourceId((String) execution.getVariable(VNF_ID));
136 executeBuildingBlock.setVnfType((String) execution.getVariable(VNF_TYPE));
137 WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
138 workflowResourceIds.setServiceInstanceId((String) execution.getVariable(SERVICE_INSTANCE_ID));
139 workflowResourceIds.setVnfId((String) execution.getVariable(VNF_ID));
140 executeBuildingBlock.setWorkflowResourceIds(workflowResourceIds);
141 executeBuildingBlock.setRequestId(requestId);
142 executeBuildingBlock.setBuildingBlock(buildingBlock);
143 executeBuildingBlock.setRequestDetails(requestDetails);
144 return executeBuildingBlock;
147 protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
148 logger.error(Strings.repeat("{} ", 5), MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
149 ErrorCode.UnknownError.getValue(), msg, ex);
150 execution.setVariable("ExecuteActivityErrorMessage", msg);
151 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
154 protected void buildAndThrowException(DelegateExecution execution, String msg) {
156 execution.setVariable("ExecuteActuvityErrorMessage", msg);
157 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);