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.slf4j.Logger
 
  31 import org.slf4j.LoggerFactory
 
  33 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
 
  34     private static final Logger logger = LoggerFactory.getLogger( CreateAAIVfModuleVolumeGroup.class);
 
  36         private XmlParser xmlParser = new XmlParser()
 
  37         ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  40          * Initialize the flow's variables.
 
  42          * @param execution The flow's execution instance.
 
  44         public void initProcessVariables(DelegateExecution execution) {
 
  45                 execution.setVariable('prefix', 'CAAIVfModVG_')
 
  46                 execution.setVariable('CAAIVfModVG_vnfId', null)
 
  47                 execution.setVariable('CAAIVfModVG_vfModuleId', null)
 
  48                 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
 
  49                 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
 
  50                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
 
  51                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
 
  52                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
 
  53                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
 
  57          * Check for missing elements in the received request.
 
  59          * @param execution The flow's execution instance.
 
  61         public void preProcessRequest(DelegateExecution execution) {
 
  62                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
 
  63                         'execution=' + execution.getId() +
 
  65                 logger.trace('Entered ' + method)
 
  68                         def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
 
  69                         logger.debug('Received request xml:\n' + xml)
 
  70                         logger.debug("CreateAAIVfModuleVolume Received Request XML: " + xml)
 
  71                         initProcessVariables(execution)
 
  73                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
 
  74                         execution.setVariable('CAAIVfModVG_vnfId', vnfId)
 
  76                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
 
  77                         execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
 
  79                         def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
 
  80                         execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
 
  82                         def cloudOwner = getRequiredNodeText(execution, xml,'cloud-owner')
 
  83                         execution.setVariable('CAAIVfModVG_cloudOwner', cloudOwner)
 
  85                         def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
 
  86                         execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
 
  88                         logger.trace('Exited ' + method)
 
  89                 } catch (BpmnError e) {
 
  91                 } catch (Exception e) {
 
  93                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
 
  99          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
 
 100          * A 200 response is expected with the VF Module in the response body.
 
 102          * @param execution The flow's execution instance.
 
 104         public void getVfModule(DelegateExecution execution) {
 
 105                 def method = getClass().getSimpleName() + '.getVfModule(' +
 
 106                         'execution=' + execution.getId() +
 
 108                 logger.trace('Entered ' + method)
 
 111                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
 
 112                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
 
 114                                 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
 
 115                                 Optional<org.onap.aai.domain.yang.VfModule> vfModule = getAAIClient().get(org.onap.aai.domain.yang.VfModule.class, resourceUri)
 
 116                                 if(vfModule.isPresent()){
 
 117                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 200)
 
 118                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', vfModule.get())
 
 120                                         execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 404)
 
 121                                         execution.setVariable('CAAIVfModVG_getVfModuleResponse', "VF-Module Not found!!")
 
 123                         }catch (Exception ex) {
 
 124                                 logger.debug('Exception occurred while executing AAI GET: {}', ex.getMessage(), ex)
 
 125                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
 
 126                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
 
 128                         logger.trace('Exited ' + method)
 
 129                 } catch (BpmnError e) {
 
 131                 } catch (Exception e) {
 
 133                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
 
 138          * Construct and send a PUT request to AAI to update the VF Module with the
 
 139          * created Volume Group relationship.
 
 141          * @param execution The flow's execution instance.
 
 143         public void updateVfModule(DelegateExecution execution) {
 
 144                 def method = getClass().getSimpleName() + '.updateVfModule(' +
 
 145                         'execution=' + execution.getId() +
 
 147                 logger.trace('Entered ' + method)
 
 150                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
 
 151                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
 
 152                         org.onap.aai.domain.yang.VfModule vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
 
 154                         // Confirm resource-version is in retrieved VF Module
 
 155                         if (vfModule.getResourceVersion() == null) {
 
 156                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
 
 158                                 throw new Exception(msg)
 
 161                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
 
 162                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
 
 163                         def cloudOwner = execution.getVariable('CAAIVfModVG_cloudOwner')
 
 164                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
 
 167                                 AAIResourceUri vfModuleUri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId,vfModuleId);
 
 168                                 AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwner, aicCloudRegion,volumeGroupId);
 
 169                                 logger.debug("Creating relationship between Vf Module: " + vfModuleUri.build().toString() + " and Volume Group: " + volumeGroupUri.build().toString())
 
 170                                 getAAIClient().connect(vfModuleUri,volumeGroupUri)
 
 171                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 200)
 
 172                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', "Success")
 
 173                                 logger.debug("CreateAAIVfModule Response code: " + 200)
 
 174                                 logger.debug("CreateAAIVfModule Response: " + "Success")
 
 175                         } catch (Exception ex) {
 
 176                                 logger.debug('Exception occurred while executing AAI PUT: {}', ex.getMessage(), ex)
 
 177                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
 
 178                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
 
 180                         logger.trace('Exited ' + method)
 
 181                 } catch (BpmnError e) {
 
 183                 } catch (Exception e) {
 
 185                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
 
 190          * Find and return the value of the Volume Group ID for the specified VF Module.  If
 
 191          * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
 
 193          * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
 
 194          * @return the value of the Volume Group ID for the specified VF Module.  If the
 
 195          * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
 
 197         private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
 
 198                 def Node currVolumeGroupRelationshipNode = null
 
 199                 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
 
 200                 for (Node relationshipNode in relationships) {
 
 201                         def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
 
 202                         if ((relatedTo != null) && relatedTo.equals('volume-group')) {
 
 203                                 currVolumeGroupRelationshipNode = relationshipNode
 
 206                 return currVolumeGroupRelationshipNode
 
 210          * Generates a WorkflowException if the AAI query returns a response code other than 200.
 
 212          * @param execution The flow's execution instance.
 
 214         public void handleAAIQueryFailure(DelegateExecution execution) {
 
 215                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
 
 216                         'execution=' + execution.getId() +
 
 218                 logger.trace('Entered ' + method)
 
 219                 logger.error('Error occurred attempting to query AAI, Response Code ' + execution.getVariable('CAAIVfModVG_getVfModuleResponseCode'));
 
 220                 ExceptionUtil exceptionUtil = new ExceptionUtil()
 
 221                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
 
 223                 logger.trace('Exited ' + method)
 
 227          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
 
 229          * @param execution The flow's execution instance.
 
 231         public void handleUpdateVfModuleFailure(DelegateExecution execution) {
 
 232                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
 
 233                         'execution=' + execution.getId() +
 
 235                 logger.trace('Entered ' + method)
 
 237                 logger.error('Error occurred attempting to update VF Module in AAI, Response Code ' + execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode'));
 
 238                 ExceptionUtil exceptionUtil = new ExceptionUtil()
 
 239                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
 
 241                 logger.trace('Exited ' + method)