89490ff6204c62b0db3c36493ee98738a51ccec3
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.scripts
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.so.db.request.beans.OrchestrationTask
25
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
33
34 class HandleOrchestrationTask extends AbstractServiceTaskProcessor {
35     private static final Logger logger = LoggerFactory.getLogger(HandleOrchestrationTask.class)
36
37     ExceptionUtil exceptionUtil = new ExceptionUtil()
38     def supportedMethod = ["GET", "POST", "PUT"]
39     def validStatus = [200, 201]
40
41     @Override
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"
47             logger.debug(msg)
48             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
49         }
50
51         String taskId = execution.getVariable("taskId")
52         if (isBlank(taskId)) {
53             String msg = "taskId is empty"
54             logger.debug(msg)
55             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
56         }
57
58         def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.requestDb.endpoint",execution)
59         def orchestrationTaskEndpoint = dbAdapterEndpoint + "/orchestrationTask/"
60         if (!"POST".equals(method)) {
61             orchestrationTaskEndpoint = orchestrationTaskEndpoint + taskId
62         }
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)
71
72         String requestId = execution.getVariable("requestId")
73         if (("POST".equals(method) || "PUT".equals(method)) && isBlank(requestId)) {
74             String msg = "requestId is empty"
75             logger.debug(msg)
76             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
77         }
78         String taskName = execution.getVariable("taskName")
79         if (("POST".equals(method) || "PUT".equals(method)) && isBlank(taskName)) {
80             String msg = "task name is empty"
81             logger.debug(msg)
82             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
83         }
84         String taskStatus = execution.getVariable("taskStatus")
85         if (("POST".equals(method) || "PUT".equals(method)) && isBlank(taskStatus)) {
86             String msg = "task status is empty"
87             logger.debug(msg)
88             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
89         }
90         String isManual = execution.getVariable("isManual")
91         if (("POST".equals(method) || "PUT".equals(method)) && isBlank(isManual)) {
92             String msg = "isManual is empty"
93             logger.debug(msg)
94             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
95         }
96         String paramJson = execution.getVariable("paramJson")
97
98         String payload = ""
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)
110         }
111         execution.setVariable("payload", payload)
112         logger.debug("End preProcessRequest")
113     }
114
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
122             logger.debug(msg)
123             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
124         }
125     }
126 }
127