58f644d53ff6eda38d136bc00936bef197bda641
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 CMCC. 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 org.camunda.bpm.engine.delegate.DelegateExecution
24
25 import static org.apache.commons.lang3.StringUtils.*
26 import org.openecomp.mso.logger.MsoLogger
27 import org.openecomp.mso.utils.UUIDChecker
28 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
30 import org.openecomp.mso.bpmn.core.WorkflowException
31 import org.openecomp.mso.bpmn.core.json.JsonUtils
32 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
33
34 import org.camunda.bpm.engine.delegate.BpmnError
35 import org.camunda.bpm.engine.runtime.Execution
36
37 import org.springframework.web.util.UriUtils
38
39
40 /**
41  * This groovy class supports the <class>ScaleCustomE2EServiceInstance.bpmn</class> process.
42  *
43  */
44 public class ScaleCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
45     String Prefix = "CRESI_"
46     ExceptionUtil exceptionUtil = new ExceptionUtil()
47     JsonUtils jsonUtil = new JsonUtils()
48     private MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
49
50     public void preProcessRequest(DelegateExecution execution) {
51         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
52         execution.setVariable("prefix", Prefix)
53         String msg = ""
54         utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)
55
56         try {
57
58             String siRequest = execution.getVariable("bpmnRequest")
59             utils.logAudit(siRequest)
60
61             String requestId = execution.getVariable("mso-request-id")
62             execution.setVariable("msoRequestId", requestId)
63             utils.log("DEBUG", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
64
65             String serviceInstanceId = execution.getVariable("serviceInstanceId")
66             if (isBlank(serviceInstanceId)) {
67                 serviceInstanceId = "NULL".toString()
68             }
69             utils.log("DEBUG", "Generated new Service Instance:" + serviceInstanceId, isDebugEnabled)
70             serviceInstanceId = UriUtils.encode(serviceInstanceId, "UTF-8")
71             execution.setVariable("serviceInstanceId", serviceInstanceId)
72
73             // service instance ID is also service ID
74             execution.setVariable("serviceId", serviceInstanceId)
75             // service instance name
76             String serviceInstanceName = jsonUtil.getJsonValue(siRequest, "service.serviceInstanceName")
77             execution.setVariable("serviceInstanceName", serviceInstanceName)
78
79             // service instance name
80             String serviceType = jsonUtil.getJsonValue(siRequest, "service.serviceType")
81             execution.setVariable("serviceType", serviceType)
82
83             // operationa ID (key)
84             //String operationKey = UUIDChecker.generateUUID(msoLogger)
85             String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
86             execution.setVariable("operationId", operationId)
87             utils.log("DEBUG", "Input Request:" + siRequest + " operationId:" + operationId, isDebugEnabled)
88
89
90             String resources = jsonUtil.getJsonValue(siRequest, "service.resources")
91             execution.setVariable("resources", resources)
92
93             // node template UUID
94             String nodeTemplateUUID = UUIDChecker.generateUUID(msoLogger)
95             execution.setVariable("nodeTemplateUUID", nodeTemplateUUID)
96
97             //subscriberInfo
98             String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "service.globalSubscriberId")
99             if (isBlank(globalSubscriberId)) {
100                 msg = "Input globalSubscriberId' is null"
101                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
102             } else {
103                 execution.setVariable("globalSubscriberId", globalSubscriberId)
104             }
105
106             String requestDescription = "request description for test"
107             execution.setVariable("requestDescription", requestDescription)
108             execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
109
110         } catch (BpmnError e) {
111             throw e;
112         } catch (Exception ex) {
113             msg = "Exception in preProcessRequest " + ex.getMessage()
114             utils.log("DEBUG", msg, isDebugEnabled)
115             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
116         }
117         utils.log("DEBUG", " ***** Exit preProcessRequest *****", isDebugEnabled)
118     }
119
120     public void sendSyncResponse(DelegateExecution execution) {
121         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
122         utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
123
124         try {
125             String operationId = execution.getVariable("operationId")
126             String serviceInstanceId = execution.getVariable("serviceInstanceId")
127             // RESTResponse for API Handler (APIH) Reply Task
128             String scaleServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${
129                 operationId
130             }"}}""".trim()
131             utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + scaleServiceRestRequest, isDebugEnabled)
132             sendWorkflowResponse(execution, 202, scaleServiceRestRequest)
133             execution.setVariable("sentSyncResponse", true)
134
135         } catch (Exception ex) {
136             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
137             utils.log("DEBUG", msg, isDebugEnabled)
138             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139         }
140         utils.log("DEBUG", " ***** Exit sendSyncResopnse *****", isDebugEnabled)
141     }
142
143
144     public void sendSyncError(DelegateExecution execution) {
145         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
146         utils.log("DEBUG", " *** sendSyncError *** ", isDebugEnabled)
147
148         try {
149             String errorMessage = ""
150             if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
151                 WorkflowException wfe = execution.getVariable("WorkflowException")
152                 errorMessage = wfe.getErrorMessage()
153             } else {
154                 errorMessage = "Sending Sync Error."
155             }
156
157             String buildworkflowException =
158                     """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
159                                         <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
160                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
161                                    </aetgt:WorkflowException>"""
162
163             utils.logAudit(buildworkflowException)
164             sendWorkflowResponse(execution, 500, buildworkflowException)
165
166         } catch (Exception ex) {
167             utils.log("DEBUG", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
168         }
169
170     }
171
172     public void prepareCompletionRequest(DelegateExecution execution) {
173         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
174         utils.log("DEBUG", " *** prepareCompletion *** ", isDebugEnabled)
175
176         try {
177             String requestId = execution.getVariable("msoRequestId")
178             String serviceInstanceId = execution.getVariable("serviceInstanceId")
179             String source = execution.getVariable("source")
180
181             String msoCompletionRequest =
182                     """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
183                                                                 xmlns:ns="http://org.openecomp/mso/request/types/v1">
184                                                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
185                                                         <request-id>${requestId}</request-id>
186                                                         <action>SCALE</action>
187                                                         <source>${source}</source>
188                                                    </request-info>
189                                                 <status-message>Service Instance was scaled successfully.</status-message>
190                                                 <serviceInstanceId>${serviceInstanceId}</serviceInstanceId>
191                                                    <mso-bpel-name>ScaleGenericALaCarteServiceInstance</mso-bpel-name>
192                                         </aetgt:MsoCompletionRequest>"""
193
194             // Format Response
195             String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
196
197             execution.setVariable("CompleteMsoProcessRequest", xmlMsoCompletionRequest)
198             utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
199
200         } catch (Exception ex) {
201             String msg = " Exception in prepareCompletion:" + ex.getMessage()
202             utils.log("DEBUG", msg, isDebugEnabled)
203             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
204         }
205         utils.log("DEBUG", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
206     }
207
208     public void prepareFalloutRequest(DelegateExecution execution) {
209         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
210         utils.log("DEBUG", " *** prepareFalloutRequest *** ", isDebugEnabled)
211
212         try {
213             WorkflowException wfex = execution.getVariable("WorkflowException")
214             utils.log("DEBUG", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
215             String requestId = execution.getVariable("msoRequestId")
216             String source = execution.getVariable("source")
217             String requestInfo =
218                     """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
219                                         <request-id>${requestId}</request-id>
220                                         <action>SCALE</action>
221                                         <source>${source}</source>
222                                    </request-info>"""
223
224             String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
225             execution.setVariable("falloutRequest", falloutRequest)
226         } catch (Exception ex) {
227             utils.log("DEBUG", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
228             String errorException = "  Bpmn error encountered in ScaleGenericALaCarteServiceInstance flow. FalloutHandlerRequest,  buildErrorResponse() - " + ex.getMessage()
229             String requestId = execution.getVariable("msoRequestId")
230             String falloutRequest =
231                     """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
232                                                                                                  xmlns:ns="http://org.openecomp/mso/request/types/v1"
233                                                                                                  xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
234                                            <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
235                                                   <request-id>${requestId}</request-id>
236                                                   <action>SCALE</action>
237                                                   <source>UUI</source>
238                                            </request-info>
239                                                 <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
240                                                         <aetgt:ErrorMessage>${errorException}</aetgt:ErrorMessage>
241                                                         <aetgt:ErrorCode>7000</aetgt:ErrorCode>
242                                                 </aetgt:WorkflowException>
243                                         </aetgt:FalloutHandlerRequest>"""
244
245             execution.setVariable("falloutRequest", falloutRequest)
246         }
247         utils.log("DEBUG", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
248     }
249
250
251     /**
252      * Init the service Operation Status
253      */
254     public void prepareInitServiceOperationStatus(DelegateExecution execution){
255         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
256         utils.log("INFO", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
257         try{
258             String serviceId = execution.getVariable("serviceInstanceId")
259             String serviceName = execution.getVariable("serviceInstanceName")
260             String operationId = execution.getVariable("operationId")
261             String operationType = "SCALE"
262             String userId = ""
263             String result = "processing"
264             String progress = "0"
265             String reason = ""
266             String operationContent = "Prepare service scaling"
267             execution.setVariable("serviceInstanceId", serviceId)
268             execution.setVariable("operationId", operationId)
269             execution.setVariable("operationType", operationType)
270
271             def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
272             execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
273             utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
274
275             String payload =
276                     """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
277                         xmlns:ns="http://org.openecomp.mso/requestsdb">
278                         <soapenv:Header/>
279                         <soapenv:Body>
280                             <ns:updateServiceOperationStatus xmlns:ns="http://org.openecomp.mso/requestsdb">
281                             <serviceId>${serviceId}</serviceId>
282                             <operationId>${operationId}</operationId>
283                             <serviceName>${serviceName}</serviceName>
284                             <operationType>${operationType}</operationType>
285                             <userId>${userId}</userId>
286                             <result>${result}</result>
287                             <operationContent>${operationContent}</operationContent>
288                             <progress>${progress}</progress>
289                             <reason>${reason}</reason>
290                         </ns:updateServiceOperationStatus>
291                     </soapenv:Body>
292                 </soapenv:Envelope>"""
293
294             payload = utils.formatXml(payload)
295             execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
296             utils.log("INFO", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
297             utils.logAudit("Scale network service updateServiceOperStatusRequest Request: " + payload)
298
299         }catch(Exception e){
300             utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
301             execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
302         }
303         utils.log("INFO", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
304     }
305 }