240b8d089b37c10b2b12fb696eb6d8a6447af611
[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.delegate.DelegateExecution
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 (DelegateExecution 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                         
122                         //operationId
123                         String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
124                         if (isBlank(operationId)) {
125                                 operationId = UUID.randomUUID().toString()
126                          }   
127                         execution.setVariable("operationId", operationId) 
128                         execution.setVariable("operationType", "UPDATE")
129                         
130                         execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
131
132                 } catch (BpmnError e) {
133                         throw e;
134                 } catch (Exception ex){
135                         msg = "Exception in preProcessRequest " + ex.getMessage()
136                         utils.log("INFO", msg, isDebugEnabled)
137                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138                 }
139                 utils.log("INFO"," ***** Exit preProcessRequest *****",  isDebugEnabled)
140         }
141         
142         /**
143          * Init the service Operation Status
144          */
145         public void prepareInitServiceOperationStatus(DelegateExecution execution){
146                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
147                 utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
148                 try{
149                         String serviceId = execution.getVariable("serviceInstanceId")
150                         String operationId = UUID.randomUUID().toString()
151                         String operationType = execution.getVariable("operationType")
152                         String userId = ""
153                         String result = "processing"
154                         String progress = "0"
155                         String reason = ""
156                         String operationContent = "Prepare service updating"
157                         utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
158                         serviceId = UriUtils.encode(serviceId,"UTF-8")
159                         execution.setVariable("serviceInstanceId", serviceId)
160                         execution.setVariable("operationId", operationId)
161                         execution.setVariable("operationType", operationType)
162
163                         def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
164                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
165                         utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
166
167                         String payload =
168                                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
169                         xmlns:ns="http://org.openecomp.mso/requestsdb">
170                         <soapenv:Header/>
171                         <soapenv:Body>
172                             <ns:updateServiceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
173                             <serviceId>${serviceId}</serviceId>
174                             <operationId>${operationId}</operationId>
175                             <operationType>${operationType}</operationType>
176                             <userId>${userId}</userId>
177                             <result>${result}</result>
178                             <operationContent>${operationContent}</operationContent>
179                             <progress>${progress}</progress>
180                             <reason>${reason}</reason>
181                         </ns:updateServiceOperationStatus>
182                     </soapenv:Body>
183                 </soapenv:Envelope>"""
184
185                         payload = utils.formatXml(payload)
186                         execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
187                         utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
188                         utils.logAudit("CreateVfModuleInfra Outgoing updateServiceOperStatusRequest Request: " + payload)
189
190                 }catch(Exception e){
191                         utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
192                         execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
193                 }
194                 utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
195         }
196
197         public void sendSyncResponse (DelegateExecution execution) {
198                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
199                 utils.log("INFO", " *** sendSyncResponse *** ", isDebugEnabled)
200
201                 try {
202                         String operationId = execution.getVariable("operationId")
203                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
204                         // RESTResponse for API Handler (APIH) Reply Task
205                         String updateServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()
206                         utils.log("INFO", " sendSyncResponse to APIH:" + "\n" + updateServiceRestRequest, isDebugEnabled)
207                         sendWorkflowResponse(execution, 202, updateServiceRestRequest)
208                         execution.setVariable("sentSyncResponse", true)
209
210                 } catch (Exception ex) {
211                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
212                         utils.log("INFO", msg, isDebugEnabled)
213                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
214                 }
215                 utils.log("INFO"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
216         }
217
218
219         public void sendSyncError (DelegateExecution execution) {
220                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
221                 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)
222
223                 try {
224                         String errorMessage = ""
225                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
226                                 WorkflowException wfe = execution.getVariable("WorkflowException")
227                                 errorMessage = wfe.getErrorMessage()
228                         } else {
229                                 errorMessage = "Sending Sync Error."
230                         }
231
232                         String buildworkflowException =
233                                         """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
234                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
235                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
236                                    </aetgt:WorkflowException>"""
237
238                         utils.logAudit(buildworkflowException)
239                         sendWorkflowResponse(execution, 500, buildworkflowException)
240
241                 } catch (Exception ex) {
242                         utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
243                 }
244
245         }
246
247         public void prepareCompletionRequest (DelegateExecution execution) {
248                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
249                 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)
250
251                 try {
252                         String requestId = execution.getVariable("msoRequestId")
253                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
254                         String source = execution.getVariable("source")
255                         
256                         String msoCompletionRequest =
257                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
258                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
259                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
260                                                         <request-id>${requestId}</request-id>
261                                                         <action>UPDATE</action>
262                                                         <source>${source}</source>
263                                                 </request-info>
264                                                 <status-message>Service Instance was updated successfully.</status-message>
265                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
266                                                 <mso-bpel-name>UpdateGenericALaCarteServiceInstance</mso-bpel-name>
267                                         </aetgt:MsoCompletionRequest>"""
268
269                         // Format Response
270                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
271
272                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
273                         utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
274
275                 } catch (Exception ex) {
276                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
277                         utils.log("INFO", msg, isDebugEnabled)
278                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
279                 }
280                 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
281         }
282
283         public void prepareFalloutRequest(DelegateExecution execution){
284                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
285                 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)
286
287                 try {
288                         WorkflowException wfex = execution.getVariable("WorkflowException")
289                         utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
290                         String requestId = execution.getVariable("msoRequestId")
291                         String source = execution.getVariable("source")
292                         String requestInfo =
293                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
294                                         <request-id>${requestId}</request-id>
295                                         <action>UPDATE</action>
296                                         <source>${source}</source>
297                                    </request-info>"""
298
299                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
300                         execution.setVariable("falloutRequest", falloutRequest)
301                 } catch (Exception ex) {
302                         utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
303                         String errorException = "  Bpmn error encountered in UpdateGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
304                         String requestId = execution.getVariable("msoRequestId")
305                         String falloutRequest =
306                                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
307                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
308                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
309                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
310                                               <request-id>${requestId}</request-id>
311                                               <action>UPDATE</action>
312                                               <source>UUI</source>
313                                            </request-info>
314                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
315                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
316                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
317                                                 </aetgt:WorkflowException>
318                                         </aetgt:FalloutHandlerRequest>"""
319
320                         execution.setVariable("falloutRequest", falloutRequest)
321                 }
322                 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
323         }       
324 }