Merge "Add APPC-related Activities for WFD"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / CompareModelofE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.scripts;
23
24 import static org.apache.commons.lang3.StringUtils.*;
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.common.scripts.VidUtils
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.domain.CompareModelsResult
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38
39 import groovy.json.*
40
41
42 /**
43  * This groovy class supports the <class>CompareModelofE2EServiceInstance.bpmn</class> process.
44  *
45  * Inputs:
46  * @param - msoRequestId
47  * @param - globalSubscriberId
48  * @param - subscriptionServiceType
49  * @param - serviceInstanceId
50  * @param - modelInvariantIdTarget
51  * @param - modelVersionIdTarget
52
53  *
54  * Outputs:
55  * @param - WorkflowException
56  */
57 public class CompareModelofE2EServiceInstance extends AbstractServiceTaskProcessor {
58     private static final Logger logger = LoggerFactory.getLogger( CompareModelofE2EServiceInstance.class);
59
60         String Prefix="CMPMDSI_"
61         private static final String DebugFlag = "isDebugEnabled"
62         
63         ExceptionUtil exceptionUtil = new ExceptionUtil()
64         JsonUtils jsonUtil = new JsonUtils()
65         VidUtils vidUtils = new VidUtils()
66
67         public void preProcessRequest (DelegateExecution execution) {
68                 execution.setVariable("prefix",Prefix)
69                 String msg = ""
70                 
71                 logger.trace("preProcessRequest Request ")
72         
73                 try {
74                         // check for incoming json message/input
75                         String siRequest = execution.getVariable("bpmnRequest")
76                         logger.debug(siRequest)
77                         
78         
79                         String requestId = execution.getVariable("mso-request-id")
80                         execution.setVariable("msoRequestId", requestId)
81                         logger.info("Input Request:" + siRequest + " reqId:" + requestId)
82                         
83                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
84                         if (isBlank(serviceInstanceId)) {
85                                 msg = "Input serviceInstanceId' is null"
86                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
87                         }       
88
89                         //subscriberInfo
90                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")
91                         if (isBlank(globalSubscriberId)) {
92                                 msg = "Input globalSubscriberId' is null"
93                                 logger.info( msg)
94                         } else {
95                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
96                         }
97         
98                         //subscriptionServiceType
99                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")
100                         if (isBlank(subscriptionServiceType)) {
101                                 msg = "Input subscriptionServiceType is null"
102                                 logger.debug( msg)
103                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
104                         } else {
105                                 execution.setVariable("serviceType", subscriptionServiceType)
106                         }
107         
108                         //modelInvariantIdTarget
109                         String modelInvariantIdTarget = jsonUtil.getJsonValue(siRequest, "modelInvariantIdTarget")
110                         if (isBlank(modelInvariantIdTarget)) {
111                                 msg = "Input modelInvariantIdTarget' is null"
112                                 logger.info( msg)
113                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
114                         } else {
115                                 execution.setVariable("modelInvariantIdTarget", modelInvariantIdTarget)
116                         }
117         
118                         //modelVersionIdTarget
119                         String modelVersionIdTarget = jsonUtil.getJsonValue(siRequest, "modelVersionIdTarget")
120                         if (isBlank(modelVersionIdTarget)) {
121                                 msg = "Input modelVersionIdTarget is null"
122                                 logger.debug( msg)
123                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
124                         } else {
125                                 execution.setVariable("modelVersionIdTarget", modelVersionIdTarget)
126                                 }
127
128                         execution.setVariable("operationType", "CompareModel") 
129         
130                 } catch (BpmnError e) {
131                         throw e;
132                 } catch (Exception ex){
133                         msg = "Exception in preProcessRequest " + ex.getMessage()
134                         logger.info(msg)
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
136                 }
137                 logger.trace("Exit preProcessRequest ")
138         }
139
140         public void sendSyncResponse (DelegateExecution execution) {
141                 logger.trace("sendSyncResponse  ")
142
143                 try {
144                         CompareModelsResult compareModelsResult = execution.getVariable("compareModelsResult")
145                         
146                         // RESTResponse (for API Handler(APIH) Reply Task)
147                         String syncResponse = compareModelsResult.toJsonStringNoRootName()
148                         logger.info(" sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
149                         sendWorkflowResponse(execution, 202, syncResponse)
150
151                 } catch (Exception ex) {
152                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
154                 }
155                 logger.trace("Exit sendSyncResopnse ")
156         }
157         
158         public void sendSyncError (DelegateExecution execution) {
159                 logger.trace("sendSyncError ")
160
161                 try {
162                         String errorMessage = ""
163                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
164                                 WorkflowException wfe = execution.getVariable("WorkflowException")
165                                 errorMessage = wfe.getErrorMessage()
166                         } else {
167                                 errorMessage = "Sending Sync Error."
168                         }
169
170                         String buildworkflowException =
171                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
172                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
173                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
174                                    </aetgt:WorkflowException>"""
175
176                         logger.debug(buildworkflowException)
177                         sendWorkflowResponse(execution, 500, buildworkflowException)
178
179                 } catch (Exception ex) {
180                         logger.info(" Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
181                 }
182
183         }
184         
185         public void prepareCompletionRequest (DelegateExecution execution) {
186                 logger.trace("prepareCompletion ")
187
188                 try {
189                         String requestId = execution.getVariable("msoRequestId")
190                         String source = execution.getVariable("source")
191                         String msoCompletionRequest =
192                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
193                                                                 xmlns:ns="http://org.onap/so/request/types/v1">
194                                                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
195                                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
196                                                         <action>COMPAREMODEL</action>
197                                                         <source>${MsoUtils.xmlEscape(source)}</source>
198                                                 </request-info>
199                                                 <aetgt:status-message>E2E Service Instance Compare model successfully.</aetgt:status-message>
200                                                 <aetgt:mso-bpel-name>CompareModelofE2EServiceInstance</aetgt:mso-bpel-name>
201                                         </aetgt:MsoCompletionRequest>"""
202
203                         // Format Response
204                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
205
206                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
207                         logger.info(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
208
209                 } catch (Exception ex) {
210                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
211                         logger.info(msg)
212                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
213                 }
214                 logger.trace("Exit prepareCompletionRequest ")
215         }
216         
217         public void prepareFalloutRequest(DelegateExecution execution){
218                 logger.trace("prepareFalloutRequest ")
219
220                 try {
221                         WorkflowException wfex = execution.getVariable("WorkflowException")
222                         logger.info(" Input Workflow Exception: " + wfex.toString())
223                         String requestId = execution.getVariable("msoRequestId")
224                         String source = execution.getVariable("source")
225                         String requestInfo =
226                         """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
227                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
228                                         <action>COMPAREMODEL</action>
229                                         <source>${MsoUtils.xmlEscape(source)}</source>
230                                    </request-info>"""
231
232                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
233                         execution.setVariable("falloutRequest", falloutRequest)
234                 } catch (Exception ex) {
235                         logger.info("Exception prepareFalloutRequest:" + ex.getMessage())
236                         String errorException = "  Bpmn error encountered in CompareModelofE2EServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
237                         String requestId = execution.getVariable("msoRequestId")
238                         String falloutRequest =
239                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
240                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
241                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
242                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
243                                               <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
244                                               <action>COMPAREMODEL</action>
245                                               <source>UUI</source>
246                                            </request-info>
247                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
248                                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorException)}</aetgt:ErrorMessage>
249                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
250                                                 </aetgt:WorkflowException>
251                                         </aetgt:FalloutHandlerRequest>"""
252
253                         execution.setVariable("falloutRequest", falloutRequest)
254                 }
255                 logger.trace("Exit prepareFalloutRequest ")
256         }
257
258 }
259