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