2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import javax.ws.rs.NotFoundException
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.camunda.bpm.model.dmn.instance.OrganizationUnit
29 import org.hibernate.engine.jdbc.Size.LobMultiplier
30 import org.onap.so.bpmn.core.WorkflowException
31 import org.onap.aai.domain.yang.GenericVnf
32 import org.onap.so.bpmn.core.UrnPropertiesReader
33 import org.onap.so.client.aai.AAIObjectType
34 import org.onap.so.client.aai.AAIResourcesClient
35 import org.onap.so.client.aai.entities.AAIResultWrapper
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
38 import org.onap.so.client.graphinventory.entities.uri.Depth
39 import org.springframework.web.util.UriUtils
40 import org.onap.so.logger.MessageEnum
41 import org.slf4j.Logger
42 import org.slf4j.LoggerFactory
46 public class PrepareUpdateAAIVfModule extends VfModuleBase {
47 private static final Logger logger = LoggerFactory.getLogger( PrepareUpdateAAIVfModule.class);
50 ExceptionUtil exceptionUtil = new ExceptionUtil()
51 private MsoUtils utils = new MsoUtils()
53 * Initialize the flow's variables.
55 * @param execution The flow's execution instance.
57 public void initProcessVariables(DelegateExecution execution) {
58 execution.setVariable('prefix', 'PUAAIVfMod_')
59 execution.setVariable('PUAAIVfMod_vnfId', null)
60 execution.setVariable('PUAAIVfMod_vfModuleId', null)
61 execution.setVariable('PUAAIVfMod_vnfName', null)
62 execution.setVariable('PUAAIVfMod_orchestrationStatus', null)
63 execution.setVariable('PUAAIVfMod_vfModule', null)
64 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
65 execution.setVariable('PUAAIVfMod_vfModuleValidationError', null)
66 execution.setVariable('PUAAIVfMod_getVnfResponseCode' ,null)
67 execution.setVariable('PUAAIVfMod_getVnfResponse', '')
68 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', null)
69 execution.setVariable('PUAAIVfMod_updateVfModuleResponse', '')
70 execution.setVariable('PUAAIVfMod_outVfModule', null)
74 * Check for missing elements in the received request.
76 * @param execution The flow's execution instance.
78 public void preProcessRequest(DelegateExecution execution) {
79 def method = getClass().getSimpleName() + '.preProcessRequest(' +
80 'execution=' + execution.getId() +
82 logger.trace('Entered ' + method)
85 def xml = execution.getVariable('PrepareUpdateAAIVfModuleRequest')
86 logger.debug('Received request xml:\n' + xml)
87 logger.debug("PrepareUpdateAAIVfModule Request : " + xml)
89 initProcessVariables(execution)
91 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
92 execution.setVariable('PUAAIVfMod_vnfId', vnfId)
94 def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
95 execution.setVariable('PUAAIVfMod_vfModuleId', vfModuleId)
97 def orchestrationStatus = getRequiredNodeText(execution, xml,'orchestration-status')
98 execution.setVariable('PUAAIVfMod_orchestrationStatus', orchestrationStatus)
100 logger.trace('Exited ' + method)
101 } catch (BpmnError e) {
103 } catch (Exception e) {
105 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
110 * Using the received vnfId, query AAI to get the corresponding Generic VNF.
111 * A 200 response is expected with the Generic VNF in the response body.
113 * @param execution The flow's execution instance.
115 public void getGenericVnf(DelegateExecution execution) {
116 def method = getClass().getSimpleName() + '.getGenericVnf(' +
117 'execution=' + execution.getId() +
119 logger.trace('Entered ' + method)
122 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
126 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
127 AAIResourcesClient resourceClient = new AAIResourcesClient()
128 AAIResultWrapper wrapper = resourceClient.get(uri.depth(Depth.ONE), NotFoundException.class)
129 GenericVnf responseData = wrapper.asBean(GenericVnf.class).get()
131 execution.setVariable('PUAAIVfMod_getVnfResponse', responseData)
132 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 200)
134 } catch (Exception ex) {
136 logger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
137 execution.setVariable('PUAAIVfMod_getVnfResponseCode', 500)
138 execution.setVariable('PUAAIVfMod_getVnfResponse', 'AAI GET Failed:' + ex.getMessage())
140 logger.trace('Exited ' + method)
141 } catch (BpmnError e) {
143 } catch (Exception e) {
145 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getGenericVnf(): ' + e.getMessage())
150 * Validate the VF Module. That is, confirm that a VF Module with the input VF Module ID
151 * exists in the retrieved Generic VNF. Then, check to make sure that if that VF Module
152 * is the base VF Module and it's not the only VF Module for this Generic VNF, that we're not
153 * attempting to delete it.
155 * @param execution The flow's execution instance.
157 public void validateVfModule(DelegateExecution execution) {
158 def method = getClass().getSimpleName() + '.validateVfModule(' +
159 'execution=' + execution.getId() +
161 logger.trace('Entered ' + method)
164 GenericVnf genericVnf = execution.getVariable('PUAAIVfMod_getVnfResponse')
165 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
166 def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
167 def vnfName = genericVnf.getVnfName()
168 execution.setVariable('PUAAIVfMod_vnfName', vnfName)
170 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
171 AAIResourcesClient resourceClient = new AAIResourcesClient()
175 // def VfModule vfModule = findVfModule(genericVnf, vfModuleId)
176 if (!resourceClient.exists(uri)) {
177 def String msg = 'VF Module \'' + vfModuleId + '\' does not exist in Generic VNF \'' + vnfId + '\''
178 execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
179 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
181 AAIResultWrapper wrapper = resourceClient.get(uri, NotFoundException.class)
182 org.onap.aai.domain.yang.VfModule vfModule = wrapper.asBean(org.onap.aai.domain.yang.VfModule.class).get()
184 def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
185 if (vfModule.isBaseVfModule && genericVnf.getVfModules().getVfModule().size() > 1 && vfModule.getOrchestrationStatus().equals('pending-delete')) {
186 def String msg = 'Orchestration status for VF Module \'' + vfModuleId +
187 '\' 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 + '\''
188 execution.setVariable('PUAAIVfMod_vfModuleValidationError', msg)
189 execution.setVariable('PUAAIVfMod_vfModuleOK', false)
191 execution.setVariable('PUAAIVfMod_vfModule', vfModule)
192 execution.setVariable('PUAAIVfMod_vfModuleOK', true)
196 logger.trace('Exited ' + method)
197 } catch (BpmnError e) {
199 } catch (Exception e) {
201 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in validateVfModule(): ' + e.getMessage())
206 * Construct and send a PATCH request to AAI to update the VF Module.
208 * @param execution The flow's execution instance.
210 public void updateVfModule(DelegateExecution execution) {
211 def method = getClass().getSimpleName() + '.updateVfModule(' +
212 'execution=' + execution.getId() +
214 logger.trace('Entered ' + method)
217 def vnfId = execution.getVariable('PUAAIVfMod_vnfId')
218 def vfModuleId = execution.getVariable('PUAAIVfMod_vfModuleId')
219 def orchestrationStatus = execution.getVariable('PUAAIVfMod_orchestrationStatus')
221 org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('PUAAIVfMod_vfModule')
223 vfModule.setVfModuleId(vfModuleId)
224 vfModule.setOrchestrationStatus(orchestrationStatus)
226 AAIResourcesClient client = new AAIResourcesClient()
227 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
228 client.update(uri, vfModule)
229 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 200)
230 // Set the output for this flow. The updated VfModule is an output, the generic VNF name, and for
231 // backward compatibilty, the heat-stack-id is an output
232 execution.setVariable('PUAAIVfMod_outVfModule', vfModule)
233 def vnfName = execution.getVariable('PUAAIVfMod_vnfName')
234 logger.debug('Output PUAAIVfMod_vnfName set to ' + vnfName)
235 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
236 execution.setVariable('WorkflowResponse', vfModule)
238 def heatStackId = vfModule.getHeatStackId()
239 execution.setVariable('PUAAIVfMod_heatStackId', heatStackId)
240 logger.debug('Output PUAAIVfMod_heatStackId set to \'' + heatStackId + '\'')
242 logger.trace('Exited ' + method)
243 } catch (BpmnError e) {
244 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
246 } catch (Exception e) {
248 execution.setVariable('PUAAIVfMod_updateVfModuleResponseCode', 500)
249 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
254 * Generates a WorkflowException if the AAI query returns a response code other than 200.
256 * @param execution The flow's execution instance.
258 public void handleVnfNotFound(DelegateExecution execution) {
259 def method = getClass().getSimpleName() + '.handleVnfNotFound(' +
260 'execution=' + execution.getId() +
262 logger.trace('Entered ' + method)
264 logger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('PUAAIVfMod_getVnfResponseCode'));
265 String processKey = getProcessKey(execution);
266 WorkflowException exception = new WorkflowException(processKey, 5000,
267 execution.getVariable('PUAAIVfMod_getVnfResponse'))
268 execution.setVariable('WorkflowException', exception)
270 logger.trace('Exited ' + method)
274 * Generates a WorkflowException if the VF Module does not pass validation.
276 * @param execution The flow's execution instance.
278 public void handleVfModuleValidationError(DelegateExecution execution) {
279 def method = getClass().getSimpleName() + '.handleVfModuleValidationError(' +
280 'execution=' + execution.getId() +
282 logger.trace('Entered ' + method)
284 def String errorMsg = 'VF Module validation error: ' + execution.getVariable('PUAAIVfMod_vfModuleValidationError')
285 logger.error(errorMsg);
286 logger.debug("PrepareUpdateAAIVfModule: Error Message : " + errorMsg)
288 String processKey = getProcessKey(execution);
289 WorkflowException exception = new WorkflowException(processKey, 5000, errorMsg)
290 execution.setVariable('WorkflowException', exception)
292 logger.trace('Exited ' + method)
296 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
298 * @param execution The flow's execution instance.
300 public void handleUpdateVfModuleFailure(DelegateExecution execution) {
301 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
302 'execution=' + execution.getId() +
304 logger.trace('Entered ' + method)
306 logger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('PUAAIVfMod_updateVfModuleResponseCode'));
307 String processKey = getProcessKey(execution);
308 WorkflowException exception = new WorkflowException(processKey, 5000,
309 execution.getVariable('PUAAIVfMod_updateVfModuleResponse'))
310 execution.setVariable('WorkflowException', exception)
312 logger.trace('Exited ' + method)