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