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