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