ec414a4ee2f958a4c1cd10e666cb3a85780199bd
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateGenericALaCarteServiceInstance.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 CreateGenericALaCarteServiceInstance 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                         //subscriberInfo
76                         String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
77                         if (isBlank(globalSubscriberId)) {
78                                 msg = "Input globalSubscriberId' is null"
79                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
80                         } else {
81                                 execution.setVariable("globalSubscriberId", globalSubscriberId)
82                         }
83
84                         //requestInfo
85                         execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))
86                         execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))
87                         execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))
88                         String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
89                         if (isBlank(productFamilyId))
90                         {
91                                 msg = "Input productFamilyId is null"
92                                 utils.log("DEBUG", msg, isDebugEnabled)
93                                 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
94                         } else {
95                                 execution.setVariable("productFamilyId", productFamilyId)
96                         }
97
98                         //modelInfo
99                         String serviceModelInfo = jsonUtil.getJsonValue(siRequest, "requestDetails.modelInfo")
100                         if (isBlank(serviceModelInfo)) {
101                                 msg = "Input serviceModelInfo is null"
102                                 utils.log("DEBUG", msg, isDebugEnabled)
103                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
104                         } else
105                         {
106                                 execution.setVariable("serviceModelInfo", serviceModelInfo)
107                         }
108
109                         utils.log("DEBUG", "modelInfo" + serviceModelInfo,  isDebugEnabled)
110
111                         //requestParameters
112                         String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.subscriptionServiceType")
113                         if (isBlank(subscriptionServiceType)) {
114                                 msg = "Input subscriptionServiceType is null"
115                                 utils.log("DEBUG", msg, isDebugEnabled)
116                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
117                         } else {
118                                 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
119                         }
120
121                         //TODO
122                         //execution.setVariable("serviceInputParams", jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams"))
123                         //execution.setVariable("failExists", true)
124
125                 } catch (BpmnError e) {
126                         throw e;
127                 } catch (Exception ex){
128                         msg = "Exception in preProcessRequest " + ex.getMessage()
129                         utils.log("DEBUG", msg, isDebugEnabled)
130                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
131                 }
132                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
133         }
134
135         public void sendSyncResponse (Execution execution) {
136                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
137                 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
138
139                 try {
140                         String requestId = execution.getVariable("msoRequestId")
141                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
142                         // RESTResponse for API Handler (APIH) Reply Task
143                         String createServiceRestRequest = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim()
144                         utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + createServiceRestRequest, isDebugEnabled)
145                         sendWorkflowResponse(execution, 202, createServiceRestRequest)
146                         execution.setVariable("sentSyncResponse", true)
147
148                 } catch (Exception ex) {
149                         String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
150                         utils.log("DEBUG", msg, isDebugEnabled)
151                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
152                 }
153                 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****",  isDebugEnabled)
154         }
155
156
157         public void sendSyncError (Execution execution) {
158                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
159                 utils.log("DEBUG", " *** sendSyncError *** ", isDebugEnabled)
160
161                 try {
162                         String errorMessage = ""
163                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
164                                 WorkflowException wfe = execution.getVariable("WorkflowException")
165                                 errorMessage = wfe.getErrorMessage()
166                         } else {
167                                 errorMessage = "Sending Sync Error."
168                         }
169
170                         String buildworkflowException =
171                                         """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
172                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
173                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
174                                    </aetgt:WorkflowException>"""
175
176                         utils.logAudit(buildworkflowException)
177                         sendWorkflowResponse(execution, 500, buildworkflowException)
178
179                 } catch (Exception ex) {
180                         utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
181                 }
182
183         }
184
185         public void prepareCompletionRequest (Execution execution) {
186                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
187                 utils.log("DEBUG", " *** prepareCompletion *** ", isDebugEnabled)
188
189                 try {
190                         String requestId = execution.getVariable("msoRequestId")
191                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
192                         String source = execution.getVariable("source")
193                         
194                         String msoCompletionRequest =
195                                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
196                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
197                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
198                                                         <request-id>${requestId}</request-id>
199                                                         <action>CREATE</action>
200                                                         <source>${source}</source>
201                                                 </request-info>
202                                                 <status-message>Service Instance was created successfully.</status-message>
203                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
204                                                 <mso-bpel-name>CreateGenericALaCarteServiceInstance</mso-bpel-name>
205                                         </aetgt:MsoCompletionRequest>"""
206
207                         // Format Response
208                         String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
209
210                         execution.setVariable("completionRequest", xmlMsoCompletionRequest)
211                         utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
212
213                 } catch (Exception ex) {
214                         String msg = " Exception in prepareCompletion:" + ex.getMessage()
215                         utils.log("DEBUG", msg, isDebugEnabled)
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217                 }
218                 utils.log("DEBUG", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
219         }
220
221         public void prepareFalloutRequest(Execution execution){
222                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
223                 utils.log("DEBUG", " *** prepareFalloutRequest *** ", isDebugEnabled)
224
225                 try {
226                         WorkflowException wfex = execution.getVariable("WorkflowException")
227                         utils.log("DEBUG", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
228                         String requestId = execution.getVariable("msoRequestId")
229                         String source = execution.getVariable("source")
230                         String requestInfo =
231                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
232                                         <request-id>${requestId}</request-id>
233                                         <action>CREATE</action>
234                                         <source>${source}</source>
235                                    </request-info>"""
236
237                         String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
238                         execution.setVariable("falloutRequest", falloutRequest)
239                 } catch (Exception ex) {
240                         utils.log("DEBUG", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
241                         String errorException = "  Bpmn error encountered in CreateGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
242                         String requestId = execution.getVariable("msoRequestId")
243                         String falloutRequest =
244                                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
245                                                                      xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
246                                                                      xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
247                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
248                                               <request-id>${requestId}</request-id>
249                                               <action>CREATE</action>
250                                               <source>VID</source>
251                                            </request-info>
252                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">\r
253                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
254                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
255                                                 </aetgt:WorkflowException>
256                                         </aetgt:FalloutHandlerRequest>"""
257
258                         execution.setVariable("falloutRequest", falloutRequest)
259                 }
260                 utils.log("DEBUG", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
261         }
262 }