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 org.camunda.bpm.engine.RuntimeService;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.delegate.JavaDelegate;
31 import org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables;
32 import org.camunda.bpm.engine.variable.VariableMap;
33 import org.onap.so.bpmn.core.WorkflowException;
34 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionBBTasks;
35 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.logger.ErrorCode;
40 import org.onap.so.logger.MessageEnum;
41 import org.onap.so.serviceinstancebeans.RequestDetails;
42 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47 import com.fasterxml.jackson.databind.ObjectMapper;
49 @Component("ExecuteActivity")
50 public class ExecuteActivity implements JavaDelegate {
52 private static final Logger logger = LoggerFactory.getLogger(ExecuteActivity.class);
53 private static final String G_BPMN_REQUEST = "bpmnRequest";
54 private static final String VNF_TYPE = "vnfType";
55 private static final String G_ACTION = "requestAction";
56 private static final String G_REQUEST_ID = "mso-request-id";
57 private static final String VNF_ID = "vnfId";
58 private static final String SERVICE_INSTANCE_ID = "serviceInstanceId";
59 private static final String WORKFLOW_SYNC_ACK_SENT = "workflowSyncAckSent";
61 private static final String SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE = "implementation";
62 private static final String ACTIVITY_PREFIX = "activity:";
64 private ObjectMapper mapper = new ObjectMapper();
67 private RuntimeService runtimeService;
69 private ExceptionBuilder exceptionBuilder;
71 private WorkflowActionBBTasks workflowActionBBTasks;
74 public void execute(DelegateExecution execution) throws Exception {
75 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
78 Boolean workflowSyncAckSent = (Boolean) execution.getVariable(WORKFLOW_SYNC_ACK_SENT);
79 if (workflowSyncAckSent == null || workflowSyncAckSent == false) {
80 workflowActionBBTasks.sendSyncAck(execution);
81 execution.setVariable(WORKFLOW_SYNC_ACK_SENT, Boolean.TRUE);
83 final String implementationString =
84 execution.getBpmnModelElementInstance().getAttributeValue(SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE);
85 logger.debug("activity implementation String: {}", implementationString);
86 if (!implementationString.startsWith(ACTIVITY_PREFIX)) {
87 buildAndThrowException(execution, "Implementation attribute has a wrong format");
89 String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, "");
90 logger.info("activityName is: {}", activityName);
92 BuildingBlock buildingBlock = buildBuildingBlock(activityName);
93 ExecuteBuildingBlock executeBuildingBlock = buildExecuteBuildingBlock(execution, requestId, buildingBlock);
95 Map<String, Object> variables = new HashMap<>();
96 variables.put("buildingBlock", executeBuildingBlock);
97 variables.put("mso-request-id", requestId);
98 variables.put("retryCount", 1);
99 variables.put("aLaCarte", true);
101 ProcessInstanceWithVariables buildingBlockResult =
102 runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables)
103 .executeWithVariablesInReturn();
104 VariableMap variableMap = buildingBlockResult.getVariables();
106 WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException");
107 if (workflowException != null) {
108 logger.error("Workflow exception is: {}", workflowException.getErrorMessage());
110 execution.setVariable("WorkflowException", workflowException);
111 } catch (Exception e) {
112 buildAndThrowException(execution, e.getMessage());
116 protected BuildingBlock buildBuildingBlock(String activityName) {
117 BuildingBlock buildingBlock = new BuildingBlock();
118 buildingBlock.setBpmnFlowName(activityName);
119 buildingBlock.setMsoId(UUID.randomUUID().toString());
120 buildingBlock.setKey("");
121 buildingBlock.setIsVirtualLink(false);
122 buildingBlock.setVirtualLinkKey("");
123 return buildingBlock;
126 protected ExecuteBuildingBlock buildExecuteBuildingBlock(DelegateExecution execution, String requestId,
127 BuildingBlock buildingBlock) throws Exception {
128 ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
129 String bpmnRequest = (String) execution.getVariable(G_BPMN_REQUEST);
130 ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
131 RequestDetails requestDetails = sIRequest.getRequestDetails();
132 executeBuildingBlock.setaLaCarte(true);
133 executeBuildingBlock.setRequestAction((String) execution.getVariable(G_ACTION));
134 executeBuildingBlock.setResourceId((String) execution.getVariable(VNF_ID));
135 executeBuildingBlock.setVnfType((String) execution.getVariable(VNF_TYPE));
136 WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
137 workflowResourceIds.setServiceInstanceId((String) execution.getVariable(SERVICE_INSTANCE_ID));
138 workflowResourceIds.setVnfId((String) execution.getVariable(VNF_ID));
139 executeBuildingBlock.setWorkflowResourceIds(workflowResourceIds);
140 executeBuildingBlock.setRequestId(requestId);
141 executeBuildingBlock.setBuildingBlock(buildingBlock);
142 executeBuildingBlock.setRequestDetails(requestDetails);
143 return executeBuildingBlock;
146 protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
147 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
148 ErrorCode.UnknownError.getValue(), msg, ex);
149 execution.setVariable("ExecuteActivityErrorMessage", msg);
150 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
153 protected void buildAndThrowException(DelegateExecution execution, String msg) {
155 execution.setVariable("ExecuteActuvityErrorMessage", msg);
156 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);