AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateCustomE2EServiceInstance.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. \r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  * \r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * \r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
23 \r
24 import static org.apache.commons.lang3.StringUtils.*;\r
25 import groovy.xml.XmlUtil\r
26 import groovy.json.*\r
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
29 \r
30 import org.openecomp.mso.bpmn.core.WorkflowException\r
31 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
32 import org.openecomp.mso.rest.APIResponse\r
33 \r
34 import java.util.UUID;\r
35 \r
36 import org.camunda.bpm.engine.delegate.BpmnError\r
37 import org.camunda.bpm.engine.delegate.DelegateExecution\r
38 import org.apache.commons.lang3.*\r
39 import org.apache.commons.codec.binary.Base64;\r
40 import org.springframework.web.util.UriUtils\r
41 \r
42 /**\r
43  * This groovy class supports the <class>CreateServiceInstance.bpmn</class> process.\r
44  * AlaCarte flow for 1702 ServiceInstance Create\r
45  *\r
46  */\r
47 public class CreateCustomE2EServiceInstance extends AbstractServiceTaskProcessor {\r
48         String Prefix="CRESI_"\r
49         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
50         JsonUtils jsonUtil = new JsonUtils()\r
51 \r
52 \r
53         public void preProcessRequest (DelegateExecution execution) {\r
54                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
55                 execution.setVariable("prefix",Prefix)\r
56                 String msg = ""\r
57                 utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)\r
58 \r
59                 try {\r
60 \r
61                         String siRequest = execution.getVariable("bpmnRequest")\r
62                         utils.logAudit(siRequest)\r
63 \r
64                         String requestId = execution.getVariable("mso-request-id")\r
65                         execution.setVariable("msoRequestId", requestId)\r
66                         utils.log("DEBUG", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)\r
67 \r
68                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
69                         if (isBlank(serviceInstanceId)) {\r
70                                 serviceInstanceId = UUID.randomUUID().toString()\r
71                         }\r
72                         utils.log("DEBUG", "Generated new Service Instance:" + serviceInstanceId, isDebugEnabled)\r
73                         serviceInstanceId = UriUtils.encode(serviceInstanceId,"UTF-8")\r
74                         execution.setVariable("serviceInstanceId", serviceInstanceId)\r
75 \r
76                         //subscriberInfo\r
77                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")\r
78                         if (isBlank(globalSubscriberId)) {\r
79                                 msg = "Input globalSubscriberId' is null"\r
80                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
81                         } else {\r
82                                 execution.setVariable("globalSubscriberId", globalSubscriberId)\r
83                         }\r
84 \r
85                         //requestInfo\r
86                         execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))\r
87                         execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))\r
88                         execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))\r
89                         String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")\r
90                         if (isBlank(productFamilyId))\r
91                         {\r
92                                 msg = "Input productFamilyId is null"\r
93                                 utils.log("DEBUG", msg, isDebugEnabled)\r
94                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
95                         } else {\r
96                                 execution.setVariable("productFamilyId", productFamilyId)\r
97                         }\r
98 \r
99                         //modelInfo\r
100                         String serviceModelInfo = jsonUtil.getJsonValue(siRequest, "requestDetails.modelInfo")\r
101                         if (isBlank(serviceModelInfo)) {\r
102                                 msg = "Input serviceModelInfo is null"\r
103                                 utils.log("DEBUG", msg, isDebugEnabled)\r
104                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
105                         } else\r
106                         {\r
107                                 execution.setVariable("serviceModelInfo", serviceModelInfo)\r
108                         }\r
109 \r
110                         utils.log("DEBUG", "modelInfo" + serviceModelInfo,  isDebugEnabled)\r
111 \r
112                         //requestParameters\r
113                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.subscriptionServiceType")\r
114                         if (isBlank(subscriptionServiceType)) {\r
115                                 msg = "Input subscriptionServiceType is null"\r
116                                 utils.log("DEBUG", msg, isDebugEnabled)\r
117                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
118                         } else {\r
119                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)\r
120                         }\r
121 \r
122                         \r
123                         /*\r
124                          * Extracting User Parameters from incoming Request and converting into a Map\r
125                          */\r
126                         def jsonSlurper = new JsonSlurper()\r
127                         def jsonOutput = new JsonOutput()\r
128 \r
129                         Map reqMap = jsonSlurper.parseText(siRequest)\r
130 \r
131                         //InputParams\r
132                         def userParams = reqMap.requestDetails?.requestParameters?.userParams\r
133 \r
134                         Map<String, String> inputMap = [:]\r
135                         if (userParams) {\r
136                                 userParams.each {\r
137                                         userParam -> inputMap.put(userParam.name, userParam.value.toString())\r
138                                 }\r
139                         }\r
140                         \r
141                         utils.log("DEBUG", "User Input Parameters map: " + userParams.toString(), isDebugEnabled)\r
142                         execution.setVariable("serviceInputParams", inputMap)\r
143                         execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")\r
144                         //TODO\r
145                         //execution.setVariable("serviceInputParams", jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams"))\r
146                         //execution.setVariable("failExists", true)\r
147 \r
148                 } catch (BpmnError e) {\r
149                         throw e;\r
150                 } catch (Exception ex){\r
151                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
152                         utils.log("DEBUG", msg, isDebugEnabled)\r
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
154                 }\r
155                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
156         }\r
157 \r
158         public void sendSyncResponse (DelegateExecution execution) {\r
159                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
160                 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)\r
161 \r
162                 try {\r
163                         String operationId = execution.getVariable("operationId")\r
164                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
165                         // RESTResponse for API Handler (APIH) Reply Task\r
166                         String createServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()\r
167                         utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + createServiceRestRequest, isDebugEnabled)\r
168                         sendWorkflowResponse(execution, 202, createServiceRestRequest)\r
169                         execution.setVariable("sentSyncResponse", true)\r
170 \r
171                 } catch (Exception ex) {\r
172                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()\r
173                         utils.log("DEBUG", msg, isDebugEnabled)\r
174                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
175                 }\r
176                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)\r
177         }\r
178 \r
179 \r
180         public void sendSyncError (DelegateExecution execution) {\r
181                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
182                 utils.log("DEBUG", " *** sendSyncError *** ", isDebugEnabled)\r
183 \r
184                 try {\r
185                         String errorMessage = ""\r
186                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {\r
187                                 WorkflowException wfe = execution.getVariable("WorkflowException")\r
188                                 errorMessage = wfe.getErrorMessage()\r
189                         } else {\r
190                                 errorMessage = "Sending Sync Error."\r
191                         }\r
192 \r
193                         String buildworkflowException =\r
194                                         """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
195                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
196                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
197                                    </aetgt:WorkflowException>"""\r
198 \r
199                         utils.logAudit(buildworkflowException)\r
200                         sendWorkflowResponse(execution, 500, buildworkflowException)\r
201 \r
202                 } catch (Exception ex) {\r
203                         utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)\r
204                 }\r
205 \r
206         }\r
207 \r
208         public void prepareCompletionRequest (DelegateExecution execution) {\r
209                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
210                 utils.log("DEBUG", " *** prepareCompletion *** ", isDebugEnabled)\r
211 \r
212                 try {\r
213                         String requestId = execution.getVariable("msoRequestId")\r
214                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
215                         String source = execution.getVariable("source")\r
216                         \r
217                         String msoCompletionRequest =\r
218                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
219                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
220                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
221                                                         <request-id>${requestId}</request-id>\r
222                                                         <action>CREATE</action>\r
223                                                         <source>${source}</source>\r
224                                                 </request-info>\r
225                                                 <status-message>Service Instance was created successfully.</status-message>\r
226                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>\r
227                                                 <mso-bpel-name>CreateGenericALaCarteServiceInstance</mso-bpel-name>\r
228                                         </aetgt:MsoCompletionRequest>"""\r
229 \r
230                         // Format Response\r
231                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
232 \r
233                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)\r
234                         utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
235 \r
236                 } catch (Exception ex) {\r
237                         String msg = " Exception in prepareCompletion:" + ex.getMessage()\r
238                         utils.log("DEBUG", msg, isDebugEnabled)\r
239                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
240                 }\r
241                 utils.log("DEBUG", "*** Exit prepareCompletionRequest ***", isDebugEnabled)\r
242         }\r
243 \r
244         public void prepareFalloutRequest(DelegateExecution execution){\r
245                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
246                 utils.log("DEBUG", " *** prepareFalloutRequest *** ", isDebugEnabled)\r
247 \r
248                 try {\r
249                         WorkflowException wfex = execution.getVariable("WorkflowException")\r
250                         utils.log("DEBUG", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)\r
251                         String requestId = execution.getVariable("msoRequestId")\r
252                         String source = execution.getVariable("source")\r
253                         String requestInfo =\r
254                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
255                                         <request-id>${requestId}</request-id>\r
256                                         <action>CREATE</action>\r
257                                         <source>${source}</source>\r
258                                    </request-info>"""\r
259 \r
260                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)\r
261                         execution.setVariable("falloutRequest", falloutRequest)\r
262                 } catch (Exception ex) {\r
263                         utils.log("DEBUG", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)\r
264                         String errorException = "  Bpmn error encountered in CreateGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()\r
265                         String requestId = execution.getVariable("msoRequestId")\r
266                         String falloutRequest =\r
267                                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
268                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
269                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
270                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
271                                               <request-id>${requestId}</request-id>\r
272                                               <action>CREATE</action>\r
273                                               <source>UUI</source>\r
274                                            </request-info>\r
275                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
276                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>\r
277                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>\r
278                                                 </aetgt:WorkflowException>\r
279                                         </aetgt:FalloutHandlerRequest>"""\r
280 \r
281                         execution.setVariable("falloutRequest", falloutRequest)\r
282                 }\r
283                 utils.log("DEBUG", "*** Exit prepareFalloutRequest ***", isDebugEnabled)\r
284         }\r
285         \r
286         /**\r
287          * Init the service Operation Status\r
288          */\r
289         public void prepareInitServiceOperationStatus(DelegateExecution execution){\r
290         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
291         utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)\r
292         try{\r
293             String serviceId = execution.getVariable("serviceInstanceId")\r
294             String operationId = UUID.randomUUID().toString()\r
295             String operationType = "CREATE"\r
296             String userId = ""\r
297             String result = "processing"\r
298             String progress = "0"\r
299             String reason = ""\r
300             String operationContent = "Prepare service creation"\r
301             utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)\r
302             serviceId = UriUtils.encode(serviceId,"UTF-8")\r
303             execution.setVariable("serviceInstanceId", serviceId)\r
304             execution.setVariable("operationId", operationId)\r
305             execution.setVariable("operationType", operationType)\r
306 \r
307             def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")\r
308             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)\r
309             utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)\r
310 \r
311             String payload =\r
312                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"\r
313                         xmlns:ns="http://org.openecomp.mso/requestsdb">\r
314                         <soapenv:Header/>\r
315                         <soapenv:Body>\r
316                             <ns:updateServiceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">\r
317                             <serviceId>${serviceId}</serviceId>\r
318                             <operationId>${operationId}</operationId>\r
319                             <operationType>${operationType}</operationType>\r
320                             <userId>${userId}</userId>\r
321                             <result>${result}</result>\r
322                             <operationContent>${operationContent}</operationContent>\r
323                             <progress>${progress}</progress>\r
324                             <reason>${reason}</reason>\r
325                         </ns:updateServiceOperationStatus>\r
326                     </soapenv:Body>\r
327                 </soapenv:Envelope>"""\r
328 \r
329             payload = utils.formatXml(payload)\r
330             execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)\r
331             utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)\r
332             utils.logAudit("CreateVfModuleInfra Outgoing updateServiceOperStatusRequest Request: " + payload)\r
333 \r
334         }catch(Exception e){\r
335             utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)\r
336             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())\r
337         }\r
338         utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)    \r
339         }\r
340         \r
341 }\r