b09161d5cd0358ed5fd7840cf404ad3c6ac79cbd
[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.AAIResourcesClient
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.RequestDBUtil
36 import org.onap.so.bpmn.core.json.JsonUtils
37 import org.onap.so.db.request.beans.ResourceOperationStatus
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
40
41 import static org.apache.commons.lang3.StringUtils.isBlank
42 import static org.apache.commons.lang3.StringUtils.isEmpty
43
44 class DoDeallocateTnNssi extends AbstractServiceTaskProcessor {
45     String Prefix = "TNDEALLOC_"
46
47     ExceptionUtil exceptionUtil = new ExceptionUtil()
48     JsonUtils jsonUtil = new JsonUtils()
49     RequestDBUtil requestDBUtil = new RequestDBUtil()
50     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
51     JsonSlurper jsonSlurper = new JsonSlurper()
52     ObjectMapper objectMapper = new ObjectMapper()
53     private static final Logger logger = LoggerFactory.getLogger(DoDeallocateTnNssi.class)
54
55
56     void preProcessRequest(DelegateExecution execution) {
57         logger.debug("Start preProcessRequest")
58
59         execution.setVariable("startTime", System.currentTimeMillis())
60         String msg = tnNssmfUtils.getExecutionInputParams(execution)
61         logger.debug("Deallocate TN NSSI input parameters: " + msg)
62
63         execution.setVariable("prefix", Prefix)
64
65         tnNssmfUtils.setSdncCallbackUrl(execution, true)
66         logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
67
68         String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
69         execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
70
71         String sliceServiceInstanceName = execution.getVariable("servicename")
72         execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
73
74         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
75         String modelUuid = execution.getVariable("modelUuid")
76         if (isEmpty(modelUuid)) {
77             modelUuid = tnNssmfUtils.getModelUuidFromServiceInstance(execution.getVariable("serviceInstanceID"))
78         }
79         def isDebugLogEnabled = true
80         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
81         String serviceModelInfo = """{
82             "modelInvariantUuid":"${modelInvariantUuid}",
83             "modelUuid":"${modelUuid}",
84             "modelVersion":""
85              }"""
86         execution.setVariable("serviceModelInfo", serviceModelInfo)
87
88         String additionalPropJsonStr = execution.getVariable("sliceParams")
89         if (isBlank(additionalPropJsonStr) ||
90                 isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution,
91                         additionalPropJsonStr,
92                         "enableSdnc", "enableSdnc"))) {
93             tnNssmfUtils.setEnableSdncConfig(execution)
94         }
95
96         logger.debug("Finish preProcessRequest")
97     }
98
99     void preprocessSdncDeallocateTnNssiRequest(DelegateExecution execution) {
100         def method = getClass().getSimpleName() + '.preprocessSdncDeallocateTnNssiRequest(' +
101                 'execution=' + execution.getId() + ')'
102         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
103         logger.trace('Entered ' + method)
104
105         try {
106             String serviceInstanceId = execution.getVariable("serviceInstanceID")
107
108             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "delete")
109
110             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
111             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
112
113         } catch (Exception e) {
114             logger.debug("Exception Occured Processing preprocessSdncDeallocateTnNssiRequest. Exception is:\n" + e)
115             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
116         }
117         logger.trace("COMPLETED preprocessSdncDeallocateTnNssiRequest Process")
118     }
119
120
121     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
122         tnNssmfUtils.validateSDNCResponse(execution, response, method)
123     }
124
125     void deleteServiceInstance(DelegateExecution execution) {
126         try {
127             AAIResourcesClient client = getAAIClient()
128             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
129             client.delete(uri)
130         } catch (BpmnError e) {
131             throw e
132         } catch (Exception ex) {
133             String msg = "Exception in DoDeallocateTnNssi.deleteServiceInstance. " + ex.getMessage()
134             logger.info(msg)
135             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
136         }
137     }
138
139     public void updateAAIOrchStatus(DelegateExecution execution) {
140         logger.debug("Start updateAAIOrchStatus")
141         String tnNssiId = execution.getVariable("sliceServiceInstanceId")
142         String orchStatus = execution.getVariable("orchestrationStatus")
143
144         try {
145             ServiceInstance si = new ServiceInstance()
146             si.setOrchestrationStatus(orchStatus)
147             AAIResourcesClient client = getAAIClient()
148             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(tnNssiId))
149             client.update(uri, si)
150         } catch (BpmnError e) {
151             throw e
152         } catch (Exception ex) {
153             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
154             logger.info(msg)
155             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
156         }
157
158         logger.debug("Finish updateAAIOrchStatus")
159     }
160
161     void prepareUpdateJobStatus(DelegateExecution execution,
162                                 String status,
163                                 String progress,
164                                 String statusDescription) {
165         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
166         String modelUuid = execution.getVariable("modelUuid")
167         String jobId = execution.getVariable("jobId")
168         String nsiId = execution.getVariable("nsiId")
169
170         ResourceOperationStatus roStatus = tnNssmfUtils.buildRoStatus(modelUuid, ssInstanceId,
171                 jobId, nsiId, "DEALLOCATE", status, progress, statusDescription)
172
173         logger.debug("DoDeallocateTnNssi: roStatus={}", roStatus)
174         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
175     }
176 }
177