c91f353293af1ea2cc6c3e3c1840b429eab2ac68
[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
21 package org.openecomp.mso.bpmn.infrastructure.scripts;
22
23 import static org.apache.commons.lang3.StringUtils.*;
24 import groovy.xml.XmlUtil
25 import groovy.json.*
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
28
29 import org.openecomp.mso.bpmn.core.WorkflowException
30 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import org.openecomp.mso.rest.APIResponse
32
33 import java.util.List;
34 import java.util.UUID;
35
36 import org.camunda.bpm.engine.delegate.BpmnError
37 import org.camunda.bpm.engine.runtime.Execution
38 import org.apache.commons.lang3.*
39 import org.apache.commons.codec.binary.Base64;
40 import org.springframework.web.util.UriUtils
41
42 /**
43  * This groovy class supports the <class>UpdateCustomE2EServiceInstance.bpmn</class> process.
44  * AlaCarte flow for 1702 ServiceInstance Update
45  *
46  */
47 public class UpdateCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
48         String Prefix="UPDSI_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51
52
53         public void preProcessRequest (Execution execution) {
54                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
55                 execution.setVariable("prefix",Prefix)
56                 String msg = ""
57                 utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)
58
59                 try {
60
61                         String siRequest = execution.getVariable("bpmnRequest")
62                         utils.logAudit(siRequest)
63
64                         String requestId = execution.getVariable("mso-request-id")
65                         execution.setVariable("msoRequestId", requestId)
66                         utils.log("INFO", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
67
68                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
69                         if (isBlank(serviceInstanceId)) {
70                                 msg = "Input serviceInstanceId' is null"
71                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
72                         }
73                                                 
74                         //subscriberInfo
75                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
76                         if (isBlank(globalSubscriberId)) {
77                                 msg = "Input globalSubscriberId' is null"
78                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
79                         } else {
80                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
81                         }
82
83                         //requestInfo
84                         execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))
85                         execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))
86                         execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))
87                         String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
88                         if (isBlank(productFamilyId))
89                         {
90                                 msg = "Input productFamilyId is null"
91                                 utils.log("INFO", msg, isDebugEnabled)
92                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
93                         } else {
94                                 execution.setVariable("productFamilyId", productFamilyId)
95                         }
96                  String userParams = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams")      
97              utils.log("INFO", "userParams:" + userParams, isDebugEnabled)
98                  List<String> paramList = jsonUtil.StringArrayToList(execution, userParams)
99                  String uuiRequest = jsonUtil.getJsonValue(paramList.get(0), "UUIRequest")
100                         //modelInfo
101                         if (isBlank(uuiRequest)) {
102                                 msg = "Input uuiRequest is null"
103                                 utils.log("INFO", msg, isDebugEnabled)
104                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
105                         } else
106                         {
107                                 execution.setVariable("uuiRequest", uuiRequest)
108                         }
109
110                         utils.log("INFO", "uuiRequest:\n" + uuiRequest,  isDebugEnabled)
111
112                         //requestParameters
113                         String serviceType = jsonUtil.getJsonValue(uuiRequest, "service.parameters.serviceType")
114                         if (isBlank(serviceType)) {
115                                 msg = "Input serviceType is null"
116                                 utils.log("INFO", msg, isDebugEnabled)
117                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
118                         } else {
119                                 execution.setVariable("serviceType", serviceType)
120                         }
121                         execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
122
123                 } catch (BpmnError e) {
124                         throw e;
125                 } catch (Exception ex){
126                         msg = "Exception in preProcessRequest " + ex.getMessage()
127                         utils.log("INFO", msg, isDebugEnabled)
128                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
129                 }
130                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
131         }
132
133         public void sendSyncResponse (Execution execution) {
134                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
135                 utils.log("INFO", " *** sendSyncResponse *** ", isDebugEnabled)
136
137                 try {
138                         String operationId = execution.getVariable("operationId")
139                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
140                         // RESTResponse for API Handler (APIH) Reply Task
141                         String updateServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()
142                         utils.log("INFO", " sendSyncResponse to APIH:" + "\n" + updateServiceRestRequest, isDebugEnabled)
143                         sendWorkflowResponse(execution, 202, updateServiceRestRequest)
144                         execution.setVariable("sentSyncResponse", true)
145
146                 } catch (Exception ex) {
147                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
148                         utils.log("INFO", msg, isDebugEnabled)
149                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
150                 }
151                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
152         }
153
154
155         public void sendSyncError (Execution execution) {
156                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
157                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)
158
159                 try {
160                         String errorMessage = ""
161                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
162                                 WorkflowException wfe = execution.getVariable("WorkflowException")
163                                 errorMessage = wfe.getErrorMessage()
164                         } else {
165                                 errorMessage = "Sending Sync Error."
166                         }
167
168                         String buildworkflowException =
169                                         """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
170                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
171                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
172                                    </aetgt:WorkflowException>"""
173
174                         utils.logAudit(buildworkflowException)
175                         sendWorkflowResponse(execution, 500, buildworkflowException)
176
177                 } catch (Exception ex) {
178                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
179                 }
180
181         }
182
183         public void prepareCompletionRequest (Execution execution) {
184                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
185                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)
186
187                 try {
188                         String requestId = execution.getVariable("msoRequestId")
189                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
190                         String source = execution.getVariable("source")
191                         
192                         String msoCompletionRequest =
193                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
194                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
195                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
196                                                         <request-id>${requestId}</request-id>
197                                                         <action>UPDATE</action>
198                                                         <source>${source}</source>
199                                                 </request-info>
200                                                 <status-message>Service Instance was updated successfully.</status-message>
201                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
202                                                 <mso-bpel-name>UpdateGenericALaCarteServiceInstance</mso-bpel-name>
203                                         </aetgt:MsoCompletionRequest>"""
204
205                         // Format Response
206                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
207
208                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
209                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
210
211                 } catch (Exception ex) {
212                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
213                         utils.log("INFO", msg, isDebugEnabled)
214                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
215                 }
216                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
217         }
218
219         public void prepareFalloutRequest(Execution execution){
220                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
221                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)
222
223                 try {
224                         WorkflowException wfex = execution.getVariable("WorkflowException")
225                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
226                         String requestId = execution.getVariable("msoRequestId")
227                         String source = execution.getVariable("source")
228                         String requestInfo =
229                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
230                                         <request-id>${requestId}</request-id>
231                                         <action>UPDATE</action>
232                                         <source>${source}</source>
233                                    </request-info>"""
234
235                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
236                         execution.setVariable("falloutRequest", falloutRequest)
237                 } catch (Exception ex) {
238                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
239                         String errorException = "  Bpmn error encountered in UpdateGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
240                         String requestId = execution.getVariable("msoRequestId")
241                         String falloutRequest =
242                                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
243                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
244                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
245                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
246                                               <request-id>${requestId}</request-id>
247                                               <action>UPDATE</action>
248                                               <source>UUI</source>
249                                            </request-info>
250                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
251                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
252                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
253                                                 </aetgt:WorkflowException>
254                                         </aetgt:FalloutHandlerRequest>"""
255
256                         execution.setVariable("falloutRequest", falloutRequest)
257                 }
258                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
259         }       
260 }