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 org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aaiclient.client.aai.AAIObjectType
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
35 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
36 private static final Logger logger = LoggerFactory.getLogger( CreateAAIVfModuleVolumeGroup.class);
38 private XmlParser xmlParser = new XmlParser()
39 ExceptionUtil exceptionUtil = new ExceptionUtil()
42 * Initialize the flow's variables.
44 * @param execution The flow's execution instance.
46 public void initProcessVariables(DelegateExecution execution) {
47 execution.setVariable('prefix', 'CAAIVfModVG_')
48 execution.setVariable('CAAIVfModVG_vnfId', null)
49 execution.setVariable('CAAIVfModVG_vfModuleId', null)
50 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
51 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
52 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
53 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
54 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
55 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
59 * Check for missing elements in the received request.
61 * @param execution The flow's execution instance.
63 public void preProcessRequest(DelegateExecution execution) {
64 def method = getClass().getSimpleName() + '.preProcessRequest(' +
65 'execution=' + execution.getId() +
67 logger.trace('Entered ' + method)
70 def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
71 logger.debug('Received request xml:\n' + xml)
72 logger.debug("CreateAAIVfModuleVolume Received Request XML: " + xml)
73 initProcessVariables(execution)
75 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
76 execution.setVariable('CAAIVfModVG_vnfId', vnfId)
78 def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
79 execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
81 def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
82 execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
84 def cloudOwner = getRequiredNodeText(execution, xml,'cloud-owner')
85 execution.setVariable('CAAIVfModVG_cloudOwner', cloudOwner)
87 def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
88 execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
90 logger.trace('Exited ' + method)
91 } catch (BpmnError e) {
93 } catch (Exception e) {
95 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
101 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
102 * A 200 response is expected with the VF Module in the response body.
104 * @param execution The flow's execution instance.
106 public void getVfModule(DelegateExecution execution) {
107 def method = getClass().getSimpleName() + '.getVfModule(' +
108 'execution=' + execution.getId() +
110 logger.trace('Entered ' + method)
113 def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
114 def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
116 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId).vfModule(vfModuleId));
117 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
118 if(vfModule.isPresent()){
119 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 200)
120 execution.setVariable('CAAIVfModVG_getVfModuleResponse', vfModule.get())
122 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 404)
123 execution.setVariable('CAAIVfModVG_getVfModuleResponse', "VF-Module Not found!!")
125 }catch (Exception ex) {
126 logger.debug('Exception occurred while executing AAI GET: {}', ex.getMessage(), ex)
127 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
128 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
130 logger.trace('Exited ' + method)
131 } catch (BpmnError e) {
133 } catch (Exception e) {
135 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
140 * Construct and send a PUT request to AAI to update the VF Module with the
141 * created Volume Group relationship.
143 * @param execution The flow's execution instance.
145 public void updateVfModule(DelegateExecution execution) {
146 def method = getClass().getSimpleName() + '.updateVfModule(' +
147 'execution=' + execution.getId() +
149 logger.trace('Entered ' + method)
152 def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
153 def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
154 org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
156 // Confirm resource-version is in retrieved VF Module
157 if (vfModule.getResourceVersion() == null) {
158 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
160 throw new Exception(msg)
163 // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
164 def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
165 def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
166 def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
169 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId).vfModule(vfModuleId));
170 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(cloudOwner, aicCloudRegion).volumeGroup(volumeGroupId));
171 logger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
172 getAAIClient().connect(vfModuleUri,volumeGroupUri)
173 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
174 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
175 logger.debug("CreateAAIVfModule Response code: " + 200)
176 logger.debug("CreateAAIVfModule Response: " + "Success")
177 } catch (Exception ex) {
178 logger.debug('Exception occurred while executing AAI PUT: {}', ex.getMessage(), ex)
179 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
180 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
182 logger.trace('Exited ' + method)
183 } catch (BpmnError e) {
185 } catch (Exception e) {
187 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
192 * Find and return the value of the Volume Group ID for the specified VF Module. If
193 * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
195 * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
196 * @return the value of the Volume Group ID for the specified VF Module. If the
197 * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
199 private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
200 def Node currVolumeGroupRelationshipNode = null
201 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
202 for (Node relationshipNode in relationships) {
203 def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
204 if ((relatedTo != null) && relatedTo.equals('volume-group')) {
205 currVolumeGroupRelationshipNode = relationshipNode
208 return currVolumeGroupRelationshipNode
212 * Generates a WorkflowException if the AAI query returns a response code other than 200.
214 * @param execution The flow's execution instance.
216 public void handleAAIQueryFailure(DelegateExecution execution) {
217 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
218 'execution=' + execution.getId() +
220 logger.trace('Entered ' + method)
221 logger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('CAAIVfModVG_getVfModuleResponseCode'));
222 ExceptionUtil exceptionUtil = new ExceptionUtil()
223 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
225 logger.trace('Exited ' + method)
229 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
231 * @param execution The flow's execution instance.
233 public void handleUpdateVfModuleFailure(DelegateExecution execution) {
234 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
235 'execution=' + execution.getId() +
237 logger.trace('Entered ' + method)
239 logger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode'));
240 ExceptionUtil exceptionUtil = new ExceptionUtil()
241 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
243 logger.trace('Exited ' + method)