17089be571481d2ad03de239c3ac162bcd737937
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.manualhandling.tasks;
22
23 import java.util.Map;
24 import java.util.HashMap;
25 import org.onap.so.logger.LoggingAnchor;
26 import org.camunda.bpm.engine.TaskService;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateTask;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.client.ticket.ExternalTicket;
32 import org.onap.so.db.request.beans.InfraActiveRequests;
33 import org.onap.so.db.request.client.RequestsDbClient;
34 import org.onap.so.logger.ErrorCode;
35 import org.onap.so.logger.MessageEnum;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class ManualHandlingTasks {
43     private static final Logger logger = LoggerFactory.getLogger(ManualHandlingTasks.class);
44
45     private static final String TASK_TYPE_PAUSE = "pause";
46     private static final String TASK_TYPE_FALLOUT = "fallout";
47     public static final String VNF_TYPE = "vnfType";
48     public static final String SERVICE_TYPE = "serviceType";
49     public static final String MSO_REQUEST_ID = "msoRequestId";
50     public static final String REQUESTOR_ID = "requestorId";
51     public static final String ERROR_CODE = "errorCode";
52     public static final String VALID_RESPONSES = "validResponses";
53     public static final String DESCRIPTION = "description";
54     public static final String BPMN_EXCEPTION = "BPMN exception: ";
55
56     @Autowired
57     private ExceptionBuilder exceptionUtil;
58
59     @Autowired
60     private RequestsDbClient requestDbclient;
61
62     public void setFalloutTaskVariables(DelegateTask task) {
63
64         DelegateExecution execution = task.getExecution();
65         try {
66             String taskId = task.getId();
67             logger.debug("taskId is: " + taskId);
68             String type = TASK_TYPE_FALLOUT;
69             String nfRole = (String) execution.getVariable(VNF_TYPE);
70             String subscriptionServiceType = (String) execution.getVariable(SERVICE_TYPE);
71             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
72             String originalRequestorId = (String) execution.getVariable(REQUESTOR_ID);
73             String description = "";
74             String timeout = "";
75             String errorSource = (String) execution.getVariable("failedActivity");
76             String errorCode = (String) execution.getVariable(ERROR_CODE);
77             String errorMessage = (String) execution.getVariable("errorText");
78             String buildingBlockName = (String) execution.getVariable("currentActivity");
79             String buildingBlockStep = (String) execution.getVariable("workStep");
80             String validResponses = (String) execution.getVariable(VALID_RESPONSES);
81
82             Map<String, String> taskVariables = new HashMap<>();
83             taskVariables.put("type", type);
84             taskVariables.put("nfRole", nfRole);
85             taskVariables.put("subscriptionServiceType", subscriptionServiceType);
86             taskVariables.put("originalRequestId", originalRequestId);
87             taskVariables.put("originalRequestorId", originalRequestorId);
88             taskVariables.put("errorSource", errorSource);
89             taskVariables.put(ERROR_CODE, errorCode);
90             taskVariables.put("errorMessage", errorMessage);
91             taskVariables.put("buildingBlockName", buildingBlockName);
92             taskVariables.put("buildingBlockStep", buildingBlockStep);
93             taskVariables.put(VALID_RESPONSES, validResponses);
94             taskVariables.put("tmeout", timeout);
95             taskVariables.put(DESCRIPTION, description);
96             TaskService taskService = execution.getProcessEngineServices().getTaskService();
97
98             taskService.setVariables(taskId, taskVariables);
99             logger.debug("successfully created fallout task: " + taskId);
100         } catch (BpmnError e) {
101             logger.debug(BPMN_EXCEPTION + e.getMessage());
102             throw e;
103         } catch (Exception ex) {
104             String msg = "Exception in setFalloutTaskVariables " + ex.getMessage();
105             logger.debug(msg);
106             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
107         }
108     }
109
110     public void setPauseTaskVariables(DelegateTask task) {
111
112         DelegateExecution execution = task.getExecution();
113
114         try {
115             String taskId = task.getId();
116             logger.debug("taskId is: " + taskId);
117             String type = TASK_TYPE_PAUSE;
118             String nfRole = (String) execution.getVariable(VNF_TYPE);
119             String subscriptionServiceType = (String) execution.getVariable(SERVICE_TYPE);
120             String originalRequestId = (String) execution.getVariable(MSO_REQUEST_ID);
121             String originalRequestorId = (String) execution.getVariable(REQUESTOR_ID);
122             String description = (String) execution.getVariable(DESCRIPTION);
123             String timeout = (String) execution.getVariable("taskTimeout");
124             String errorSource = "";
125             String errorCode = "";
126             String errorMessage = "";
127             String buildingBlockName = "";
128             String buildingBlockStep = "";
129             String validResponses = (String) execution.getVariable(VALID_RESPONSES);
130
131             Map<String, String> taskVariables = new HashMap<>();
132             taskVariables.put("type", type);
133             taskVariables.put("nfRole", nfRole);
134             taskVariables.put(DESCRIPTION, description);
135             taskVariables.put("timeout", timeout);
136             taskVariables.put("subscriptionServiceType", subscriptionServiceType);
137             taskVariables.put("originalRequestId", originalRequestId);
138             taskVariables.put("originalRequestorId", originalRequestorId);
139             taskVariables.put("errorSource", errorSource);
140             taskVariables.put(ERROR_CODE, errorCode);
141             taskVariables.put("errorMessage", errorMessage);
142             taskVariables.put("buildingBlockName", buildingBlockName);
143             taskVariables.put("buildingBlockStep", buildingBlockStep);
144             taskVariables.put(VALID_RESPONSES, validResponses);
145             TaskService taskService = execution.getProcessEngineServices().getTaskService();
146
147             taskService.setVariables(taskId, taskVariables);
148             logger.debug("successfully created pause task: " + taskId);
149         } catch (BpmnError e) {
150             logger.debug(BPMN_EXCEPTION + e.getMessage());
151             throw e;
152         } catch (Exception ex) {
153             String msg = "Exception in setPauseTaskVariables " + ex.getMessage();
154             logger.debug(msg);
155             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
156         }
157
158     }
159
160     public void completeTask(DelegateTask task) {
161
162         DelegateExecution execution = task.getExecution();
163
164         try {
165
166             String taskId = task.getId();
167             logger.debug("taskId is: " + taskId);
168             TaskService taskService = execution.getProcessEngineServices().getTaskService();
169
170             Map<String, Object> taskVariables = taskService.getVariables(taskId);
171             String responseValue = (String) taskVariables.get("responseValue");
172
173             logger.debug("Received responseValue on completion: " + responseValue);
174             // Have to set the first letter of the response to upper case
175             String responseValueUppercaseStart =
176                     responseValue.substring(0, 1).toUpperCase() + responseValue.substring(1);
177             logger.debug("ResponseValue to taskListener: " + responseValueUppercaseStart);
178             execution.setVariable("responseValueTask", responseValueUppercaseStart);
179
180         } catch (BpmnError e) {
181             logger.debug(BPMN_EXCEPTION + e.getMessage());
182             throw e;
183         } catch (Exception ex) {
184             String msg = "Exception in completeManualTask " + ex.getMessage();
185             logger.debug(msg);
186             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
187         }
188
189     }
190
191     public void createExternalTicket(DelegateExecution execution) {
192
193         try {
194             ExternalTicket ticket = new ExternalTicket();
195
196             ticket.setRequestId((String) execution.getVariable(MSO_REQUEST_ID));
197             ticket.setCurrentActivity((String) execution.getVariable("currentActivity"));
198             ticket.setNfRole((String) execution.getVariable(VNF_TYPE));
199             ticket.setDescription((String) execution.getVariable(DESCRIPTION));
200             ticket.setSubscriptionServiceType((String) execution.getVariable(SERVICE_TYPE));
201             ticket.setRequestorId((String) execution.getVariable(REQUESTOR_ID));
202             ticket.setTimeout((String) execution.getVariable("taskTimeout"));
203             ticket.setErrorSource((String) execution.getVariable("failedActivity"));
204             ticket.setErrorCode((String) execution.getVariable(ERROR_CODE));
205             ticket.setErrorMessage((String) execution.getVariable("errorText"));
206             ticket.setWorkStep((String) execution.getVariable("workStep"));
207
208             ticket.createTicket();
209         } catch (BpmnError e) {
210             String msg = "BPMN error in createAOTSTicket " + e.getMessage();
211             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN",
212                     ErrorCode.UnknownError.getValue());
213         } catch (Exception ex) {
214             String msg = "Exception in createExternalTicket " + ex.getMessage();
215             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN",
216                     ErrorCode.UnknownError.getValue());
217         }
218
219     }
220
221     public void updateRequestDbStatus(DelegateExecution execution, String status) {
222         try {
223             String requestId = (String) execution.getVariable(MSO_REQUEST_ID);
224             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
225
226             request.setRequestStatus(status);
227             request.setLastModifiedBy("ManualHandling");
228
229             requestDbclient.updateInfraActiveRequests(request);
230         } catch (Exception e) {
231             logger.error("Unable to save the updated request status to the DB", e);
232         }
233     }
234
235 }