2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package com.att.bpm.scripts
22 import java.io.Serializable;
24 import org.camunda.bpm.engine.runtime.Execution
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
33 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
35 def Prefix="DAAIVfMod_"
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)
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","")
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)
74 AaiUtil aaiUriUtil = new AaiUtil(this)
75 def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
76 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
78 execution.setVariable("DAAIVfMod_genericVnfEndpoint", "${aai_uri}/" + vnfId)
79 execution.setVariable("DAAIVfMod_vfModuleEndpoint", "${aai_uri}/" + vnfId +
80 "/vf-modules/vf-module/" + vfModuleId)
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()
92 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
94 RESTConfig config = new RESTConfig(endPoint);
95 utils.log("DEBUG","queryAAIForGenericVnf() endpoint-" + endPoint, isDebugEnabled)
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)
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")
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) {
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())
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)
130 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
132 RESTConfig config = new RESTConfig(endPoint);
133 utils.log("DEBUG","deleteGenericVnf() endpoint-" + endPoint, isDebugEnabled)
134 def responseData = ""
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)
141 APIResponse response = client.httpDelete()
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) {
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())
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()
163 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
165 RESTConfig config = new RESTConfig(endPoint);
166 utils.log("DEBUG","deleteVfModule() endpoint-" + endPoint, isDebugEnabled)
167 def responseData = ""
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)
174 APIResponse response = client.httpDelete()
175 utils.logAudit("DeleteAAIVfModule - invoking httpDelete to AAI")
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)
184 } catch (Exception ex) {
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())
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)
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)
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)
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)
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)
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)
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)
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")
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"))
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)
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")
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)
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) {
299 String processKey = getProcessKey(execution);
300 WorkflowException exception = new WorkflowException(processKey, errorCode,
301 execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
302 execution.setVariable("WorkflowException", exception)
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")
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)
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)
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)
337 // if the responses get populated corerctly, we should never get here
338 errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
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)
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)