2  * ============LICENSE_START=======================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ============LICENSE_END=========================================================
\r 
  21 package org.openecomp.mso.bpmn.common.scripts
\r 
  23 import org.camunda.bpm.engine.delegate.BpmnError
\r 
  24 import org.camunda.bpm.engine.runtime.Execution
\r 
  25 import org.openecomp.mso.rest.APIResponse
\r 
  28 public class CreateAAIVfModuleVolumeGroup extends AbstractServiceTaskProcessor {
\r 
  30         private XmlParser xmlParser = new XmlParser()
\r 
  31         ExceptionUtil exceptionUtil = new ExceptionUtil()
\r 
  34          * Initialize the flow's variables.
\r 
  36          * @param execution The flow's execution instance.
\r 
  38         public void initProcessVariables(Execution execution) {
\r 
  39                 execution.setVariable('prefix', 'CAAIVfModVG_')
\r 
  40                 execution.setVariable('CAAIVfModVG_vnfId', null)
\r 
  41                 execution.setVariable('CAAIVfModVG_vfModuleId', null)
\r 
  42                 execution.setVariable('CAAIVfModVG_aicCloudRegion', null)
\r 
  43                 execution.setVariable('CAAIVfModVG_volumeGroupId', null)
\r 
  44                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode' ,null)
\r 
  45                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', '')
\r 
  46                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', null)
\r 
  47                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', '')
\r 
  51          * Check for missing elements in the received request.
\r 
  53          * @param execution The flow's execution instance.
\r 
  55         public void preProcessRequest(Execution execution) {
\r 
  56                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
\r 
  57                         'execution=' + execution.getId() +
\r 
  59                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r 
  60                 logDebug('Entered ' + method, isDebugLogEnabled)
\r 
  63                         def xml = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
\r 
  64                         logDebug('Received request xml:\n' + xml, isDebugLogEnabled)
\r 
  65                         utils.logAudit("CreateAAIVfModuleVolume Received Request XML: " + xml)
\r 
  66                         initProcessVariables(execution)
\r 
  68                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
\r 
  69                         execution.setVariable('CAAIVfModVG_vnfId', vnfId)
\r 
  71                         def vfModuleId = getRequiredNodeText(execution, xml,'vf-module-id')
\r 
  72                         execution.setVariable('CAAIVfModVG_vfModuleId', vfModuleId)
\r 
  74                         def aicCloudRegion = getRequiredNodeText(execution, xml,'aic-cloud-region')
\r 
  75                         execution.setVariable('CAAIVfModVG_aicCloudRegion', aicCloudRegion)
\r 
  77                         def volumeGroupId = getRequiredNodeText(execution, xml,'volume-group-id')
\r 
  78                         execution.setVariable('CAAIVfModVG_volumeGroupId', volumeGroupId)
\r 
  80                         logDebug('Exited ' + method, isDebugLogEnabled)
\r 
  81                 } catch (BpmnError e) {
\r 
  83                 } catch (Exception e) {
\r 
  84                         logError('Caught exception in ' + method, e)
\r 
  85                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
\r 
  91          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VF Module.
\r 
  92          * A 200 response is expected with the VF Module in the response body.
\r 
  94          * @param execution The flow's execution instance.
\r 
  96         public void getVfModule(Execution execution) {
\r 
  97                 def method = getClass().getSimpleName() + '.getVfModule(' +
\r 
  98                         'execution=' + execution.getId() +
\r 
 100                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r 
 101                 logDebug('Entered ' + method, isDebugLogEnabled)
\r 
 104                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
\r 
 105                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
\r 
 107                         // Construct endpoint
\r 
 108                         AaiUtil aaiUtil = new AaiUtil(this)
\r 
 109                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
\r 
 110                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
\r 
 111                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")
\r 
 114                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
\r 
 115                                 utils.logAudit("aaiResponse GET TO AAI Endpoint: " + endPoint)
\r 
 116                                 APIResponse response = aaiUtil.executeAAIGetCall(execution, endPoint)
\r 
 117                                 def responseData = response.getResponseBodyAsString()
\r 
 118                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', response.getStatusCode())
\r 
 119                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', responseData)
\r 
 121                                 utils.logAudit("CreateAAIVfModule Response Code: " + response.getStatusCode())
\r 
 122                                 utils.logAudit("CreateAAIVfModule Response: " + response)
\r 
 123                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
\r 
 124                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
\r 
 125                         } catch (Exception ex) {
\r 
 126                                 ex.printStackTrace()
\r 
 127                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
\r 
 128                                 execution.setVariable('CAAIVfModVG_getVfModuleResponseCode', 500)
\r 
 129                                 execution.setVariable('CAAIVfModVG_getVfModuleResponse', 'AAI GET Failed:' + ex.getMessage())
\r 
 131                         logDebug('Exited ' + method, isDebugLogEnabled)
\r 
 132                 } catch (BpmnError e) {
\r 
 134                 } catch (Exception e) {
\r 
 135                         logError('Caught exception in ' + method, e)
\r 
 136                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getVfModule(): ' + e.getMessage())
\r 
 141          * Construct and send a PUT request to AAI to update the VF Module with the
\r 
 142          * created Volume Group relationship.
\r 
 144          * @param execution The flow's execution instance.
\r 
 146         public void updateVfModule(Execution execution) {
\r 
 147                 def method = getClass().getSimpleName() + '.updateVfModule(' +
\r 
 148                         'execution=' + execution.getId() +
\r 
 150                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r 
 151                 logDebug('Entered ' + method, isDebugLogEnabled)
\r 
 154                         def vnfId = execution.getVariable('CAAIVfModVG_vnfId')
\r 
 155                         def vfModuleId = execution.getVariable('CAAIVfModVG_vfModuleId')
\r 
 156                         def vfModule = execution.getVariable('CAAIVfModVG_getVfModuleResponse')
\r 
 157                         def origRequest = execution.getVariable('CreateAAIVfModuleVolumeGroupRequest')
\r 
 158                         def Node vfModuleNode = xmlParser.parseText(vfModule)
\r 
 160                         // Confirm resource-version is in retrieved VF Module
\r 
 161                         if (utils.getChildNode(vfModuleNode, 'resource-version') == null) {
\r 
 162                                 def msg = 'Can\'t update VF Module ' + vfModuleId + ' since \'resource-version\' is missing'
\r 
 164                                 throw new Exception(msg)
\r 
 167                         // Construct payload by creating a Volume Group relationhip and inserting it into the VF Module
\r 
 168                         def aicCloudRegion = execution.getVariable('CAAIVfModVG_aicCloudRegion')
\r 
 169                         def volumeGroupId = execution.getVariable('CAAIVfModVG_volumeGroupId')
\r 
 170                         def Node vgRelationshipNode = createVolumeGroupRelationshipNode(aicCloudRegion, volumeGroupId)
\r 
 171                         insertVolumeGroupRelationshipNode(vfModuleNode, vgRelationshipNode)
\r 
 172                         def payload = utils.nodeToString(vfModuleNode)
\r 
 174                         // Construct endpoint
\r 
 175                         AaiUtil aaiUtil = new AaiUtil(this)
\r 
 176                         def aai_uri = aaiUtil.getNetworkGenericVnfUri(execution)
\r 
 177                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
\r 
 178                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + URLEncoder.encode(vnfId, "UTF-8") + '/vf-modules/vf-module/' + URLEncoder.encode(vfModuleId, "UTF-8")
\r 
 181                                 utils.logAudit("CreateAAIVfModuleVolume Sendind PUT to AAI Endpoint \n " + endPoint + " with payload \n " + payload)
\r 
 182                                 logDebug('sending PUT to AAI endpoint \'' + endPoint + '\'' + 'with payload \n' + payload, isDebugLogEnabled)
\r 
 183                                 APIResponse response = aaiUtil.executeAAIPutCall(execution, endPoint, payload)
\r 
 184                                 def responseData = response.getResponseBodyAsString()
\r 
 185                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', response.getStatusCode())
\r 
 186                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', responseData)
\r 
 188                                 utils.logAudit("CreateAAIVfModule Response code: " + response.getStatusCode())
\r 
 189                                 utils.logAudit("CreateAAIVfModule Response: " + responseData)
\r 
 190                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
\r 
 191                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
\r 
 192                         } catch (Exception ex) {
\r 
 193                                 ex.printStackTrace()
\r 
 194                                 logDebug('Exception occurred while executing AAI PUT:' + ex.getMessage(),isDebugLogEnabled)
\r 
 195                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponseCode', 500)
\r 
 196                                 execution.setVariable('CAAIVfModVG_updateVfModuleResponse', 'AAI PUT Failed:' + ex.getMessage())
\r 
 198                         logDebug('Exited ' + method, isDebugLogEnabled)
\r 
 199                 } catch (BpmnError e) {
\r 
 201                 } catch (Exception e) {
\r 
 202                         logError('Caught exception in ' + method, e)
\r 
 203                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateVfModule(): ' + e.getMessage())
\r 
 208          * Construct a Volume Group relationship Node with the given AIC Cloud Region and
\r 
 209          * Volume Group ID for insertion into a VF Module.
\r 
 211          * @param aicCloudRegion Cloud Region ID to use in the Volume Group relationship
\r 
 212          * @param volumeGroupId Volume Group ID to use in the Volume Group relationship
\r 
 213          * @return a Node representing the new Volume Group relationship
\r 
 215         private Node createVolumeGroupRelationshipNode(String aicCloudRegion, String volumeGroupId) {
\r 
 217                 def Node relatedTo = new Node(null, 'related-to', 'volume-group')
\r 
 219                 def Node relationshipKeyCO = new Node(null, 'relationship-key', 'cloud-region.cloud-owner')
\r 
 220                 def Node relationshipValueCO = new Node(null, 'relationship-value', 'att-aic')
\r 
 221                 def Node relationshipDataCO = new Node(null, 'relationship-data')
\r 
 222                 relationshipDataCO.append(relationshipKeyCO)
\r 
 223                 relationshipDataCO.append(relationshipValueCO)
\r 
 225                 def Node relationshipKeyCRI = new Node(null, 'relationship-key', 'cloud-region.cloud-region-id')
\r 
 226                 def Node relationshipValueCRI = new Node(null, 'relationship-value', aicCloudRegion)
\r 
 227                 def Node relationshipDataCRI = new Node(null, 'relationship-data')
\r 
 228                 relationshipDataCRI.append(relationshipKeyCRI)
\r 
 229                 relationshipDataCRI.append(relationshipValueCRI)
\r 
 231                 def Node relationshipKeyVGI = new Node(null, 'relationship-key', 'volume-group.volume-group-id')
\r 
 232                 def Node relationshipValueVGI = new Node(null, 'relationship-value', volumeGroupId)
\r 
 233                 def Node relationshipDataVGI = new Node(null, 'relationship-data')
\r 
 234                 relationshipDataVGI.append(relationshipKeyVGI)
\r 
 235                 relationshipDataVGI.append(relationshipValueVGI)
\r 
 237                 def Node volumeGroupRelationship = new Node(null, 'relationship')
\r 
 238                 volumeGroupRelationship.append(relatedTo)
\r 
 239                 volumeGroupRelationship.append(relationshipDataCO)
\r 
 240                 volumeGroupRelationship.append(relationshipDataCRI)
\r 
 241                 volumeGroupRelationship.append(relationshipDataVGI)
\r 
 243                 return volumeGroupRelationship;
\r 
 247          * Insert the given Volume Group relationship Node into the given VF Module.
\r 
 248          * If the VF Module does NOT contain a relationship list:
\r 
 249          *      - Create a relationship list containing the Volume Group relationship and insert it into the VF Module
\r 
 250          * If the VF Module contains a relationship list but not a Volume Group relationship:
\r 
 251          *      - Insert the the Volume Group relationship into the relationship lsit
\r 
 252          * If the VF Module contains a relationship list and has a Volume Group relationship:
\r 
 253          *      - Replace the existing Volume Group relationship with the new one
\r 
 254          * @param vfModuleNode
\r 
 255          * @param volumeGroupRelationshipNode
\r 
 257         private void insertVolumeGroupRelationshipNode(Node vfModuleNode, Node volumeGroupRelationshipNode) {
\r 
 258                 def Node relationshipList = utils.getChildNode(vfModuleNode, 'relationship-list')
\r 
 259                 if (relationshipList == null) {
\r 
 260                         relationshipList = new Node(null, 'relationship-list')
\r 
 261                         relationshipList.append(volumeGroupRelationshipNode)
\r 
 262                         vfModuleNode.append(relationshipList)
\r 
 264                         def Node currVolumeGroupRelationshipNode = getCurrVolumeGroupRelationshipNode(relationshipList)
\r 
 265                         if (currVolumeGroupRelationshipNode == null) {
\r 
 266                                 relationshipList.append(volumeGroupRelationshipNode)
\r 
 268                                 currVolumeGroupRelationshipNode.replaceNode(volumeGroupRelationshipNode)
\r 
 274          * Find and return the value of the Volume Group ID for the specified VF Module.  If
\r 
 275          * the value of the Volume Group ID cannot be found for any reason, 'null' is returned.
\r 
 277          * @param vfModuleNode VF Module (as a Node) retrieved from AAI.
\r 
 278          * @return the value of the Volume Group ID for the specified VF Module.  If the
\r 
 279          * value of the Volume Group ID cannot be found for any reason, 'null' is returned.
\r 
 281         private Node getCurrVolumeGroupRelationshipNode(Node relationshipList) {
\r 
 282                 def Node currVolumeGroupRelationshipNode = null
\r 
 283                 def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
\r 
 284                 for (Node relationshipNode in relationships) {
\r 
 285                         def String relatedTo = utils.getChildNodeText(relationshipNode, 'related-to')
\r 
 286                         if ((relatedTo != null) && relatedTo.equals('volume-group')) {
\r 
 287                                 currVolumeGroupRelationshipNode = relationshipNode
\r 
 290                 return currVolumeGroupRelationshipNode
\r 
 294          * Generates a WorkflowException if the AAI query returns a response code other than 200.
\r 
 296          * @param execution The flow's execution instance.
\r 
 298         public void handleAAIQueryFailure(Execution execution) {
\r 
 299                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
\r 
 300                         'execution=' + execution.getId() +
\r 
 302                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r 
 303                 logDebug('Entered ' + method, isDebugLogEnabled)
\r 
 305                 logError('Error occurred attempting to query AAI, Response Code ' +
\r 
 306                         execution.getVariable('CAAIVfModVG_getVfModuleResponseCode') + ', Error Response ' +
\r 
 307                         execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
\r 
 308                 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r 
 309                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_getVfModuleResponse'))
\r 
 311                 logDebug('Exited ' + method, isDebugLogEnabled)
\r 
 315          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
\r 
 317          * @param execution The flow's execution instance.
\r 
 319         public void handleUpdateVfModuleFailure(Execution execution) {
\r 
 320                 def method = getClass().getSimpleName() + '.handleUpdateVfModuleFailure(' +
\r 
 321                         'execution=' + execution.getId() +
\r 
 323                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r 
 324                 logDebug('Entered ' + method, isDebugLogEnabled)
\r 
 326                 logError('Error occurred attempting to update VF Module in AAI, Response Code ' +
\r 
 327                         execution.getVariable('CAAIVfModVG_updateVfModuleResponseCode') + ', Error Response ' +
\r 
 328                         execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
\r 
 329                 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r 
 330                 exceptionUtil.buildWorkflowException(execution, 5000, execution.getVariable('CAAIVfModVG_updateVfModuleResponse'))
\r 
 332                 logDebug('Exited ' + method, isDebugLogEnabled)
\r