Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / DeleteAAIVfModule.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.common.scripts
22 import org.camunda.bpm.engine.delegate.DelegateExecution
23 import org.onap.aai.domain.yang.GenericVnf
24 import org.onap.so.client.aai.AAIObjectType
25 import org.onap.so.client.aai.entities.uri.AAIResourceUri
26 import org.onap.so.client.aai.entities.uri.AAIUriFactory
27 import org.onap.so.client.graphinventory.entities.uri.Depth
28 import org.onap.so.logger.MessageEnum
29 import org.onap.so.logger.MsoLogger
30
31 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
32         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteAAIVfModule.class);
33
34         def Prefix="DAAIVfMod_"
35         ExceptionUtil exceptionUtil = new ExceptionUtil()
36     private MsoUtils utils = new MsoUtils()
37         public void initProcessVariables(DelegateExecution execution) {
38                 execution.setVariable("prefix",Prefix)
39                 execution.setVariable("DAAIVfMod_vnfId",null)
40                 execution.setVariable("DAAIVfMod_vnfName",null)
41                 execution.setVariable("DAAIVfMod_genVnfRsrcVer",null)
42                 execution.setVariable("DAAIVfMod_vfModuleId",null)
43                 execution.setVariable("DAAIVfMod_vfModRsrcVer",null)
44                 execution.setVariable("DAAIVfMod_moduleExists",false)
45                 execution.setVariable("DAAIVfMod_isBaseModule", false)
46                 execution.setVariable("DAAIVfMod_isLastModule", false)
47
48                 // DeleteAAIVfModule workflow response variable placeholders
49                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode",null)
50                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse","")
51                 execution.setVariable("DAAIVfMod_parseModuleResponse","")
52                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode",null)
53                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse","")
54                 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode",null)
55                 execution.setVariable("DAAIVfMod_deleteVfModuleResponse","")
56
57         }
58         
59         // parse the incoming DELETE_VF_MODULE request and store the Generic Vnf
60         // and Vf Module Ids in the flow DelegateExecution
61         public void preProcessRequest(DelegateExecution execution) {
62                 def xml = execution.getVariable("DeleteAAIVfModuleRequest")
63                 msoLogger.debug("DeleteAAIVfModule Request: " + xml)
64                 msoLogger.debug("input request xml:" + xml)
65                 initProcessVariables(execution)
66                 def vnfId = utils.getNodeText(xml,"vnf-id")
67                 def vfModuleId = utils.getNodeText(xml,"vf-module-id")
68                 execution.setVariable("DAAIVfMod_vnfId", vnfId)
69                 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
70         }
71         
72         // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id
73         // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist
74         public void queryAAIForGenericVnf(DelegateExecution execution) {
75                 
76                 def vnfId = execution.getVariable("DAAIVfMod_vnfId")
77
78                 try {
79                         AaiUtil aaiUriUtil = new AaiUtil(this)
80                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
81                         uri.depth(Depth.ONE)
82             Optional<GenericVnf> genericVnf = getAAIClient().get(GenericVnf.class, uri)
83
84             if(genericVnf.isPresent()) {
85                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 200)
86                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", genericVnf.get())
87
88             }else{
89                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 404)
90                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "Vnf Not Found!")
91             }
92
93                 } catch (Exception ex) {
94                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
95                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())
96                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")
97                 }
98         }
99         
100         // construct and send a DELETE request to A&AI to delete a Generic Vnf
101         // note: to get here, all the modules associated with the Generic Vnf must already be deleted
102         public void deleteGenericVnf(DelegateExecution execution) {
103
104                 try {
105                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
106                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
107             getAAIClient().delete(uri)
108                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", 200)
109                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", "Vnf Deleted")
110                 } catch (Exception ex) {
111                         ex.printStackTrace()
112                         msoLogger.debug("Exception occurred while executing AAI DELETE:" + ex.getMessage())
113                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
114                 }
115         }
116
117         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
118         public void deleteVfModule(DelegateExecution execution) {
119                 def responseData = ""
120                 try {
121                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
122                         String vfModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
123                         
124                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
125
126             getAAIClient().delete(uri)
127                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", 200)
128                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", "Vf Module Deleted")
129                 } catch (Exception ex) {
130                         ex.printStackTrace()
131                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
132                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
133                 }
134         }
135         
136         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
137         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
138         // there are no Add-on Modules present
139         public void parseForVfModule(DelegateExecution execution) {
140         GenericVnf genericVnf = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
141                 
142                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
143                 msoLogger.debug("Vf Module to be deleted: " + delModuleId)
144
145         execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
146
147         execution.setVariable("DAAIVfMod_moduleExists", false)
148         execution.setVariable("DAAIVfMod_isBaseModule", false)
149         execution.setVariable("DAAIVfMod_isLastModule", false)
150         if(genericVnf.getVfModules()!= null && !genericVnf.getVfModules().getVfModule().isEmpty()){
151             Optional<org.onap.aai.domain.yang.VfModule> vfModule = genericVnf.getVfModules().getVfModule().stream().
152                     filter{ v -> v.getVfModuleId().equals(delModuleId)}.findFirst()
153             if(vfModule.isPresent()){
154                 execution.setVariable("DAAIVfMod_moduleExists", true)
155                 execution.setVariable("DAAIVfMod_vfModRsrcVer", vfModule.get().getResourceVersion())
156
157                 if (vfModule.get().isBaseVfModule  && genericVnf.getVfModules().getVfModule().size() != 1) {
158                     execution.setVariable("DAAIVfMod_parseModuleResponse",
159                             "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
160                                     execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
161                     execution.setVariable("DAAIVfMod_isBaseModule", true)
162                 } else {
163                     if (vfModule.get().isBaseVfModule && genericVnf.getVfModules().getVfModule().size() == 1) {
164                         execution.setVariable("DAAIVfMod_parseModuleResponse",
165                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
166                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
167                         execution.setVariable("DAAIVfMod_isBaseModule", true)
168                         execution.setVariable("DAAIVfMod_isLastModule", true)
169                     } else {
170                         if (genericVnf.getVfModules().getVfModule().size() == 1) {
171                             execution.setVariable("DAAIVfMod_parseModuleResponse",
172                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
173                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
174                             execution.setVariable("DAAIVfMod_isLastModule", true)
175                         } else {
176                             execution.setVariable("DAAIVfMod_parseModuleResponse",
177                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
178                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
179                         }
180                     }
181                 }
182                 msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
183             }
184         }
185         if (execution.getVariable("DAAIVfMod_moduleExists") == false) { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
186             msoLogger.debug("Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId"))
187             execution.setVariable("DAAIVfMod_parseModuleResponse",
188                     "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
189                             execution.getVariable("DAAIVfMod_vnfName"))
190         }
191         }
192         
193         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
194         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
195         // there are no Add-on Modules present
196         public void parseForResourceVersion(DelegateExecution execution) {
197         GenericVnf genericVnf =  execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
198                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
199                 msoLogger.debug("Latest Generic VNF Resource Version: " + genericVnf.getResourceVersion())
200         }
201         
202         
203         // generates a WorkflowException if the A&AI query returns a response code other than 200
204         public void handleAAIQueryFailure(DelegateExecution execution) {
205                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred attempting to query AAI, Response Code " + execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " + execution.getVariable("DAAIVfMod_queryGenericVnfResponse"), "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
206                 def errorCode = 5000
207                 // set the errorCode to distinguish between a A&AI failure
208                 // and the Generic Vnf Id not found
209                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
210                         errorCode = 1002
211                 }
212                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
213         }
214         
215         // generates a WorkflowException if
216         //              - the A&AI Vf Module DELETE returns a response code other than 200
217         //              - the Vf Module is a Base Module that is not the last Vf Module
218         //              - the Vf Module does not exist for the Generic Vnf
219         public void handleDeleteVfModuleFailure(DelegateExecution execution) {
220                 def errorCode = 2000
221                 def errorResponse = ""
222                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
223                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
224                         msoLogger.debug("AAI failure deleting a Vf Module: " + execution.getVariable("DAAIVfMod_deleteVfModuleResponse"))
225                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
226                         msoLogger.debug("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
227                         errorCode = 5000
228                 } else {
229                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
230                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {
231                                 // attempt to delete a Base Module that is not the last Vf Module
232                                 msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
233                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
234                                 msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
235                                 errorCode = 1002
236                         } else {
237                                 // attempt to delete a non-existant Vf Module
238                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
239                                         msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
240                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
241                                         msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
242                                         errorCode = 1002
243                                 } else {
244                                         // if the responses get populated corerctly, we should never get here
245                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
246                                 }
247                         }
248                 }
249
250                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during DeleteAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
251                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
252
253         }
254
255         // generates a WorkflowException if
256         //              - the A&AI Generic Vnf DELETE returns a response code other than 200
257         public void handleDeleteGenericVnfFailure(DelegateExecution execution) {
258                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "AAI error occurred deleting the Generic Vnf", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"));
259                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
260         }
261 }