Fixes for bugs found in Transport Slicing integration
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeallocateTnNssi.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
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 com.fasterxml.jackson.databind.ObjectMapper
24 import groovy.json.JsonSlurper
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.ServiceInstance
28 import org.onap.aaiclient.client.aai.AAIObjectType
29 import org.onap.aaiclient.client.aai.AAIResourcesClient
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
34 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
35 import org.onap.so.bpmn.common.scripts.ExceptionUtil
36 import org.onap.so.bpmn.common.scripts.RequestDBUtil
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.db.request.beans.ResourceOperationStatus
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
41
42 class DoDeallocateTnNssi extends AbstractServiceTaskProcessor {
43     String Prefix = "TNDEALLOC_"
44
45     ExceptionUtil exceptionUtil = new ExceptionUtil()
46     JsonUtils jsonUtil = new JsonUtils()
47     RequestDBUtil requestDBUtil = new RequestDBUtil()
48     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
49     JsonSlurper jsonSlurper = new JsonSlurper()
50     ObjectMapper objectMapper = new ObjectMapper()
51     private static final Logger logger = LoggerFactory.getLogger(DoDeallocateTnNssi.class)
52
53
54     void preProcessRequest(DelegateExecution execution) {
55         logger.debug("Start preProcessRequest")
56
57         execution.setVariable("startTime", System.currentTimeMillis())
58         String msg = tnNssmfUtils.getExecutionInputParams(execution)
59         logger.debug("Deallocate TN NSSI input parameters: " + msg)
60
61         execution.setVariable("prefix", Prefix)
62
63         tnNssmfUtils.setSdncCallbackUrl(execution, true)
64         logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
65
66         String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
67         execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
68
69         String sliceServiceInstanceName = execution.getVariable("servicename")
70         execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
71
72
73         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
74         String modelUuid = execution.getVariable("modelUuid")
75         //here modelVersion is not set, we use modelUuid to decompose the service.
76         def isDebugLogEnabled = true
77         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
78         String serviceModelInfo = """{
79             "modelInvariantUuid":"${modelInvariantUuid}",
80             "modelUuid":"${modelUuid}",
81             "modelVersion":""
82              }"""
83         execution.setVariable("serviceModelInfo", serviceModelInfo)
84         logger.debug("Finish preProcessRequest")
85     }
86
87     void preprocessSdncDeallocateTnNssiRequest(DelegateExecution execution) {
88         def method = getClass().getSimpleName() + '.preprocessSdncDeallocateTnNssiRequest(' +
89                 'execution=' + execution.getId() + ')'
90         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
91         logger.trace('Entered ' + method)
92
93         try {
94             String serviceInstanceId = execution.getVariable("serviceInstanceID")
95
96             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "delete")
97
98             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
99             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
100
101         } catch (Exception e) {
102             logger.debug("Exception Occured Processing preprocessSdncDeallocateTnNssiRequest. Exception is:\n" + e)
103             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
104         }
105         logger.trace("COMPLETED preprocessSdncDeallocateTnNssiRequest Process")
106     }
107
108
109     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
110         tnNssmfUtils.validateSDNCResponse(execution, response, method)
111     }
112
113     void deleteServiceInstance(DelegateExecution execution) {
114         try {
115             AAIResourcesClient client = getAAIClient()
116             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
117             client.delete(uri)
118         } catch (BpmnError e) {
119             throw e
120         } catch (Exception ex) {
121             String msg = "Exception in DoDeallocateTnNssi.deleteServiceInstance. " + ex.getMessage()
122             logger.info(msg)
123             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
124         }
125     }
126
127     public void updateAAIOrchStatus(DelegateExecution execution) {
128         logger.debug("Start updateAAIOrchStatus")
129         String tnNssiId = execution.getVariable("sliceServiceInstanceId")
130         String orchStatus = execution.getVariable("orchestrationStatus")
131
132         try {
133             ServiceInstance si = new ServiceInstance()
134             si.setOrchestrationStatus(orchStatus)
135             AAIResourcesClient client = getAAIClient()
136             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(tnNssiId))
137             client.update(uri, si)
138         } catch (BpmnError e) {
139             throw e
140         } catch (Exception ex) {
141             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
142             logger.info(msg)
143             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
144         }
145
146         logger.debug("Finish updateAAIOrchStatus")
147     }
148
149     void prepareUpdateJobStatus(DelegateExecution execution,
150                                 String status,
151                                 String progress,
152                                 String statusDescription) {
153         String serviceId = execution.getVariable("sliceServiceInstanceId")
154         String jobId = execution.getVariable("jobId")
155         String nsiId = execution.getVariable("nsiId")
156
157         ResourceOperationStatus roStatus = new ResourceOperationStatus()
158         roStatus.setServiceId(serviceId)
159         roStatus.setOperationId(jobId)
160         roStatus.setResourceTemplateUUID(nsiId)
161         roStatus.setOperType("Deallocate")
162         roStatus.setProgress(progress)
163         roStatus.setStatus(status)
164         roStatus.setStatusDescription(statusDescription)
165         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
166     }
167 }
168