Merge "bump the so version to 1.7.4"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeallocateNSSI.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 package org.onap.so.bpmn.infrastructure.scripts
21
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
25 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
26 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
27 import org.onap.so.beans.nsmf.DeAllocateNssi
28 import org.onap.so.beans.nsmf.EsrInfo
29 import org.onap.so.beans.nsmf.NetworkType
30 import org.onap.so.beans.nsmf.NssiResponse
31 import org.onap.so.beans.nsmf.ServiceInfo
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
35 import org.onap.so.bpmn.common.scripts.RequestDBUtil
36 import org.onap.so.bpmn.core.domain.ServiceArtifact
37 import org.onap.so.bpmn.core.domain.ServiceDecomposition
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.db.request.beans.OperationStatus
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
42
43
44 class DoDeallocateNSSI extends AbstractServiceTaskProcessor
45 {
46     private final String PREFIX ="DoDeallocateNSSI"
47
48     private ExceptionUtil exceptionUtil = new ExceptionUtil()
49     private JsonUtils jsonUtil = new JsonUtils()
50     ObjectMapper objectMapper = new ObjectMapper()
51     private RequestDBUtil requestDBUtil = new RequestDBUtil()
52     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
53
54     private static final Logger LOGGER = LoggerFactory.getLogger( DoDeallocateNSSI.class)
55
56     @Override
57     void preProcessRequest(DelegateExecution execution) {
58         LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")
59
60         def currentNSSI = execution.getVariable("currentNSSI")
61         if (!currentNSSI) {
62             String msg = "currentNSSI is null"
63             LOGGER.error(msg)
64             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
65         }
66
67         LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
68     }
69
70     /**
71      *
72      * @param execution
73      */
74     void prepareDecomposeService(DelegateExecution execution)
75     {
76         LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
77         try
78         {
79             def currentNSSI = execution.getVariable("currentNSSI")
80             String modelInvariantUuid = currentNSSI['modelInvariantId']
81             String modelVersionId = currentNSSI['modelVersionId']
82             String serviceModelInfo = """{
83             "modelInvariantUuid":"${modelInvariantUuid}",
84             "modelUuid":"${modelVersionId}",
85             "modelVersion":""
86              }"""
87             execution.setVariable("serviceModelInfo", serviceModelInfo)
88         }
89         catch (any)
90         {
91             String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
92             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
93         }
94         LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
95     }
96
97     /**
98      * get vendor Info
99      * @param execution
100      */
101     void processDecomposition(DelegateExecution execution) {
102         LOGGER.debug("*****${PREFIX} start processDecomposition *****")
103
104         try {
105             ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
106             ServiceArtifact serviceArtifact = serviceDecomposition ?.getServiceInfo()?.getServiceArtifact()?.get(0)
107             String content = serviceArtifact.getContent()
108             String vendor = jsonUtil.getJsonValue(content, "metadata.vendor")
109             String domainType  = jsonUtil.getJsonValue(content, "metadata.domainType")
110
111             def currentNSSI = execution.getVariable("currentNSSI")
112             currentNSSI['vendor'] = vendor
113             currentNSSI['domainType'] = domainType
114             LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType))
115
116         } catch (any) {
117             String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
118             LOGGER.debug(exceptionMessage)
119             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
120         }
121         LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
122     }
123     
124     /**
125      * send deallocate request to nssmf
126      * @param execution
127      */
128     void sendRequestToNSSMF(DelegateExecution execution)
129     {
130         LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
131         def currentNSSI = execution.getVariable("currentNSSI")
132         String snssai= currentNSSI['snssai']
133         String profileId = currentNSSI['profileId']
134         String nssiId = currentNSSI['nssiServiceInstanceId']
135         String nsiId = currentNSSI['nsiServiceInstanceId']
136         String scriptName = execution.getVariable("scriptName")
137
138         String serviceInvariantUuid = currentNSSI['modelInvariantId']
139         String serviceUuid = currentNSSI['modelId']
140         String globalSubscriberId = currentNSSI['globalSubscriberId']
141         String subscriptionServiceType = execution.getVariable("serviceType")
142         
143         DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
144         deAllocateNssi.setNsiId(nsiId)
145         deAllocateNssi.setNssiId(nssiId)
146         deAllocateNssi.setTerminateNssiOption(0)
147         deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
148         deAllocateNssi.setScriptName(scriptName)
149         
150         ServiceInfo serviceInfo = new ServiceInfo()
151         serviceInfo.setServiceInvariantUuid(serviceInvariantUuid)
152         serviceInfo.setServiceUuid(serviceUuid)
153         serviceInfo.setNsiId(nsiId)
154         serviceInfo.setGlobalSubscriberId(globalSubscriberId)
155         serviceInfo.setSubscriptionServiceType(subscriptionServiceType)
156         String serviceInfoString = objectMapper.writeValueAsString(serviceInfo)
157         
158         EsrInfo esrInfo = getEsrInfo(currentNSSI)
159         String esrInfoString = objectMapper.writeValueAsString(esrInfo)
160         
161         execution.setVariable("deAllocateNssi",deAllocateNssi)
162         execution.setVariable("esrInfo", esrInfoString)
163         execution.setVariable("serviceInfo", serviceInfoString)
164         String nssmfRequest = """
165                 {
166                   "deAllocateNssi": ${objectMapper.writeValueAsString(deAllocateNssi)},
167                   "esrInfo":  ${esrInfoString},
168                   "serviceInfo": ${serviceInfoString}
169                 }
170               """
171
172         String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s", profileId)
173
174         NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
175         if (nssmfResponse != null) {
176             currentNSSI['jobId']= nssmfResponse.getJobId() ?: "" 
177             currentNSSI['jobProgress'] = 0            
178             execution.setVariable("currentNSSI", currentNSSI)    
179         } else {
180             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
181         }
182         LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
183     }
184
185 /**
186      * send to nssmf query progress
187      * @param execution
188      */
189     void prepareJobStatusRequest(DelegateExecution execution)
190     {
191         def currentNSSI = execution.getVariable("currentNSSI")
192         String jobId = currentNSSI['jobId']
193
194         execution.setVariable("responseId", "3")
195         execution.setVariable("jobId", jobId)
196     }
197
198     
199     /**
200      * send to nssmf query progress
201      * @param execution
202      */
203     void handleJobStatus(DelegateExecution execution)
204     {
205         try 
206         {
207         String jobStatusResponse = execution.getVariable("responseDescriptor")
208         String status = jsonUtil.getJsonValue(jobStatusResponse,"status")
209         def statusDescription = jsonUtil.getJsonValue(jobStatusResponse,"statusDescription")
210         def progress = jsonUtil.getJsonValue(jobStatusResponse,"progress")
211         if(!status.equalsIgnoreCase("failed"))
212         {
213             if(!progress)
214             {
215                 LOGGER.error("job progress is null or empty!")
216                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
217             }
218             def currentNSSI = execution.getVariable("currentNSSI")
219             int oldProgress = currentNSSI['jobProgress']
220             int currentProgress = Integer.parseInt(progress)
221
222             execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
223             execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
224             currentNSSI['jobProgress'] = currentProgress
225             currentNSSI['status'] = status
226             currentNSSI['statusDescription'] = statusDescription
227
228             String nssiId = currentNSSI['nssiServiceInstanceId']
229             String nsiId = currentNSSI['nsiServiceInstanceId']
230             LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
231         }
232           else {
233             execution.setVariable("isNeedUpdateDB", "true")
234             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
235         }
236         }
237         catch (any)
238         {
239             String msg = "Received a Bad Response from NSSMF. cause-"+any.getCause()
240             LOGGER.error(any.printStackTrace())
241             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
242         }
243     }
244
245     private EsrInfo getEsrInfo(def currentNSSI)
246     {
247         String domaintype = currentNSSI['domainType']
248         String vendor = currentNSSI['vendor']
249         
250         EsrInfo info = new EsrInfo()
251         info.setNetworkType(NetworkType.fromString(domaintype))
252         info.setVendor(vendor)
253         return info
254     }
255
256  /**
257      * handle job status
258      * prepare update requestdb
259      * @param execution
260      */
261     void prepareUpdateOperationStatus(DelegateExecution execution)
262     {
263         def currentNSSI = execution.getVariable("currentNSSI")
264         int currentProgress = currentNSSI["jobProgress"]
265         def proportion = currentNSSI['proportion']
266         def statusDes = currentNSSI["statusDescription"]
267         int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
268         def status = currentNSSI['status']
269         
270         OperationStatus operationStatus = new OperationStatus()
271         operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
272         operationStatus.setOperationId(currentNSSI['operationId'] as String)
273         operationStatus.setOperation("DELETE")
274         operationStatus.setResult(status as String)
275         operationStatus.setProgress(progress as String)
276         operationStatus.setOperationContent(statusDes as String)
277         requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
278         LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
279     }
280     
281     /**
282      * delete slice profile from aai
283      * @param execution
284      */
285     void delSliceProfileFromAAI(DelegateExecution execution)
286     {
287         LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
288         def currentNSSI = execution.getVariable("currentNSSI")
289         String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
290         String profileId = currentNSSI['profileId']
291         String globalSubscriberId = currentNSSI["globalSubscriberId"]
292         String serviceType = execution.getVariable("serviceType")
293
294         try
295         {
296             LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
297             AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
298                 AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(nssiServiceInstanceId).sliceProfile(profileId))
299             if (!getAAIClient().exists(resourceUri)) {
300                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
301             }
302             getAAIClient().delete(resourceUri)
303         }
304         catch (any)
305         {
306             String msg = "delete slice profile from aai failed! cause-"+any.getCause()
307             LOGGER.error(any.printStackTrace())
308             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
309         }
310         LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")
311     }
312 }