c70c971bc7b87c667948bdffe50dbddc5f43e96c
[so.git] /
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.openecomp.mso.bpmn.infrastructure.scripts;
21
22 import static org.apache.commons.lang3.StringUtils.*;
23 import groovy.xml.XmlUtil
24 import groovy.json.*
25
26 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
27 import org.openecomp.mso.bpmn.core.domain.ServiceInstance
28 import org.openecomp.mso.bpmn.core.domain.CompareModelsResult
29 import org.openecomp.mso.bpmn.core.domain.ModelInfo
30 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
32 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
34 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
35 import org.openecomp.mso.bpmn.common.scripts.VidUtils
36 import org.openecomp.mso.bpmn.core.RollbackData
37 import org.openecomp.mso.bpmn.core.WorkflowException
38 import org.openecomp.mso.rest.APIResponse;
39 import org.openecomp.mso.rest.RESTClient
40 import org.openecomp.mso.rest.RESTConfig
41
42 import java.util.UUID;
43 import javax.xml.parsers.DocumentBuilder
44 import javax.xml.parsers.DocumentBuilderFactory
45
46 import org.camunda.bpm.engine.delegate.BpmnError
47 import org.camunda.bpm.engine.delegate.DelegateExecution
48 import org.json.JSONObject;
49 import org.json.JSONArray;
50 import org.apache.commons.lang3.*
51 import org.apache.commons.codec.binary.Base64;
52 import org.springframework.web.util.UriUtils;
53
54 import org.w3c.dom.Document
55 import org.w3c.dom.Element
56 import org.w3c.dom.Node
57 import org.w3c.dom.NodeList
58 import org.xml.sax.InputSource
59 /**
60  * This groovy class supports the <class>CompareModelofE2EServiceInstance.bpmn</class> process.
61  *
62  * Inputs:
63  * @param - msoRequestId
64  * @param - globalSubscriberId
65  * @param - subscriptionServiceType
66  * @param - serviceInstanceId
67  * @param - modelInvariantIdTarget
68  * @param - modelVersionIdTarget
69
70  *
71  * Outputs:
72  * @param - WorkflowException
73  */
74 public class CompareModelofE2EServiceInstance extends AbstractServiceTaskProcessor {
75
76         String Prefix="CMPMDSI_"
77         private static final String DebugFlag = "isDebugEnabled"
78         
79         ExceptionUtil exceptionUtil = new ExceptionUtil()
80         JsonUtils jsonUtil = new JsonUtils()
81         VidUtils vidUtils = new VidUtils()
82
83         public void preProcessRequest (DelegateExecution execution) {
84                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
85                 execution.setVariable("prefix",Prefix)
86                 String msg = ""
87                 
88                 utils.log("INFO", " *** preProcessRequest Request *** ", isDebugEnabled)
89         
90                 try {
91                         // check for incoming json message/input
92                         String siRequest = execution.getVariable("bpmnRequest")
93                         utils.logAudit(siRequest)
94                         
95         
96                         String requestId = execution.getVariable("mso-request-id")
97                         execution.setVariable("msoRequestId", requestId)
98                         utils.log("INFO", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
99                         
100                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
101                         if (isBlank(serviceInstanceId)) {
102                                 msg = "Input serviceInstanceId' is null"
103                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
104                         }       
105
106                         /*
107                          * Extracting User Parameters from incoming Request and converting into a Map
108                          */
109                         def jsonSlurper = new JsonSlurper()
110                         def jsonOutput = new JsonOutput()
111         
112                         Map reqMap = jsonSlurper.parseText(siRequest)
113         
114                         //InputParams
115                         def userParams = reqMap.requestDetails?.requestParameters?.userParams
116         
117                         Map<String, String> inputMap = [:]
118                         if (userParams) {
119                                 userParams.each {
120                                         userParam -> inputMap.put(userParam.name, userParam.value.toString())
121                                 }
122                         }
123                         execution.setVariable("operationType", "CompareModel") 
124                         
125                         utils.log("DEBUG", "User Input Parameters map: " + userParams.toString(), isDebugEnabled)
126                         execution.setVariable("serviceInputParams", inputMap)
127         
128                 } catch (BpmnError e) {
129                         throw e;
130                 } catch (Exception ex){
131                         msg = "Exception in preProcessRequest " + ex.getMessage()
132                         utils.log("INFO", msg, isDebugEnabled)
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
134                 }
135                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
136         }
137
138         public void sendSyncResponse (DelegateExecution execution) {
139                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
140                 utils.log("INFO", " *** sendSyncResponse  *** ", isDebugEnabled)
141
142                 try {
143                         CompareModelsResult compareModelsResult = execution.getVariable("compareModelsResult")
144                         
145                         // RESTResponse (for API Handler(APIH) Reply Task)
146                         String syncResponse = compareModelsResult.toString()
147                         utils.log("INFO", " sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse, isDebugEnabled)
148                         sendWorkflowResponse(execution, 202, syncResponse)
149
150                 } catch (Exception ex) {
151                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
152                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
153                 }
154                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
155         }
156         
157         public void sendSyncError (DelegateExecution execution) {
158                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
159                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)
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.openecomp/mso/workflow/schema/v1">
172                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
173                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
174                                    </aetgt:WorkflowException>"""
175
176                         utils.logAudit(buildworkflowException)
177                         sendWorkflowResponse(execution, 500, buildworkflowException)
178
179                 } catch (Exception ex) {
180                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
181                 }
182
183         }
184         
185         public void prepareCompletionRequest (DelegateExecution execution) {
186                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
187                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)
188
189                 try {
190                         String requestId = execution.getVariable("msoRequestId")
191                         String source = execution.getVariable("source")
192                         String msoCompletionRequest =
193                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
194                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">
195                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
196                                                         <request-id>${requestId}</request-id>
197                                                         <action>COMPAREMODEL</action>
198                                                         <source>${source}</source>
199                                                 </request-info>
200                                                 <aetgt:status-message>E2E Service Instance Compare model successfully.</aetgt:status-message>
201                                                 <aetgt:mso-bpel-name>CompareModelofE2EServiceInstance</aetgt:mso-bpel-name>
202                                         </aetgt:MsoCompletionRequest>"""
203
204                         // Format Response
205                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
206
207                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
208                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
209
210                 } catch (Exception ex) {
211                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
212                         utils.log("INFO", msg, isDebugEnabled)
213                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
214                 }
215                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
216         }
217         
218         public void prepareFalloutRequest(DelegateExecution execution){
219                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
220                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)
221
222                 try {
223                         WorkflowException wfex = execution.getVariable("WorkflowException")
224                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
225                         String requestId = execution.getVariable("msoRequestId")
226                         String source = execution.getVariable("source")
227                         String requestInfo =
228                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
229                                         <request-id>${requestId}</request-id>
230                                         <action>COMPAREMODEL</action>
231                                         <source>${source}</source>
232                                    </request-info>"""
233
234                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
235                         execution.setVariable("falloutRequest", falloutRequest)
236                 } catch (Exception ex) {
237                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
238                         String errorException = "  Bpmn error encountered in CompareModelofE2EServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
239                         String requestId = execution.getVariable("msoRequestId")
240                         String falloutRequest =
241                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
242                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"
243                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
244                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
245                                               <request-id>${requestId}</request-id>
246                                               <action>COMPAREMODEL</action>
247                                               <source>UUI</source>
248                                            </request-info>
249                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
250                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
251                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
252                                                 </aetgt:WorkflowException>
253                                         </aetgt:FalloutHandlerRequest>"""
254
255                         execution.setVariable("falloutRequest", falloutRequest)
256                 }
257                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
258         }
259
260 }
261