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