/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * Copyright (C) 2018 CMCC. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.so.bpmn.infrastructure.scripts import static org.apache.commons.lang3.StringUtils.* import org.camunda.bpm.engine.delegate.BpmnError import org.camunda.bpm.engine.delegate.DelegateExecution import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor import org.onap.so.bpmn.common.scripts.ExceptionUtil import org.onap.so.bpmn.common.scripts.MsoUtils import org.onap.so.bpmn.core.WorkflowException import org.onap.so.bpmn.core.json.JsonUtils import org.onap.so.bpmn.core.UrnPropertiesReader import org.onap.so.logger.MessageEnum import org.onap.so.logger.MsoLogger import org.onap.so.utils.UUIDChecker import org.springframework.web.util.UriUtils /** * This groovy class supports the ScaleCustomE2EServiceInstance.bpmn process. * */ public class ScaleCustomE2EServiceInstance extends AbstractServiceTaskProcessor { private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ScaleCustomE2EServiceInstance.class); String Prefix = "CRESI_" ExceptionUtil exceptionUtil = new ExceptionUtil() JsonUtils jsonUtil = new JsonUtils() public void preProcessRequest(DelegateExecution execution) { execution.setVariable("prefix", Prefix) String msg = "" msoLogger.trace("preProcessRequest() ") try { String siRequest = execution.getVariable("bpmnRequest") msoLogger.debug(siRequest) String requestId = execution.getVariable("mso-request-id") execution.setVariable("msoRequestId", requestId) msoLogger.debug("Input Request:" + siRequest + " reqId:" + requestId) String serviceInstanceId = execution.getVariable("serviceInstanceId") if (isBlank(serviceInstanceId)) { serviceInstanceId = "NULL".toString() } msoLogger.debug("Generated new Service Instance:" + serviceInstanceId) serviceInstanceId = UriUtils.encode(serviceInstanceId, "UTF-8") execution.setVariable("serviceInstanceId", serviceInstanceId) // service instance ID is also service ID execution.setVariable("serviceId", serviceInstanceId) // service instance name String serviceInstanceName = jsonUtil.getJsonValue(siRequest, "service.serviceInstanceName") execution.setVariable("serviceInstanceName", serviceInstanceName) // service instance name String serviceType = jsonUtil.getJsonValue(siRequest, "service.serviceType") execution.setVariable("serviceType", serviceType) // operationa ID (key) //String operationKey = UUIDChecker.generateUUID(msoLogger) String operationId = jsonUtil.getJsonValue(siRequest, "operationId") execution.setVariable("operationId", operationId) msoLogger.debug("Input Request:" + siRequest + " operationId:" + operationId) String resources = jsonUtil.getJsonValue(siRequest, "service.resources") execution.setVariable("resources", resources) // node template UUID String nodeTemplateUUID = UUIDChecker.generateUUID(msoLogger) execution.setVariable("nodeTemplateUUID", nodeTemplateUUID) //subscriberInfo String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "service.globalSubscriberId") if (isBlank(globalSubscriberId)) { msg = "Input globalSubscriberId' is null" exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg) } else { execution.setVariable("globalSubscriberId", globalSubscriberId) } String requestDescription = "request description for test" execution.setVariable("requestDescription", requestDescription) execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter") } catch (BpmnError e) { throw e; } catch (Exception ex) { msg = "Exception in preProcessRequest " + ex.getMessage() msoLogger.debug(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg) } msoLogger.trace("Exit preProcessRequest ") } public void sendSyncResponse(DelegateExecution execution) { msoLogger.trace("sendSyncResponse ") try { String operationId = execution.getVariable("operationId") String serviceInstanceId = execution.getVariable("serviceInstanceId") // RESTResponse for API Handler (APIH) Reply Task String scaleServiceRestRequest = """{"operationId":"${operationId}"}""".trim() msoLogger.debug(" sendSyncResponse to APIH:" + "\n" + scaleServiceRestRequest) sendWorkflowResponse(execution, 202, scaleServiceRestRequest) execution.setVariable("sentSyncResponse", true) } catch (Exception ex) { String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage() msoLogger.debug(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg) } msoLogger.trace("Exit sendSyncResopnse ") } public void sendSyncError(DelegateExecution execution) { msoLogger.trace("sendSyncError ") try { String errorMessage = "" if (execution.getVariable("WorkflowException") instanceof WorkflowException) { WorkflowException wfe = execution.getVariable("WorkflowException") errorMessage = wfe.getErrorMessage() } else { errorMessage = "Sending Sync Error." } String buildworkflowException = """ ${MsoUtils.xmlEscape(errorMessage)} 7000 """ msoLogger.debug(buildworkflowException) sendWorkflowResponse(execution, 500, buildworkflowException) } catch (Exception ex) { msoLogger.debug(" Sending Sync Error Activity Failed. " + "\n" + ex.getMessage()) } } public void prepareCompletionRequest(DelegateExecution execution) { msoLogger.trace("prepareCompletion ") try { String requestId = execution.getVariable("msoRequestId") String serviceInstanceId = execution.getVariable("serviceInstanceId") String source = execution.getVariable("source") String msoCompletionRequest = """ ${MsoUtils.xmlEscape(requestId)} SCALE ${MsoUtils.xmlEscape(source)} Service Instance was scaled successfully. ${MsoUtils.xmlEscape(serviceInstanceId)} ScaleGenericALaCarteServiceInstance """ // Format Response String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest) execution.setVariable("CompleteMsoProcessRequest", xmlMsoCompletionRequest) msoLogger.debug(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest) } catch (Exception ex) { String msg = " Exception in prepareCompletion:" + ex.getMessage() msoLogger.debug(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg) } msoLogger.trace("Exit prepareCompletionRequest ") } public void prepareFalloutRequest(DelegateExecution execution) { msoLogger.trace("prepareFalloutRequest ") try { WorkflowException wfex = execution.getVariable("WorkflowException") msoLogger.debug(" Input Workflow Exception: " + wfex.toString()) String requestId = execution.getVariable("msoRequestId") String source = execution.getVariable("source") String requestInfo = """ ${MsoUtils.xmlEscape(requestId)} SCALE ${MsoUtils.xmlEscape(source)} """ String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo) execution.setVariable("falloutRequest", falloutRequest) } catch (Exception ex) { msoLogger.debug("Exception prepareFalloutRequest:" + ex.getMessage()) String errorException = " Bpmn error encountered in ScaleGenericALaCarteServiceInstance flow. FalloutHandlerRequest, buildErrorResponse() - " + ex.getMessage() String requestId = execution.getVariable("msoRequestId") String falloutRequest = """ ${MsoUtils.xmlEscape(requestId)} SCALE UUI ${MsoUtils.xmlEscape(errorException)} 7000 """ execution.setVariable("falloutRequest", falloutRequest) } msoLogger.trace("Exit prepareFalloutRequest ") } /** * Init the service Operation Status */ public void prepareInitServiceOperationStatus(DelegateExecution execution){ msoLogger.trace("STARTED prepareInitServiceOperationStatus Process ") try{ String serviceId = execution.getVariable("serviceInstanceId") //String serviceName = execution.getVariable("serviceInstanceName") String operationId = execution.getVariable("operationId") String operationType = "SCALE" String userId = "" String result = "processing" String progress = "0" String reason = "" String operationContent = "Prepare service scaling" execution.setVariable("serviceInstanceId", serviceId) execution.setVariable("operationId", operationId) execution.setVariable("operationType", operationType) def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution) execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint) msoLogger.info("DB Adapter Endpoint is: " + dbAdapterEndpoint) String payload = """ ${MsoUtils.xmlEscape(serviceId)} ${MsoUtils.xmlEscape(operationId)} ${MsoUtils.xmlEscape(serviceName)} ${MsoUtils.xmlEscape(operationType)} ${MsoUtils.xmlEscape(userId)} ${MsoUtils.xmlEscape(result)} ${MsoUtils.xmlEscape(operationContent)} ${MsoUtils.xmlEscape(progress)} ${MsoUtils.xmlEscape(reason)} """ payload = utils.formatXml(payload) execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload) msoLogger.info("Outgoing updateServiceOperStatusRequest: \n" + payload) msoLogger.debug("Scale network service updateServiceOperStatusRequest Request: " + payload) }catch(Exception e){ msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing prepareInitServiceOperationStatus.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e); execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage()) } msoLogger.trace("COMPLETED prepareInitServiceOperationStatus Process ") } }