2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
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.infrastructure.scripts
22 import static org.apache.commons.lang3.StringUtils.*;
24 import javax.xml.parsers.DocumentBuilder
25 import javax.xml.parsers.DocumentBuilderFactory
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution
30 import org.json.JSONArray
31 import org.json.JSONObject;
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.MsoUtils
35 import org.onap.so.bpmn.core.WorkflowException
36 import org.onap.so.bpmn.core.domain.Resource
37 import org.onap.so.bpmn.core.domain.ServiceDecomposition;
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.springframework.web.util.UriUtils;
40 import org.w3c.dom.Document
41 import org.w3c.dom.Element
42 import org.w3c.dom.Node
43 import org.w3c.dom.NodeList
44 import org.xml.sax.InputSource
49 * This groovy class supports the <class>DoDeleteE2EServiceInstance.bpmn</class> process.
52 * @param - msoRequestId
53 * @param - globalSubscriberId - O
54 * @param - subscriptionServiceType - O
55 * @param - serviceInstanceId
56 * @param - serviceInstanceName - O
57 * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
58 * @param - sdncVersion
59 * @param - failNotFound - TODO
60 * @param - serviceInputParams - TODO
62 * @param - delResourceList
63 * @param - serviceRelationShip
66 * @param - WorkflowException
70 public class DoDeleteE2EServiceInstance extends AbstractServiceTaskProcessor {
72 String Prefix="DDEESI_"
73 ExceptionUtil exceptionUtil = new ExceptionUtil()
74 JsonUtils jsonUtil = new JsonUtils()
76 public void preProcessRequest (DelegateExecution execution) {
77 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
78 utils.log("INFO"," ***** preProcessRequest *****", isDebugEnabled)
82 String requestId = execution.getVariable("msoRequestId")
83 execution.setVariable("prefix",Prefix)
86 //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
87 String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
88 if (globalSubscriberId == null)
90 execution.setVariable("globalSubscriberId", "")
93 //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
94 String serviceType = execution.getVariable("serviceType")
95 if (serviceType == null)
97 execution.setVariable("serviceType", "")
100 //Generated in parent for AAI PUT
101 String serviceInstanceId = execution.getVariable("serviceInstanceId")
102 if (isBlank(serviceInstanceId)){
103 msg = "Input serviceInstanceId is null"
104 utils.log("INFO", msg, isDebugEnabled)
105 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
108 String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
109 if (isBlank(sdncCallbackUrl)) {
110 msg = "URN_mso_workflow_sdncadapter_callback is null"
111 utils.log("INFO", msg, isDebugEnabled)
112 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
114 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
115 utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
117 StringBuilder sbParams = new StringBuilder()
118 Map<String, String> paramsMap = execution.getVariable("serviceInputParams")
119 if (paramsMap != null)
121 sbParams.append("<service-input-parameters>")
122 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
124 String paramName = entry.getKey()
125 String paramValue = entry.getValue()
128 <name>${MsoUtils.xmlEscape(paramName)}</name>
129 <value>${MsoUtils.xmlEscape(paramValue)}</value>
132 sbParams.append(paramsXml)
134 sbParams.append("</service-input-parameters>")
136 String siParamsXml = sbParams.toString()
137 if (siParamsXml == null)
139 execution.setVariable("siParamsXml", siParamsXml)
141 } catch (BpmnError e) {
143 } catch (Exception ex){
144 msg = "Exception in preProcessRequest " + ex.getMessage()
145 utils.log("INFO", msg, isDebugEnabled)
146 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
148 utils.log("INFO"," ***** Exit preProcessRequest *****", isDebugEnabled)
151 public void postProcessAAIGET(DelegateExecution execution) {
152 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
153 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
157 String serviceInstanceId = execution.getVariable("serviceInstanceId")
158 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
159 String serviceType = ""
162 utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
164 String siData = execution.getVariable("GENGS_service")
165 utils.log("INFO", "SI Data", isDebugEnabled)
168 msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId
169 utils.log("INFO", msg, isDebugEnabled)
170 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
174 InputSource source = new InputSource(new StringReader(siData));
175 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
176 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
177 Document serviceXml = docBuilder.parse(source)
178 serviceXml.getDocumentElement().normalize()
179 // get model invariant id
180 // Get Template uuid and version
181 if (utils.nodeExists(siData, "model-invariant-id") && utils.nodeExists(siData, "model-version-id") ) {
182 utils.log("INFO", "SI Data model-invariant-id and model-version-id exist:", isDebugEnabled)
183 def modelInvariantId = serviceXml.getElementsByTagName("model-invariant-id").item(0).getTextContent()
184 def modelVersionId = serviceXml.getElementsByTagName("model-version-id").item(0).getTextContent()
186 // Set Original Template info
187 execution.setVariable("model-invariant-id-original", modelInvariantId)
188 execution.setVariable("model-version-id-original", modelVersionId)
191 utils.log("INFO", "SI Data" + siData, isDebugEnabled)
192 //Confirm there are no related service instances (vnf/network or volume)
193 if (utils.nodeExists(siData, "relationship-list")) {
194 utils.log("INFO", "SI Data relationship-list exists:", isDebugEnabled)
195 JSONArray jArray = new JSONArray()
197 XmlParser xmlParser = new XmlParser()
198 Node root = xmlParser.parseText(siData)
199 def relation_list = utils.getChildNode(root, 'relationship-list')
200 def relationships = utils.getIdenticalChildren(relation_list, 'relationship')
202 for (def relation: relationships) {
203 def jObj = getRelationShipData(relation, isDebugEnabled)
207 execution.setVariable("serviceRelationShip", jArray.toString())
210 // NodeList nodeList = serviceXml.getElementsByTagName("relationship")
211 // JSONArray jArray = new JSONArray()
212 // for (int x = 0; x < nodeList.getLength(); x++) {
213 // Node node = nodeList.item(x)
214 // if (node.getNodeType() == Node.ELEMENT_NODE) {
215 // Element eElement = (Element) node
216 // def e = eElement.getElementsByTagName("related-to").item(0).getTextContent() //for ns
217 // if(e.equals("service-instance")){
218 // def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
219 // utils.log("INFO", "ServiceInstance Related NS :" + relatedObject, isDebugEnabled)
220 // NodeList dataList = node.getChildNodes()
221 // if(null != dataList) {
222 // JSONObject jObj = new JSONObject()
223 // for (int i = 0; i < dataList.getLength(); i++) {
224 // Node dNode = dataList.item(i)
225 // if(dNode.getNodeName() == "relationship-data") {
226 // Element rDataEle = (Element)dNode
227 // def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
228 // def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
229 // if(eKey.equals("service-instance.service-instance-id")){
230 // jObj.put("resourceInstanceId", eValue)
234 // else if(dNode.getNodeName() == "related-to-property"){
235 // Element rDataEle = (Element)dNode
236 // def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
237 // def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
238 // if(eKey.equals("service-instance.service-instance-name")){
239 // jObj.put("resourceType", eValue)
243 // utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
246 // //for overlay/underlay
247 // }else if (e.equals("configuration")){
248 // def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
249 // utils.log("INFO", "ServiceInstance Related Configuration :" + relatedObject, isDebugEnabled)
250 // NodeList dataList = node.getChildNodes()
251 // if(null != dataList) {
252 // JSONObject jObj = new JSONObject()
253 // for (int i = 0; i < dataList.getLength(); i++) {
254 // Node dNode = dataList.item(i)
255 // if(dNode.getNodeName() == "relationship-data") {
256 // Element rDataEle = (Element)dNode
257 // def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
258 // def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
259 // if(eKey.equals("configuration.configuration-id")){
260 // jObj.put("resourceInstanceId", eValue)
263 // else if(dNode.getNodeName() == "related-to-property"){
264 // Element rDataEle = (Element)dNode
265 // def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
266 // def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
267 // if(eKey.equals("configuration.configuration-type")){
268 // jObj.put("resourceType", eValue)
272 // utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
276 // }else if (e.equals("sp-partner")){
281 // execution.setVariable("serviceRelationShip", jArray.toString())
285 boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
287 utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)
288 WorkflowException workflowException = execution.getVariable("WorkflowException")
289 utils.logAudit("workflowException: " + workflowException)
290 if(workflowException != null){
291 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
295 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
296 utils.log("INFO", msg, isDebugEnabled)
297 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
301 utils.log("INFO","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
303 }catch (BpmnError e) {
305 } catch (Exception ex) {
306 msg = "Exception in DoDeleteE2EServiceInstance.postProcessAAIGET. " + ex.getMessage()
307 utils.log("INFO", msg, isDebugEnabled)
308 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
310 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
313 private JSONObject getRelationShipData(node, isDebugEnabled){
314 JSONObject jObj = new JSONObject()
316 def relation = utils.nodeToString(node)
317 def rt = utils.getNodeText1(relation, "related-to")
319 def rl = utils.getNodeText1(relation, "related-link")
320 utils.log("INFO", "ServiceInstance Related NS/Configuration :" + rl, isDebugEnabled)
322 def rl_datas = utils.getIdenticalChildren(node, "relationship-data")
323 for(def rl_data : rl_datas) {
324 def eKey = utils.getChildNodeText(rl_data, "relationship-key")
325 def eValue = utils.getChildNodeText(rl_data, "relationship-value")
327 if ((rt == "service-instance" && eKey.equals("service-instance.service-instance-id"))
328 //for overlay/underlay
329 || (rt == "configuration" && eKey.equals("configuration.configuration-id")
331 jObj.put("resourceInstanceId", eValue)
334 if(rt == "sp-partner" && eKey.equals("sp-partner.id")) {
335 jObj.put("resourceInstanceId", eValue)
336 String sppartnerName = "sp-partner" + eValue
337 jObj.put("resourceType", sppartnerName)
341 def rl_props = utils.getIdenticalChildren(node, "related-to-property")
342 for(def rl_prop : rl_props) {
343 def eKey = utils.getChildNodeText(rl_prop, "property-key")
344 def eValue = utils.getChildNodeText(rl_prop, "property-value")
345 if((rt == "service-instance" && eKey.equals("service-instance.service-instance-name"))
346 //for overlay/underlay
347 || (rt == "configuration" && eKey.equals("configuration.configuration-type"))){
348 jObj.put("resourceType", eValue)
352 utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
357 public void getCurrentNS(DelegateExecution execution){
358 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
359 utils.log("INFO", "======== Start getCurrentNS Process ======== ", isDebugEnabled)
361 def currentIndex = execution.getVariable("currentNSIndex")
362 List<String> nsSequence = execution.getVariable("nsSequence")
363 String nsResourceType = nsSequence.get(currentIndex)
365 // GET AAI by Name, not ID, for process convenient
366 execution.setVariable("GENGS_type", "service-instance")
367 execution.setVariable("GENGS_serviceInstanceId", "")
368 execution.setVariable("GENGS_serviceInstanceName", nsResourceType)
370 utils.log("INFO", "======== COMPLETED getCurrentNS Process ======== ", isDebugEnabled)
373 public void prepareDecomposeService(DelegateExecution execution) {
374 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
377 utils.log("DEBUG", " ***** Inside prepareDecomposeService of create generic e2e service ***** ", isDebugEnabled)
378 String modelInvariantUuid = execution.getVariable("model-invariant-id-original")
379 String modelVersionId = execution.getVariable("model-version-id-original")
381 String serviceModelInfo = """{
382 "modelInvariantUuid":"${modelInvariantUuid}",
383 "modelUuid":"${modelVersionId}",
386 execution.setVariable("serviceModelInfo", serviceModelInfo)
388 utils.log("DEBUG", " ***** Completed prepareDecomposeService of create generic e2e service ***** ", isDebugEnabled)
389 } catch (Exception ex) {
390 // try error in method block
391 String exceptionMessage = "Bpmn error encountered in create generic e2e service flow. Unexpected Error from method prepareDecomposeService() - " + ex.getMessage()
392 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
396 public void postDecomposeService(DelegateExecution execution) {
397 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
399 utils.log("DEBUG", " ***** Inside processDecomposition() of delete generic e2e service flow ***** ", isDebugEnabled)
401 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
403 // service model info
404 execution.setVariable("serviceModelInfo", serviceDecomposition.getModelInfo())
406 List<Resource> deleteResourceList = serviceDecomposition.getServiceResources()
407 String serviceRelationShip = execution.getVariable("serviceRelationShip")
408 def jsonSlurper = new JsonSlurper()
409 def jsonOutput = new JsonOutput()
411 List relationShipList = null
412 if (serviceRelationShip != null) {
413 relationShipList = jsonSlurper.parseText(serviceRelationShip)
416 //Set the real resource instance id to the decomosed resource list
417 for (Resource resource: deleteResourceList) {
418 //reset the resource instance id , because in the decompose flow ,its a random one.
419 resource.setResourceId("");
420 //match the resource-instance-name and the model name
421 if (relationShipList != null) {
422 relationShipList.each {
423 if (StringUtils.containsIgnoreCase(it.resourceType, resource.getModelInfo().getModelName())) {
424 resource.setResourceId(it.resourceInstanceId);
429 execution.setVariable("deleteResourceList", deleteResourceList)
430 utils.log("DEBUG", "delete resource list : " + deleteResourceList, isDebugEnabled)
431 } catch (Exception ex) {
432 String exceptionMessage = "Bpmn error encountered in create generic e2e service flow. processDecomposition() - " + ex.getMessage()
433 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
434 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
436 utils.log("DEBUG", " ***** exit processDecomposition() of delete generic e2e service flow ***** ", isDebugEnabled)
439 public void preInitResourcesOperStatus(DelegateExecution execution){
440 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
442 utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
444 String serviceId = execution.getVariable("serviceInstanceId")
445 String operationId = execution.getVariable("operationId")
446 String operationType = execution.getVariable("operationType")
447 String resourceTemplateUUIDs = ""
448 String result = "processing"
449 String progress = "0"
451 String operationContent = "Prepare service creation"
452 utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
453 serviceId = UriUtils.encode(serviceId,"UTF-8")
454 execution.setVariable("serviceInstanceId", serviceId)
455 execution.setVariable("operationId", operationId)
456 execution.setVariable("operationType", operationType)
457 List<Resource> deleteResourceList = execution.getVariable("deleteResourceList")
459 String serviceRelationShip = execution.getVariable("serviceRelationShip")
460 for(Resource resource : deleteResourceList){
461 resourceTemplateUUIDs = resourceTemplateUUIDs + resource.getModelInfo().getModelCustomizationUuid() + ":"
465 execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
468 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
469 xmlns:ns="http://org.onap.so/requestsdb">
472 <ns:initResourceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
473 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
474 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
475 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
476 <resourceTemplateUUIDs>${MsoUtils.xmlEscape(resourceTemplateUUIDs)}</resourceTemplateUUIDs>
477 </ns:initResourceOperationStatus>
479 </soapenv:Envelope>"""
481 payload = utils.formatXml(payload)
482 execution.setVariable("CVFMI_initResOperStatusRequest", payload)
483 utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
484 utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
487 utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
488 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
490 utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
494 * post config request.
496 public void postConfigRequest(execution){