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;
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.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;
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";
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;
72 public void execute(DelegateExecution execution) throws Exception {
73 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
76 final String implementationString = execution.getBpmnModelElementInstance().getAttributeValue(SERVICE_TASK_IMPLEMENTATION_ATTRIBUTE);
77 logger.debug("activity implementation String: {}", implementationString);
78 if (!implementationString.startsWith(ACTIVITY_PREFIX)) {
79 buildAndThrowException(execution, "Implementation attribute has a wrong format");
81 String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, "");
82 logger.info("activityName is: {}", activityName);
84 BuildingBlock buildingBlock = buildBuildingBlock(activityName);
85 ExecuteBuildingBlock executeBuildingBlock = buildExecuteBuildingBlock(execution, requestId, buildingBlock);
87 Map<String, Object> variables = new HashMap<>();
88 variables.put("buildingBlock", executeBuildingBlock);
89 variables.put("mso-request-id", requestId);
90 variables.put("retryCount", 1);
91 variables.put("aLaCarte", true);
93 ProcessInstanceWithVariables buildingBlockResult = runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables).executeWithVariablesInReturn();
94 VariableMap variableMap = buildingBlockResult.getVariables();
96 WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException");
97 if (workflowException != null) {
98 logger.error("Workflow exception is: {}", workflowException.getErrorMessage());
100 execution.setVariable("WorkflowException", workflowException);
102 catch (Exception e) {
103 buildAndThrowException(execution, e.getMessage());
107 protected BuildingBlock buildBuildingBlock(String activityName) {
108 BuildingBlock buildingBlock = new BuildingBlock();
109 buildingBlock.setBpmnFlowName(activityName);
110 buildingBlock.setMsoId(UUID.randomUUID().toString());
111 buildingBlock.setKey("");
112 buildingBlock.setIsVirtualLink(false);
113 buildingBlock.setVirtualLinkKey("");
114 return buildingBlock;
117 protected ExecuteBuildingBlock buildExecuteBuildingBlock(DelegateExecution execution, String requestId,
118 BuildingBlock buildingBlock) throws Exception {
119 ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
120 String bpmnRequest = (String) execution.getVariable(G_BPMN_REQUEST);
121 ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
122 RequestDetails requestDetails = sIRequest.getRequestDetails();
123 executeBuildingBlock.setaLaCarte(true);
124 executeBuildingBlock.setRequestAction((String) execution.getVariable(G_ACTION));
125 executeBuildingBlock.setResourceId((String) execution.getVariable(VNF_ID));
126 executeBuildingBlock.setVnfType((String) execution.getVariable(VNF_TYPE));
127 WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
128 workflowResourceIds.setServiceInstanceId((String) execution.getVariable(SERVICE_INSTANCE_ID));
129 workflowResourceIds.setVnfId((String) execution.getVariable(VNF_ID));
130 executeBuildingBlock.setWorkflowResourceIds(workflowResourceIds);
131 executeBuildingBlock.setRequestId(requestId);
132 executeBuildingBlock.setBuildingBlock(buildingBlock);
133 executeBuildingBlock.setRequestDetails(requestDetails);
134 return executeBuildingBlock;
137 protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
138 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
139 ErrorCode.UnknownError.getValue(), msg, ex);
140 execution.setVariable("ExecuteActivityErrorMessage", msg);
141 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
144 protected void buildAndThrowException(DelegateExecution execution, String msg) {
146 execution.setVariable("ExecuteActuvityErrorMessage", msg);
147 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);