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