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 org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.client.aai.AAIObjectType
26 import org.onap.so.client.aai.entities.uri.AAIResourceUri
27 import org.onap.so.client.aai.entities.uri.AAIUriFactory
28 import org.onap.so.logger.MsoLogger
30 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
31 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateAAIVfModuleVolumeGroup.class);
33 private XmlParser xmlParser = new XmlParser()
34 ExceptionUtil exceptionUtil = new ExceptionUtil()
37 * Initialize the flow's variables.
39 * @param execution The flow's execution instance.
41 public void initProcessVariables(DelegateExecution execution) {
42 execution.setVariable('prefix', 'CAAIVfModVG_')
43 execution.setVariable('CAAIVfModVG_vnfId', null)
44 execution.setVariable('CAAIVfModVG_vfModuleId', null)
45 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
46 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
47 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
48 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
49 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
50 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
54 * Check for missing elements in the received request.
56 * @param execution The flow's execution instance.
58 public void preProcessRequest(DelegateExecution execution) {
59 def method = getClass().getSimpleName() + '.preProcessRequest(' +
60 'execution=' + execution.getId() +
62 msoLogger.trace('Entered ' + method)
65 def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
66 msoLogger.debug('Received request xml:\n' + xml)
67 msoLogger.debug("CreateAAIVfModuleVolume Received Request XML: " + xml)
68 initProcessVariables(execution)
70 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
71 execution.setVariable('CAAIVfModVG_vnfId', vnfId)
73 def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
74 execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
76 def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
77 execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
79 def cloudOwner = getRequiredNodeText(execution, xml,'cloud-owner')
80 execution.setVariable('CAAIVfModVG_cloudOwner', cloudOwner)
82 def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
83 execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
85 msoLogger.trace('Exited ' + method)
86 } catch (BpmnError e) {
88 } catch (Exception e) {
90 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
96 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
97 * A 200 response is expected with the VF Module in the response body.
99 * @param execution The flow's execution instance.
101 public void getVfModule(DelegateExecution execution) {
102 def method = getClass().getSimpleName() + '.getVfModule(' +
103 'execution=' + execution.getId() +
105 msoLogger.trace('Entered ' + method)
108 def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
109 def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
111 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
112 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
113 if(vfModule.isPresent()){
114 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 200)
115 execution.setVariable('CAAIVfModVG_getVfModuleResponse', vfModule.get())
117 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 404)
118 execution.setVariable('CAAIVfModVG_getVfModuleResponse', "VF-Module Not found!!")
120 }catch (Exception ex) {
122 msoLogger.debug('Exception occurred while executing AAI GET:' + ex.getMessage())
123 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
124 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
126 msoLogger.trace('Exited ' + method)
127 } catch (BpmnError e) {
129 } catch (Exception e) {
131 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
136 * Construct and send a PUT request to AAI to update the VF Module with the
137 * created Volume Group relationship.
139 * @param execution The flow's execution instance.
141 public void updateVfModule(DelegateExecution execution) {
142 def method = getClass().getSimpleName() + '.updateVfModule(' +
143 'execution=' + execution.getId() +
145 msoLogger.trace('Entered ' + method)
148 def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
149 def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
150 org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
152 // Confirm resource-version is in retrieved VF Module
153 if (vfModule.getResourceVersion() == null) {
154 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
155 msoLogger.error( msg);
156 throw new Exception(msg)
159 // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
160 def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
161 def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
162 def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
165 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId,vfModuleId);
166 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwner, aicCloudRegion,volumeGroupId);
167 msoLogger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
168 getAAIClient().connect(vfModuleUri,volumeGroupUri)
169 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
170 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
171 msoLogger.debug("CreateAAIVfModule Response code: " + 200)
172 msoLogger.debug("CreateAAIVfModule Response: " + "Success")
173 } catch (Exception ex) {
175 msoLogger.debug('Exception occurred while executing AAI PUT:' + ex.getMessage())
176 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
177 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
179 msoLogger.trace('Exited ' + method)
180 } catch (BpmnError e) {
182 } catch (Exception e) {
184 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
189 * Find and return the value of the Volume Group ID for the specified VF Module. If
190 * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
192 * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
193 * @return the value of the Volume Group ID for the specified VF Module. If the
194 * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
196 private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
197 def Node currVolumeGroupRelationshipNode = null
198 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
199 for (Node relationshipNode in relationships) {
200 def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
201 if ((relatedTo != null) && relatedTo.equals('volume-group')) {
202 currVolumeGroupRelationshipNode = relationshipNode
205 return currVolumeGroupRelationshipNode
209 * Generates a WorkflowException if the AAI query returns a response code other than 200.
211 * @param execution The flow's execution instance.
213 public void handleAAIQueryFailure(DelegateExecution execution) {
214 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
215 'execution=' + execution.getId() +
217 msoLogger.trace('Entered ' + method)
218 msoLogger.error( 'Error occurred attempting to query AAI, Response Code ' + execution.getVariable('CAAIVfModVG_getVfModuleResponseCode'));
219 ExceptionUtil exceptionUtil = new ExceptionUtil()
220 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
222 msoLogger.trace('Exited ' + method)
226 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
228 * @param execution The flow's execution instance.
230 public void handleUpdateVfModuleFailure(DelegateExecution execution) {
231 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
232 'execution=' + execution.getId() +
234 msoLogger.trace('Entered ' + method)
236 msoLogger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode'));
237 ExceptionUtil exceptionUtil = new ExceptionUtil()
238 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
240 msoLogger.trace('Exited ' + method)