Containerization feature of SO
[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.rest.APIResponse
26 import org.onap.so.rest.RESTClient;
27 import org.onap.so.rest.RESTConfig;
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_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 DelegateExecution
63         public void preProcessRequest(DelegateExecution execution) {
64                 def xml = execution.getVariable("DeleteAAIVfModuleRequest")
65                 msoLogger.debug("DeleteAAIVfModule Request: " + xml)
66                 msoLogger.debug("input request xml:" + xml)
67                 initProcessVariables(execution)
68                 def vnfId = utils.getNodeText(xml,"vnf-id")
69                 def vfModuleId = utils.getNodeText(xml,"vf-module-id")
70                 execution.setVariable("DAAIVfMod_vnfId", vnfId)
71                 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
72                 
73                 AaiUtil aaiUriUtil = new AaiUtil(this)
74                 def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
75                 msoLogger.debug('AAI URI is: ' + aai_uri)
76                 
77                 execution.setVariable("DAAIVfMod_genericVnfEndpoint", "${aai_uri}/" + vnfId)
78                 execution.setVariable("DAAIVfMod_vfModuleEndpoint", "${aai_uri}/" + vnfId +
79                          "/vf-modules/vf-module/" + vfModuleId)
80         }
81         
82         // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id
83         // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist
84         public void queryAAIForGenericVnf(DelegateExecution execution) {
85                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
86                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("DAAIVfMod_genericVnfEndpoint") + "?depth=1"
87                 msoLogger.debug("DeleteAAIVfModule endPoint: " + endPoint)
88                 def aaiRequestId = utils.getRequestID()
89
90                 RESTConfig config = new RESTConfig(endPoint);
91                 msoLogger.debug("queryAAIForGenericVnf() endpoint-" + endPoint)
92                 def responseData = ""
93                 try {
94                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
95                                 addHeader("Accept","application/xml");
96                         String basicAuthCred = utils.getBasicAuth(UrnPropertiesReader.getVariable("aai.auth", execution),UrnPropertiesReader.getVariable("mso.msoKey", execution))
97                                 
98                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {
99                                 client.addAuthorizationHeader(basicAuthCred)
100                         }
101                         msoLogger.debug("invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint)
102                         APIResponse response = client.httpGet()
103                         msoLogger.debug("DeleteAAIVfModule - invoking httpGet to AAI")
104
105                         responseData = response.getResponseBodyAsString()
106                         execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", response.getStatusCode())
107                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", responseData)
108                         msoLogger.debug("AAI Response: " + responseData)
109                         msoLogger.debug("Response code:" + response.getStatusCode())
110                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
111                 } catch (Exception ex) {
112                         msoLogger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
113                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())
114                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")
115
116                 }
117         }
118         
119         // construct and send a DELETE request to A&AI to delete a Generic Vnf
120         // note: to get here, all the modules associated with the Generic Vnf must already be deleted
121         public void deleteGenericVnf(DelegateExecution execution) {
122                 def aaiRequestId = utils.getRequestID()
123                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("DAAIVfMod_genericVnfEndpoint") +
124                         "/?resource-version=" + execution.getVariable("DAAIVfMod_genVnfRsrcVer")
125                 msoLogger.debug("AAI endPoint: " + endPoint)
126                 RESTConfig config = new RESTConfig(endPoint);
127                 msoLogger.debug("deleteGenericVnf() endpoint-" + endPoint)
128                 def responseData = ""
129                 try {
130                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
131                                 addHeader("Accept","application/xml");
132                         
133                         String basicAuthCred = utils.getBasicAuth(UrnPropertiesReader.getVariable("aai.auth", execution),UrnPropertiesReader.getVariable("mso.msoKey", execution))
134                                         
135                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {
136                                 client.addAuthorizationHeader(basicAuthCred)
137                         }
138                         APIResponse response = client.httpDelete()
139                                 
140                         responseData = response.getResponseBodyAsString()
141                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", response.getStatusCode())
142                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", responseData)
143                         msoLogger.debug("Response code:" + response.getStatusCode())
144                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
145                 } catch (Exception ex) {
146                         ex.printStackTrace()
147                         msoLogger.debug("Exception occurred while executing AAI DELETE:" + ex.getMessage())
148                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
149                 }
150         }
151
152         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
153         public void deleteVfModule(DelegateExecution execution) {
154                 def endPoint = UrnPropertiesReader.getVariable("aai.endpoint", execution) + execution.getVariable("DAAIVfMod_vfModuleEndpoint") +
155                         "/?resource-version=" + execution.getVariable("DAAIVfMod_vfModRsrcVer")
156                 def aaiRequestId = utils.getRequestID()
157
158                 RESTConfig config = new RESTConfig(endPoint);
159                 msoLogger.debug("deleteVfModule() endpoint-" + endPoint)
160                 def responseData = ""
161                 try {
162                         RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
163                                 addHeader("Accept","application/xml");
164                         
165                         String basicAuthCred = utils.getBasicAuth(UrnPropertiesReader.getVariable("aai.auth", execution),UrnPropertiesReader.getVariable("mso.msoKey", execution))
166                                         
167                         if (basicAuthCred != null && !"".equals(basicAuthCred)) {
168                                 client.addAuthorizationHeader(basicAuthCred)
169                         }
170                         APIResponse response = client.httpDelete()
171                         
172                         msoLogger.debug("DeleteAAIVfModule - invoking httpDelete to AAI")
173                         
174                         responseData = response.getResponseBodyAsString()
175                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", response.getStatusCode())
176                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", responseData)
177                         msoLogger.debug("DeleteAAIVfModule - AAI Response" + responseData)
178                         msoLogger.debug("Response code:" + response.getStatusCode())
179                         msoLogger.debug("Response:" + System.lineSeparator()+responseData)
180
181                 } catch (Exception ex) {
182                         ex.printStackTrace()
183                         msoLogger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
184                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
185                 }
186         }
187         
188         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
189         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
190         // there are no Add-on Modules present
191         public void parseForVfModule(DelegateExecution execution) {
192                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
193                 msoLogger.debug("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
194                 
195                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
196                 msoLogger.debug("Vf Module to be deleted: " + delModuleId)
197                 List <String> qryModuleIdList = utils.getMultNodes(xml, "vf-module-id")
198                 List <String> qryBaseModuleList = utils.getMultNodes(xml, "is-base-vf-module")
199                 List <String> qryResourceVerList = utils.getMultNodes(xml, "resource-version")
200                 execution.setVariable("DAAIVfMod_moduleExists", false)
201                 execution.setVariable("DAAIVfMod_isBaseModule", false)
202                 execution.setVariable("DAAIVfMod_isLastModule", false)
203                 //
204                 def isBaseVfModule = "false"
205                 // loop through the Vf Module Ids looking for a match
206                 if (qryModuleIdList != null && !qryModuleIdList.empty) {
207                         msoLogger.debug("Existing Vf Module Id List: " + qryModuleIdList)
208                         msoLogger.debug("Existing Vf Module Resource Version List: " + qryResourceVerList)
209                         def moduleCntr = 0
210                         // the Generic Vnf resource-version in the 1st entry in the query response
211                         execution.setVariable("DAAIVfMod_genVnfRsrcVer", qryResourceVerList[moduleCntr])
212                         for (String qryModuleId : qryModuleIdList) {
213                                 if (delModuleId.equals(qryModuleId)) {
214                                         // a Vf Module with the requested Id exists
215                                         execution.setVariable("DAAIVfMod_moduleExists", true)
216                                         // find the corresponding value for the is-base-vf-module field
217                                         isBaseVfModule = qryBaseModuleList[moduleCntr]
218                                         // find the corresponding value for the resource-version field
219                                         // note: the Generic Vnf entry also has a resource-version field, so
220                                         //       add 1 to the index to get the corresponding Vf Module value
221                                         execution.setVariable("DAAIVfMod_vfModRsrcVer", qryResourceVerList[moduleCntr+1])
222                                         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"))
223                                         break
224                                 }
225                                 moduleCntr++
226                         }
227                 }
228                 
229                 // determine if the module to be deleted is a Base Module and/or the Last Module
230                 if (execution.getVariable("DAAIVfMod_moduleExists") == true) {
231                         if (isBaseVfModule.equals("true") && qryModuleIdList.size() != 1) {
232                                 execution.setVariable("DAAIVfMod_parseModuleResponse",
233                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
234                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
235                                 execution.setVariable("DAAIVfMod_isBaseModule", true)
236                         } else {
237                                 if (isBaseVfModule.equals("true") && qryModuleIdList.size() == 1) {
238                                         execution.setVariable("DAAIVfMod_parseModuleResponse",
239                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
240                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
241                                         execution.setVariable("DAAIVfMod_isBaseModule", true)
242                                         execution.setVariable("DAAIVfMod_isLastModule", true)
243                                 } else {
244                                         if (qryModuleIdList.size() == 1) {
245                                                 execution.setVariable("DAAIVfMod_parseModuleResponse",
246                                                         "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
247                                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
248                                                 execution.setVariable("DAAIVfMod_isLastModule", true)
249                                         } else {
250                                         execution.setVariable("DAAIVfMod_parseModuleResponse",
251                                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
252                                                 execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
253                                         }
254                                 }
255                         }
256                         msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
257                 } else { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
258                         msoLogger.debug("Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId"))
259                         execution.setVariable("DAAIVfMod_parseModuleResponse",
260                                 "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
261                                 execution.getVariable("DAAIVfMod_vnfName"))
262                 }
263         }
264         
265         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
266         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
267         // there are no Add-on Modules present
268         public void parseForResourceVersion(DelegateExecution execution) {
269                 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
270                 msoLogger.debug("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
271                 String resourceVer = utils.getNodeText(xml, "resource-version")
272                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", resourceVer)
273                 msoLogger.debug("Latest Generic VNF Resource Version: " + resourceVer)
274         }
275         
276         
277         // generates a WorkflowException if the A&AI query returns a response code other than 200
278         public void handleAAIQueryFailure(DelegateExecution execution) {
279                 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, "");
280                 def errorCode = 5000
281                 // set the errorCode to distinguish between a A&AI failure
282                 // and the Generic Vnf Id not found
283                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
284                         errorCode = 1002
285                 }
286                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
287         }
288         
289         // generates a WorkflowException if
290         //              - the A&AI Vf Module DELETE returns a response code other than 200
291         //              - the Vf Module is a Base Module that is not the last Vf Module
292         //              - the Vf Module does not exist for the Generic Vnf
293         public void handleDeleteVfModuleFailure(DelegateExecution execution) {
294                 def errorCode = 2000
295                 def errorResponse = ""
296                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
297                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
298                         msoLogger.debug("AAI failure deleting a Vf Module: " + execution.getVariable("DAAIVfMod_deleteVfModuleResponse"))
299                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
300                         msoLogger.debug("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
301                         errorCode = 5000
302                 } else {
303                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
304                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {
305                                 // attempt to delete a Base Module that is not the last Vf Module
306                                 msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
307                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
308                                 msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
309                                 errorCode = 1002
310                         } else {
311                                 // attempt to delete a non-existant Vf Module
312                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
313                                         msoLogger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
314                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
315                                         msoLogger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
316                                         errorCode = 1002
317                                 } else {
318                                         // if the responses get populated corerctly, we should never get here
319                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
320                                 }
321                         }
322                 }
323
324                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Error occurred during DeleteAAIVfModule flow", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, errorResponse);
325                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
326
327         }
328
329         // generates a WorkflowException if
330         //              - the A&AI Generic Vnf DELETE returns a response code other than 200
331         public void handleDeleteGenericVnfFailure(DelegateExecution execution) {
332                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "AAI error occurred deleting the Generic Vnf", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"));
333                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
334         }
335 }