2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Huawei Technologies Co., Ltd. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.so.db.request.beans.OrchestrationTask
26 import static org.apache.commons.lang3.StringUtils.*
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.slf4j.Logger
32 import org.slf4j.LoggerFactory
34 class HandleOrchestrationTask extends AbstractServiceTaskProcessor {
35 private static final Logger logger = LoggerFactory.getLogger(HandleOrchestrationTask.class)
37 ExceptionUtil exceptionUtil = new ExceptionUtil()
38 def supportedMethod = ["GET", "POST", "PUT"]
39 def validStatus = [200, 201]
42 public void preProcessRequest(DelegateExecution execution) {
43 logger.debug("Start preProcessRequest")
44 String method = execution.getVariable("method")
45 if (!supportedMethod.contains(method)) {
46 String msg = "Method: " + method + " is not supported"
48 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
51 String taskId = execution.getVariable("taskId")
52 if (isBlank(taskId)) {
53 String msg = "taskId is empty"
55 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
58 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.requestDb.endpoint",execution)
59 def orchestrationTaskEndpoint = dbAdapterEndpoint + "/orchestrationTask/"
60 if (!"POST".equals(method)) {
61 orchestrationTaskEndpoint = orchestrationTaskEndpoint + taskId
63 execution.setVariable("url", orchestrationTaskEndpoint)
64 logger.debug("DB Adapter Endpoint is: " + orchestrationTaskEndpoint)
65 def dbAdapterAuth = UrnPropertiesReader.getVariable("mso.adapters.requestDb.auth")
66 Map<String, String> headerMap = [:]
67 headerMap.put("content-type", "application/json")
68 headerMap.put("Authorization", dbAdapterAuth)
69 execution.setVariable("headerMap", headerMap)
70 logger.debug("DB Adapter Header is: " + headerMap)
72 String requestId = execution.getVariable("requestId")
73 if (("POST".equals(method) || "PUT".equals(method)) && isBlank(requestId)) {
74 String msg = "requestId is empty"
76 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
78 String taskName = execution.getVariable("taskName")
79 if (("POST".equals(method) || "PUT".equals(method)) && isBlank(taskName)) {
80 String msg = "task name is empty"
82 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
84 String taskStatus = execution.getVariable("taskStatus")
85 if (("POST".equals(method) || "PUT".equals(method)) && isBlank(taskStatus)) {
86 String msg = "task status is empty"
88 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
90 String isManual = execution.getVariable("isManual")
91 if (("POST".equals(method) || "PUT".equals(method)) && isBlank(isManual)) {
92 String msg = "isManual is empty"
94 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
96 String paramJson = execution.getVariable("paramJson")
99 if ("POST".equals(method) || "PUT".equals(method)) {
100 OrchestrationTask task = new OrchestrationTask()
101 task.setTaskId(taskId)
102 task.setRequestId(requestId)
103 task.setName(taskName)
104 task.setStatus(taskStatus)
105 task.setIsManual(isManual)
106 task.setParams(paramJson)
107 ObjectMapper objectMapper = new ObjectMapper()
108 payload = objectMapper.writeValueAsString(task)
109 logger.debug("Outgoing payload is \n" + payload)
111 execution.setVariable("payload", payload)
112 logger.debug("End preProcessRequest")
115 public void postProcess(DelegateExecution execution) {
116 Integer statusCode = execution.getVariable("statusCode")
117 logger.debug("statusCode: " + statusCode)
118 String response = execution.getVariable("response")
119 logger.debug("response: " + response)
120 if (!validStatus.contains(statusCode)) {
121 String msg = "Error in sending orchestrationTask request. \nstatusCode: " + statusCode + "\nresponse: " + response
123 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)