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 org.onap.so.bpmn.common.scripts
23 import javax.ws.rs.NotFoundException
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.camunda.bpm.model.dmn.instance.OrganizationUnit
27 import org.hibernate.engine.jdbc.Size.LobMultiplier
28 import org.onap.so.bpmn.core.WorkflowException
29 import org.onap.aai.domain.yang.GenericVnf
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.client.aai.AAIObjectType
32 import org.onap.so.client.aai.AAIResourcesClient
33 import org.onap.so.client.aai.entities.AAIResultWrapper
34 import org.onap.so.client.aai.entities.uri.AAIResourceUri
35 import org.onap.so.client.aai.entities.uri.AAIUriFactory
36 import org.onap.so.client.graphinventory.entities.uri.Depth
37 import org.springframework.web.util.UriUtils
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
43 public class PrepareUpdateAAIVfModule extends VfModuleBase {
44 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, PrepareUpdateAAIVfModule.class);
47 ExceptionUtil exceptionUtil = new ExceptionUtil()
48 private MsoUtils utils = new MsoUtils()
50 * Initialize the flow's variables.
52 * @param execution The flow's execution instance.
54 public void initProcessVariables(DelegateExecution execution) {
55 execution.setVariable('prefix', 'PUAAIVfMod_')
56 execution.setVariable('PUAAIVfMod_vnfId', null)
57 execution.setVariable('PUAAIVfMod_vfModuleId', null)
58 execution.setVariable('PUAAIVfMod_vnfName', null)
59 execution.setVariable('PUAAIVfMod_orchestrationStatus', null)
60 execution.setVariable('PUAAIVfMod_vfModule', null)
61 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
62 execution.setVariable('PUAAIVfMod_vfModuleValidationError', null)
63 execution.setVariable('PUAAIVfMod_getVnfResponseCode' ,null)
64 execution.setVariable('PUAAIVfMod_getVnfResponse', '')
65 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', null)
66 execution.setVariable('PUAAIVfMod_updateVfModuleResponse', '')
67 execution.setVariable('PUAAIVfMod_outVfModule', null)
71 * Check for missing elements in the received request.
73 * @param execution The flow's execution instance.
75 public void preProcessRequest(DelegateExecution execution) {
76 def method = getClass().getSimpleName() + '.preProcessRequest(' +
77 'execution=' + execution.getId() +
79 msoLogger.trace('Entered ' + method)
82 def xml = execution.getVariable('PrepareUpdateAAIVfModuleRequest')
83 msoLogger.debug('Received request xml:\n' + xml)
84 msoLogger.debug("PrepareUpdateAAIVfModule Request : " + xml)
86 initProcessVariables(execution)
88 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
89 execution.setVariable('PUAAIVfMod_vnfId', vnfId)
91 def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
92 execution.setVariable('PUAAIVfMod_vfModuleId', vfModuleId)
94 def orchestrationStatus = getRequiredNodeText(execution, xml,'orchestration-status')
95 execution.setVariable('PUAAIVfMod_orchestrationStatus', orchestrationStatus)
97 msoLogger.trace('Exited ' + method)
98 } catch (BpmnError e) {
100 } catch (Exception e) {
102 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
107 * Using the received vnfId, query AAI to get the corresponding Generic VNF.
108 * A 200 response is expected with the Generic VNF in the response body.
110 * @param execution The flow's execution instance.
112 public void getGenericVnf(DelegateExecution execution) {
113 def method = getClass().getSimpleName() + '.getGenericVnf(' +
114 'execution=' + execution.getId() +
116 msoLogger.trace('Entered ' + method)
119 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
123 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
124 AAIResourcesClient resourceClient = new AAIResourcesClient()
125 AAIResultWrapper wrapper = resourceClient.get(uri.depth(Depth.ONE), NotFoundException.class)
126 GenericVnf responseData = wrapper.asBean(GenericVnf.class).get()
128 execution.setVariable('PUAAIVfMod_getVnfResponse', responseData)
129 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 200)
131 } catch (Exception ex) {
133 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
134 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 500)
135 execution.setVariable('PUAAIVfMod_getVnfResponse', 'AAI GET Failed:' + ex.getMessage())
137 msoLogger.trace('Exited ' + method)
138 } catch (BpmnError e) {
140 } catch (Exception e) {
142 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getGenericVnf(): ' + e.getMessage())
147 * Validate the VF Module. That is, confirm that a VF Module with the input VF Module ID
148 * exists in the retrieved Generic VNF. Then, check to make sure that if that VF Module
149 * is the base VF Module and it's not the only VF Module for this Generic VNF, that we're not
150 * attempting to delete it.
152 * @param execution The flow's execution instance.
154 public void validateVfModule(DelegateExecution execution) {
155 def method = getClass().getSimpleName() + '.validateVfModule(' +
156 'execution=' + execution.getId() +
158 msoLogger.trace('Entered ' + method)
161 GenericVnf genericVnf = execution.getVariable('PUAAIVfMod_getVnfResponse')
162 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
163 def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
164 def vnfName = genericVnf.getVnfName()
165 execution.setVariable('PUAAIVfMod_vnfName', vnfName)
167 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
168 AAIResourcesClient resourceClient = new AAIResourcesClient()
172 // def VfModule vfModule = findVfModule(genericVnf, vfModuleId)
173 if (!resourceClient.exists(uri)) {
174 def String msg = 'VF Module \'' + vfModuleId + '\' does not exist in Generic VNF \'' + vnfId + '\''
175 execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
176 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
178 AAIResultWrapper wrapper = resourceClient.get(uri, NotFoundException.class)
179 org.onap.aai.domain.yang.VfModule vfModule = wrapper.asBean(org.onap.aai.domain.yang.VfModule.class).get()
181 def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
182 if (vfModule.isBaseVfModule && genericVnf.getVfModules().getVfModule().size() > 1 && vfModule.getOrchestrationStatus().equals('pending-delete')) {
183 def String msg = 'Orchestration status for VF Module \'' + vfModuleId +
184 '\' cannot be set to \'pending-delete\' since it is the base VF Module and it\'s not the only VF Module in Generic VNF \'' + vnfId + '\''
185 execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
186 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
188 execution.setVariable('PUAAIVfMod_vfModule', vfModule)
189 execution.setVariable('PUAAIVfMod_vfModuleOK', true)
193 msoLogger.trace('Exited ' + method)
194 } catch (BpmnError e) {
196 } catch (Exception e) {
198 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in validateVfModule(): ' + e.getMessage())
203 * Construct and send a PATCH request to AAI to update the VF Module.
205 * @param execution The flow's execution instance.
207 public void updateVfModule(DelegateExecution execution) {
208 def method = getClass().getSimpleName() + '.updateVfModule(' +
209 'execution=' + execution.getId() +
211 msoLogger.trace('Entered ' + method)
214 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
215 def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
216 def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
218 org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('PUAAIVfMod_vfModule')
220 vfModule.setVfModuleId(vfModuleId)
221 vfModule.setOrchestrationStatus(orchestrationStatus)
223 AAIResourcesClient client = new AAIResourcesClient()
224 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
225 client.update(uri, vfModule)
226 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 200)
227 // Set the output for this flow. The updated VfModule is an output, the generic VNF name, and for
228 // backward compatibilty, the heat-stack-id is an output
229 execution.setVariable('PUAAIVfMod_outVfModule', vfModule)
230 def vnfName = execution.getVariable('PUAAIVfMod_vnfName')
231 msoLogger.debug('Output PUAAIVfMod_vnfName set to ' + vnfName)
232 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
233 execution.setVariable('WorkflowResponse', vfModule)
235 def heatStackId = vfModule.getHeatStackId()
236 execution.setVariable('PUAAIVfMod_heatStackId', heatStackId)
237 msoLogger.debug('Output PUAAIVfMod_heatStackId set to \'' + heatStackId + '\'')
239 msoLogger.trace('Exited ' + method)
240 } catch (BpmnError e) {
241 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
243 } catch (Exception e) {
245 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
246 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
251 * Generates a WorkflowException if the AAI query returns a response code other than 200.
253 * @param execution The flow's execution instance.
255 public void handleVnfNotFound(DelegateExecution execution) {
256 def method = getClass().getSimpleName() + '.handleVnfNotFound(' +
257 'execution=' + execution.getId() +
259 msoLogger.trace('Entered ' + method)
261 msoLogger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('PUAAIVfMod_getVnfResponseCode'));
262 String processKey = getProcessKey(execution);
263 WorkflowException exception = new WorkflowException(processKey, 5000,
264 execution.getVariable('PUAAIVfMod_getVnfResponse'))
265 execution.setVariable('WorkflowException', exception)
267 msoLogger.trace('Exited ' + method)
271 * Generates a WorkflowException if the VF Module does not pass validation.
273 * @param execution The flow's execution instance.
275 public void handleVfModuleValidationError(DelegateExecution execution) {
276 def method = getClass().getSimpleName() + '.handleVfModuleValidationError(' +
277 'execution=' + execution.getId() +
279 msoLogger.trace('Entered ' + method)
281 def String errorMsg = 'VF Module validation error: ' + execution.getVariable('PUAAIVfMod_vfModuleValidationError')
282 msoLogger.error(errorMsg);
283 msoLogger.debug("PrepareUpdateAAIVfModule: Error Message : " + errorMsg)
285 String processKey = getProcessKey(execution);
286 WorkflowException exception = new WorkflowException(processKey, 5000, errorMsg)
287 execution.setVariable('WorkflowException', exception)
289 msoLogger.trace('Exited ' + method)
293 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
295 * @param execution The flow's execution instance.
297 public void handleUpdateVfModuleFailure(DelegateExecution execution) {
298 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
299 'execution=' + execution.getId() +
301 msoLogger.trace('Entered ' + method)
303 msoLogger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('PUAAIVfMod_updateVfModuleResponseCode'));
304 String processKey = getProcessKey(execution);
305 WorkflowException exception = new WorkflowException(processKey, 5000,
306 execution.getVariable('PUAAIVfMod_updateVfModuleResponse'))
307 execution.setVariable('WorkflowException', exception)
309 msoLogger.trace('Exited ' + method)