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