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.infrastructure.scripts
 
  25 import org.camunda.bpm.engine.delegate.BpmnError
 
  26 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
 
  28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  29 import org.onap.so.bpmn.common.scripts.MsoUtils
 
  30 import org.onap.so.bpmn.core.WorkflowException
 
  31 import org.onap.so.logger.MessageEnum
 
  32 import org.onap.so.logger.MsoLogger
 
  33 import org.slf4j.Logger
 
  34 import org.slf4j.LoggerFactory
 
  37 public class UpdateVfModule extends AbstractServiceTaskProcessor {
 
  38     private static final Logger logger = LoggerFactory.getLogger( UpdateVfModule.class);
 
  40         ExceptionUtil exceptionUtil = new ExceptionUtil()
 
  43          * Initialize the flow's variables.
 
  45          * @param execution The flow's execution instance.
 
  47         public void initProcessVariables(DelegateExecution execution) {
 
  48                 execution.setVariable('prefix', 'UPDVfMod_')
 
  49                 execution.setVariable('UPDVfMod_Request', null)
 
  50                 execution.setVariable('UPDVfMod_requestInfo', null)
 
  51                 execution.setVariable('UPDVfMod_requestId', null)
 
  52                 execution.setVariable('UPDVfMod_source', null)
 
  53                 execution.setVariable('UPDVfMod_vnfInputs', null)
 
  54                 execution.setVariable('UPDVfMod_vnfId', null)
 
  55                 execution.setVariable('UPDVfMod_vfModuleId', null)
 
  56                 execution.setVariable('UPDVfMod_tenantId', null)
 
  57                 execution.setVariable('UPDVfMod_volumeGroupId', null)
 
  58                 execution.setVariable('UPDVfMod_vnfParams', null)
 
  59                 execution.setVariable('UPDVfMod_updateInfraRequest', null)
 
  60                 execution.setVariable('UpdateVfModuleSuccessIndicator', false)
 
  64          * Check for missing elements in the received request.
 
  66          * @param execution The flow's execution instance.
 
  68         public void preProcessRequest(DelegateExecution execution) {
 
  69                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
 
  70                         'execution=' + execution.getId() +
 
  73                 logger.trace('Entered ' + method)
 
  76                         initProcessVariables(execution)
 
  77                         String request = validateRequest(execution)
 
  79                         logger.debug("UpdateVfModule request: " + request)
 
  80                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
 
  81                         execution.setVariable('UPDVfMod_requestInfo', requestInfo)
 
  82                         execution.setVariable('UPDVfMod_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
 
  83                         execution.setVariable('UPDVfMod_source', getNodeTextForce(requestInfo, 'source'))
 
  85                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
 
  86                         execution.setVariable('UPDVfMod_vnfInputs', vnfInputs)
 
  87                         execution.setVariable('UPDVfMod_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
 
  88                         execution.setVariable('UPDVfMod_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
 
  89                         execution.setVariable('UPDVfMod_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
 
  90                         execution.setVariable('UPDVfMod_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
 
  92                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
 
  93                         execution.setVariable('UPDVfMod_vnfParams', vnfParams)
 
  95                         logger.trace('Exited ' + method)
 
  96                 } catch (BpmnError e) {
 
  98                 } catch (Exception e) {
 
  99                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 100                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 101                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 102                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
 
 107          * Prepare and send the synchronous response for this flow.
 
 109          * @param execution The flow's execution instance.
 
 111         public void sendSynchResponse(DelegateExecution execution) {
 
 112                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
 
 113                         'execution=' + execution.getId() +
 
 116                 logger.trace('Entered ' + method)
 
 119                         def requestInfo = execution.getVariable('UPDVfMod_requestInfo')
 
 120                         def requestId = execution.getVariable('UPDVfMod_requestId')
 
 121                         def source = execution.getVariable('UPDVfMod_source')
 
 122                         def progress = getNodeTextForce(requestInfo, 'progress')
 
 123                         if (progress.isEmpty()) {
 
 126                         def startTime = getNodeTextForce(requestInfo, 'start-time')
 
 127                         if (startTime.isEmpty()) {
 
 128                                 startTime = System.currentTimeMillis()
 
 130                         def vnfInputs = execution.getVariable('UPDVfMod_vnfInputs')
 
 132                         String synchResponse = """
 
 133                                 <vnf-request xmlns="http://org.onap/so/infra/vnf-request/v1">
 
 135                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
 
 136                                                 <action>UPDATE_VF_MODULE</action>
 
 137                                                 <request-status>IN_PROGRESS</request-status>
 
 138                                                 <progress>${MsoUtils.xmlEscape(progress)}</progress>
 
 139                                                 <start-time>${MsoUtils.xmlEscape(startTime)}</start-time>
 
 140                                                 <source>${MsoUtils.xmlEscape(source)}</source>
 
 146                         synchResponse = utils.formatXml(synchResponse)
 
 147                         sendWorkflowResponse(execution, 200, synchResponse)
 
 149                         logger.debug("UpdateVfModule Synch Response: " + synchResponse)
 
 150                         logger.trace('Exited ' + method)
 
 151                 } catch (BpmnError e) {
 
 153                 } catch (Exception e) {
 
 154                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 155                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 156                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 157                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendSynchResponse(): ' + e.getMessage())
 
 162          * Prepare the Request for invoking the DoUpdateVfModule subflow.
 
 164          * NOTE: Currently, the method just logs passing through as the
 
 165          * incoming Request to this main flow is used as the Request to
 
 166          * the DoUpdateVfModule subflow. No preparation processing is
 
 169          * @param execution The flow's execution instance.
 
 171         public void prepDoUpdateVfModule(DelegateExecution execution) {
 
 172                 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
 
 173                         'execution=' + execution.getId() +
 
 176                 logger.trace('Entered ' + method)
 
 180                         logger.trace('Exited ' + method)
 
 181                 } catch (BpmnError e) {
 
 183                 } catch (Exception e) {
 
 184                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 185                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 186                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 187                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
 
 192          * Prepare the Request for updating the DB for this Infra Request.
 
 194          * @param execution The flow's execution instance.
 
 196         public void prepUpdateInfraRequest(DelegateExecution execution) {
 
 197                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
 
 198                         'execution=' + execution.getId() +
 
 201                 logger.trace('Entered ' + method)
 
 204                         def requestId = execution.getVariable('UPDVfMod_requestId')
 
 205                         def vnfId = execution.getVariable('UPDVfMod_vnfId')
 
 206                         def vfModuleId = execution.getVariable('UPDVfMod_vfModuleId')
 
 207                         def tenantId = execution.getVariable('UPDVfMod_tenantId')
 
 208                         def volumeGroupId = execution.getVariable('UPDVfMod_volumeGroupId')
 
 210                         String updateInfraRequest = """
 
 211                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 
 212                                                 xmlns:req="http://org.onap.so/requestsdb">
 
 215                                                 <req:updateInfraRequest>
 
 216                                                         <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
 
 217                                                         <lastModifiedBy>BPEL</lastModifiedBy>
 
 218                                                         <requestStatus>COMPLETE</requestStatus>
 
 219                                                         <progress>100</progress>
 
 221                                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
 
 222                                                                 <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
 
 223                                                                 <tenant-id>${MsoUtils.xmlEscape(tenantId)}</tenant-id>
 
 224                                                                 <volume-group-id>${MsoUtils.xmlEscape(volumeGroupId)}</volume-group-id>
 
 226                                                 </req:updateInfraRequest>
 
 231                         updateInfraRequest = utils.formatXml(updateInfraRequest)
 
 232                         execution.setVariable('UPDVfMod_updateInfraRequest', updateInfraRequest)
 
 233                         logger.debug('Request for Update Infra Request:\n' + updateInfraRequest)
 
 235                         logger.debug("UpdateVfModule Infra Request: " + updateInfraRequest)
 
 236                         logger.trace('Exited ' + method)
 
 237                 } catch (BpmnError e) {
 
 239                 } catch (Exception e) {
 
 240                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 241                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 242                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 243                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
 
 248          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
 
 250          * @param execution the execution
 
 251          * @param resultVar the execution variable in which the result will be stored
 
 253         public void completionHandlerPrep(DelegateExecution execution, String resultVar) {
 
 254                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
 
 255                         'execution=' + execution.getId() +
 
 256                         ', resultVar=' + resultVar +
 
 259                 logger.trace('Entered ' + method)
 
 262                         def requestInfo = getVariable(execution, 'UPDVfMod_requestInfo')
 
 265                                 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
 
 266                                                 xmlns:reqtype="http://org.onap/so/request/types/v1">
 
 268                                         <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
 
 269                                 </sdncadapterworkflow:MsoCompletionRequest>
 
 272                         content = utils.formatXml(content)
 
 273                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
 
 274                         execution.setVariable(resultVar, content)
 
 276                         logger.debug("UpdateVfModule CompletionHandler Request: " + content)
 
 277                         logger.trace('Exited ' + method)
 
 278                 } catch (BpmnError e) {
 
 280                 } catch (Exception e) {
 
 281                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 282                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 283                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 284                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
 
 289          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
 
 291          * @param execution the execution
 
 292          * @param resultVar the execution variable in which the result will be stored
 
 294         public void falloutHandlerPrep(DelegateExecution execution, String resultVar) {
 
 295                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
 
 296                         'execution=' + execution.getId() +
 
 297                         ', resultVar=' + resultVar +
 
 300                 logger.trace('Entered ' + method)
 
 303                         def prefix = execution.getVariable('prefix')
 
 304                         def request = getVariable(execution, prefix+'Request')
 
 305                         def requestInformation = utils.getNodeXml(request, 'request-information', false)
 
 307                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
 
 308                         def errorResponseCode = workflowException.getErrorCode()
 
 309                         def errorResponseMsg = workflowException.getErrorMessage()
 
 310                         def encErrorResponseMsg = ""
 
 311                         if (errorResponseMsg != null) {
 
 312                                 encErrorResponseMsg = errorResponseMsg
 
 316                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
 
 317                                                 xmlns:reqtype="http://org.onap/so/request/types/v1"
 
 318                                                 xmlns:msoservtypes="http://org.onap/so/request/types/v1"
 
 319                                                 xmlns:structuredtypes="http://org.onap/so/structured/types/v1">
 
 320                                         ${requestInformation}
 
 321                                         <sdncadapterworkflow:WorkflowException>
 
 322                                                 <sdncadapterworkflow:ErrorMessage>${MsoUtils.xmlEscape(encErrorResponseMsg)}</sdncadapterworkflow:ErrorMessage>
 
 323                                                 <sdncadapterworkflow:ErrorCode>${MsoUtils.xmlEscape(errorResponseCode)}</sdncadapterworkflow:ErrorCode>
 
 324                                         </sdncadapterworkflow:WorkflowException>
 
 325                                 </sdncadapterworkflow:FalloutHandlerRequest>
 
 327                         content = utils.formatXml(content)
 
 328                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
 
 329                         execution.setVariable(resultVar, content)
 
 331                         logger.debug("UpdateVfModule fallOutHandler Request: " + content)
 
 332                         logger.trace('Exited ' + method)
 
 333                 } catch (BpmnError e) {
 
 335                 } catch (Exception e) {
 
 336                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
 
 337                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
 
 338                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
 
 339                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')