519f064802fcde20ed4725a5db46fc2fc6d41948
[so.git] /
1 /*-
2
3  * ============LICENSE_START=======================================================
4  * ONAP - SO
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. 
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
23 package org.onap.so.bpmn.infrastructure.scripts;
24
25 import static org.apache.commons.lang3.StringUtils.*;
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution
30 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.common.scripts.MsoUtils
33 import org.onap.so.bpmn.common.scripts.VidUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.bpmn.core.UrnPropertiesReader
37 import org.onap.so.logger.MsoLogger
38 import org.springframework.web.util.UriUtils;
39
40 import groovy.json.*
41
42 /**
43  * This groovy class supports the <class>DelE2EServiceInstance.bpmn</class> process.
44  *
45  */
46 public class DeleteCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
47
48         String Prefix="DELSI_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils()
52         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteCustomE2EServiceInstance.class);
53         
54         public void preProcessRequest (DelegateExecution execution) {
55                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
56                 execution.setVariable("prefix",Prefix)
57                 String msg = ""
58                 
59                 msoLogger.info("Starting preProcessRequest")
60
61                 try {
62                         // check for incoming json message/input
63                         String siRequest = execution.getVariable("bpmnRequest")
64                         msoLogger.debug(siRequest)
65                         
66                         
67                         String requestId = execution.getVariable("mso-request-id")
68                         execution.setVariable("msoRequestId", requestId)
69                         msoLogger.info("Input Request:" + siRequest + " reqId:" + requestId)
70                         
71                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
72                         if (isBlank(serviceInstanceId)) {
73                                 msg = "Input serviceInstanceId' is null"
74                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
75                         }
76                 
77                                 
78                         //requestInfo
79 //                      String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
80 //                      if (isBlank(productFamilyId))
81 //                      {
82 //                              msg = "Input productFamilyId is null"
83 //                              utils.log("INFO", msg, isDebugEnabled)
84 //                              //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
85 //                      } else {
86 //                              execution.setVariable("productFamilyId", productFamilyId)
87 //                      }
88                         String source = jsonUtil.getJsonValue(siRequest, "source")
89                         execution.setVariable("source", source)
90                         
91                         //subscriberInfo
92                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")
93                         if (isBlank(globalSubscriberId)) {
94                                 msg = "Input globalSubscriberId' is null"
95                                 msoLogger.info(msg)
96                         } else {
97                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
98                         }
99                         
100                         //requestParameters
101                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")
102                         if (isBlank(subscriptionServiceType)) {
103                                 msg = "Input subscriptionServiceType is null"
104                                 msoLogger.debug(msg)
105                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
106                         } else {
107                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
108                         }
109                         String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
110                         execution.setVariable("operationId", operationId)
111                         
112                         execution.setVariable("operationType", "DELETE") 
113                 } catch (BpmnError e) {
114                         throw e;
115                 } catch (Exception ex){
116                         msg = "Exception in preProcessRequest " + ex.getMessage()
117                         msoLogger.info(msg)
118                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
119                 }
120                 msoLogger.trace("Exit preProcessRequest")
121         }
122
123         public void sendSyncResponse (DelegateExecution execution) {
124                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
125                 msoLogger.trace("Staring sendSyncResponse")
126
127                 try {
128                         String operationId = execution.getVariable("operationId")
129                         String syncResponse = """{"operationId":"${operationId}"}""".trim()
130                         msoLogger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
131                         sendWorkflowResponse(execution, 202, syncResponse)
132
133                 } catch (Exception ex) {
134                         String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
136                 }
137                 msoLogger.trace("Exit sendSyncResopnse")
138         }
139         
140         public void sendSyncError (DelegateExecution execution) {
141                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
142                 msoLogger.info("Starting sendSyncError")
143
144                 try {
145                         String errorMessage = ""
146                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
147                                 WorkflowException wfe = execution.getVariable("WorkflowException")
148                                 errorMessage = wfe.getErrorMessage()
149                         } else {
150                                 errorMessage = "Sending Sync Error."
151                         }
152
153                         String buildworkflowException =
154                                 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
155                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
156                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
157                                    </aetgt:WorkflowException>"""
158
159                         msoLogger.info(buildworkflowException)
160                         sendWorkflowResponse(execution, 500, buildworkflowException)
161
162                 } catch (Exception ex) {
163                         msoLogger.info("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
164                 }
165
166         }
167         
168         public void prepareCompletionRequest (DelegateExecution execution) {
169                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
170                 msoLogger.trace("Starting prepareCompletion")
171
172                 try {
173                         String requestId = execution.getVariable("msoRequestId")
174                         String source = execution.getVariable("source")
175                         String msoCompletionRequest =
176                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
177                                                                 xmlns:ns="http://org.onap/so/request/types/v1">
178                                                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
179                                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
180                                                         <action>DELETE</action>
181                                                         <source>${MsoUtils.xmlEscape(source)}</source>
182                                                 </request-info>
183                                                 <aetgt:status-message>E2E Service Instance was deleted successfully.</aetgt:status-message>
184                                                 <aetgt:mso-bpel-name>DeleteCustomE2EServiceInstance</aetgt:mso-bpel-name>
185                                         </aetgt:MsoCompletionRequest>"""
186
187                         // Format Response
188                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
189
190                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
191                         msoLogger.info(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
192
193                 } catch (Exception ex) {
194                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
195                         msoLogger.info(msg)
196                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
197                 }
198                 msoLogger.trace("Exit prepareCompletionRequest")
199         }
200         
201         public void prepareFalloutRequest(DelegateExecution execution){
202                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
203                 msoLogger.trace("Starting prepareFalloutRequest ")
204
205                 try {
206                         WorkflowException wfex = execution.getVariable("WorkflowException")
207                         msoLogger.info(" Input Workflow Exception: " + wfex.toString())
208                         String requestId = execution.getVariable("msoRequestId")
209                         String source = execution.getVariable("source")
210                         String requestInfo =
211                         """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
212                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
213                                         <action>DELETE</action>
214                                         <source>${MsoUtils.xmlEscape(source)}</source>
215                                    </request-info>"""
216
217                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
218                         execution.setVariable("falloutRequest", falloutRequest)
219                 } catch (Exception ex) {
220                         msoLogger.info("Exception prepareFalloutRequest:" + ex.getMessage())
221                         String errorException = "  Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
222                         String requestId = execution.getVariable("msoRequestId")
223                         String falloutRequest =
224                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
225                                                                      xmlns:ns="http://org.onap/so/request/types/v1"
226                                                                      xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
227                                            <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
228                                               <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
229                                               <action>DELETE</action>
230                                               <source>VID</source>
231                                            </request-info>
232                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
233                                                         <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorException)}</aetgt:ErrorMessage>
234                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
235                                                 </aetgt:WorkflowException>
236                                         </aetgt:FalloutHandlerRequest>"""
237
238                         execution.setVariable("falloutRequest", falloutRequest)
239                 }
240                 msoLogger.trace("Exit prepareFalloutRequest")
241         }
242         
243
244         // *******************************
245         //     Build DB request Section
246         // *******************************
247         public void prepareDBRequest (DelegateExecution execution) {
248                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
249                 execution.setVariable("prefix", Prefix)
250
251                 try {
252                         msoLogger.info("Starting prepareDBRequest")
253
254                         String requestId = execution.getVariable("DELSI_requestId")
255                         String statusMessage = "E2E Service Instance successfully deleted."
256
257                         //TODO - verify the format for Service Instance Delete,
258                         String dbRequest =
259                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
260                                                 <soapenv:Header/>
261                                                 <soapenv:Body>
262                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
263                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
264                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
265                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
266                                                                 <responseBody></responseBody>
267                                                                 <requestStatus>COMPLETED</requestStatus>
268                                                                 <progress>100</progress>
269                                                         </ns:updateInfraRequest>
270                                                    </soapenv:Body>
271                                            </soapenv:Envelope>"""
272
273                    String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
274                    execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)
275                   msoLogger.info(buildDeleteDBRequestAsString)
276
277                 } catch (Exception ex) {
278                         // try error in method block
279                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()
280                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
281
282                 }
283
284          }
285
286         // *******************************
287         //     Build Error Section
288         // *******************************
289         public void prepareDBRequestError (DelegateExecution execution) {
290                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
291                 execution.setVariable("prefix", Prefix)
292
293                 msoLogger.trace("Start prepareDBRequestError")
294
295                 try {
296                         String requestId = execution.getVariable("DELSI_requestId")
297                         String statusMessage = ""
298                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
299                                 WorkflowException wfe = execution.getVariable("WorkflowException")
300                                 statusMessage = wfe.getErrorMessage()
301
302                         } else {
303                                 statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "
304                         }
305
306                         //TODO - verify the format for Service Instance Create,
307                         String dbRequest =
308                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
309                                                 <soapenv:Header/>
310                                                 <soapenv:Body>
311                                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
312                                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
313                                                                 <lastModifiedBy>BPMN</lastModifiedBy>
314                                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
315                                                                 <responseBody></responseBody>
316                                                                 <requestStatus>FAILED</requestStatus>
317                                                         </ns:updateInfraRequest>
318                                                    </soapenv:Body>
319                                            </soapenv:Envelope>"""
320
321                    String buildDBRequestAsString = utils.formatXml(dbRequest)
322                    execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)
323                    msoLogger.info(buildDBRequestAsString)
324
325                 } catch (Exception ex) {
326                         // try error in method block
327                         String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()
328                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
329
330                 }
331
332          }
333
334         public void processJavaException(DelegateExecution execution) {
335                 //TODO:
336         }
337
338         /**
339          * Init the service Operation Status
340          */
341         public void prepareInitServiceOperationStatus(DelegateExecution execution){
342                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
343                 utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
344                 try{
345                         String serviceId = execution.getVariable("serviceInstanceId")
346                         String operationId = execution.getVariable("operationId")
347                         String userId = ""
348                         String result = "processing"
349                         String progress = "0"
350                         String reason = ""
351                         String operationContent = "Prepare service creation"
352                         utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
353                         serviceId = UriUtils.encode(serviceId,"UTF-8")
354
355                         def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
356                         execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
357                         utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
358
359                         String payload =
360                                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
361                         xmlns:ns="http://org.onap.so/requestsdb">
362                         <soapenv:Header/>
363                         <soapenv:Body>
364                             <ns:updateServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
365                             <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
366                             <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
367                             <operationType>DELETE</operationType>
368                             <userId>${MsoUtils.xmlEscape(userId)}</userId>
369                             <result>${MsoUtils.xmlEscape(result)}</result>
370                             <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
371                             <progress>${MsoUtils.xmlEscape(progress)}</progress>
372                             <reason>${MsoUtils.xmlEscape(reason)}</reason>
373                         </ns:updateServiceOperationStatus>
374                     </soapenv:Body>
375                 </soapenv:Envelope>"""
376
377                         payload = utils.formatXml(payload)
378                         execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
379                         utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
380
381                 }catch(Exception e){
382                         utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
383                         execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
384                 }
385                 utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
386         }
387 }