Dynamic Cloud Owner Support
[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.so.bpmn.core.WorkflowException
24 import org.onap.so.bpmn.core.UrnPropertiesReader
25 import org.onap.so.client.aai.AAIObjectType
26 import org.onap.so.client.aai.entities.uri.AAIResourceUri
27 import org.onap.so.client.aai.entities.uri.AAIUriFactory
28 import org.onap.so.client.graphinventory.entities.uri.Depth
29 import org.onap.so.rest.APIResponse
30 import org.onap.so.rest.RESTClient;
31 import org.onap.so.rest.RESTConfig;
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
34
35 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
36         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteAAIVfModule.class);
37
38         def Prefix="DAAIVfMod_"
39         ExceptionUtil exceptionUtil = new ExceptionUtil()
40     private MsoUtils utils = new MsoUtils()
41         public void initProcessVariables(DelegateExecution execution) {
42                 execution.setVariable("prefix",Prefix)
43                 execution.setVariable("DAAIVfMod_vnfId",null)
44                 execution.setVariable("DAAIVfMod_vnfName",null)
45                 execution.setVariable("DAAIVfMod_genVnfRsrcVer",null)
46                 execution.setVariable("DAAIVfMod_vfModuleId",null)
47                 execution.setVariable("DAAIVfMod_vfModRsrcVer",null)
48                 execution.setVariable("DAAIVfMod_moduleExists",false)
49                 execution.setVariable("DAAIVfMod_isBaseModule", false)
50                 execution.setVariable("DAAIVfMod_isLastModule", false)
51
52                 // DeleteAAIVfModule workflow response variable placeholders
53                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode",null)
54                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse","")
55                 execution.setVariable("DAAIVfMod_parseModuleResponse","")
56                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode",null)
57                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse","")
58                 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode",null)
59                 execution.setVariable("DAAIVfMod_deleteVfModuleResponse","")
60
61         }
62         
63         // parse the incoming DELETE_VF_MODULE request and store the Generic Vnf
64         // and Vf Module Ids in the flow DelegateExecution
65         public void preProcessRequest(DelegateExecution execution) {
66                 def xml = execution.getVariable("DeleteAAIVfModuleRequest")
67                 msoLogger.debug("DeleteAAIVfModule Request: " + xml)
68                 msoLogger.debug("input request xml:" + xml)
69                 initProcessVariables(execution)
70                 def vnfId = utils.getNodeText(xml,"vnf-id")
71                 def vfModuleId = utils.getNodeText(xml,"vf-module-id")
72                 execution.setVariable("DAAIVfMod_vnfId", vnfId)
73                 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
74         }
75         
76         // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id
77         // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist
78         public void queryAAIForGenericVnf(DelegateExecution execution) {
79                 
80                 def vnfId = execution.getVariable("DAAIVfMod_vnfId")
81
82                 try {
83                         AaiUtil aaiUriUtil = new AaiUtil(this)
84                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
85                         uri.depth(Depth.ONE)
86                         String endPoint = aaiUriUtil.createAaiUri(uri)
87                         
88                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, endPoint)
89                         
90                         msoLogger.debug('Response code:' + response.getStatusCode())
91                         msoLogger.debug('Response:' + response.getResponseBodyAsString())
92
93                         execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", response.getStatusCode())
94                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", response.getResponseBodyAsString())
95
96                 } catch (Exception ex) {
97                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
98                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())
99                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")
100                 }
101         }
102         
103         // construct and send a DELETE request to A&AI to delete a Generic Vnf
104         // note: to get here, all the modules associated with the Generic Vnf must already be deleted
105         public void deleteGenericVnf(DelegateExecution execution) {
106
107                 try {
108                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
109                         String resourceVersion =  execution.getVariable("DAAIVfMod_genVnfRsrcVer")
110                         
111                         AaiUtil aaiUriUtil = new AaiUtil(this)
112                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
113                         uri.resourceVersion(resourceVersion)
114                         String endPoint = aaiUriUtil.createAaiUri(uri)
115                         
116                         APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, endPoint)
117                                 
118                         def responseData = response.getResponseBodyAsString()
119                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", response.getStatusCode())
120                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", responseData)
121                         msoLogger.debug("Response code:" + response.getStatusCode())
122                         msoLogger.debug("Response:" + System.lineSeparator() + responseData)
123                 } catch (Exception ex) {
124                         ex.printStackTrace()
125                         msoLogger.debug("Exception occurred while executing AAI DELETE:" + ex.getMessage())
126                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
127                 }
128         }
129
130         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
131         public void deleteVfModule(DelegateExecution execution) {
132                 def responseData = ""
133                 try {
134                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
135                         String vfModuleId = execution.setVariable("DAAIVfMod_vfModuleId")
136                         String resourceVersion =  execution.getVariable("DAAIVfMod_vfModRsrcVer")
137                         
138                         AaiUtil aaiUriUtil = new AaiUtil(this)
139                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
140                         uri.resourceVersion(resourceVersion)
141                         String endPoint = aaiUriUtil.createAaiUri(uri)
142                         
143                         APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, endPoint)
144                         
145                         responseData = response.getResponseBodyAsString()
146                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", response.getStatusCode())
147                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", responseData)
148                         msoLogger.debug("DeleteAAIVfModule - AAI Response" + responseData)
149                         msoLogger.debug("Response code:" + response.getStatusCode())
150                         msoLogger.debug("Response:" + System.lineSeparator() + responseData)
151
152                 } catch (Exception ex) {
153                         ex.printStackTrace()
154                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
155                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
156                 }
157         }
158         
159         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
160         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
161         // there are no Add-on Modules present
162         public void parseForVfModule(DelegateExecution execution) {
163                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
164                 msoLogger.debug("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
165                 
166                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
167                 msoLogger.debug("Vf Module to be deleted: " + delModuleId)
168                 List <String> qryModuleIdList = utils.getMultNodes(xml, "vf-module-id")
169                 List <String> qryBaseModuleList = utils.getMultNodes(xml, "is-base-vf-module")
170                 List <String> qryResourceVerList = utils.getMultNodes(xml, "resource-version")
171                 execution.setVariable("DAAIVfMod_moduleExists", false)
172                 execution.setVariable("DAAIVfMod_isBaseModule", false)
173                 execution.setVariable("DAAIVfMod_isLastModule", false)
174                 //
175                 def isBaseVfModule = "false"
176                 // loop through the Vf Module Ids looking for a match
177                 if (qryModuleIdList != null && !qryModuleIdList.empty) {
178                         msoLogger.debug("Existing Vf Module Id List: " + qryModuleIdList)
179                         msoLogger.debug("Existing Vf Module Resource Version List: " + qryResourceVerList)
180                         def moduleCntr = 0
181                         // the Generic Vnf resource-version in the 1st entry in the query response
182                         execution.setVariable("DAAIVfMod_genVnfRsrcVer", qryResourceVerList[moduleCntr])
183                         for (String qryModuleId : qryModuleIdList) {
184                                 if (delModuleId.equals(qryModuleId)) {
185                                         // a Vf Module with the requested Id exists
186                                         execution.setVariable("DAAIVfMod_moduleExists", true)
187                                         // find the corresponding value for the is-base-vf-module field
188                                         isBaseVfModule = qryBaseModuleList[moduleCntr]
189                                         // find the corresponding value for the resource-version field
190                                         // note: the Generic Vnf entry also has a resource-version field, so
191                                         //       add 1 to the index to get the corresponding Vf Module value
192                                         execution.setVariable("DAAIVfMod_vfModRsrcVer", qryResourceVerList[moduleCntr+1])
193                                         msoLogger.debug("Match found for Vf Module Id " + qryModuleId + " for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId") + ", Base Module is " + isBaseVfModule + ", Resource Version is " + execution.getVariable("vfModRsrcVer"))
194                                         break
195                                 }
196                                 moduleCntr++
197                         }
198                 }
199                 
200                 // determine if the module to be deleted is a Base Module and/or the Last Module
201                 if (execution.getVariable("DAAIVfMod_moduleExists") == true) {
202                         if (isBaseVfModule.equals("true") && qryModuleIdList.size() != 1) {
203                                 execution.setVariable("DAAIVfMod_parseModuleResponse",
204                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
205                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
206                                 execution.setVariable("DAAIVfMod_isBaseModule", true)
207                         } else {
208                                 if (isBaseVfModule.equals("true") && qryModuleIdList.size() == 1) {
209                                         execution.setVariable("DAAIVfMod_parseModuleResponse",
210                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
211                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
212                                         execution.setVariable("DAAIVfMod_isBaseModule", true)
213                                         execution.setVariable("DAAIVfMod_isLastModule", true)
214                                 } else {
215                                         if (qryModuleIdList.size() == 1) {
216                                                 execution.setVariable("DAAIVfMod_parseModuleResponse",
217                                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
218                                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
219                                                 execution.setVariable("DAAIVfMod_isLastModule", true)
220                                         } else {
221                                         execution.setVariable("DAAIVfMod_parseModuleResponse",
222                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
223                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
224                                         }
225                                 }
226                         }
227                         msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
228                 } else { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
229                         msoLogger.debug("Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId"))
230                         execution.setVariable("DAAIVfMod_parseModuleResponse",
231                                 "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
232                                 execution.getVariable("DAAIVfMod_vnfName"))
233                 }
234         }
235         
236         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
237         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
238         // there are no Add-on Modules present
239         public void parseForResourceVersion(DelegateExecution execution) {
240                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
241                 msoLogger.debug("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
242                 String resourceVer = utils.getNodeText(xml, "resource-version")
243                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", resourceVer)
244                 msoLogger.debug("Latest Generic VNF Resource Version: " + resourceVer)
245         }
246         
247         
248         // generates a WorkflowException if the A&AI query returns a response code other than 200
249         public void handleAAIQueryFailure(DelegateExecution execution) {
250                 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, "");
251                 def errorCode = 5000
252                 // set the errorCode to distinguish between a A&AI failure
253                 // and the Generic Vnf Id not found
254                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
255                         errorCode = 1002
256                 }
257                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
258         }
259         
260         // generates a WorkflowException if
261         //              - the A&AI Vf Module DELETE returns a response code other than 200
262         //              - the Vf Module is a Base Module that is not the last Vf Module
263         //              - the Vf Module does not exist for the Generic Vnf
264         public void handleDeleteVfModuleFailure(DelegateExecution execution) {
265                 def errorCode = 2000
266                 def errorResponse = ""
267                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
268                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
269                         msoLogger.debug("AAI failure deleting a Vf Module: " + execution.getVariable("DAAIVfMod_deleteVfModuleResponse"))
270                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
271                         msoLogger.debug("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
272                         errorCode = 5000
273                 } else {
274                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
275                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {
276                                 // attempt to delete a Base Module that is not the last Vf Module
277                                 msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
278                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
279                                 msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
280                                 errorCode = 1002
281                         } else {
282                                 // attempt to delete a non-existant Vf Module
283                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
284                                         msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
285                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
286                                         msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
287                                         errorCode = 1002
288                                 } else {
289                                         // if the responses get populated corerctly, we should never get here
290                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
291                                 }
292                         }
293                 }
294
295                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during DeleteAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
296                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
297
298         }
299
300         // generates a WorkflowException if
301         //              - the A&AI Generic Vnf DELETE returns a response code other than 200
302         public void handleDeleteGenericVnfFailure(DelegateExecution execution) {
303                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "AAI error occurred deleting the Generic Vnf", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"));
304                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
305         }
306 }