VoLTE groovy draft files
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateCustomE2EServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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 import org.openecomp.mso.bpmn.common.scripts.VidUtils
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.UUID;
34
35 import org.camunda.bpm.engine.delegate.BpmnError
36 import org.camunda.bpm.engine.runtime.Execution
37 import org.apache.commons.lang3.*
38 import org.apache.commons.codec.binary.Base64;
39 import org.springframework.web.util.UriUtils
40
41 /**
42  * This groovy class supports the <class>CreateServiceInstance.bpmn</class> process.
43  * AlaCarte flow for 1702 ServiceInstance Create
44  *
45  */
46 public class CreateCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
47         String Prefix="CRESI_"
48         ExceptionUtil exceptionUtil = new ExceptionUtil()
49         JsonUtils jsonUtil = new JsonUtils()
50         VidUtils vidUtils = new VidUtils()
51
52         public void preProcessRequest (Execution execution) {
53                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
54                 execution.setVariable("prefix",Prefix)
55                 String msg = ""
56                 utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)
57
58                 try {
59
60                         String siRequest = execution.getVariable("bpmnRequest")
61                         utils.logAudit(siRequest)
62
63                         String requestId = execution.getVariable("mso-request-id")
64                         execution.setVariable("msoRequestId", requestId)
65                         utils.log("DEBUG", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
66
67                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
68                         if (isBlank(serviceInstanceId)) {
69                                 serviceInstanceId = UUID.randomUUID().toString()
70                         }
71                         utils.log("DEBUG", "Generated new Service Instance:" + serviceInstanceId, isDebugEnabled)
72                         serviceInstanceId = UriUtils.encode(serviceInstanceId,"UTF-8")
73                         execution.setVariable("serviceInstanceId", serviceInstanceId)
74
75                         //parameters
76                         String p_domainHost = jsonUtil.getJsonValue(siRequest, "service.parameters.domainHost")
77                         if (isBlank(p_domainHost)) {
78                                 msg = "Input parameters domainHost is null"
79                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
80                         } else {
81                                 execution.setVariable("p_domainHost", p_domainHost)
82                         }
83
84                         String p_nodeTemplateName = jsonUtil.getJsonValue(siRequest, "service.parameters.nodeTemplateName")
85                         if (isBlank(p_nodeTemplateName)) {
86                                 msg = "Input parameters nodeTemplateName is null"
87                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
88                         } else {
89                                 execution.setVariable("p_nodeTemplateName", p_nodeTemplateName)
90                         }
91                         
92                         String p_nodeType = jsonUtil.getJsonValue(siRequest, "service.parameters.nodeType")
93                         if (isBlank(p_nodeType)) {
94                                 msg = "Input parameters nodeType is null"
95                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
96                         } else {
97                                 execution.setVariable("p_nodeType", p_nodeType)
98                         }
99
100                         //segments
101                         def jsonSlurper = new JsonSlurper()
102                         //def jsonOutput = new JsonOutput()
103
104                         Map reqMap = jsonSlurper.parseText(siRequest)           
105                         
106                         def p_segments = reqMap.service?.parameters?.segments
107                         
108                         //List<Segment> segList = []
109                         //if (p_segments) {
110                         //    p_segments.each {
111                         //              p_segment -> segList.add(p_segment)
112                         //              //p_segment.domainHost
113                         //      }
114                         //}
115                         //execution.setVariable("segments", segList)
116                         
117                         //location Constraints
118                         def p_locationConstraints = reqMap.service?.parameters?.nsParameters?.locationConstraints
119                         if(p_locationConstraints){
120                         //Copy data. no data available now so ignoring
121                         }
122                         
123                         //additionalParamForNs
124                         String p_param1 = jsonUtil.getJsonValue(siRequest, "service.parameters.nsParameters.additionalParamForNs.E2EServcie.param1")
125                         if (isBlank(p_param1)) {
126                                 msg = "Input parameters p_param1 is null"
127                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
128                         } else {
129                                 execution.setVariable("p_param1", p_param1)
130                         }
131                         
132                         String p_param2 = jsonUtil.getJsonValue(siRequest, "service.parameters.nsParameters.additionalParamForNs.E2EServcie.param2")
133                         if (isBlank(p_param2)) {
134                                 msg = "Input parameters p_param2 is null"
135                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
136                         } else {
137                                 execution.setVariable("p_param2", p_param2)
138                         }
139
140                         //subscriberInfo
141                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
142                         if (isBlank(globalSubscriberId)) {
143                                 msg = "Input globalSubscriberId' is null"
144                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
145                         } else {
146                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
147                         }
148
149                         //requestInfo
150                         execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))
151                         execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))
152                         execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))
153                         String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
154                         if (isBlank(productFamilyId))
155                         {
156                                 msg = "Input productFamilyId is null"
157                                 utils.log("DEBUG", msg, isDebugEnabled)
158                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
159                         } else {
160                                 execution.setVariable("productFamilyId", productFamilyId)
161                         }
162
163                         //modelInfo
164                         String serviceModelInfo = jsonUtil.getJsonValue(siRequest, "requestDetails.modelInfo")
165                         if (isBlank(serviceModelInfo)) {
166                                 msg = "Input serviceModelInfo is null"
167                                 utils.log("DEBUG", msg, isDebugEnabled)
168                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
169                         } else
170                         {
171                                 execution.setVariable("serviceModelInfo", serviceModelInfo)
172                         }
173
174                         utils.log("DEBUG", "modelInfo" + serviceModelInfo,  isDebugEnabled)
175
176                         //requestParameters
177                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.subscriptionServiceType")
178                         if (isBlank(subscriptionServiceType)) {
179                                 msg = "Input subscriptionServiceType is null"
180                                 utils.log("DEBUG", msg, isDebugEnabled)
181                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
182                         } else {
183                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
184                         }
185
186                         //TODO
187                         //execution.setVariable("serviceInputParams", jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams"))
188                         //execution.setVariable("failExists", true)
189
190                 } catch (BpmnError e) {
191                         throw e;
192                 } catch (Exception ex){
193                         msg = "Exception in preProcessRequest " + ex.getMessage()
194                         utils.log("DEBUG", msg, isDebugEnabled)
195                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
196                 }
197                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
198         }
199
200         public void sendSyncResponse (Execution execution) {
201                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
202                 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
203
204                 try {
205                         String requestId = execution.getVariable("msoRequestId")
206                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
207                         // RESTResponse for API Handler (APIH) Reply Task
208                         String createServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${requestId}"}}""".trim()
209                         utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + createServiceRestRequest, isDebugEnabled)
210                         sendWorkflowResponse(execution, 202, createServiceRestRequest)
211                         execution.setVariable("sentSyncResponse", true)
212
213                 } catch (Exception ex) {
214                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
215                         utils.log("DEBUG", msg, isDebugEnabled)
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217                 }
218                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
219         }
220
221
222         public void sendSyncError (Execution execution) {
223                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
224                 utils.log("DEBUG", " *** sendSyncError *** ", isDebugEnabled)
225
226                 try {
227                         String errorMessage = ""
228                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
229                                 WorkflowException wfe = execution.getVariable("WorkflowException")
230                                 errorMessage = wfe.getErrorMessage()
231                         } else {
232                                 errorMessage = "Sending Sync Error."
233                         }
234
235                         String buildworkflowException =
236                                         """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
237                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
238                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
239                                    </aetgt:WorkflowException>"""
240
241                         utils.logAudit(buildworkflowException)
242                         sendWorkflowResponse(execution, 500, buildworkflowException)
243
244                 } catch (Exception ex) {
245                         utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
246                 }
247
248         }
249
250         public void prepareCompletionRequest (Execution execution) {
251                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
252                 utils.log("DEBUG", " *** prepareCompletion *** ", isDebugEnabled)
253
254                 try {
255                         String requestId = execution.getVariable("msoRequestId")
256                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
257                         String source = execution.getVariable("source")
258                         
259                         String msoCompletionRequest =
260                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
261                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
262                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
263                                                         <request-id>${requestId}</request-id>
264                                                         <action>CREATE</action>
265                                                         <source>${source}</source>
266                                                 </request-info>
267                                                 <status-message>Service Instance was created successfully.</status-message>
268                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
269                                                 <mso-bpel-name>CreateGenericALaCarteServiceInstance</mso-bpel-name>
270                                         </aetgt:MsoCompletionRequest>"""
271
272                         // Format Response
273                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
274
275                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
276                         utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
277
278                 } catch (Exception ex) {
279                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
280                         utils.log("DEBUG", msg, isDebugEnabled)
281                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
282                 }
283                 utils.log("DEBUG", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
284         }
285
286         public void prepareFalloutRequest(Execution execution){
287                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
288                 utils.log("DEBUG", " *** prepareFalloutRequest *** ", isDebugEnabled)
289
290                 try {
291                         WorkflowException wfex = execution.getVariable("WorkflowException")
292                         utils.log("DEBUG", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
293                         String requestId = execution.getVariable("msoRequestId")
294                         String source = execution.getVariable("source")
295                         String requestInfo =
296                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
297                                         <request-id>${requestId}</request-id>
298                                         <action>CREATE</action>
299                                         <source>${source}</source>
300                                    </request-info>"""
301
302                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
303                         execution.setVariable("falloutRequest", falloutRequest)
304                 } catch (Exception ex) {
305                         utils.log("DEBUG", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
306                         String errorException = "  Bpmn error encountered in CreateGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
307                         String requestId = execution.getVariable("msoRequestId")
308                         String falloutRequest =
309                                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
310                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
311                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
312                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
313                                               <request-id>${requestId}</request-id>
314                                               <action>CREATE</action>
315                                               <source>VID</source>
316                                            </request-info>
317                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
318                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
319                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
320                                                 </aetgt:WorkflowException>
321                                         </aetgt:FalloutHandlerRequest>"""
322
323                         execution.setVariable("falloutRequest", falloutRequest)
324                 }
325                 utils.log("DEBUG", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
326         }
327 }