SO changes for Service Intent
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCloudLeasedLineDelete.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
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
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 import static org.apache.commons.lang3.StringUtils.isBlank
41 import static org.apache.commons.lang3.StringUtils.isEmpty
42
43 class DoCloudLeasedLineDelete extends AbstractServiceTaskProcessor {
44     String Prefix = "DCLL_"
45
46     ExceptionUtil exceptionUtil = new ExceptionUtil()
47     JsonUtils jsonUtil = new JsonUtils()
48     RequestDBUtil requestDBUtil = new RequestDBUtil()
49     ServiceIntentUtils serviceIntentUtils = new ServiceIntentUtils()
50     private static final Logger logger = LoggerFactory.getLogger(DoCloudLeasedLineDelete.class)
51
52
53     void preProcessRequest(DelegateExecution execution) {
54         logger.debug("Start preProcessRequest")
55
56         execution.setVariable("startTime", System.currentTimeMillis())
57         String msg = serviceIntentUtils.getExecutionInputParams(execution)
58         logger.debug("Deallocate CLL input parameters: " + msg)
59
60         execution.setVariable("prefix", Prefix)
61
62         serviceIntentUtils.setSdncCallbackUrl(execution, true)
63         logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
64
65         String cllId = execution.getVariable("serviceInstanceID")
66         execution.setVariable("cllId", cllId)
67
68         String cllName = execution.getVariable("servicename")
69         execution.setVariable("cllName", cllName)
70
71         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
72         String modelUuid = execution.getVariable("modelUuid")
73         if (isEmpty(modelUuid)) {
74             modelUuid = serviceIntentUtils.getModelUuidFromServiceInstance(execution.getVariable("serviceInstanceID"))
75         }
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
85         String additionalPropJsonStr = execution.getVariable("serviceIntentParams")
86         if (isBlank(additionalPropJsonStr) ||
87                 isBlank(serviceIntentUtils.setExecVarFromJsonIfExists(execution,
88                         additionalPropJsonStr,
89                         "enableSdnc", "enableSdnc"))) {
90             serviceIntentUtils.setEnableSdncConfig(execution)
91         }
92
93         logger.debug("Finish preProcessRequest")
94     }
95
96     void preprocessSdncDeleteCllRequest(DelegateExecution execution) {
97         def method = getClass().getSimpleName() + '.preprocessSdncDeallocateCllRequest(' +
98                 'execution=' + execution.getId() + ')'
99         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
100         logger.trace('Entered ' + method)
101
102         try {
103             String serviceInstanceId = execution.getVariable("serviceInstanceID")
104
105             String sdncRequest = serviceIntentUtils.buildSDNCRequest(execution, serviceInstanceId, "delete")
106
107             execution.setVariable("CLL_SDNCRequest", sdncRequest)
108             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
109
110         } catch (Exception e) {
111             logger.debug("Exception Occurred Processing preprocessSdncDeallocateCllRequest. Exception is:\n" + e)
112             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
113         }
114         logger.trace("COMPLETED preprocessSdncDeallocateCllRequest Process")
115     }
116
117
118     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
119         serviceIntentUtils.validateSDNCResponse(execution, response, method)
120     }
121
122     void deleteServiceInstance(DelegateExecution execution) {
123         try {
124             AAIResourcesClient client = getAAIClient()
125             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
126             client.delete(uri)
127         } catch (BpmnError e) {
128             throw e
129         } catch (Exception ex) {
130             String msg = "Exception in DoDeallocateCll.deleteServiceInstance. " + ex.getMessage()
131             logger.info(msg)
132             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
133         }
134     }
135
136     public void updateAAIOrchStatus(DelegateExecution execution) {
137         logger.debug("Start updateAAIOrchStatus")
138         String cllId = execution.getVariable("cllId")
139         String orchStatus = execution.getVariable("orchestrationStatus")
140
141         try {
142             ServiceInstance si = new ServiceInstance()
143             si.setOrchestrationStatus(orchStatus)
144             AAIResourcesClient client = getAAIClient()
145             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(cllId))
146             client.update(uri, si)
147         } catch (BpmnError e) {
148             throw e
149         } catch (Exception ex) {
150             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
151             logger.info(msg)
152             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
153         }
154
155         logger.debug("Finish updateAAIOrchStatus")
156     }
157
158     void prepareUpdateJobStatus(DelegateExecution execution,
159                                 String status,
160                                 String progress,
161                                 String statusDescription) {
162         String cllId = execution.getVariable("cllId")
163         String modelUuid = execution.getVariable("modelUuid")
164         String jobId = execution.getVariable("jobId")
165         String nsiId = execution.getVariable("nsiId")
166
167         ResourceOperationStatus roStatus = serviceIntentUtils.buildRoStatus(modelUuid, cllId,
168                 jobId, nsiId, "DELETE", status, progress, statusDescription)
169
170         logger.debug("prepareUpdateJobStatus: roStatus={}", roStatus)
171         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
172     }
173 }
174