699e9bf40a95503f3b6d82347eb7db8dac28ef30
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVnf.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
21 package org.onap.so.bpmn.infrastructure.scripts
22
23
24 import javax.xml.parsers.DocumentBuilder
25 import javax.xml.parsers.DocumentBuilderFactory
26
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.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.common.scripts.VidUtils
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.onap.so.client.aai.AAIResourcesClient
35 import org.onap.so.client.aai.AAIObjectType
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
38 import org.w3c.dom.Document
39 import org.w3c.dom.Element
40 import org.w3c.dom.Node
41 import org.w3c.dom.NodeList
42 import org.xml.sax.InputSource
43
44 import org.onap.so.logger.MessageEnum
45 import org.onap.so.logger.MsoLogger
46
47 /**
48  * This class supports the DoDeleteVnf subFlow
49  * with the Deletion of a generic vnf for
50  * infrastructure.
51  *
52  */
53 class DoDeleteVnf extends AbstractServiceTaskProcessor {
54         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoDeleteVnf.class);
55
56         String Prefix="DoDVNF_"
57         ExceptionUtil exceptionUtil = new ExceptionUtil()
58         JsonUtils jsonUtil = new JsonUtils()
59         VidUtils vidUtils = new VidUtils(this)
60
61         /**
62          * This method gets and validates the incoming
63          * request.
64          *
65          * @param - execution
66          *
67          */
68         public void preProcessRequest(DelegateExecution execution) {
69
70                 execution.setVariable("prefix",Prefix)
71                 msoLogger.trace("STARTED DoDeleteVnf PreProcessRequest Process")
72
73                 execution.setVariable("DoDVNF_SuccessIndicator", false)
74                 execution.setVariable("DoDVNF_vnfInUse", false)
75
76                 try{
77                         // Get Variables
78
79                         String vnfId = execution.getVariable("vnfId")
80                         execution.setVariable("DoDVNF_vnfId", vnfId)
81                         msoLogger.debug("Incoming Vnf(Instance) Id is: " + vnfId)
82
83                         // Setting for sub flow calls
84                         execution.setVariable("DoDVNF_type", "generic-vnf")
85                 }catch(BpmnError b){
86                         msoLogger.debug("Rethrowing MSOWorkflowException")
87                         throw b
88                 }catch(Exception e){
89                         msoLogger.debug(" Error Occured in DoDeleteVnf PreProcessRequest method!" + e)
90                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf PreProcessRequest")
91
92                 }
93                 msoLogger.trace("COMPLETED DoDeleteVnf PreProcessRequest Process ")
94         }
95
96
97         public void processGetVnfResponse(DelegateExecution execution){
98
99                 execution.setVariable("prefix",Prefix)
100                 msoLogger.trace("STARTED DoDeleteVnf processGetVnfResponse Process ")
101                 try {
102                         String vnf = execution.getVariable("DoDVNF_genericVnf")
103                         String resourceVersion = utils.getNodeText(vnf, "resource-version")
104                         execution.setVariable("DoDVNF_resourceVersion", resourceVersion)
105
106                         if(utils.nodeExists(vnf, "relationship")){
107                                 InputSource source = new InputSource(new StringReader(vnf));
108                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
109                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
110                                 Document vnfXml = docBuilder.parse(source)
111
112                                 NodeList nodeList = vnfXml.getElementsByTagName("relationship")
113                                 for (int x = 0; x < nodeList.getLength(); x++) {
114                                         Node node = nodeList.item(x)
115                                         if (node.getNodeType() == Node.ELEMENT_NODE) {
116                                                 Element eElement = (Element) node
117                                                 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()
118                                                 if(e.equals("volume-group") || e.equals("l3-network")){
119                                                         msoLogger.debug("Generic Vnf still has relationship to OpenStack.")
120                                                         execution.setVariable("DoDVNF_vnfInUse", true)
121                                                 }else{
122                                                         msoLogger.debug("Relationship NOT related to OpenStack")
123                                                 }
124                                         }
125                                 }
126                         }
127
128                         if(utils.nodeExists(vnf, "vf-module")){
129                                 execution.setVariable("DoDVNF_vnfInUse", true)
130                                 msoLogger.debug("Generic Vnf still has vf-modules.")
131                         }
132
133
134                 } catch (Exception ex) {
135                         msoLogger.debug("Error Occured in DoDeleteVnf processGetVnfResponse Process " + ex.getMessage())
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf processGetVnfResponse Process")
137
138                 }
139                 msoLogger.trace("COMPLETED DoDeleteVnf processGetVnfResponse Process ")
140         }
141
142         /**
143          * Deletes the generic vnf from aai
144          */
145         public void deleteVnf(DelegateExecution execution) {
146                 msoLogger.trace("STARTED deleteVnf")
147                 try {
148                         String vnfId = execution.getVariable("DoDVNF_vnfId")
149
150                         AAIResourcesClient resourceClient = new AAIResourcesClient();
151                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
152                         resourceClient.delete(uri)
153
154                         msoLogger.trace("COMPLETED deleteVnf")
155                 } catch (Exception ex) {
156                         msoLogger.debug("Error Occured in DoDeleteVnf deleteVnf Process " + ex.getMessage())
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf deleteVnf Process")
158                 }
159         }
160
161 }