2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
26 import org.onap.so.bpmn.common.scripts.ExceptionUtil
27 import org.onap.so.bpmn.common.scripts.RequestDBUtil
28 import org.onap.so.bpmn.core.json.JsonUtils
29 import org.onap.so.client.aai.AAIResourcesClient
30 import org.onap.so.db.request.beans.OperationStatus
31 import org.slf4j.Logger
32 import org.slf4j.LoggerFactory
34 import java.util.concurrent.TimeUnit
36 import static org.apache.commons.lang3.StringUtils.isBlank
38 class CheckServiceProcessStatus extends AbstractServiceTaskProcessor {
43 ExceptionUtil exceptionUtil = new ExceptionUtil()
45 RequestDBUtil requestDBUtil = new RequestDBUtil()
47 JsonUtils jsonUtil = new JsonUtils()
49 AAIResourcesClient client = getAAIClient()
51 private static final Logger logger = LoggerFactory.getLogger(CheckServiceProcessStatus.class)
54 void preProcessRequest(DelegateExecution execution) {
55 logger.debug(Prefix + "CheckServiceProcessStatus preProcessRequest Start")
56 execution.setVariable("prefix", Prefix)
58 String serviceInstanceId = execution.getVariable("serviceInstanceId")
59 String operationId = execution.getVariable("operationId")
60 String parentServiceInstanceId = execution.getVariable("parentServiceInstanceId")
61 String parentOperationId = execution.getVariable("parentOperationId")
63 if (isBlank(serviceInstanceId) || isBlank(operationId)) {
64 String msg = "Exception in" + Prefix + "preProcessRequest: Input serviceInstanceId or operationId is null"
65 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
68 if (isBlank(parentServiceInstanceId) || isBlank(parentOperationId)) {
69 execution.setVariable("isNeedUpdateParentStatus", false)
72 String globalSubscriberId = execution.getVariable("globalSubscriberId")
73 if (isBlank(globalSubscriberId)) {
74 execution.setVariable("globalSubscriberId", "5GCustomer")
77 // serviceType: type of service
78 String serviceType = execution.getVariable("processServiceType")
79 if (isBlank(serviceType)) {
80 execution.setVariable("processServiceType", "service")
83 // operationType: type of service
84 String operationType = execution.getVariable("operationType")
85 if (isBlank(operationType)) {
86 execution.setVariable("operationType", "CREATE")
89 //successConditions: processing end success conditions
90 List<String> successConditions = execution.getVariable("successConditions") as List
92 //errorConditions: processing end error conditions
93 List<String> errorConditions = execution.getVariable("errorConditions") as List
95 if ((successConditions == null || successConditions.size() < 1)
96 && (errorConditions == null || errorConditions.size() < 1)) {
97 String msg = "Exception in" + Prefix + "preProcessRequest: conditions is null"
98 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100 for (int i = 0; i < successConditions.size(); i++) {
101 String condition = successConditions.get(i)
102 successConditions.set(i, condition.toLowerCase())
104 for (int i = 0; i < errorConditions.size(); i++) {
105 String condition = errorConditions.get(i)
106 errorConditions.set(i, condition.toLowerCase())
110 execution.setVariable("startTime", System.currentTimeMillis())
112 String initProgress = execution.getVariable("initProgress")
114 if (isBlank(initProgress)) {
115 execution.setVariable("initProgress", 0)
118 String endProgress = execution.getVariable("endProgress")
120 if (isBlank(endProgress)) {
121 execution.setVariable("endProgress", 100)
124 execution.setVariable("progress", 0)
125 logger.debug(Prefix + "preProcessRequest Exit")
130 * check service status through request operation id, update operation status
132 def preCheckServiceStatusReq = { DelegateExecution execution ->
133 logger.trace(Prefix + "preCheckServiceStatusReq Start")
134 String serviceInstanceId = execution.getVariable("serviceInstanceId") as String
135 String operationId = execution.getVariable("operationId") as String
136 requestDBUtil.getOperationStatus(execution, serviceInstanceId, operationId)
137 logger.trace(Prefix + "preCheckServiceStatusReq Exit")
142 * handle service status, if service status is finished or error, set the service status
145 def handlerServiceStatusResp = { DelegateExecution execution ->
146 logger.trace(Prefix + "handlerServiceStatusResp Start")
149 def dbResponseCode = execution.getVariable("dbResponseCode") as Integer
150 if (dbResponseCode >= 200 && dbResponseCode < 400) {
151 String dbResponse = execution.getVariable("dbResponse")
152 def dbResponseJson = jsonUtil.xml2json(dbResponse) as String
154 String result = jsonUtil.getJsonValue(dbResponseJson,
155 "Envelope.Body.getServiceOperationStatusResponse.return.result")
157 if (isSuccessCompleted(execution, result)) {
159 handlerSuccess(execution, result)
160 execution.setVariable("isAllFinished", "true")
162 logger.debug(Prefix + "handlerServiceStatusResp: service success finished, dbResponse_result: "
165 } else if (isErrorCompleted(execution, result)) {
167 handlerError(execution, result)
168 execution.setVariable("isAllFinished", "true")
170 logger.debug(Prefix + "handlerServiceStatusResp: service error finished, dbResponse_result: "
174 String progress = jsonUtil.getJsonValue(dbResponseJson,
175 "Envelope.Body.getServiceOperationStatusResponse.return.progress")
177 String oldProgress = execution.getVariable("progress")
179 if (progress == oldProgress) {
180 execution.setVariable("isNeedUpdateDB", false)
182 execution.setVariable("progress", progress)
183 execution.setVariable("isNeedUpdateDB", true)
185 execution.setVariable("isAllFinished", "false")
186 TimeUnit.SECONDS.sleep(10)
189 execution.setVariable("isAllFinished", "false")
191 TimeUnit.MILLISECONDS.sleep(10)
194 } catch (BpmnError e) {
196 } catch (Exception ex) {
197 msg = "Exception in " + Prefix + "handlerServiceStatusResp: " + ex.getMessage()
199 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
202 logger.trace(Prefix + "handlerServiceStatusResp Exit")
206 def timeWaitDelay = { DelegateExecution execution ->
208 Long startTime = execution.getVariable("startTime") as Long
209 Long timeOut = execution.getVariable("timeOut") as Long
211 timeOut = timeOut == null ? 3 * 60 * 60 * 1000 : timeOut
213 if (System.currentTimeMillis() - startTime > timeOut) {
215 handlerTimeOut(execution)
216 execution.setVariable("isTimeOut", "YES")
219 execution.setVariable("isTimeOut", "NO")
224 private handlerTimeOut = { DelegateExecution execution ->
226 Map<String, Object> paramMap = execution.getVariable("timeOutParamMap") as Map
228 handlerProcess(execution, "error", paramMap, "error", "with timeout")
232 private handlerSuccess = { DelegateExecution execution, String result ->
234 Map<String, Object> paramMap = execution.getVariable("successParamMap") as Map
236 handlerProcess(execution, result, paramMap, "deactivated", "success")
240 private handlerError = { DelegateExecution execution, String result ->
242 Map<String, Object> paramMap = execution.getVariable("errorParamMap") as Map
244 handlerProcess(execution, result, paramMap, "error", "with error")
248 private handlerProcess = { DelegateExecution execution, String result, def paramMap, def status, def msg ->
250 if (paramMap != null) {
251 for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
252 execution.setVariable(entry.getKey(), entry.getValue())
257 if (isBlank(execution.getVariable("operationStatus") as String)) {
258 execution.setVariable("operationStatus", result)
262 if (isBlank(execution.getVariable("operationContent") as String)) {
263 String operationContent = execution.getVariable("processServiceType") + " " +
264 execution.getVariable("operationType") + " operation finished " + msg
265 execution.setVariable("operationContent", operationContent)
268 if (isBlank(execution.getVariable("orchestrationStatus") as String)) {
269 execution.setVariable("orchestrationStatus", status)
276 * judge if the service processing success finished
278 private isSuccessCompleted = { DelegateExecution execution, String result ->
280 //successConditions: processing end success conditions
281 List<String> successConditions = execution.getVariable("successConditions") as List
283 result = result.toLowerCase()
284 if (successConditions.contains(result)) {
292 * judge if the service processing error finished
294 private isErrorCompleted = { DelegateExecution execution, String result ->
296 //errorConditions: processing end error conditions
297 List<String> errorConditions = execution.getVariable("errorConditions") as List
299 result = result.toLowerCase()
300 if (errorConditions.contains(result)) {
307 def preUpdateOperationProgress = { DelegateExecution execution ->
308 logger.trace(Prefix + "prepareUpdateOperationStatus Start")
310 def progress = execution.getVariable("progress") as Integer
311 def initProgress = execution.getVariable("initProgress") as Integer
312 def endProgress = execution.getVariable("endProgress") as Integer
314 def resProgress = (initProgress + (endProgress - initProgress) / 100 * progress) as Integer
316 def operationType = execution.getVariable("operationType")
317 def operationContent = execution.getVariable("processServiceType") + " " +
318 operationType + " operation processing " + resProgress
320 // update status creating
321 OperationStatus status = new OperationStatus()
322 status.setServiceId(execution.getVariable("parentServiceInstanceId") as String)
323 status.setOperationId(execution.getVariable("parentOperationId") as String)
324 status.setOperation(operationType as String)
325 status.setResult("processing")
326 status.setProgress(resProgress as String)
327 status.setOperationContent(operationContent as String)
328 status.setUserId(execution.getVariable("globalSubscriberId") as String)
330 requestDBUtil.prepareUpdateOperationStatus(execution, status)
331 logger.trace(Prefix + "prepareUpdateOperationStatus Exit")