fbbb07ec4d9c907ab0b2cce0cdbeec61ef0ab3c0
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteVnf.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts\r
21 \r
22 \r
23 import javax.xml.parsers.DocumentBuilder\r
24 import javax.xml.parsers.DocumentBuilderFactory\r
25 \r
26 import org.apache.commons.lang3.*\r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.runtime.Execution\r
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
31 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
32 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
33 import org.w3c.dom.Document\r
34 import org.w3c.dom.Element\r
35 import org.w3c.dom.Node\r
36 import org.w3c.dom.NodeList\r
37 import org.xml.sax.InputSource\r
38 \r
39 \r
40 /**\r
41  * This class supports the DoDeleteVnf subFlow\r
42  * with the Deletion of a generic vnf for\r
43  * infrastructure.\r
44  *\r
45  */\r
46 class DoDeleteVnf extends AbstractServiceTaskProcessor {\r
47 \r
48         String Prefix="DoDVNF_"\r
49         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
50         JsonUtils jsonUtil = new JsonUtils()\r
51         VidUtils vidUtils = new VidUtils(this)\r
52 \r
53         /**\r
54          * This method gets and validates the incoming\r
55          * request.\r
56          *\r
57          * @param - execution\r
58          *\r
59          */\r
60         public void preProcessRequest(Execution execution) {\r
61                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
62                 execution.setVariable("prefix",Prefix)\r
63                 utils.log("DEBUG", " *** STARTED DoDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)\r
64 \r
65                 execution.setVariable("DoDVNF_SuccessIndicator", false)\r
66                 execution.setVariable("DoDVNF_vnfInUse", false)\r
67 \r
68                 try{\r
69                         // Get Variables\r
70 \r
71                         String vnfId = execution.getVariable("vnfId")\r
72                         execution.setVariable("DoDVNF_vnfId", vnfId)\r
73                         utils.log("DEBUG", "Incoming Vnf(Instance) Id is: " + vnfId, isDebugEnabled)\r
74 \r
75                         // Setting for sub flow calls\r
76                         execution.setVariable("DoDVNF_type", "generic-vnf")\r
77                 }catch(BpmnError b){\r
78                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
79                         throw b\r
80                 }catch(Exception e){\r
81                         utils.log("DEBUG", " Error Occured in DoDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)\r
82                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf PreProcessRequest")\r
83 \r
84                 }\r
85                 utils.log("DEBUG", "*** COMPLETED DoDeleteVnf PreProcessRequest Process ***", isDebugEnabled)\r
86         }\r
87 \r
88 \r
89         public void processGetVnfResponse(Execution execution){\r
90                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
91                 execution.setVariable("prefix",Prefix)\r
92                 utils.log("DEBUG", " *** STARTED DoDeleteVnf processGetVnfResponse Process *** ", isDebugEnabled)\r
93                 try {\r
94                         String vnf = execution.getVariable("DoDVNF_genericVnf")\r
95                         String resourceVersion = utils.getNodeText1(vnf, "resource-version")\r
96                         execution.setVariable("DoDVNF_resourceVersion", resourceVersion)\r
97 \r
98                         if(utils.nodeExists(vnf, "relationship")){\r
99                                 InputSource source = new InputSource(new StringReader(vnf));\r
100                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
101                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()\r
102                                 Document vnfXml = docBuilder.parse(source)\r
103 \r
104                                 NodeList nodeList = vnfXml.getElementsByTagName("relationship")\r
105                                 for (int x = 0; x < nodeList.getLength(); x++) {\r
106                                         Node node = nodeList.item(x)\r
107                                         if (node.getNodeType() == Node.ELEMENT_NODE) {\r
108                                                 Element eElement = (Element) node\r
109                                                 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()\r
110                                                 if(e.equals("volume-group") || e.equals("l3-network")){\r
111                                                         utils.log("DEBUG", "Generic Vnf still has relationship to OpenStack.", isDebugEnabled)\r
112                                                         execution.setVariable("DoDVNF_vnfInUse", true)\r
113                                                 }else{\r
114                                                         utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)\r
115                                                 }\r
116                                         }\r
117                                 }\r
118                         }\r
119 \r
120                         if(utils.nodeExists(vnf, "vf-module")){\r
121                                 execution.setVariable("DoDVNF_vnfInUse", true)\r
122                                 utils.log("DEBUG", "Generic Vnf still has vf-modules.", isDebugEnabled)\r
123                         }\r
124 \r
125 \r
126                 } catch (Exception ex) {\r
127                         utils.log("DEBUG", "Error Occured in DoDeleteVnf processGetVnfResponse Process " + ex.getMessage(), isDebugEnabled)\r
128                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf processGetVnfResponse Process")\r
129 \r
130                 }\r
131                 utils.log("DEBUG", "*** COMPLETED DoDeleteVnf processGetVnfResponse Process ***", isDebugEnabled)\r
132         }\r
133 \r
134 \r
135 \r
136 }\r