5a7722d67948163bff7f46453dd9811d760baca5
[so.git] /
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="ASS_"
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 nsiId = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties.nsiInfo.nsiId")
98                         if (isBlank(nsiId)) {
99                                 msg = "Input nsiId is null"
100                                 logger.debug(msg)
101                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
102                         } else
103                         {
104                                 execution.setVariable("nsiId", nsiId)
105                         }
106
107                         String networkType = jsonUtil.getJsonValue(subnetInstanceReq, "networkType")
108                         if (isBlank(networkType)) {
109                                 msg = "Input networkType is null"
110                                 logger.debug(msg)
111                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
112                         } else
113                         {
114                                 execution.setVariable("networkType", networkType.toUpperCase())
115                         }
116
117                         //requestParameters, subscriptionServiceType is 5G
118                         String subscriptionServiceType = jsonUtil.getJsonValue(subnetInstanceReq, "subscriptionServiceType")
119                         if (isBlank(subscriptionServiceType)) {
120                                 msg = "Input subscriptionServiceType is null"
121                                 logger.debug(msg)
122                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
123                         } else {
124                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
125                         }
126
127                         String jobId = UUID.randomUUID().toString()
128                         execution.setVariable("jobId", jobId)
129
130                         String sliceParams = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties")
131                         execution.setVariable("sliceParams", sliceParams)
132
133                 } catch(BpmnError e) {
134                         throw e
135                 } catch(Exception ex) {
136                         msg = "Exception in AllocateSliceSubnet.preProcessRequest " + ex.getMessage()
137                         logger.debug(msg)
138                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139                 }
140                 logger.debug(Prefix + "preProcessRequest Exit")
141         }
142
143
144         /**
145          * create operation status in request db
146          *
147          * Init the Operation Status
148          */
149         def prepareInitOperationStatus = { DelegateExecution execution ->
150                 logger.debug(Prefix + "prepareInitOperationStatus Start")
151
152                 String serviceId = execution.getVariable("dummyServiceId")
153                 String jobId = execution.getVariable("jobId")
154                 String nsiId = execution.getVariable("nsiId")
155                 logger.debug("Generated new job for Service Instance serviceId:" + serviceId + " jobId:" + jobId)
156
157                 ResourceOperationStatus initStatus = new ResourceOperationStatus()
158                 initStatus.setServiceId(serviceId)
159                 initStatus.setOperationId(jobId)
160                 initStatus.setResourceTemplateUUID(nsiId)
161                 initStatus.setOperType("Allocate")
162                 requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
163
164                 logger.debug(Prefix + "prepareInitOperationStatus Exit")
165         }
166
167
168         /**
169          * return sync response
170          */
171         def sendSyncResponse = { DelegateExecution execution ->
172                 logger.debug(Prefix + "sendSyncResponse Start")
173                 try {
174                         String jobId = execution.getVariable("jobId")
175                         String allocateSyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
176                                                                                                 .trim().replaceAll(" ", "").trim().replaceAll(" ", "")
177
178                         logger.debug("sendSyncResponse to APIH:" + "\n" + allocateSyncResponse)
179                         sendWorkflowResponse(execution, 202, allocateSyncResponse)
180
181                         execution.setVariable("sentSyncResponse", true)
182                 } catch (Exception ex) {
183                         String msg = "Exception in sendSyncResponse:" + ex.getMessage()
184                         logger.debug(msg)
185                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
186                 }
187                 logger.debug(Prefix + "sendSyncResponse Exit")
188         }
189         
190 }