2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.bpmn.common.scripts
\r
22 import org.camunda.bpm.engine.runtime.Execution
\r
23 import org.openecomp.mso.bpmn.core.WorkflowException
\r
24 import org.openecomp.mso.rest.APIResponse
\r
25 import org.openecomp.mso.rest.RESTClient;
\r
26 import org.openecomp.mso.rest.RESTConfig;
\r
29 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
\r
31 def Prefix="DAAIVfMod_"
\r
32 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
33 public void initProcessVariables(Execution execution) {
\r
34 execution.setVariable("prefix",Prefix)
\r
35 execution.setVariable("DAAIVfMod_vnfId",null)
\r
36 execution.setVariable("DAAIVfMod_vnfName",null)
\r
37 execution.setVariable("DAAIVfMod_genVnfRsrcVer",null)
\r
38 execution.setVariable("DAAIVfMod_vfModuleId",null)
\r
39 execution.setVariable("DAAIVfMod_vfModRsrcVer",null)
\r
40 execution.setVariable("DAAIVfMod_genericVnfEndpoint",null)
\r
41 execution.setVariable("DAAIVfMod_vfModuleEndpoint",null)
\r
42 execution.setVariable("DAAIVfMod_moduleExists",false)
\r
43 execution.setVariable("DAAIVfMod_isBaseModule", false)
\r
44 execution.setVariable("DAAIVfMod_isLastModule", false)
\r
46 // DeleteAAIVfModule workflow response variable placeholders
\r
47 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode",null)
\r
48 execution.setVariable("DAAIVfMod_queryGenericVnfResponse","")
\r
49 execution.setVariable("DAAIVfMod_parseModuleResponse","")
\r
50 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode",null)
\r
51 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse","")
\r
52 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode",null)
\r
53 execution.setVariable("DAAIVfMod_deleteVfModuleResponse","")
\r
57 // parse the incoming DELETE_VF_MODULE request and store the Generic Vnf
\r
58 // and Vf Module Ids in the flow Execution
\r
59 public void preProcessRequest(Execution execution) {
\r
60 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
61 def xml = execution.getVariable("DeleteAAIVfModuleRequest")
\r
62 utils.logAudit("DeleteAAIVfModule Request: " + xml)
\r
63 utils.log("DEBUG", "input request xml:" + xml, isDebugEnabled)
\r
64 initProcessVariables(execution)
\r
65 def vnfId = utils.getNodeText(xml,"vnf-id")
\r
66 def vfModuleId = utils.getNodeText(xml,"vf-module-id")
\r
67 execution.setVariable("DAAIVfMod_vnfId", vnfId)
\r
68 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
\r
70 AaiUtil aaiUriUtil = new AaiUtil(this)
\r
71 def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
\r
72 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
\r
74 execution.setVariable("DAAIVfMod_genericVnfEndpoint", "${aai_uri}/" + vnfId)
\r
75 execution.setVariable("DAAIVfMod_vfModuleEndpoint", "${aai_uri}/" + vnfId +
\r
76 "/vf-modules/vf-module/" + vfModuleId)
\r
79 // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id
\r
80 // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist
\r
81 public void queryAAIForGenericVnf(Execution execution) {
\r
82 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
83 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
\r
84 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_genericVnfEndpoint") + "?depth=1"
\r
85 utils.logAudit("DeleteAAIVfModule endPoint: " + endPoint)
\r
86 def aaiRequestId = UUID.randomUUID().toString()
\r
88 RESTConfig config = new RESTConfig(endPoint);
\r
89 utils.log("DEBUG","queryAAIForGenericVnf() endpoint-" + endPoint, isDebugEnabled)
\r
90 def responseData = ""
\r
92 RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
\r
93 addHeader("Accept","application/xml");
\r
94 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
\r
96 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
\r
97 client.addAuthorizationHeader(basicAuthCred)
\r
99 utils.log("DEBUG", "invoking GET call to AAI endpoint :"+System.lineSeparator()+endPoint,isDebugEnabled)
\r
100 APIResponse response = client.httpGet()
\r
101 utils.logAudit("DeleteAAIVfModule - invoking httpGet to AAI")
\r
103 responseData = response.getResponseBodyAsString()
\r
104 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", response.getStatusCode())
\r
105 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", responseData)
\r
106 utils.logAudit("AAI Response: " + responseData)
\r
107 utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)
\r
108 utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)
\r
109 } catch (Exception ex) {
\r
110 utils.log("DEBUG", "Exception occurred while executing AAI GET:" + ex.getMessage(),isDebugEnabled)
\r
111 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())
\r
112 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")
\r
117 // construct and send a DELETE request to A&AI to delete a Generic Vnf
\r
118 // note: to get here, all the modules associated with the Generic Vnf must already be deleted
\r
119 public void deleteGenericVnf(Execution execution) {
\r
120 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
121 def aaiRequestId = UUID.randomUUID().toString()
\r
122 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_genericVnfEndpoint") +
\r
123 "/?resource-version=" + execution.getVariable("DAAIVfMod_genVnfRsrcVer")
\r
124 utils.logAudit("AAI endPoint: " + endPoint)
\r
125 RESTConfig config = new RESTConfig(endPoint);
\r
126 utils.log("DEBUG","deleteGenericVnf() endpoint-" + endPoint, isDebugEnabled)
\r
127 def responseData = ""
\r
129 RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
\r
130 addHeader("Accept","application/xml");
\r
132 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
\r
134 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
\r
135 client.addAuthorizationHeader(basicAuthCred)
\r
137 APIResponse response = client.httpDelete()
\r
139 responseData = response.getResponseBodyAsString()
\r
140 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", response.getStatusCode())
\r
141 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", responseData)
\r
142 utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)
\r
143 utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)
\r
144 } catch (Exception ex) {
\r
145 ex.printStackTrace()
\r
146 utils.log("DEBUG", "Exception occurred while executing AAI DELETE:" + ex.getMessage(),isDebugEnabled)
\r
147 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
\r
151 // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
\r
152 public void deleteVfModule(Execution execution) {
\r
153 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
154 def endPoint = execution.getVariable("URN_aai_endpoint") + execution.getVariable("DAAIVfMod_vfModuleEndpoint") +
\r
155 "/?resource-version=" + execution.getVariable("DAAIVfMod_vfModRsrcVer")
\r
156 def aaiRequestId = UUID.randomUUID().toString()
\r
158 RESTConfig config = new RESTConfig(endPoint);
\r
159 utils.log("DEBUG","deleteVfModule() endpoint-" + endPoint, isDebugEnabled)
\r
160 def responseData = ""
\r
162 RESTClient client = new RESTClient(config).addHeader("X-TransactionId", aaiRequestId).addHeader("X-FromAppId", "MSO").
\r
163 addHeader("Accept","application/xml");
\r
165 String basicAuthCred = utils.getBasicAuth(execution.getVariable("URN_aai_auth"),execution.getVariable("URN_mso_msoKey"))
\r
167 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
\r
168 client.addAuthorizationHeader(basicAuthCred)
\r
170 APIResponse response = client.httpDelete()
\r
172 utils.logAudit("DeleteAAIVfModule - invoking httpDelete to AAI")
\r
174 responseData = response.getResponseBodyAsString()
\r
175 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", response.getStatusCode())
\r
176 execution.setVariable("DAAIVfMod_deleteVfModuleResponse", responseData)
\r
177 utils.logAudit("DeleteAAIVfModule - AAI Response" + responseData)
\r
178 utils.log("DEBUG", "Response code:" + response.getStatusCode(), isDebugEnabled)
\r
179 utils.log("DEBUG", "Response:" + System.lineSeparator()+responseData,isDebugEnabled)
\r
181 } catch (Exception ex) {
\r
182 ex.printStackTrace()
\r
183 utils.log("DEBUG", "Exception occurred while executing AAI PUT:" + ex.getMessage(),isDebugEnabled)
\r
184 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
\r
188 // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
\r
189 // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
\r
190 // there are no Add-on Modules present
\r
191 public void parseForVfModule(Execution execution) {
\r
192 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
193 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
\r
194 utils.logAudit("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
\r
196 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
\r
197 utils.log("DEBUG", "Vf Module to be deleted: " + delModuleId, isDebugEnabled)
\r
198 List <String> qryModuleIdList = utils.getMultNodes(xml, "vf-module-id")
\r
199 List <String> qryBaseModuleList = utils.getMultNodes(xml, "is-base-vf-module")
\r
200 List <String> qryResourceVerList = utils.getMultNodes(xml, "resource-version")
\r
201 execution.setVariable("DAAIVfMod_moduleExists", false)
\r
202 execution.setVariable("DAAIVfMod_isBaseModule", false)
\r
203 execution.setVariable("DAAIVfMod_isLastModule", false)
\r
205 def isBaseVfModule = "false"
\r
206 // loop through the Vf Module Ids looking for a match
\r
207 if (qryModuleIdList != null && !qryModuleIdList.empty) {
\r
208 utils.log("DEBUG", "Existing Vf Module Id List: " + qryModuleIdList, isDebugEnabled)
\r
209 utils.log("DEBUG", "Existing Vf Module Resource Version List: " + qryResourceVerList, isDebugEnabled)
\r
211 // the Generic Vnf resource-version in the 1st entry in the query response
\r
212 execution.setVariable("DAAIVfMod_genVnfRsrcVer", qryResourceVerList[moduleCntr])
\r
213 for (String qryModuleId : qryModuleIdList) {
\r
214 if (delModuleId.equals(qryModuleId)) {
\r
215 // a Vf Module with the requested Id exists
\r
216 execution.setVariable("DAAIVfMod_moduleExists", true)
\r
217 // find the corresponding value for the is-base-vf-module field
\r
218 isBaseVfModule = qryBaseModuleList[moduleCntr]
\r
219 // find the corresponding value for the resource-version field
\r
220 // note: the Generic Vnf entry also has a resource-version field, so
\r
221 // add 1 to the index to get the corresponding Vf Module value
\r
222 execution.setVariable("DAAIVfMod_vfModRsrcVer", qryResourceVerList[moduleCntr+1])
\r
223 utils.log("DEBUG", "Match found for Vf Module Id " + qryModuleId + " for Generic Vnf Id " +
\r
224 execution.getVariable("DAAIVfMod_vnfId") + ", Base Module is " + isBaseVfModule +
\r
225 ", Resource Version is " + execution.getVariable("vfModRsrcVer"), isDebugEnabled)
\r
232 // determine if the module to be deleted is a Base Module and/or the Last Module
\r
233 if (execution.getVariable("DAAIVfMod_moduleExists") == true) {
\r
234 if (isBaseVfModule.equals("true") && qryModuleIdList.size() != 1) {
\r
235 execution.setVariable("DAAIVfMod_parseModuleResponse",
\r
236 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
\r
237 execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
\r
238 execution.setVariable("DAAIVfMod_isBaseModule", true)
\r
240 if (isBaseVfModule.equals("true") && qryModuleIdList.size() == 1) {
\r
241 execution.setVariable("DAAIVfMod_parseModuleResponse",
\r
242 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
\r
243 execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
\r
244 execution.setVariable("DAAIVfMod_isBaseModule", true)
\r
245 execution.setVariable("DAAIVfMod_isLastModule", true)
\r
247 if (qryModuleIdList.size() == 1) {
\r
248 execution.setVariable("DAAIVfMod_parseModuleResponse",
\r
249 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
\r
250 execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
\r
251 execution.setVariable("DAAIVfMod_isLastModule", true)
\r
253 execution.setVariable("DAAIVfMod_parseModuleResponse",
\r
254 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
\r
255 execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
\r
259 utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)
\r
260 } else { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
\r
261 utils.log("DEBUG", "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
\r
262 execution.getVariable("DAAIVfMod_vnfId"), isDebugEnabled)
\r
263 execution.setVariable("DAAIVfMod_parseModuleResponse",
\r
264 "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
\r
265 execution.getVariable("DAAIVfMod_vnfName"))
\r
269 // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
\r
270 // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
\r
271 // there are no Add-on Modules present
\r
272 public void parseForResourceVersion(Execution execution) {
\r
273 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
274 def xml = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
\r
275 utils.logAudit("DeleteAAIVfModule - queryGenericVnfResponse" + xml)
\r
276 String resourceVer = utils.getNodeText1(xml, "resource-version")
\r
277 execution.setVariable("DAAIVfMod_genVnfRsrcVer", resourceVer)
\r
278 utils.log("DEBUG", "Latest Generic VNF Resource Version: " + resourceVer)
\r
282 // generates a WorkflowException if the A&AI query returns a response code other than 200
\r
283 public void handleAAIQueryFailure(Execution execution) {
\r
284 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
286 utils.log("ERROR", "Error occurred attempting to query AAI, Response Code " +
\r
287 execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " +
\r
288 execution.getVariable("DAAIVfMod_queryGenericVnfResponse"), isDebugEnabled)
\r
289 def errorCode = 5000
\r
290 // set the errorCode to distinguish between a A&AI failure
\r
291 // and the Generic Vnf Id not found
\r
292 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
\r
295 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
\r
298 // generates a WorkflowException if
\r
299 // - the A&AI Vf Module DELETE returns a response code other than 200
\r
300 // - the Vf Module is a Base Module that is not the last Vf Module
\r
301 // - the Vf Module does not exist for the Generic Vnf
\r
302 public void handleDeleteVfModuleFailure(Execution execution) {
\r
303 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
305 def errorCode = 2000
\r
306 def errorResponse = ""
\r
307 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
\r
308 execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
\r
309 utils.log("DEBUG", "AAI failure deleting a Vf Module: " +
\r
310 execution.getVariable("DAAIVfMod_deleteVfModuleResponse"), isDebugEnabled)
\r
311 errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
\r
312 utils.logAudit("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
\r
315 if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
\r
316 execution.getVariable("DAAIVfMod_isLastModule") == false) {
\r
317 // attempt to delete a Base Module that is not the last Vf Module
\r
318 utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)
\r
319 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
\r
320 utils.logAudit("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
\r
323 // attempt to delete a non-existant Vf Module
\r
324 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
\r
325 utils.log("DEBUG", execution.getVariable("DAAIVfMod_parseModuleResponse"), isDebugEnabled)
\r
326 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
\r
327 utils.logAudit("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
\r
330 // if the responses get populated corerctly, we should never get here
\r
331 errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
\r
336 utils.log("ERROR", "Error occurred during DeleteAAIVfModule flow: " + errorResponse, isDebugEnabled)
\r
337 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
\r
341 // generates a WorkflowException if
\r
342 // - the A&AI Generic Vnf DELETE returns a response code other than 200
\r
343 public void handleDeleteGenericVnfFailure(Execution execution) {
\r
344 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
345 utils.log("ERROR", "AAI error occurred deleting the Generic Vnf: "
\r
346 + execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"), isDebugEnabled)
\r
347 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
\r