rename package for external use
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25
26 import javax.xml.parsers.DocumentBuilder
27 import javax.xml.parsers.DocumentBuilderFactory
28
29 import org.apache.commons.lang3.*
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.onap.aai.domain.yang.GenericVnf
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.VidUtils
36 import org.onap.so.bpmn.core.json.JsonUtils
37 import org.onap.aaiclient.client.graphinventory.entities.uri.Depth;
38 import org.onap.aaiclient.client.aai.AAIResourcesClient
39 import org.onap.aaiclient.client.aai.AAIObjectType
40 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
41 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
42 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
43 import org.w3c.dom.Document
44 import org.w3c.dom.Element
45 import org.w3c.dom.Node
46 import org.w3c.dom.NodeList
47 import org.xml.sax.InputSource
48
49 import org.slf4j.Logger
50 import org.slf4j.LoggerFactory
51
52 /**
53  * This class supports the DoDeleteVnf subFlow
54  * with the Deletion of a generic vnf for
55  * infrastructure.
56  *
57  */
58 class DoDeleteVnf extends AbstractServiceTaskProcessor {
59     private static final Logger logger = LoggerFactory.getLogger( DoDeleteVnf.class);
60
61         String Prefix="DoDVNF_"
62         ExceptionUtil exceptionUtil = new ExceptionUtil()
63         JsonUtils jsonUtil = new JsonUtils()
64         VidUtils vidUtils = new VidUtils(this)
65
66         /**
67          * This method gets and validates the incoming
68          * request.
69          *
70          * @param - execution
71          *
72          */
73         public void preProcessRequest(DelegateExecution execution) {
74
75                 execution.setVariable("prefix",Prefix)
76                 logger.trace("STARTED DoDeleteVnf PreProcessRequest Process")
77
78                 execution.setVariable("DoDVNF_SuccessIndicator", false)
79                 execution.setVariable("DoDVNF_vnfInUse", false)
80
81                 try{
82                         // Get Variables
83
84                         String vnfId = execution.getVariable("vnfId")
85                         execution.setVariable("DoDVNF_vnfId", vnfId)
86                         logger.debug("Incoming Vnf(Instance) Id is: " + vnfId)
87
88                 }catch(BpmnError b){
89                         logger.debug("Rethrowing MSOWorkflowException")
90                         throw b
91                 }catch(Exception e){
92                         logger.debug(" Error Occured in DoDeleteVnf PreProcessRequest method!" + e)
93                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf PreProcessRequest")
94
95                 }
96                 logger.trace("COMPLETED DoDeleteVnf PreProcessRequest Process ")
97         }
98
99
100         public void getVnf(DelegateExecution execution){
101
102                 execution.setVariable("prefix",Prefix)
103                 logger.trace("STARTED DoDeleteVnf getVnf Process ")
104                 try {
105
106                         AAIResourcesClient resourceClient = new AAIResourcesClient()
107                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, execution.getVariable('vnfId'))
108
109                         if(resourceClient.exists(uri)){
110                                 execution.setVariable("GENGV_FoundIndicator", true)
111                                 AAIResultWrapper wrapper = resourceClient.get(uri.depth(Depth.ONE))
112                                 if(wrapper.getRelationships().isPresent()){
113                                         List<AAIResourceUri> relationships = wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.CLOUD_REGION)
114                                         relationships.addAll(wrapper.getRelationships().get().getRelatedAAIUris(AAIObjectType.L3_NETWORK))
115                                         if(!relationships.isEmpty()){
116                                                 execution.setVariable("DoDVNF_vnfInUse", true)
117                                         }else{
118                                                 logger.debug("Relationship NOT related to OpenStack")
119                                         }
120                                 }
121
122                                 Optional<GenericVnf> vnf = wrapper.asBean(GenericVnf.class)
123                                 if (vnf.get() != null) {
124                                         if (vnf.get().getVfModules() != null) {
125                                                 if((vnf.get().getVfModules().getVfModule() != null) && !vnf.get().getVfModules().getVfModule().isEmpty()){                      
126                                                         execution.setVariable("DoDVNF_vnfInUse", true)
127                                                 }
128                                         }                       
129                                 }
130                         }else{
131                                 execution.setVariable("GENGV_FoundIndicator", false)
132                         }
133
134                 } catch (Exception ex) {
135                         logger.debug("Error Occured in DoDeleteVnf getVnf Process " + ex.getMessage())
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoDeleteVnf getVnf Process")
137
138                 }
139                 logger.trace("COMPLETED DoDeleteVnf getVnf Process ")
140         }
141
142         /**
143          * Deletes the generic vnf from aai
144          */
145         public void deleteVnf(DelegateExecution execution) {
146                 logger.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                         logger.trace("COMPLETED deleteVnf")
155                 } catch (Exception ex) {
156                         logger.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 }