[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteVnf.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
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 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22
23 import javax.xml.parsers.DocumentBuilder
24 import javax.xml.parsers.DocumentBuilderFactory
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
31 import org.openecomp.mso.bpmn.common.scripts.VidUtils
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33 import org.w3c.dom.Document
34 import org.w3c.dom.Element
35 import org.w3c.dom.Node
36 import org.w3c.dom.NodeList
37 import org.xml.sax.InputSource
38
39
40 /**
41  * This class supports the DoDeleteVnf subFlow
42  * with the Deletion of a generic vnf for
43  * infrastructure.
44  *
45  */
46 class DoDeleteVnf extends AbstractServiceTaskProcessor {
47
48         String Prefix="DoDVNF_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils(this)
52
53         /**
54          * This method gets and validates the incoming
55          * request.
56          *
57          * @param - execution
58          *
59          */
60         public void preProcessRequest(Execution execution) {
61                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
62                 execution.setVariable("prefix",Prefix)
63                 utils.log("DEBUG", " *** STARTED DoDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)
64
65                 execution.setVariable("DoDVNF_SuccessIndicator", false)
66                 execution.setVariable("DoDVNF_vnfInUse", false)
67
68                 try{
69                         // Get Variables
70
71                         String vnfId = execution.getVariable("vnfId")
72                         execution.setVariable("DoDVNF_vnfId", vnfId)
73                         utils.log("DEBUG", "Incoming Vnf(Instance) Id is: " + vnfId, isDebugEnabled)
74
75                         // Setting for sub flow calls
76                         execution.setVariable("DoDVNF_type", "generic-vnf")
77                 }catch(BpmnError b){
78                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
79                         throw b
80                 }catch(Exception e){
81                         utils.log("DEBUG", " Error Occured in DoDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)
82                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf PreProcessRequest")
83
84                 }
85                 utils.log("DEBUG", "*** COMPLETED DoDeleteVnf PreProcessRequest Process ***", isDebugEnabled)
86         }
87
88
89         public void processGetVnfResponse(Execution execution){
90                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
91                 execution.setVariable("prefix",Prefix)
92                 utils.log("DEBUG", " *** STARTED DoDeleteVnf processGetVnfResponse Process *** ", isDebugEnabled)
93                 try {
94                         String vnf = execution.getVariable("DoDVNF_genericVnf")
95                         String resourceVersion = utils.getNodeText1(vnf, "resource-version")
96                         execution.setVariable("DoDVNF_resourceVersion", resourceVersion)
97
98                         if(utils.nodeExists(vnf, "relationship")){
99                                 InputSource source = new InputSource(new StringReader(vnf));
100                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
101                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
102                                 Document vnfXml = docBuilder.parse(source)
103
104                                 NodeList nodeList = vnfXml.getElementsByTagName("relationship")
105                                 for (int x = 0; x < nodeList.getLength(); x++) {
106                                         Node node = nodeList.item(x)
107                                         if (node.getNodeType() == Node.ELEMENT_NODE) {
108                                                 Element eElement = (Element) node
109                                                 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()
110                                                 if(e.equals("volume-group") || e.equals("l3-network")){
111                                                         utils.log("DEBUG", "Generic Vnf still has relationship to OpenStack.", isDebugEnabled)
112                                                         execution.setVariable("DoDVNF_vnfInUse", true)
113                                                 }else{
114                                                         utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)
115                                                 }
116                                         }
117                                 }
118                         }
119
120                         if(utils.nodeExists(vnf, "vf-module")){
121                                 execution.setVariable("DoDVNF_vnfInUse", true)
122                                 utils.log("DEBUG", "Generic Vnf still has vf-modules.", isDebugEnabled)
123                         }
124
125
126                 } catch (Exception ex) {
127                         utils.log("DEBUG", "Error Occured in DoDeleteVnf processGetVnfResponse Process " + ex.getMessage(), isDebugEnabled)
128                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf processGetVnfResponse Process")
129
130                 }
131                 utils.log("DEBUG", "*** COMPLETED DoDeleteVnf processGetVnfResponse Process ***", isDebugEnabled)
132         }
133
134
135
136 }