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