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