SO changes for Service Intent
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / CreateServiceIntentInstance.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, Wipro Limited.
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 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
32
33 import static org.apache.commons.lang3.StringUtils.isBlank
34
35 class CreateServiceIntentInstance extends AbstractServiceTaskProcessor {
36
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)
43
44     @Override
45     void preProcessRequest(DelegateExecution execution) {
46         logger.debug(Prefix + "preProcessRequest Start")
47         execution.setVariable("prefix", Prefix)
48         execution.setVariable("startTime", System.currentTimeMillis())
49         def msg
50         try {
51             // get request input
52             String subnetInstanceReq = execution.getVariable("bpmnRequest")
53             logger.debug(subnetInstanceReq)
54
55             serviceIntentUtils.setCommonExecutionVars(execution)
56
57             //modelInfo
58             String modelInvariantUuid = jsonUtil.getJsonValue(subnetInstanceReq, "modelInvariantUuid")
59             if (isBlank(modelInvariantUuid)) {
60                 msg = "Input modelInvariantUuid is null"
61                 logger.debug(msg)
62                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
63             } else {
64                 execution.setVariable("modelInvariantUuid", modelInvariantUuid)
65             }
66
67             logger.debug("modelInvariantUuid: " + modelInvariantUuid)
68
69             String modelUuid = jsonUtil.getJsonValue(subnetInstanceReq, "modelUuid")
70             if (isBlank(modelUuid)) {
71                 msg = "Input modelUuid is null"
72                 logger.debug(msg)
73                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
74             } else {
75                 execution.setVariable("modelUuid", modelUuid)
76             }
77
78             logger.debug("modelUuid: " + modelUuid)
79
80             String additionalPropJsonStr = execution.getVariable("serviceIntentParams")
81             String siId = jsonUtil.getJsonValue(additionalPropJsonStr, "serviceInstanceID") //for debug
82             if (isBlank(siId)) {
83                 siId = UUID.randomUUID().toString()
84             }
85
86             logger.debug("serviceInstanceID: " + modelUuid)
87             execution.setVariable("serviceInstanceID", siId)
88
89             String sST = jsonUtil.getJsonValue(subnetInstanceReq, "sst")
90             execution.setVariable("sst", sST)
91
92             String jobId = UUID.randomUUID().toString()
93             execution.setVariable("jobId", jobId)
94
95         } catch (BpmnError e) {
96             throw e
97         } catch (Exception ex) {
98             msg = "Exception in CreateServiceIntentInstance.preProcessRequest " + ex.getMessage()
99             logger.debug(msg)
100             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
101         }
102         logger.debug(Prefix + "preProcessRequest Exit")
103     }
104
105
106     /**
107      * create operation status in request db
108      *
109      * Init the Operation Status
110      */
111     def prepareInitOperationStatus = { DelegateExecution execution ->
112         logger.debug(Prefix + "prepareInitOperationStatus Start")
113
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)
118
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)
126
127         logger.debug(Prefix + "prepareInitOperationStatus Exit")
128     }
129
130
131     /**
132      * return sync response
133      */
134     def sendSyncResponse = { DelegateExecution execution ->
135         logger.debug(Prefix + "sendSyncResponse Start")
136         try {
137             String jobId = execution.getVariable("jobId")
138             String allocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
139                     .trim().replaceAll(" ", "").trim().replaceAll(" ", "")
140
141             logger.debug("sendSyncResponse to APIH:" + "\n" + allocateSyncResponse)
142             sendWorkflowResponse(execution, 202, allocateSyncResponse)
143
144             execution.setVariable("sentSyncResponse", true)
145         } catch (Exception ex) {
146             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
147             logger.debug(msg)
148             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
149         }
150         logger.debug(Prefix + "sendSyncResponse Exit")
151     }
152
153 }