aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/activity/ExecuteActivity.java
blob: d39e58b77854a3fd5282d4aeb796fcfccfb7d050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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 com.google.common.base.Strings;
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.ErrorCode;
import org.onap.so.logger.MessageEnum;
import org.onap.so.serviceinstancebeans.RequestDetails;
import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 Logger logger = LoggerFactory.getLogger(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);
            logger.debug("activity implementation String: {}", implementationString);
            if (!implementationString.startsWith(ACTIVITY_PREFIX)) {
                buildAndThrowException(execution, "Implementation attribute has a wrong format");
            }
            String activityName = implementationString.replaceFirst(ACTIVITY_PREFIX, "");
            logger.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(G_REQUEST_ID, requestId);
            variables.put("retryCount", 1);
            variables.put("aLaCarte", true);

            ProcessInstanceWithVariables buildingBlockResult =
                    runtimeService.createProcessInstanceByKey("ExecuteBuildingBlock").setVariables(variables)
                            .executeWithVariablesInReturn();
            VariableMap variableMap = buildingBlockResult.getVariables();

            WorkflowException workflowException = (WorkflowException) variableMap.get("WorklfowException");
            if (workflowException != null) {
                logger.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) {
        logger.error(Strings.repeat("{} ", 5), MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                ErrorCode.UnknownError.getValue(), msg, ex);
        execution.setVariable("ExecuteActivityErrorMessage", msg);
        exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
    }

    protected void buildAndThrowException(DelegateExecution execution, String msg) {
        logger.error(msg);
        execution.setVariable("ExecuteActuvityErrorMessage", msg);
        exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
    }
}