78cafa7be5ed57806fdcc25238bfa5e196efd1e1
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / AllocateSliceSubnet.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 import static org.apache.commons.lang3.StringUtils.isBlank
33
34 class AllocateSliceSubnet extends AbstractServiceTaskProcessor {
35
36     String Prefix="AllocateSliceSubnet_"
37     ExceptionUtil exceptionUtil = new ExceptionUtil()
38     RequestDBUtil requestDBUtil = new RequestDBUtil()
39     JsonUtils jsonUtil = new JsonUtils()
40     private static final Logger logger = LoggerFactory.getLogger(AllocateSliceSubnet.class)
41
42     @Override
43     void preProcessRequest(DelegateExecution execution) {
44         logger.debug(Prefix + "preProcessRequest Start")
45         execution.setVariable("prefix", Prefix)
46         execution.setVariable("startTime", System.currentTimeMillis())
47         def msg
48         try {
49             // get request input
50             String subnetInstanceReq = execution.getVariable("bpmnRequest")
51             logger.debug(subnetInstanceReq)
52
53             String requestId = execution.getVariable("mso-request-id")
54             execution.setVariable("msoRequestId", requestId)
55             logger.debug("Input Request:" + subnetInstanceReq + " reqId:" + requestId)
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             {
65                 execution.setVariable("modelInvariantUuid", modelInvariantUuid)
66             }
67
68             logger.debug("modelInvariantUuid: " + modelInvariantUuid)
69
70             String modelUuid = jsonUtil.getJsonValue(subnetInstanceReq, "modelUuid")
71             if (isBlank(modelUuid)) {
72                 msg = "Input modelUuid is null"
73                 logger.debug(msg)
74                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
75             } else
76             {
77                 execution.setVariable("modelUuid", modelUuid)
78             }
79
80             logger.debug("modelUuid: " + modelUuid)
81
82
83             //subscriberInfo
84             String globalSubscriberId = jsonUtil.getJsonValue(subnetInstanceReq, "globalSubscriberId")
85             if (isBlank(globalSubscriberId)) {
86                 msg = "Input globalSubscriberId' is null"
87                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
88             } else {
89                 execution.setVariable("globalSubscriberId", globalSubscriberId)
90             }
91             String dummyServiceId = new UUID(0,0).toString();
92             execution.setVariable("dummyServiceId", dummyServiceId)
93             logger.debug("dummyServiceId: " + dummyServiceId)
94             String servicename = jsonUtil.getJsonValue(subnetInstanceReq, "name")
95             execution.setVariable("servicename", servicename)
96
97             String sST = jsonUtil.getJsonValue(subnetInstanceReq, "sst")
98             execution.setVariable("sst", sST)
99
100             String nsiId = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties.nsiInfo.nsiId")
101             if (isBlank(nsiId)) {
102                 msg = "Input nsiId is null"
103                 logger.debug(msg)
104                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105             } else
106             {
107                 execution.setVariable("nsiId", nsiId)
108             }
109
110             String networkType = jsonUtil.getJsonValue(subnetInstanceReq, "networkType")
111             if (isBlank(networkType)) {
112                 msg = "Input networkType is null"
113                 logger.debug(msg)
114                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
115             } else
116             {
117                 execution.setVariable("networkType", networkType.toUpperCase())
118             }
119
120             //requestParameters, subscriptionServiceType is 5G
121             String subscriptionServiceType = jsonUtil.getJsonValue(subnetInstanceReq, "subscriptionServiceType")
122             if (isBlank(subscriptionServiceType)) {
123                 msg = "Input subscriptionServiceType is null"
124                 logger.debug(msg)
125                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
126             } else {
127                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
128             }
129
130             String jobId = UUID.randomUUID().toString()
131             execution.setVariable("jobId", jobId)
132
133             String sliceParams = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties")
134             execution.setVariable("sliceParams", sliceParams)
135
136         } catch(BpmnError e) {
137             throw e
138         } catch(Exception ex) {
139             msg = "Exception in AllocateSliceSubnet.preProcessRequest " + ex.getMessage()
140             logger.debug(msg)
141             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
142         }
143         logger.debug(Prefix + "preProcessRequest Exit")
144     }
145
146
147     /**
148      * create operation status in request db
149      *
150      * Init the Operation Status
151      */
152     def prepareInitOperationStatus = { DelegateExecution execution ->
153         logger.debug(Prefix + "prepareInitOperationStatus Start")
154
155         String modelUuid = execution.getVariable("modelUuid")
156         String jobId = execution.getVariable("jobId")
157         String nsiId = execution.getVariable("nsiId")
158         logger.debug("Generated new job for Service Instance serviceId:" + modelUuid + " jobId:" + jobId)
159
160         ResourceOperationStatus initStatus = new ResourceOperationStatus()
161         initStatus.setServiceId(nsiId)  // set nsiId to this field
162         initStatus.setOperationId(jobId)    // set jobId to this field
163         initStatus.setResourceTemplateUUID(modelUuid)   // set modelUuid to this field
164         initStatus.setOperType("ALLOCATE")
165         //initStatus.setResourceInstanceID() // set nssiId to this field
166         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
167
168         logger.debug(Prefix + "prepareInitOperationStatus Exit")
169     }
170
171
172     /**
173      * return sync response
174      */
175     def sendSyncResponse = { DelegateExecution execution ->
176         logger.debug(Prefix + "sendSyncResponse Start")
177         try {
178             String jobId = execution.getVariable("jobId")
179             String allocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
180                                                 .trim().replaceAll(" ", "").trim().replaceAll(" ", "")
181
182             logger.debug("sendSyncResponse to APIH:" + "\n" + allocateSyncResponse)
183             sendWorkflowResponse(execution, 202, allocateSyncResponse)
184
185             execution.setVariable("sentSyncResponse", true)
186         } catch (Exception ex) {
187             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
188             logger.debug(msg)
189             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
190         }
191         logger.debug(Prefix + "sendSyncResponse Exit")
192     }
193
194 }