2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, Wipro Limited.
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.db.request.beans.ResourceOperationStatus
30 import org.slf4j.Logger
31 import org.slf4j.LoggerFactory
33 import static org.apache.commons.lang3.StringUtils.isBlank
35 class CreateServiceIntentInstance extends AbstractServiceTaskProcessor {
37 String Prefix = "CreateSiInstance_"
38 ExceptionUtil exceptionUtil = new ExceptionUtil()
39 RequestDBUtil requestDBUtil = new RequestDBUtil()
40 JsonUtils jsonUtil = new JsonUtils()
41 ServiceIntentUtils serviceIntentUtils = new ServiceIntentUtils()
42 private static final Logger logger = LoggerFactory.getLogger(CreateServiceIntentInstance.class)
45 void preProcessRequest(DelegateExecution execution) {
46 logger.debug(Prefix + "preProcessRequest Start")
47 execution.setVariable("prefix", Prefix)
48 execution.setVariable("startTime", System.currentTimeMillis())
52 String subnetInstanceReq = execution.getVariable("bpmnRequest")
53 logger.debug(subnetInstanceReq)
55 serviceIntentUtils.setCommonExecutionVars(execution)
58 String modelInvariantUuid = jsonUtil.getJsonValue(subnetInstanceReq, "modelInvariantUuid")
59 if (isBlank(modelInvariantUuid)) {
60 msg = "Input modelInvariantUuid is null"
62 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
64 execution.setVariable("modelInvariantUuid", modelInvariantUuid)
67 logger.debug("modelInvariantUuid: " + modelInvariantUuid)
69 String modelUuid = jsonUtil.getJsonValue(subnetInstanceReq, "modelUuid")
70 if (isBlank(modelUuid)) {
71 msg = "Input modelUuid is null"
73 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
75 execution.setVariable("modelUuid", modelUuid)
78 logger.debug("modelUuid: " + modelUuid)
80 String additionalPropJsonStr = execution.getVariable("serviceIntentParams")
81 String siId = jsonUtil.getJsonValue(additionalPropJsonStr, "serviceInstanceID") //for debug
83 siId = UUID.randomUUID().toString()
86 logger.debug("serviceInstanceID: " + modelUuid)
87 execution.setVariable("serviceInstanceID", siId)
89 String sST = jsonUtil.getJsonValue(subnetInstanceReq, "sst")
90 execution.setVariable("sst", sST)
92 String jobId = UUID.randomUUID().toString()
93 execution.setVariable("jobId", jobId)
95 } catch (BpmnError e) {
97 } catch (Exception ex) {
98 msg = "Exception in CreateServiceIntentInstance.preProcessRequest " + ex.getMessage()
100 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
102 logger.debug(Prefix + "preProcessRequest Exit")
107 * create operation status in request db
109 * Init the Operation Status
111 def prepareInitOperationStatus = { DelegateExecution execution ->
112 logger.debug(Prefix + "prepareInitOperationStatus Start")
114 String modelUuid = execution.getVariable("modelUuid")
115 String jobId = execution.getVariable("jobId")
116 String nsiId = execution.getVariable("serviceInstanceID")
117 logger.debug("Generated new job for Service Instance serviceId:" + modelUuid + " jobId:" + jobId)
119 ResourceOperationStatus initStatus = new ResourceOperationStatus()
120 initStatus.setServiceId(nsiId) // set nsiId to this field
121 initStatus.setOperationId(jobId) // set jobId to this field
122 initStatus.setResourceTemplateUUID(modelUuid) // set modelUuid to this field
123 initStatus.setOperType("Create")
124 //initStatus.setResourceInstanceID() // set nssiId to this field
125 requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
127 logger.debug(Prefix + "prepareInitOperationStatus Exit")
132 * return sync response
134 def sendSyncResponse = { DelegateExecution execution ->
135 logger.debug(Prefix + "sendSyncResponse Start")
137 String jobId = execution.getVariable("jobId")
138 String allocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
139 .trim().replaceAll(" ", "").trim().replaceAll(" ", "")
141 logger.debug("sendSyncResponse to APIH:" + "\n" + allocateSyncResponse)
142 sendWorkflowResponse(execution, 202, allocateSyncResponse)
144 execution.setVariable("sentSyncResponse", true)
145 } catch (Exception ex) {
146 String msg = "Exception in sendSyncResponse:" + ex.getMessage()
148 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
150 logger.debug(Prefix + "sendSyncResponse Exit")