Dynamic Cloud Owner Support
[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.aai.domain.yang.GenericVnf
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.common.scripts.VidUtils
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.client.graphinventory.entities.uri.Depth;
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.uri.AAIResourceUri
40 import org.onap.so.client.aai.entities.uri.AAIUriFactory
41 import org.w3c.dom.Document
42 import org.w3c.dom.Element
43 import org.w3c.dom.Node
44 import org.w3c.dom.NodeList
45 import org.xml.sax.InputSource
46
47 import org.onap.so.logger.MessageEnum
48 import org.onap.so.logger.MsoLogger
49
50 /**
51  * This class supports the DoDeleteVnf subFlow
52  * with the Deletion of a generic vnf for
53  * infrastructure.
54  *
55  */
56 class DoDeleteVnf extends AbstractServiceTaskProcessor {
57         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoDeleteVnf.class);
58
59         String Prefix="DoDVNF_"
60         ExceptionUtil exceptionUtil = new ExceptionUtil()
61         JsonUtils jsonUtil = new JsonUtils()
62         VidUtils vidUtils = new VidUtils(this)
63
64         /**
65          * This method gets and validates the incoming
66          * request.
67          *
68          * @param - execution
69          *
70          */
71         public void preProcessRequest(DelegateExecution execution) {
72
73                 execution.setVariable("prefix",Prefix)
74                 msoLogger.trace("STARTED DoDeleteVnf PreProcessRequest Process")
75
76                 execution.setVariable("DoDVNF_SuccessIndicator", false)
77                 execution.setVariable("DoDVNF_vnfInUse", false)
78
79                 try{
80                         // Get Variables
81
82                         String vnfId = execution.getVariable("vnfId")
83                         execution.setVariable("DoDVNF_vnfId", vnfId)
84                         msoLogger.debug("Incoming Vnf(Instance) Id is: " + vnfId)
85
86                 }catch(BpmnError b){
87                         msoLogger.debug("Rethrowing MSOWorkflowException")
88                         throw b
89                 }catch(Exception e){
90                         msoLogger.debug(" Error Occured in DoDeleteVnf PreProcessRequest method!" + e)
91                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf PreProcessRequest")
92
93                 }
94                 msoLogger.trace("COMPLETED DoDeleteVnf PreProcessRequest Process ")
95         }
96
97
98         public void getVnf(DelegateExecution execution){
99
100                 execution.setVariable("prefix",Prefix)
101                 msoLogger.trace("STARTED DoDeleteVnf getVnf Process ")
102                 try {
103
104                         AAIResourcesClient resourceClient = new AAIResourcesClient()
105                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, execution.getVariable('vnfId'))
106
107                         if(resourceClient.exists(uri)){
108                                 execution.setVariable("GENGV_FoundIndicator", true)
109                                 AAIResultWrapper wrapper = resourceClient.get(uri.depth(Depth.ONE))
110                                 if(wrapper.getRelationships().isPresent()){
111                                         List<AAIResourceUri> relationships = wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.CLOUD_REGION)
112                                         relationships.addAll(wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.L3_NETWORK))
113                                         if(!relationships.isEmpty()){
114                                                 execution.setVariable("DoDVNF_vnfInUse", true)
115                                         }else{
116                                                 msoLogger.debug("Relationship NOT related to OpenStack")
117                                         }
118                                 }
119
120                                 Optional<GenericVnf> vnf = wrapper.asBean(GenericVnf.class)
121                                 if(!vnf.get().getVfModules().getVfModule().isEmpty()){
122                                         execution.setVariable("DoDVNF_vnfInUse", true)
123                                 }
124                         }else{
125                                 execution.setVariable("GENGV_FoundIndicator", false)
126                         }
127
128                 } catch (Exception ex) {
129                         msoLogger.debug("Error Occured in DoDeleteVnf getVnf Process " + ex.getMessage())
130                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf getVnf Process")
131
132                 }
133                 msoLogger.trace("COMPLETED DoDeleteVnf getVnf Process ")
134         }
135
136         /**
137          * Deletes the generic vnf from aai
138          */
139         public void deleteVnf(DelegateExecution execution) {
140                 msoLogger.trace("STARTED deleteVnf")
141                 try {
142                         String vnfId = execution.getVariable("DoDVNF_vnfId")
143
144                         AAIResourcesClient resourceClient = new AAIResourcesClient();
145                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
146                         resourceClient.delete(uri)
147
148                         msoLogger.trace("COMPLETED deleteVnf")
149                 } catch (Exception ex) {
150                         msoLogger.debug("Error Occured in DoDeleteVnf deleteVnf Process " + ex.getMessage())
151                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf deleteVnf Process")
152                 }
153         }
154
155 }