eabe58488e9342fbf734cc32a889c6cdfb58450d
[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     RequestDBUtil requestDBUtil = new RequestDBUtil()
39
40     private static final Logger logger = LoggerFactory.getLogger(ActivateSliceSubnet.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             //subscriberInfo
58             String globalSubscriberId = jsonUtil.getJsonValue(subnetInstanceReq, "globalSubscriberId")
59             if (isBlank(globalSubscriberId)) {
60                 msg = "Input globalSubscriberId' is null"
61                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
62             } else {
63                 execution.setVariable("globalSubscriberId", globalSubscriberId)
64             }
65
66             //NSSI ID
67             String serviceInstanceID = jsonUtil.getJsonValue(subnetInstanceReq, "serviceInstanceID")
68             if (isBlank(serviceInstanceID)) {
69                 msg = "Input serviceInstanceID is null"
70                 logger.debug(msg)
71                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
72             } else
73             {
74                 execution.setVariable("serviceInstanceID", serviceInstanceID)
75             }
76             String nsiId = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties.nsiId")
77             if (isBlank(nsiId)) {
78                 msg = "Input nsiId is null"
79                 logger.debug(msg)
80                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
81             } else
82             {
83                 execution.setVariable("nsiId", nsiId)
84             }
85             String networkType = jsonUtil.getJsonValue(subnetInstanceReq, "networkType")
86             if (isBlank(networkType)) {
87                 msg = "Input networkType is null"
88                 logger.debug(msg)
89                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
90             } else
91             {
92                 execution.setVariable("networkType", networkType.toUpperCase())
93             }
94             //requestParameters, subscriptionServiceType is 5G
95             String subscriptionServiceType = jsonUtil.getJsonValue(subnetInstanceReq, "subscriptionServiceType")
96             if (isBlank(subscriptionServiceType)) {
97                 msg = "Input subscriptionServiceType is null"
98                 logger.debug(msg)
99                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100             } else {
101                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
102             }
103
104             //operationType = deactivateInstance/activateInstance
105             String operationType = execution.getVariable("requestAction")
106             if (isBlank(operationType)) {
107                 msg = "Input operationType is null"
108                 logger.debug(msg)
109                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
110             } else {
111                 execution.setVariable("operationType", operationType)
112             }
113
114             String jobId = UUID.randomUUID().toString()
115             execution.setVariable("jobId", jobId)
116
117             String sliceParams = jsonUtil.getJsonValue(subnetInstanceReq, "additionalProperties")
118             execution.setVariable("sliceParams", sliceParams)
119
120         } catch(BpmnError e) {
121             throw e
122         } catch(Exception ex) {
123             msg = "Exception in ActivateSliceSubnet.preProcessRequest " + ex.getMessage()
124             logger.debug(msg)
125             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
126         }
127         logger.debug(Prefix + "preProcessRequest Exit")
128     }
129
130
131     /**
132      * create operation status in request db
133      *
134      * Init the Operation Status
135      */
136     def prepareInitOperationStatus = { DelegateExecution execution ->
137         logger.debug(Prefix + "prepareInitOperationStatus Start")
138
139         String serviceId = execution.getVariable("serviceInstanceID")
140         String jobId = execution.getVariable("jobId")
141         String nsiId = execution.getVariable("nsiId")
142         String operationType = execution.getVariable("operationType")
143         logger.debug("Generated new job for Service Instance serviceId:" + serviceId + " jobId:" + jobId)
144
145         ResourceOperationStatus initStatus = new ResourceOperationStatus()
146         initStatus.setServiceId(serviceId)
147         initStatus.setOperationId(jobId)
148         initStatus.setResourceTemplateUUID(nsiId)
149         initStatus.setOperType(operationType)
150         requestDBUtil.prepareInitResourceOperationStatus(execution, initStatus)
151
152         logger.debug(Prefix + "prepareInitOperationStatus Exit")
153     }
154
155
156
157     /**
158      * return sync response
159      */
160     def sendSyncResponse = { DelegateExecution execution ->
161         logger.debug(Prefix + "sendSyncResponse Start")
162         try {
163             String jobId = execution.getVariable("jobId")
164             String activateSyncResponse = """{"jobId": "${jobId}","status": "processing"}"""
165                                                 .trim().replaceAll(" ", "")
166
167             logger.debug("sendSyncResponse to APIH:" + "\n" + activateSyncResponse)
168             sendWorkflowResponse(execution, 202, activateSyncResponse)
169
170             execution.setVariable("sentSyncResponse", true)
171         } catch (Exception ex) {
172             String msg = "Exception in sendSyncResponse:" + ex.getMessage()
173             logger.debug(msg)
174             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
175         }
176         logger.debug(Prefix + "sendSyncResponse Exit")
177     }
178
179 }