8e7a4f727dddc3aa18a689f8bdc97c4327baf18c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.slf4j.Logger
25 import org.slf4j.LoggerFactory
26 import org.onap.so.bpmn.common.scripts.ExceptionUtil
27 import org.onap.so.bpmn.common.scripts.MsoUtils
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.workflow.context.WorkflowContext
30 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder
31 import org.onap.so.bpmn.core.WorkflowException
32 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*
33
34 class PNFSoftwareUpgrade extends AbstractServiceTaskProcessor {
35     private static final Logger logger = LoggerFactory.getLogger(PNFSoftwareUpgrade.class)
36
37     ExceptionUtil exceptionUtil = new ExceptionUtil()
38     String prefix = "PnfSwUpgrade_"
39
40     @Override
41     void preProcessRequest(DelegateExecution execution) {
42     }
43
44     void sendResponse(DelegateExecution execution) {
45         def requestId = execution.getVariable(REQUEST_ID)
46         def instanceId = execution.getVariable(PNF_CORRELATION_ID)
47         logger.debug("Send response for requestId: {}, instanceId: {}", requestId, instanceId)
48
49         String response = """{"requestReferences":{"requestId":"${requestId}", "instanceId":"${instanceId}"}}""".trim()
50         sendWorkflowResponse(execution, 200, response)
51     }
52
53     static WorkflowContext getWorkflowContext(DelegateExecution execution) {
54         String requestId = execution.getVariable(REQUEST_ID)
55         return WorkflowContextHolder.getInstance().getWorkflowContext(requestId)
56     }
57
58     void prepareCompletion(DelegateExecution execution) {
59         try {
60             String requestId = execution.getVariable(REQUEST_ID)
61             logger.debug("Prepare Completion of PNF Software Upgrade for requestId: {}", requestId)
62
63             String msoCompletionRequest =
64                     """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
65                                 xmlns:ns="http://org.onap/so/request/types/v1">
66                         <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
67                             <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
68                             <action>UPDATE</action>
69                             <source>VID</source>
70                         </request-info>
71                         <aetgt:status-message>PNF has been upgraded successfully.</aetgt:status-message>
72                         <aetgt:mso-bpel-name>PNF_SOFTWARE_UPGRADE</aetgt:mso-bpel-name>
73                     </aetgt:MsoCompletionRequest>"""
74             String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
75
76             execution.setVariable(prefix + "CompleteMsoProcessRequest", xmlMsoCompletionRequest)
77
78             logger.debug("CompleteMsoProcessRequest of PNF Software Upgrade - " + "\n" + xmlMsoCompletionRequest)
79         } catch (Exception e) {
80             String msg = "Prepare Completion error for PNF software upgrade - " + e.getMessage()
81             logger.error(msg)
82             exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
83         }
84     }
85
86     void prepareFalloutHandler(DelegateExecution execution) {
87         WorkflowContext workflowContext = getWorkflowContext(execution)
88         if (workflowContext == null) {
89             logger.debug("Error occurred before sending response to API handler, and send it now")
90             sendResponse(execution)
91         }
92
93         try {
94             String requestId = execution.getVariable(REQUEST_ID)
95             logger.debug("Prepare FalloutHandler of PNF Software Upgrade for requestId: {}", requestId)
96
97             WorkflowException workflowException = execution.getVariable("WorkflowException")
98             String errorCode = String.valueOf(workflowException.getErrorCode())
99             String errorMessage = workflowException.getErrorMessage()
100             String falloutHandlerRequest =
101                     """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
102                                 xmlns:ns="http://org.onap/so/request/types/v1">
103                         <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
104                             <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
105                             <action>UPDATE</action>
106                             <source>VID</source>
107                         </request-info>
108                         <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
109                             <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
110                             <aetgt:ErrorCode>${MsoUtils.xmlEscape(errorCode)}</aetgt:ErrorCode>
111                         </aetgt:WorkflowException>
112                     </aetgt:FalloutHandlerRequest>"""
113             String xmlFalloutHandlerRequest = utils.formatXml(falloutHandlerRequest)
114
115             execution.setVariable(prefix + "FalloutHandlerRequest", xmlFalloutHandlerRequest)
116
117             logger.debug("FalloutHandlerRequest of PNF Software Upgrade - " + "\n" + xmlFalloutHandlerRequest)
118         } catch (Exception e) {
119             String msg = "Prepare FalloutHandler error for PNF software upgrade - " + e.getMessage()
120             logger.error(msg)
121             exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
122         }
123     }
124 }