2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.bpmn.vcpe.scripts
22 import org.camunda.bpm.engine.delegate.BpmnError
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.so.bpmn.common.scripts.AaiUtil
25 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
26 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
27 import org.onap.so.bpmn.common.scripts.ExceptionUtil
28 import org.onap.so.bpmn.common.scripts.NetworkUtils
29 import org.onap.so.bpmn.common.scripts.VidUtils
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.bpmn.core.WorkflowException
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.onap.so.logger.MessageEnum
34 import org.onap.so.logger.MsoLogger
35 import org.onap.so.rest.APIResponse
36 import org.onap.so.client.aai.AAIResourcesClient
37 import org.onap.so.client.aai.AAIObjectType
38 import org.onap.so.client.aai.entities.AAIResultWrapper
39 import org.onap.so.client.aai.entities.Relationships
40 import org.onap.so.client.aai.entities.uri.AAIResourceUri
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory
42 import javax.ws.rs.NotFoundException
43 import org.json.JSONObject
45 import static org.apache.commons.lang3.StringUtils.isBlank
49 * This groovy class supports the <class>DeleteVcpeResCustService.bpmn</class> process.
54 public class DeleteVcpeResCustService extends AbstractServiceTaskProcessor {
55 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteVcpeResCustService.class);
57 private static final String DebugFlag = "isDebugLogEnabled"
59 String Prefix = "DVRCS_"
60 ExceptionUtil exceptionUtil = new ExceptionUtil()
61 JsonUtils jsonUtil = new JsonUtils()
62 VidUtils vidUtils = new VidUtils()
63 CatalogDbUtils catalogDbUtils = new CatalogDbUtils()
64 NetworkUtils networkUtils = new NetworkUtils()
67 * This method is executed during the preProcessRequest task of the <class>DeleteVcpeResCustService.bpmn</class> process.
70 public InitializeProcessVariables(DelegateExecution execution){
71 /* Initialize all the process variables in this block */
73 execution.setVariable("DeleteVcpeResCustServiceRequest", "")
74 execution.setVariable("msoRequestId", "")
75 execution.setVariable(Prefix+"vnfsDeletedCount", 0)
76 execution.setVariable(Prefix+"vnfsCount", 0)
79 // **************************************************
80 // Pre or Prepare Request Section
81 // **************************************************
83 * This method is executed during the preProcessRequest task of the <class>CreateServiceInstance.bpmn</class> process.
86 public void preProcessRequest (DelegateExecution execution) {
87 def isDebugEnabled=execution.getVariable(DebugFlag)
88 execution.setVariable("prefix",Prefix)
90 msoLogger.trace("Inside preProcessRequest DeleteVcpeResCustService Request ")
93 // initialize flow variables
94 InitializeProcessVariables(execution)
96 // check for incoming json message/input
97 String DeleteVcpeResCustServiceRequest = execution.getVariable("bpmnRequest")
98 msoLogger.debug(DeleteVcpeResCustServiceRequest)
99 execution.setVariable("DeleteVcpeResCustServiceRequest", DeleteVcpeResCustServiceRequest);
100 println 'DeleteVcpeResCustServiceRequest - ' + DeleteVcpeResCustServiceRequest
103 String requestId = execution.getVariable("mso-request-id")
104 execution.setVariable("msoRequestId", requestId)
106 String serviceInstanceId = execution.getVariable("serviceInstanceId")
107 if ((serviceInstanceId == null) || (serviceInstanceId.isEmpty())) {
108 String dataErrorMessage = " Element 'serviceInstanceId' is missing. "
109 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
112 String requestAction = execution.getVariable("requestAction")
113 execution.setVariable("requestAction", requestAction)
115 String source = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.requestInfo.source")
116 if ((source == null) || (source.isEmpty())) {
119 execution.setVariable("source", source)
121 // extract globalSubscriberId
122 String globalSubscriberId = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.subscriberInfo.globalSubscriberId")
124 // global-customer-id is optional on Delete
126 execution.setVariable("globalSubscriberId", globalSubscriberId)
127 execution.setVariable("globalCustomerId", globalSubscriberId)
129 String suppressRollback = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.requestInfo.suppressRollback")
130 execution.setVariable("disableRollback", suppressRollback)
131 msoLogger.debug("Incoming Suppress/Disable Rollback is: " + suppressRollback)
133 String productFamilyId = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.requestInfo.productFamilyId")
134 execution.setVariable("productFamilyId", productFamilyId)
135 msoLogger.debug("Incoming productFamilyId is: " + productFamilyId)
137 // extract subscriptionServiceType
138 String subscriptionServiceType = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.requestParameters.subscriptionServiceType")
139 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
140 msoLogger.debug("Incoming subscriptionServiceType is: " + subscriptionServiceType)
142 // extract cloud configuration
143 String cloudConfiguration = jsonUtil.getJsonValue(DeleteVcpeResCustServiceRequest, "requestDetails.cloudConfiguration")
144 execution.setVariable("cloudConfiguration", cloudConfiguration)
145 msoLogger.debug("cloudConfiguration: "+ cloudConfiguration)
146 String lcpCloudRegionId = jsonUtil.getJsonValue(cloudConfiguration, "lcpCloudRegionId")
147 execution.setVariable("lcpCloudRegionId", lcpCloudRegionId)
148 msoLogger.debug("lcpCloudRegionId: "+ lcpCloudRegionId)
149 String tenantId = jsonUtil.getJsonValue(cloudConfiguration, "tenantId")
150 execution.setVariable("tenantId", tenantId)
151 msoLogger.debug("tenantId: "+ tenantId)
153 String sdncVersion = "1707"
154 execution.setVariable("sdncVersion", sdncVersion)
155 msoLogger.debug("sdncVersion: "+ sdncVersion)
157 //For Completion Handler & Fallout Handler
159 """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
160 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
161 <action>DELETE</action>
162 <source>${MsoUtils.xmlEscape(source)}</source>
165 execution.setVariable(Prefix+"requestInfo", requestInfo)
167 msoLogger.trace("Completed preProcessRequest DeleteVcpeResCustServiceRequest Request ")
169 } catch (BpmnError e) {
171 } catch (Exception ex){
172 String exceptionMessage = "Bpmn error encountered in DeleteVcpeResCustService flow. Unexpected from method preProcessRequest() - " + ex.getMessage()
173 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
177 public void sendSyncResponse(DelegateExecution execution) {
178 def isDebugEnabled=execution.getVariable(DebugFlag)
180 msoLogger.trace("Inside sendSyncResponse of DeleteVcpeResCustService ")
183 String serviceInstanceId = execution.getVariable("serviceInstanceId")
184 String requestId = execution.getVariable("mso-request-id")
186 // RESTResponse (for API Handler (APIH) Reply Task)
187 String syncResponse ="""{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()
189 msoLogger.debug(" sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
190 sendWorkflowResponse(execution, 202, syncResponse)
191 } catch (Exception ex) {
192 String exceptionMessage = "Bpmn error encountered in DeleteVcpeResCustService flow. Unexpected from method preProcessRequest() - " + ex.getMessage()
193 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
198 * Gets the service instance and its related resources from aai
202 public void getServiceInstance(DelegateExecution execution) {
204 String serviceInstanceId = execution.getVariable('serviceInstanceId')
206 AAIResourcesClient resourceClient = new AAIResourcesClient()
207 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
209 if(resourceClient.exists(uri)){
210 AAIResultWrapper wrapper = resourceClient.get(uri, NotFoundException.class)
211 Optional<Relationships> relationships = wrapper.getRelationships()
213 def (TXC_found, TXC_id) = new Tuple(false, null)
214 def (BRG_found, BRG_id) = new Tuple(false, null)
215 List relatedVnfIdList = []
217 if(relationships.isPresent()){
219 List<AAIResourceUri> vnfUris = relationships.get().getRelatedAAIUris(AAIObjectType.GENERIC_VNF)
220 for(AAIResourceUri u:vnfUris){
221 Map<String, String> keys = u.getURIKeys()
222 String vnfId = keys.get("vnf-id")
223 relatedVnfIdList.add(vnfId)
225 List<AAIResourceUri> arUris = relationships.get().getRelatedAAIUris(AAIObjectType.ALLOTTED_RESOURCE)
226 for(AAIResourceUri u:arUris){
227 String ar = resourceClient.get(u).getJson()
229 def type = jsonUtil.getJsonValue(ar, "type")
230 def id = jsonUtil.getJsonValue(ar, "id")
232 if(type == "TunnelXConn" || type == "Tunnel XConn") {
233 msoLogger.debug("TunnelXConn AR found")
237 }else if(type == "BRG") {
238 msoLogger.debug("BRG AR found")
243 execution.setVariable(Prefix+"TunnelXConn", TXC_found)
244 execution.setVariable("TXC_allottedResourceId", TXC_id)
245 msoLogger.debug("TXC_allottedResourceId: " + TXC_id)
247 execution.setVariable(Prefix+"BRG", BRG_found)
248 execution.setVariable("BRG_allottedResourceId", BRG_id)
249 msoLogger.debug("BRG_allottedResourceId: " + BRG_id)
254 execution.setVariable(Prefix+"vnfsCount", relatedVnfIdList.size())
255 if(relatedVnfIdList.size() > 0) {
256 execution.setVariable(Prefix+"relatedVnfIdList", relatedVnfIdList)
260 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
263 }catch(BpmnError e) {
265 }catch(NotFoundException e) {
266 msoLogger.debug("Service Instance does not exist AAI")
267 exceptionUtil.buildAndThrowWorkflowException(execution, 404, "Service Instance was not found in aai")
268 }catch(Exception ex) {
269 String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
271 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
276 // *******************************
278 // *******************************
279 public void prepareVnfAndModulesDelete (DelegateExecution execution) {
280 def isDebugEnabled=execution.getVariable(DebugFlag)
281 msoLogger.trace("Inside prepareVnfAndModulesDelete of DeleteVcpeResCustService ")
284 List vnfList = execution.getVariable(Prefix+"relatedVnfIdList")
285 int vnfsDeletedCount = execution.getVariable(Prefix+"vnfsDeletedCount")
286 String vnfModelInfoString = ""
288 if (vnfList.size() > 0 ) {
289 vnfId = vnfList.get(vnfsDeletedCount.intValue())
292 execution.setVariable("vnfId", vnfId)
293 msoLogger.debug("need to delete vnfId:" + vnfId)
295 msoLogger.trace("Completed prepareVnfAndModulesDelete of DeleteVcpeResCustService ")
296 } catch (Exception ex) {
297 // try error in method block
298 String exceptionMessage = "Bpmn error encountered in DeleteVcpeResCustService flow. Unexpected Error from method prepareVnfAndModulesDelete() - " + ex.getMessage()
299 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
303 // *******************************
304 // Validate Vnf request Section -> increment count
305 // *******************************
306 public void validateVnfDelete (DelegateExecution execution) {
307 def isDebugEnabled=execution.getVariable(DebugFlag)
308 msoLogger.trace("Inside validateVnfDelete of DeleteVcpeResCustService ")
311 int vnfsDeletedCount = execution.getVariable(Prefix+"vnfsDeletedCount")
314 execution.setVariable(Prefix+"vnfsDeletedCount", vnfsDeletedCount)
316 msoLogger.debug(" ***** Completed validateVnfDelete of DeleteVcpeResCustService ***** "+" vnf # "+vnfsDeletedCount)
317 } catch (Exception ex) {
318 // try error in method block
319 String exceptionMessage = "Bpmn error encountered in DeleteVcpeResCustService flow. Unexpected Error from method validateVnfDelete() - " + ex.getMessage()
320 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
325 // *****************************************
326 // Prepare Completion request Section
327 // *****************************************
328 public void postProcessResponse (DelegateExecution execution) {
329 def isDebugEnabled=execution.getVariable(DebugFlag)
330 msoLogger.trace("Inside postProcessResponse of DeleteVcpeResCustService ")
333 String source = execution.getVariable("source")
334 String requestId = execution.getVariable("msoRequestId")
336 String msoCompletionRequest =
337 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
338 xmlns:ns="http://org.onap/so/request/types/v1">
339 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
340 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
341 <action>DELETE</action>
342 <source>${MsoUtils.xmlEscape(source)}</source>
344 <aetgt:status-message>vCPE Res Cust Service Instance has been deleted successfully.</aetgt:status-message>
345 <aetgt:mso-bpel-name>BPMN Service Instance macro action: DELETE</aetgt:mso-bpel-name>
346 </aetgt:MsoCompletionRequest>"""
349 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
351 msoLogger.debug(xmlMsoCompletionRequest)
352 execution.setVariable(Prefix+"Success", true)
353 execution.setVariable(Prefix+"CompleteMsoProcessRequest", xmlMsoCompletionRequest)
354 msoLogger.debug(" SUCCESS flow, going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
355 } catch (BpmnError e) {
358 } catch (Exception ex) {
359 // try error in method block
360 String exceptionMessage = "Bpmn error encountered in DeleteServiceInstance flow. Unexpected Error from method postProcessResponse() - " + ex.getMessage()
361 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
365 public void prepareFalloutRequest(DelegateExecution execution){
366 def isDebugEnabled=execution.getVariable(DebugFlag)
367 msoLogger.trace("STARTED DeleteVcpeResCustService prepareFalloutRequest Process ")
370 WorkflowException wfex = execution.getVariable("WorkflowException")
371 msoLogger.debug(" Incoming Workflow Exception: " + wfex.toString())
372 String requestInfo = execution.getVariable(Prefix+"requestInfo")
373 msoLogger.debug(" Incoming Request Info: " + requestInfo)
375 String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
377 execution.setVariable(Prefix+"falloutRequest", falloutRequest)
378 } catch (Exception ex) {
379 msoLogger.debug("Error Occured in DeleteVcpeResCustService prepareFalloutRequest Process " + ex.getMessage())
380 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVcpeResCustService prepareFalloutRequest Process")
382 msoLogger.trace("COMPLETED DeleteVcpeResCustService prepareFalloutRequest Process ")
386 public void sendSyncError (DelegateExecution execution) {
387 def isDebugEnabled=execution.getVariable(DebugFlag)
388 msoLogger.trace("Inside sendSyncError() of DeleteVcpeResCustService ")
391 String errorMessage = ""
392 if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
393 WorkflowException wfe = execution.getVariable("WorkflowException")
394 errorMessage = wfe.getErrorMessage()
396 errorMessage = "Sending Sync Error."
399 String buildworkflowException =
400 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
401 <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
402 <aetgt:ErrorCode>7000</aetgt:ErrorCode>
403 </aetgt:WorkflowException>"""
405 msoLogger.debug(buildworkflowException)
406 sendWorkflowResponse(execution, 500, buildworkflowException)
407 } catch (Exception ex) {
408 msoLogger.debug(" Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
412 public void processJavaException(DelegateExecution execution){
413 def isDebugEnabled=execution.getVariable(DebugFlag)
414 execution.setVariable("prefix",Prefix)
416 msoLogger.debug("Caught a Java Exception")
417 msoLogger.debug("Started processJavaException Method")
418 msoLogger.debug("Variables List: " + execution.getVariables())
419 execution.setVariable(Prefix+"unexpectedError", "Caught a Java Lang Exception") // Adding this line temporarily until this flows error handling gets updated
420 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Caught a Java Lang Exception")
422 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Rethrowing MSOWorkflowException", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
425 msoLogger.debug("Caught Exception during processJavaException Method: " + e)
426 execution.setVariable(Prefix+"unexpectedError", "Exception in processJavaException method") // Adding this line temporarily until this flows error handling gets updated
427 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Exception in processJavaException method")
429 msoLogger.debug("Completed processJavaException Method")