diff options
Diffstat (limited to 'bpmn/MSOGammaBPMN/src/main/java/org')
14 files changed, 2649 insertions, 0 deletions
diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/MSOGammaApplication.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/MSOGammaApplication.java new file mode 100644 index 0000000000..4dadd8c127 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/MSOGammaApplication.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma; + +import java.util.List; + +import org.camunda.bpm.application.PostDeploy; +import org.camunda.bpm.application.PreUndeploy; +import org.camunda.bpm.application.ProcessApplication; +import org.camunda.bpm.application.ProcessApplicationInfo; +import org.camunda.bpm.application.impl.ServletProcessApplication; +import org.camunda.bpm.engine.ProcessEngine; + +import org.openecomp.mso.logger.MsoLogger; + +/** + * @since Version 1.0 + * + */ +@ProcessApplication("MSO Gamma Application") +public class MSOGammaApplication extends ServletProcessApplication { + + private MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + + @PostDeploy + public void postDeploy(ProcessEngine processEngineInstance) { + long startTime = System.currentTimeMillis(); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Post deployment complete..."); + } + + @PreUndeploy + public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo, List<ProcessEngine> processEngines) { + long startTime = System.currentTimeMillis(); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Pre Undeploy complete..."); + + } + +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/SDNCAdapterCallbackServiceImpl.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/SDNCAdapterCallbackServiceImpl.java new file mode 100644 index 0000000000..4b3b8eb36e --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/SDNCAdapterCallbackServiceImpl.java @@ -0,0 +1,255 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.HashMap; +import java.util.Map; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebResult; +import javax.jws.WebService; +import javax.ws.rs.core.Context; +import javax.xml.ws.WebServiceContext; + +import org.camunda.bpm.BpmPlatform; +import org.camunda.bpm.engine.MismatchingMessageCorrelationException; +import org.camunda.bpm.engine.ProcessEngineServices; +import org.camunda.bpm.engine.RuntimeService; + +import com.att.domain2.workflow.sdnc.adapter.callback.wsdl.v1.SDNCCallbackAdapterPortType; +import com.att.domain2.workflow.sdnc.adapter.schema.v1.SDNCAdapterCallbackRequest; +import com.att.domain2.workflow.sdnc.adapter.schema.v1.SDNCAdapterResponse; +import org.openecomp.mso.bpmn.core.PropertyConfiguration; +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; +/** + * @version 1.0 + * + */ +@WebService(serviceName="SDNCAdapterCallbackService", targetNamespace="http://domain2.att.com/workflow/sdnc/adapter/schema/v1") +public class SDNCAdapterCallbackServiceImpl implements SDNCCallbackAdapterPortType { + + private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + private final int DEFAULT_RETRY_ATTEMPTS = 60; + private final int DEFAULT_SLEEP_TIME = 500; + + private final String logMarker = "[SDNC-CALLBACK]"; + + @Context WebServiceContext wsContext; + + private volatile ProcessEngineServices pes4junit = null; + + @WebMethod(operationName = "SDNCAdapterCallback") + @WebResult(name = "SDNCAdapterResponse", targetNamespace = "http://domain2.att.com/workflow/sdnc/adapter/schema/v1", partName = "SDNCAdapterCallbackResponse") + public SDNCAdapterResponse sdncAdapterCallback( + @WebParam(name = "SDNCAdapterCallbackRequest", targetNamespace = "http://domain2.att.com/workflow/sdnc/adapter/schema/v1", partName = "SDNCAdapterCallbackRequest") + SDNCAdapterCallbackRequest sdncAdapterCallbackRequest) { + + //Callback URL to use http://localhost:8080/mso/SDNCAdapterCallbackService + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + String receivedRequestId = sdncAdapterCallbackRequest.getCallbackHeader().getRequestId(); + msoLogger.setServiceName("MSO." + "sdncAdapter"); + msoLogger.setLogContext(receivedRequestId, "N/A"); + msoLogger.debug(logMarker + "Received callback response:" + sdncAdapterCallbackRequest.toString()); + SDNCAdapterResponse sdncAdapterResponse; + long startTime = System.currentTimeMillis(); + + /* Check to make sure the process instance is reay for correlation*/ + isReadyforCorrelation(runtimeService, receivedRequestId); + + msoLogger.debug(logMarker + "*** Received MSO sdncAdapterCallbackService ******"); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO sdncAdapterCallbackService"); + + msoLogger.debug(logMarker + "Callback response string:\n" + sdncAdapterCallbackRequest.toString()); + + String reqId = receivedRequestId; + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("SDNCA_requestId", reqId ); + variables.put("sdncAdapterCallbackRequest", sdncAdapterCallbackRequest.toString()); + + /*Correlating the response with the running instance*/ + + // NOTE: the following loop is a workaround for problems we've had + // with reliability of the runtime service. It seems that queries + // sometimes return results, and sometimes they don't. This might + // be a problem in mysql only. We aren't sure if it affects camunda + // on oracle or mariadb. The workaround is to repeat the request + // a number of times until it succeeds. If it doesn't succeed after + // 60 tries, then we give up. + + int maxAttempts = DEFAULT_RETRY_ATTEMPTS; + int attempt = 1; + int sleepTime = DEFAULT_SLEEP_TIME; + + Map<String,String> bpmnProperties = getMSOBPMNURNProperties(); + if (bpmnProperties != null) { + try { + maxAttempts = Integer.parseInt(bpmnProperties.get("mso.callbackRetryAttempts")); + msoLogger.debug(logMarker + "mso.callbackRetryAttempts=" + maxAttempts); + sleepTime = Integer.parseInt(bpmnProperties.get("mso.callbackRetrySleepTime")); + msoLogger.debug(logMarker + "mso.callbackRetrySleepTime:" + sleepTime); + } catch (Exception ex) { + + msoLogger.info (MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", logMarker + + "Error parsing mso.callbackRetrySleepTime/mso.callbackRetryAttempts:" + + sleepTime + ":" + + maxAttempts); + + msoLogger.error (MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logMarker + + "Error parsing mso.callbackRetrySleepTime/mso.callbackRetryAttempts:" + + sleepTime + ":" + + maxAttempts); + + } + } + + while (true) { + try { + // sdncAdapterCallbackRequest is the message event name (defined in the bpmn process) + runtimeService.createMessageCorrelation("sdncAdapterCallbackRequest") + .setVariables(variables) + .processInstanceVariableEquals("SDNCA_requestId", reqId).correlate(); + sdncAdapterResponse = new SDNCAdapterResponse(); + msoLogger.debug(logMarker + "***** Completed processing of MSO sdncAdapterCallbackService ******"); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logMarker + + "Completed the execution of MSO SDNCAdapterCallbackService."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO SDNCAdapterCallbackService.", "BPMN", + MsoLogger.getServiceName(), "sdncAdapterCallback"); + + return sdncAdapterResponse; + } catch(MismatchingMessageCorrelationException e) { + msoLogger.debug(logMarker + "[CORM]correlation id mismatch (attempt " + attempt + "/" + maxAttempts + ")"); + if (attempt == maxAttempts) { + // Couldn't correlate requestId to any active flow + //MsoLogger logger = MsoLogger.getMsoLogger("SDNCAdapterCallbackService"); + String msg = + "SDNC Adapter Callback Service received a SDNC Adapter Callback Request with RequestId '" + + receivedRequestId + + "' but that RequestId could not be correlated to any active process - ignoring the Request"; + sdncAdapterResponse = new SDNCAdapterExceptionResponse(e); + + msoLogger.error (MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logMarker + + "Completed the execution of MSO SDNCAdapterCallbackService." ); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO SDNCAdapterCallbackService.", "BPMN", + MsoLogger.getServiceName(), "sdncAdapterCallback"); + + return sdncAdapterResponse; + } + + try { + Thread.sleep(sleepTime); + } catch (InterruptedException e2) { + String msg = + "SDNC Adapter Callback Service received a SDNC Adapter Callback Request with RequestId '" + + receivedRequestId + + "' but correlation was interrupted"; + sdncAdapterResponse = new SDNCAdapterExceptionResponse(e); + + msoLogger.error (MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logMarker + + "Completed the execution of MSO SDNCAdapterCallbackService."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO SDNCAdapterCallbackService.", "BPMN", + MsoLogger.getServiceName(), "sdncAdapterCallback"); + + return sdncAdapterResponse; + } + } + + attempt++; + } + } + + + private Map<String,String> getMSOBPMNURNProperties() { + PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance(); + Map<String,String> props = propertyConfiguration.getProperties("mso.bpmn.urn.properties"); + return props; + } + + private void isReadyforCorrelation(RuntimeService runtimeService, + String receivedRequestId) { + long waitingInstances = runtimeService.createExecutionQuery() // + .messageEventSubscriptionName("sdncAdapterCallbackRequest") + .processVariableValueEquals("SDNCA_requestId", receivedRequestId).count(); + //Workaround for performance testing, explicit wait for a second for the transactions to be committed + try { + Thread.sleep(1000); + } catch (InterruptedException e1) { + } + + int retries = 50; + while (waitingInstances==0 && retries > 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + + msoLogger.error (MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker, e); + + } // you can still play with the numbers + waitingInstances = runtimeService.createExecutionQuery() // + .messageEventSubscriptionName("sdncAdapterCallbackRequest") + .processVariableValueEquals("SDNCA_requestId", receivedRequestId).count(); + retries--; + } + } + + private ProcessEngineServices getProcessEngineServices() { + if (pes4junit == null) { + return BpmPlatform.getDefaultProcessEngine(); + } else { + return pes4junit; + } + } + + @WebMethod(exclude=true) + public void setProcessEngineServices4junit(ProcessEngineServices pes) { + pes4junit = pes; + } + + public class SDNCAdapterExceptionResponse extends SDNCAdapterResponse { + private Exception ex; + + public SDNCAdapterExceptionResponse(Exception ex) { + super(); + this.ex = ex; + } + + public Exception getException() { + return ex; + } + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterNotifyServiceImpl.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterNotifyServiceImpl.java new file mode 100644 index 0000000000..509f9c67f3 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterNotifyServiceImpl.java @@ -0,0 +1,537 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.HashMap; +import java.util.Map; + +import javax.jws.Oneway; +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.ws.rs.core.Context; +import javax.xml.ws.Action; +import javax.xml.ws.RequestWrapper; +import javax.xml.ws.WebServiceContext; + +import org.camunda.bpm.BpmPlatform; +import org.camunda.bpm.engine.MismatchingMessageCorrelationException; +import org.camunda.bpm.engine.ProcessEngineServices; +import org.camunda.bpm.engine.RuntimeService; + +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.CreateVnfNotification; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.DeleteVnfNotification; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.MsoExceptionCategory; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.QueryVnfNotification; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.RollbackVnfNotification; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.UpdateVnfNotification; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.VnfAdapterNotify; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.VnfRollback; +import com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.VnfStatus; +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; + +/** + * This is the service class for VnfAdapterNotify + * TODO: Add addition VnfAdapterNotify Methods for remaining VnfAdapterNotify operations. + */ + +@WebService(serviceName = "vnfAdapterNotify", targetNamespace = "http://com.att.mso/vnfNotify") +public class VnfAdapterNotifyServiceImpl implements VnfAdapterNotify{ + + private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + + private final String logMarker = "[VNF-NOTIFY]"; + + @Context WebServiceContext wsContext; + + private volatile ProcessEngineServices pes4junit = null; + + /** + * + * @param errorMessage + * @param exception + * @param messageId + * @param completed + */ + @WebMethod(operationName = "rollbackVnfNotification") + @Oneway + @RequestWrapper(localName = "rollbackVnfNotification", targetNamespace = "http://com.att.mso/vnfNotify", className = "org.openecomp.mso.adapters.vnf.async.client.RollbackVnfNotification") + @Action(input = "http://com.att.mso/notify/adapterNotify/rollbackVnfNotificationRequest") + public void rollbackVnfNotification( + @WebParam(name = "messageId", targetNamespace = "") + String messageId, + @WebParam(name = "completed", targetNamespace = "") + boolean completed, + @WebParam(name = "exception", targetNamespace = "") + MsoExceptionCategory exception, + @WebParam(name = "errorMessage", targetNamespace = "") + String errorMessage) { + + + + RollbackVnfNotification rollbackVnfNotification = new RollbackVnfNotification(); + + rollbackVnfNotification.setMessageId(messageId); + rollbackVnfNotification.setCompleted(completed); + rollbackVnfNotification.setException(exception); + rollbackVnfNotification.setErrorMessage(errorMessage); + + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + MsoLogger.setServiceName("MSO." + "vnfAdapterRollback"); + MsoLogger.setLogContext(messageId, "N/A"); + msoLogger.debug(logMarker + "Received RollbackVnfNotification" + rollbackVnfNotification.toString()); + + long startTime = System.currentTimeMillis(); + try { + + /* Check to make sure the process instance is ready for correlation*/ + isReadyforCorrelation(runtimeService, messageId, "rollbackVnfNotificationCallback", "VNFRB_messageId"); + + msoLogger.debug(logMarker + "*** Received MSO rollbackVnfNotification Callback ******"); + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO VnfAdapterNotifyService "); + msoLogger.debug(logMarker + "Rollback VNF Notification string:\n" + rollbackVnfNotification.toString()); + + System.out.println("testing ROllbackVnfNotification : " + rollbackVnfNotification.toString()); + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFRB_messageId", messageId ); + variables.put("rollbackVnfNotificationCallback", rollbackVnfNotification.toString()); + + /*Correlating the response with the running instance*/ + + runtimeService.createMessageCorrelation("rollbackVnfNotificationCallback").setVariables(variables) + .processInstanceVariableEquals("VNFRB_messageId", messageId).correlate(); + + msoLogger.debug(logMarker + "***** Completed processing of MSO VnfAdapterNotifyService ******"); + } catch(MismatchingMessageCorrelationException e) { + msoLogger.debug(logMarker + "[CORM]correlation id mismatch"); + String msg = + "VNF Adapter Notify Service received a Create VNF Notification request with RequestId '" + + messageId + + "' but that RequestId could not be correlated to any active process - ignoring the request"; + + msoLogger.error (MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg); + + } + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Rollback VNF Notification."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Rollback VNF Notification.", "BPMN", + MsoLogger.getServiceName(), "rollbackVnfNotification"); + + return; + } + + + + /** + * + * @param errorMessage + * @param vnfExists + * @param status + * @param exception + * @param outputs + * @param messageId + * @param vnfId + * @param completed + */ + @WebMethod(operationName = "queryVnfNotification") + @Oneway + @RequestWrapper(localName = "queryVnfNotification", targetNamespace = "http://com.att.mso/vnfNotify", className = "org.openecomp.mso.adapters.vnf.async.client.QueryVnfNotification") + @Action(input = "http://com.att.mso/notify/adapterNotify/queryVnfNotificationRequest") + public void queryVnfNotification( + @WebParam(name = "messageId", targetNamespace = "") + String messageId, + @WebParam(name = "completed", targetNamespace = "") + boolean completed, + @WebParam(name = "exception", targetNamespace = "") + MsoExceptionCategory exception, + @WebParam(name = "errorMessage", targetNamespace = "") + String errorMessage, + @WebParam(name = "vnfExists", targetNamespace = "") + Boolean vnfExists, + @WebParam(name = "vnfId", targetNamespace = "") + String vnfId, + @WebParam(name = "status", targetNamespace = "") + VnfStatus status, + @WebParam(name = "outputs", targetNamespace = "") + com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.QueryVnfNotification.Outputs outputs){ + + QueryVnfNotification queryVnfNotification = new QueryVnfNotification(); + + queryVnfNotification.setMessageId(messageId); + queryVnfNotification.setCompleted(completed); + queryVnfNotification.setException(exception); + queryVnfNotification.setErrorMessage(errorMessage); + queryVnfNotification.setVnfExists(vnfExists); + queryVnfNotification.setVnfId(vnfId); + queryVnfNotification.setStatus(status); + queryVnfNotification.setOutputs(outputs); + + + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + msoLogger.setServiceName("MSO." + "vnf Adapter Query"); + msoLogger.setLogContext(messageId, "N/A"); + msoLogger.debug(logMarker + "Received QueryVnfNotification" + queryVnfNotification.toString()); + + System.out.println("Received QueryVnfNotification : " + queryVnfNotification.toString()); + + long startTime = System.currentTimeMillis(); + try { + + /* Check to make sure the process instance is ready for correlation*/ + isReadyforCorrelation(runtimeService, messageId, "queryVnfNotificationCallback", "VNFQ_messageId"); + + msoLogger.debug(logMarker + "*** Received MSO queryVnfNotification Callback ******"); + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO VnfAdapterNotifyService "); + msoLogger.debug(logMarker + "Query VNF Notification string:\n" + queryVnfNotification.toString()); + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFQ_messageId", messageId ); + variables.put("queryVnfNotificationCallback", queryVnfNotification.toString()); + + /*Correlating the response with the running instance*/ + + runtimeService.createMessageCorrelation("queryVnfNotificationCallback").setVariables(variables) + .processInstanceVariableEquals("VNFQ_messageId", messageId).correlate(); + + msoLogger.debug(logMarker + "***** Completed processing of MSO VnfAdapterNotifyService ******"); + } catch(MismatchingMessageCorrelationException e) { + msoLogger.debug(logMarker + "[CORM]correlation id mismatch"); + String msg = + "VNF Adapter Notify Service received a Query VNF Notification request with RequestId '" + + messageId + + "' but that RequestId could not be correlated to any active process - ignoring the request"; + + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + } + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Query VNF Notification."); + + return; + } + + + + + /** + * + * @param errorMessage + * @param exception + * @param rollback + * @param outputs + * @param messageId + * @param vnfId + * @param completed + */ + @WebMethod(operationName = "createVnfNotification") + @Oneway + @RequestWrapper(localName = "createVnfNotification", targetNamespace = "http://com.att.mso/vnfNotify", className = "org.openecomp.mso.adapters.vnf.async.client.CreateVnfNotification") + @Action(input = "http://com.att.mso/notify/adapterNotify/createVnfNotificationRequest") + public void createVnfNotification( + @WebParam(name = "messageId", targetNamespace = "") + String messageId, + @WebParam(name = "completed", targetNamespace = "") + boolean completed, + @WebParam(name = "exception", targetNamespace = "") + MsoExceptionCategory exception, + @WebParam(name = "errorMessage", targetNamespace = "") + String errorMessage, + @WebParam(name = "vnfId", targetNamespace = "") + String vnfId, + @WebParam(name = "outputs", targetNamespace = "") + com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.CreateVnfNotification.Outputs outputs, + @WebParam(name = "rollback", targetNamespace = "") + VnfRollback rollback){ + + CreateVnfNotification createVnfNotification = new CreateVnfNotification(); + + createVnfNotification.setMessageId(messageId); + createVnfNotification.setCompleted(completed); + createVnfNotification.setException(exception); + createVnfNotification.setErrorMessage(errorMessage); + createVnfNotification.setVnfId(vnfId); + createVnfNotification.setOutputs(outputs); + createVnfNotification.setRollback(rollback); + + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + msoLogger.setServiceName("MSO." + "vnf Adapter Create"); + msoLogger.setLogContext(messageId, "N/A"); + msoLogger.debug(logMarker + "Received CreateVnfNotification - " + createVnfNotification.toString()); + + long startTime = System.currentTimeMillis(); + try { + + /* Check to make sure the process instance is ready for correlation*/ + isReadyforCorrelation(runtimeService, messageId, "createVnfNotificationCallback", "VNFC_messageId"); + + msoLogger.debug(logMarker + "*** Received MSO createVnfNotification Callback ******"); + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO VnfAdapterNotifyService "); + + msoLogger.debug(logMarker + "Create VNF Notification string:\n" + createVnfNotification.toString()); + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFC_messageId", messageId ); + variables.put("createVnfNotificationCallback", createVnfNotification.toString()); + + /*Correlating the response with the running instance*/ + + runtimeService.createMessageCorrelation("createVnfNotificationCallback").setVariables(variables) + .processInstanceVariableEquals("VNFC_messageId", messageId).correlate(); + + msoLogger.debug(logMarker + "***** Completed processing of MSO VnfAdapterNotifyService ******"); + } catch(MismatchingMessageCorrelationException e) { + msoLogger.debug(logMarker + "[CORM]correlation id mismatch"); + String msg = + "VNF Adapter Notify Service received a Create VNF Notification request with RequestId '" + + messageId + + "' but that RequestId could not be correlated to any active process - ignoring the request"; + + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + + } + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Query VNF Notification."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Query VNF Notification.", "BPMN", + MsoLogger.getServiceName(), "createVnfNotification"); + + return; + } + + /** + * + * @param errorMessage + * @param exception + * @param rollback + * @param outputs + * @param messageId + * @param completed + */ + @WebMethod(operationName = "updateVnfNotification") + @Oneway + @RequestWrapper(localName = "updateVnfNotification", targetNamespace = "http://com.att.mso/vnfNotify", className = "org.openecomp.mso.adapters.vnf.async.client.UpdateVnfNotification") + @Action(input = "http://com.att.mso/notify/adapterNotify/updateVnfNotificationRequest") + public void updateVnfNotification( + @WebParam(name = "messageId", targetNamespace = "") + String messageId, + @WebParam(name = "completed", targetNamespace = "") + boolean completed, + @WebParam(name = "exception", targetNamespace = "") + MsoExceptionCategory exception, + @WebParam(name = "errorMessage", targetNamespace = "") + String errorMessage, + @WebParam(name = "outputs", targetNamespace = "") + com.att.domain2.workflow.vnf.async.adapter.callback.wsdl.v1.UpdateVnfNotification.Outputs outputs, + @WebParam(name = "rollback", targetNamespace = "") + VnfRollback rollback){ + + UpdateVnfNotification updateVnfNotification = new UpdateVnfNotification(); + + updateVnfNotification.setMessageId(messageId); + updateVnfNotification.setCompleted(completed); + updateVnfNotification.setException(exception); + updateVnfNotification.setErrorMessage(errorMessage); + updateVnfNotification.setOutputs(outputs); + updateVnfNotification.setRollback(rollback); + + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + msoLogger.setServiceName("MSO." + "vnf Adapter Update"); + msoLogger.setLogContext(messageId, "N/A"); + msoLogger.debug(logMarker + "Received UpdateVnfNotification - " + updateVnfNotification.toString()); + + long startTime = System.currentTimeMillis(); + try { + + // Check to make sure the process instance is ready for correlation + isReadyforCorrelation(runtimeService, messageId, "updateVnfNotificationCallback", "VNFU_messageId"); + + msoLogger.debug(logMarker + "*** Received MSO updateVnfNotification Callback ******"); + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO VnfAdapterNotifyService "); + + msoLogger.debug(logMarker + "Update VNF Notification string:\n" + updateVnfNotification.toString()); + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFU_messageId", messageId ); + variables.put("updateVnfNotificationCallback", updateVnfNotification.toString()); + + //Correlating the response with the running instance + runtimeService.createMessageCorrelation("updateVnfNotificationCallback").setVariables(variables) + .processInstanceVariableEquals("VNFU_messageId", messageId).correlate(); + + msoLogger.debug(logMarker + "***** Completed processing of MSO VnfAdapterNotifyService ******"); + + } catch(MismatchingMessageCorrelationException e) { + msoLogger.debug(logMarker + "[CORM]correlation id mismatch"); + String msg = + "VNF Adapter Notify Service received a Update VNF Notification request with RequestId '" + + messageId + + "' but that RequestId could not be correlated to any active process - ignoring the request"; + + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + + } + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Update VNF Notification."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO Vnf Adapter Notify for Update VNF Notification.", "BPMN", + MsoLogger.getServiceName(), "updateVnfNotification"); + + return; + } + + /** + * + * @param errorMessage + * @param exception + * @param messageId + * @param completed + */ + + //@WebService(serviceName="VNFAdapterDeleteCallbackV1", targetNamespace="http://com.att.mso/vnfNotify") + @WebMethod(operationName = "deleteVnfNotification") + @Oneway + @RequestWrapper(localName = "deleteVnfNotification", targetNamespace = "http://com.att.mso/vnfNotify", className = "org.openecomp.mso.adapters.vnf.async.client.DeleteVnfNotification") + @Action(input = "http://com.att.mso/notify/adapterNotify/deleteVnfNotificationRequest") + public void deleteVnfNotification( + @WebParam(name = "messageId", targetNamespace = "") + String messageId, + @WebParam(name = "completed", targetNamespace = "") + boolean completed, + @WebParam(name = "exception", targetNamespace = "") + MsoExceptionCategory exception, + @WebParam(name = "errorMessage", targetNamespace = "") + String errorMessage) { + + //Callback URL to use http://localhost:8080/mso/services/VNFAdapterDeleteCallbackV1 + + //DeleteVnfNotification Class + DeleteVnfNotification deleteVnfNotification = new DeleteVnfNotification(); + deleteVnfNotification.setMessageId(messageId); + deleteVnfNotification.setCompleted(completed); + deleteVnfNotification.setException(exception); + deleteVnfNotification.setErrorMessage(errorMessage); + + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + MsoLogger.setServiceName("MSO." + "vnfAdapterDelete"); + MsoLogger.setLogContext(messageId, "N/A"); + msoLogger.debug(logMarker + "Received DeleteVnfNotification callback: " + deleteVnfNotification.toString()); + + long startTime = System.currentTimeMillis(); + try { + + /* Check to make sure the process instance is ready for correlation*/ + //isReadyforCorrelation(runtimeService, messageId, "deleteVnfACallback", "VNFDEL_uuid"); + + msoLogger.debug(logMarker + " *** Received MSO deleteVnfACallback ******"); + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO deleteVnfACallback "); + msoLogger.debug(logMarker + " Callback response string:\n" + deleteVnfNotification.toString()); + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFDEL_uuid", messageId); + variables.put("deleteVnfACallback", deleteVnfNotification.toString()); + + /*Correlating the response with the running instance*/ + + runtimeService.createMessageCorrelation("deleteVnfACallback") + .setVariables(variables) + .processInstanceVariableEquals("VNFDEL_uuid", messageId).correlate(); + + msoLogger.debug(logMarker + "***** Completed processing of MSO deleteVnfACallback ******"); + + } catch(MismatchingMessageCorrelationException e) { + + msoLogger.debug(logMarker + " [CORM]correlation id mismatch"); + // Couldn't correlate requestId to any active flow + //MsoLogger logger = MsoLogger.getMsoLogger("SDNCAdapterCallbackService"); + + String msg = + "Vnf Adapter Callback Service received a Vnf Adapter Callback with messageId '" + + messageId + + "' but that messageId could not be correlated to any active process - ignoring the Request"; + + msoLogger.error(MessageEnum.BPMN_SDNC_CALLBACK_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, logMarker + ":" + msg, e); + + } + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO VNFAdapterDeleteCallbackV1."); + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + "Completed the execution of MSO VNFAdapterDeleteCallbackV1.", "BPMN", + MsoLogger.getServiceName(), "deleteVnfNotification"); + + return; + } + + private void isReadyforCorrelation(RuntimeService runtimeService, String requestId, String responseName, String correlationValue) { + + long waitingInstances = runtimeService.createExecutionQuery().messageEventSubscriptionName(responseName).processVariableValueEquals(correlationValue, requestId).count(); + int retries = 50; + while (waitingInstances==0 && retries > 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + // should I add new exception Message to MessageEnum??? + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logMarker, e); + + } // you can still play with the numbers + waitingInstances = runtimeService.createExecutionQuery() // + .messageEventSubscriptionName(responseName) + .processVariableValueEquals(correlationValue, requestId).count(); + retries--; + } + } + + + private ProcessEngineServices getProcessEngineServices() { + if (pes4junit == null) { + return BpmPlatform.getDefaultProcessEngine(); + } else { + return pes4junit; + } + } + + @WebMethod(exclude=true) + public void setProcessEngineServices4junit(ProcessEngineServices pes) { + pes4junit = pes; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterRestNotifyResource.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterRestNotifyResource.java new file mode 100644 index 0000000000..5199322931 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/VnfAdapterRestNotifyResource.java @@ -0,0 +1,206 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.camunda.bpm.BpmPlatform; +import org.camunda.bpm.engine.MismatchingMessageCorrelationException; +import org.camunda.bpm.engine.ProcessEngineServices; +import org.camunda.bpm.engine.RuntimeService; +import org.slf4j.MDC; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; + +/** + * Listens for REST notifications from the VNF Adapter and injects each one + * into a waiting BPMN processes. + */ +@Path("/vnfAdapterRestNotify") +public class VnfAdapterRestNotifyResource { + private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + private static final String LOGMARKER = "[VNF-REST-NOTIFY]"; + + private ProcessEngineServices pes4junit = null; + + @POST + @Path("/") + @Consumes(MediaType.APPLICATION_XML) + @Produces(MediaType.TEXT_PLAIN) + public Response notify(String content) { + LOGGER.debug(LOGMARKER + " Received VNF Adapter REST Notification:" + + System.lineSeparator() + content); + + String messageId = null; + long startTime = System.currentTimeMillis(); + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource source = new InputSource(new StringReader(content)); + Document doc = builder.parse(source); + doc.normalize(); + + Element rootElement = doc.getDocumentElement(); + NodeList childList = rootElement.getChildNodes(); + + for (int i = 0; i < childList.getLength(); i++) { + Node childNode = childList.item(i); + if (childNode.getNodeType() == Node.ELEMENT_NODE) { + Element childElement = (Element) childNode; + + String childElementName = childElement.getLocalName(); + if (childElementName == null) { + childElementName = childElement.getNodeName(); + } + + if ("messageId".equals(childElementName)) { + messageId = childElement.getTextContent(); + } + } + } + } catch (Exception e) { + String msg = "Failed to parse VNF Adapter REST Notification: " + e; + LOGGER.debug(LOGMARKER + " " + msg); + LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, LOGMARKER + ":" + msg, e); + + return Response.status(400).entity(e).build(); + } + + if (messageId == null || messageId.isEmpty()) { + String msg = "No messageId in VNF Adapter REST Notification"; + LOGGER.debug(LOGMARKER + " " + msg); + LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, LOGMARKER + ":" + msg); + + return Response.status(400).entity(msg).build(); + } + + MsoLogger.setServiceName("MSO." + "vnfAdapterRestNotify"); + MsoLogger.setLogContext(messageId, "N/A"); + + LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO vnfAdapterRestNotify "); + + try { + ProcessEngineServices pes = getProcessEngineServices(); + RuntimeService runtimeService = pes.getRuntimeService(); + + if (!isReadyforCorrelation(runtimeService, "VNFREST_messageId", messageId, "vnfAdapterRestCallbackMessage")) { + String msg = "No process is waiting to receive vnfAdapterRestCallbackMessage with VNFREST_messageId='" + messageId + "'"; + LOGGER.debug(LOGMARKER + " " + msg); + LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, LOGMARKER + ":" + msg); + + LOGGER.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + "Call to MSO vnfAdapterRestNotify ", "BPMN", MsoLogger.getServiceName(), "vnfAdapterRestNotify"); + + LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Call to MSO VnfAdapterNotifyService "); + + + return Response.status(500).entity(msg).build(); + } + + Map<String,Object> variables = new HashMap<String,Object>(); + variables.put("VNFREST_messageId", messageId); + variables.put("VNFREST_callback", content); + + runtimeService.createMessageCorrelation("vnfAdapterRestCallbackMessage").setVariables(variables) + .processInstanceVariableEquals("VNFREST_messageId", messageId).correlate(); + + LOGGER.debug(LOGMARKER + " Completed processing of VNF Adapter REST Notification"); + } catch (MismatchingMessageCorrelationException e) { + LOGGER.debug(LOGMARKER + "[CORM] correlation id mismatch"); + String msg = "vnfAdapterRestNotify received a notification with messageId='" + + messageId + "' but it could not be correlated to any active process - ignoring the request"; + LOGGER.debug(msg); + LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, LOGMARKER, e); + + LOGGER.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.InternalError, + LOGMARKER + "Completed vnfAdapterRestNotify with error ", "BPMN", MsoLogger.getServiceName(), "vnfAdapterRestNotify"); + + LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.InternalError, "Completed vnfAdapterRestNotify with error "); + + return Response.status(500).entity(msg).build(); + } + LOGGER.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + "Completed vnfAdapterRestNotify", "BPMN", MsoLogger.getServiceName(), "vnfAdapterRestNotify"); + + LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Completed vnfAdapterRestNotify"); + + return Response.status(204).build(); + } + + private boolean isReadyforCorrelation(RuntimeService runtimeService, + String correlationVariable, String correlationValue, String messageName) { + long waitingInstances = runtimeService.createExecutionQuery() + .messageEventSubscriptionName(messageName) + .processVariableValueEquals(correlationVariable, correlationValue) + .count(); + + int retries = 50; + while (waitingInstances == 0 && retries > 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, LOGMARKER, e); + + return false; + } + + waitingInstances = runtimeService.createExecutionQuery() + .messageEventSubscriptionName(messageName) + .processVariableValueEquals(correlationVariable, correlationValue) + .count(); + + retries--; + } + + return waitingInstances != 0; + } + + private ProcessEngineServices getProcessEngineServices() { + if (pes4junit == null) { + return BpmPlatform.getDefaultProcessEngine(); + } else { + return pes4junit; + } + } + + public void setProcessEngineServices4junit(ProcessEngineServices pes) { + pes4junit = pes; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowAsyncResource.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowAsyncResource.java new file mode 100644 index 0000000000..2302b76750 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowAsyncResource.java @@ -0,0 +1,302 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +import org.camunda.bpm.engine.ProcessEngineServices; +import org.camunda.bpm.engine.ProcessEngines; +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.impl.core.variable.VariableMapImpl; +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.jboss.resteasy.annotations.Suspend; +import org.jboss.resteasy.spi.AsynchronousResponse; +import org.slf4j.MDC; + +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; + + +/** + * + * @version 1.0 + * Asynchronous Workflow processing using JAX RS RESTeasy implementation + * Both Synchronous and Asynchronous BPMN process can benefit from this implementation since the workflow gets executed in the background + * and the server thread is freed up, server scales better to process more incoming requests + * + * Usage: For synchronous process, when you are ready to send the response invoke the callback to write the response + * For asynchronous process - the activity may send a acknowledgement response and then proceed further on executing the process + */ +@Path("/async") +public class WorkflowAsyncResource { + + private WorkflowContextHolder contextHolder = WorkflowContextHolder.getInstance(); + private ProcessEngineServices pes4junit = null; + + private MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + + private static final String logMarker = "[WRKFLOW-RESOURCE]"; + private static final int DEFAULT_WAIT_TIME = 30000; //default wait time + + /** + * Asynchronous JAX-RS method that starts a process instance. + * @param asyncResponse an object that will receive the asynchronous response + * @param processKey the process key + * @param variableMap input variables to the process + */ + @POST + @Path("/services/{processKey}") + @Produces("application/json") + @Consumes("application/json") + public void startProcessInstanceByKey(final @Suspend(180000) AsynchronousResponse asyncResponse, + @PathParam("processKey") String processKey, VariableMapImpl variableMap) { + + WorkflowResponse response = new WorkflowResponse(); + long startTime = System.currentTimeMillis(); + Map<String, Object> inputVariables = null; + WorkflowContext workflowContext = null; + + try { + inputVariables = getInputVariables(variableMap); + setLogContext(processKey, inputVariables); + + // This variable indicates that the flow was invoked asynchronously + inputVariables.put("isAsyncProcess", "true"); + + workflowContext = new WorkflowContext(processKey, getRequestId(inputVariables), + asyncResponse, getWaitTime(inputVariables)); + + msoLogger.debug("Adding the workflow context into holder: " + + workflowContext.getProcessKey() + ":" + + workflowContext.getRequestId() + ":" + + workflowContext.getTimeout()); + + contextHolder.put(workflowContext); + + ProcessThread processThread = new ProcessThread(processKey, inputVariables); + processThread.start(); + } catch (Exception e) { + setLogContext(processKey, inputVariables); + + if (workflowContext != null) { + contextHolder.remove(workflowContext); + } + + msoLogger.debug(logMarker + "Exception in startProcessInstance by key"); + response.setMessage("Fail" ); + response.setResponse("Error occurred while executing the process: " + e); + response.setMessageCode(500); + recordEvents(processKey, response, startTime); + + msoLogger.error (MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logMarker + + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse()); + + Response errorResponse = Response.serverError().entity(response).build(); + asyncResponse.setResponse(errorResponse); + } + } + + /** + * + * @version 1.0 + * + */ + class ProcessThread extends Thread { + private final String processKey; + private final Map<String,Object> inputVariables; + + public ProcessThread(String processKey, Map<String, Object> inputVariables) { + this.processKey = processKey; + this.inputVariables = inputVariables; + } + + public void run() { + + String processInstanceId = null; + long startTime = System.currentTimeMillis(); + + try { + setLogContext(processKey, inputVariables); + + // Note: this creates a random businessKey if it wasn't specified. + String businessKey = getBusinessKey(inputVariables); + + msoLogger.debug(logMarker + "***Received MSO startProcessInstanceByKey with processKey: " + + processKey + " and variables: " + inputVariables); + + msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logMarker + + "Call to MSO workflow/services in Camunda. Received MSO startProcessInstanceByKey with processKey:" + + processKey + " and variables: " + inputVariables); + + RuntimeService runtimeService = getProcessEngineServices().getRuntimeService(); + ProcessInstance processInstance = runtimeService.startProcessInstanceByKey( + processKey, businessKey, inputVariables); + processInstanceId = processInstance.getId(); + + msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " " + + (processInstance.isEnded() ? "ENDED" : "RUNNING")); + } catch (Exception e) { + + msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError, + logMarker + "Error in starting the process: "+ e.getMessage()); + + WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse(); + callbackResponse.setStatusCode(500); + callbackResponse.setMessage("Fail"); + callbackResponse.setResponse("Error occurred while executing the process: " + e); + + // TODO: is the processInstanceId used by the API handler? I don't think so. + // It may be null here. + WorkflowContextHolder.getInstance().processCallback( + processKey, processInstanceId, + getRequestId(inputVariables), + callbackResponse); + } + } + } + + + /** + * Callback resource which is invoked from BPMN to process to send the workflow response + * + * @param processKey + * @param processInstanceId + * @param requestId + * @param callbackResponse + * @return + */ + @POST + @Path("/services/callback/{processKey}/{processInstanceId}/{requestId}") + @Produces("application/json") + @Consumes("application/json") + public Response processWorkflowCallback( + @PathParam("processKey") String processKey, + @PathParam("processInstanceId") String processInstanceId, + @PathParam("requestId")String requestId, + WorkflowCallbackResponse callbackResponse) { + + msoLogger.debug(logMarker + "Process instance ID:" + processInstanceId + ":" + requestId + ":" + processKey + ":" + isProcessEnded(processInstanceId)); + msoLogger.debug(logMarker + "About to process the callback request:" + callbackResponse.getResponse() + ":" + callbackResponse.getMessage() + ":" + callbackResponse.getStatusCode()); + return contextHolder.processCallback(processKey, processInstanceId, requestId, callbackResponse); + } + + // Note: the business key is used to identify the process in unit tests + private String getBusinessKey(Map<String, Object> inputVariables) { + Object businessKey = inputVariables.get("att-mso-business-key"); + if (businessKey == null ) { + businessKey = UUID.randomUUID().toString(); + inputVariables.put("att-mso-business-key", businessKey); + } + return businessKey.toString(); + } + + private String getRequestId(Map<String, Object> inputVariables) { + Object requestId = inputVariables.get("att-mso-request-id"); + if (requestId == null ) { + requestId = UUID.randomUUID().toString(); + inputVariables.put("att-mso-request-id", requestId); + } + return requestId.toString(); + } + + private long getWaitTime(Map<String, Object> inputVariables) + { + String timeout = inputVariables.get("att-mso-service-request-timeout") == null + ? null : inputVariables.get("att-mso-service-request-timeout").toString(); + + if (timeout != null) { + try { + return Long.parseLong(timeout)*1000; + } catch (NumberFormatException nex) { + msoLogger.debug("Invalid input for att-mso-service-request-timeout"); + } + } + + return DEFAULT_WAIT_TIME; + } + + private void recordEvents(String processKey, WorkflowResponse response, + long startTime) { + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse(), "BPMN", MDC.get(processKey), null); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + response.getMessage() + "for processKey: " + processKey + " with response: " + response.getResponse()); + + } + + private void setLogContext(String processKey, + Map<String, Object> inputVariables) { + MsoLogger.setServiceName("MSO." + processKey); + if (inputVariables != null) { + MsoLogger.setLogContext(getKeyValueFromInputVariables(inputVariables,"att-mso-request-id"), getKeyValueFromInputVariables(inputVariables,"att-mso-service-instance-id")); + } + } + + private String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) { + if (inputVariables == null) return ""; + Object requestId = inputVariables.get(key); + if (requestId != null) return requestId.toString(); + return "N/A"; + } + + private boolean isProcessEnded(String processInstanceId) { + ProcessEngineServices pes = getProcessEngineServices(); + return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ; + } + + + private ProcessEngineServices getProcessEngineServices() { + if (pes4junit == null) { + return ProcessEngines.getDefaultProcessEngine(); + } else { + return pes4junit; + } + } + + public void setProcessEngineServices4junit(ProcessEngineServices pes) { + pes4junit = pes; + } + + private Map<String, Object> getInputVariables(VariableMapImpl variableMap) { + Map<String, Object> inputVariables = new HashMap<String,Object>(); + @SuppressWarnings("unchecked") + Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables"); + for (String vName : vMap.keySet()) { + @SuppressWarnings("unchecked") + Map<String, Object> valueMap = (Map<String,Object>)vMap.get(vName); // value, type + inputVariables.put(vName, valueMap.get("value")); + } + return inputVariables; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowCallbackResponse.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowCallbackResponse.java new file mode 100644 index 0000000000..425f0b20ad --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowCallbackResponse.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +/** + * @version 1.0 + * Workflow Response bean to generate workflow response in JSON format + */ +public class WorkflowCallbackResponse { + + private String response; + private int statusCode; + private String message; + + public String getResponse() { + return response; + } + public void setResponse(String response) { + this.response = response; + } + public int getStatusCode() { + return statusCode; + } + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContext.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContext.java new file mode 100644 index 0000000000..f44dfa9ccc --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContext.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +import org.jboss.resteasy.spi.AsynchronousResponse; + +/** + * @version 1.0 + * Workflow context object used to send timeout response, if workflow instance does not write the response in time + */ +public class WorkflowContext implements Delayed { + private final String processKey; + private final String requestId; + private final AsynchronousResponse asynchronousResponse; + private final long startTime; + private final long timeout; + + public WorkflowContext(String processKey, String requestId, + AsynchronousResponse asynchronousResponse, long timeout) { + this.processKey = processKey; + this.requestId = requestId; + this.asynchronousResponse = asynchronousResponse; + this.timeout = timeout; + this.startTime = System.currentTimeMillis(); + } + + public String getRequestId() { + return requestId; + } + + public String getProcessKey() { + return processKey; + } + + public AsynchronousResponse getAsynchronousResponse() { + return asynchronousResponse; + } + + public long getTimeout() { + return timeout; + } + + public long getStartTime() { + return startTime; + } + + /** + * Required implementation by Delay queue + * Returns the elapsed time for this context + */ + @Override + public long getDelay(TimeUnit unit) { + // 0 or negative means this object is considered to be expired + return unit.convert(startTime + timeout - System.currentTimeMillis(), unit); + } + + /** + * Required implementation by Delay queue + * Compares the object to determine whether the object can be marked as expired + */ + @Override + public int compareTo(Delayed object) { + WorkflowContext that = (WorkflowContext) object; + long thisEndTime = startTime + timeout; + long thatEndTime = that.startTime + that.timeout; + + if (thisEndTime < thatEndTime) { + return -1; + } else if (thisEndTime > thatEndTime) { + return 1; + } else { + return 0; + } + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContextHolder.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContextHolder.java new file mode 100644 index 0000000000..a42a6d67a8 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowContextHolder.java @@ -0,0 +1,188 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.concurrent.DelayQueue; +import java.util.concurrent.TimeUnit; + +import javax.ws.rs.core.Response; + +import org.jboss.resteasy.spi.AsynchronousResponse; +import org.slf4j.MDC; + +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; + +/** + * Workflow Context Holder instance which can be accessed elsewhere either in groovy scripts or Java + * @version 1.0 + * + */ +public class WorkflowContextHolder { + + private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + private static final String logMarker = "[WORKFLOW-CONTEXT-HOLDER]"; + private static WorkflowContextHolder instance = null; + + /** + * Delay Queue which holds workflow context holder objects + */ + private final DelayQueue<WorkflowContext> responseQueue = new DelayQueue<WorkflowContext>(); + private final TimeoutThread timeoutThread = new TimeoutThread(); + + private WorkflowContextHolder() { + timeoutThread.start(); + } + + /** + * Singleton holder which eliminates hot lock + * Since the JVM synchronizes static method there is no synchronization needed for this method + * @return + */ + public static synchronized WorkflowContextHolder getInstance() { + if (instance == null) { + instance = new WorkflowContextHolder(); + } + return instance; + } + + public void put(WorkflowContext context) { + msoLogger.debug(logMarker + " Adding context to the queue: " + + context.getRequestId()); + responseQueue.put(context); + } + + public void remove(WorkflowContext context) { + msoLogger.debug(logMarker + " Removing context from the queue: " + + context.getRequestId()); + responseQueue.remove(context); + } + + public WorkflowContext getWorkflowContext(String requestId) { + // Note: DelayQueue interator is threadsafe + for (WorkflowContext context : responseQueue) { + if (requestId.equals(context.getRequestId())) { + msoLogger.debug("Found context for request id: " + requestId); + return context; + } + } + + msoLogger.debug("Unable to find context for request id: " + requestId); + return null; + } + + /** + * Builds the callback response object to respond to client + * @param processKey + * @param processInstanceId + * @param requestId + * @param callbackResponse + * @return + */ + public Response processCallback(String processKey, String processInstanceId, + String requestId, WorkflowCallbackResponse callbackResponse) { + WorkflowResponse workflowResponse = new WorkflowResponse(); + WorkflowContext workflowContext = getWorkflowContext(requestId); + + if (workflowContext == null) { + msoLogger.debug("Unable to correlate workflow context for request id: " + requestId + + ":processInstance Id:" + processInstanceId + + ":process key:" + processKey); + workflowResponse.setMessage("Fail"); + workflowResponse.setMessageCode(400); + workflowResponse.setResponse("Unable to correlate workflow context, bad request. Request Id: " + requestId); + return Response.serverError().entity(workflowResponse).build(); + } + + responseQueue.remove(workflowContext); + + msoLogger.debug("Using callback response for request id: " + requestId); + workflowResponse.setResponse(callbackResponse.getResponse()); + workflowResponse.setProcessInstanceID(processInstanceId); + workflowResponse.setMessageCode(callbackResponse.getStatusCode()); + workflowResponse.setMessage(callbackResponse.getMessage()); + sendWorkflowResponseToClient(processKey, workflowContext, workflowResponse); + return Response.ok().entity(workflowResponse).build(); + } + + /** + * Send the response to client asynchronously when invoked by the BPMN process + * @param processKey + * @param workflowContext + * @param workflowResponse + */ + private void sendWorkflowResponseToClient(String processKey, WorkflowContext workflowContext, + WorkflowResponse workflowResponse) { + msoLogger.debug(logMarker + "Sending the response for request id: " + workflowContext.getRequestId()); + recordEvents(processKey, workflowResponse, workflowContext.getStartTime()); + Response response = Response.status(workflowResponse.getMessageCode()).entity(workflowResponse).build(); + AsynchronousResponse asyncResp = workflowContext.getAsynchronousResponse(); + asyncResp.setResponse(response); + } + + /** + * Timeout thread which monitors the delay queue for expired context and send timeout response + * to client + * + * */ + private class TimeoutThread extends Thread { + public void run() { + while (!isInterrupted()) { + try { + WorkflowContext requestObject = responseQueue.take(); + msoLogger.debug("Time remaining for request id: " + requestObject.getRequestId() + ":" + requestObject.getDelay(TimeUnit.MILLISECONDS)); + msoLogger.debug("Preparing timeout response for " + requestObject.getProcessKey() + ":" + ":" + requestObject.getRequestId()); + WorkflowResponse response = new WorkflowResponse(); + response.setMessage("Fail"); + response.setResponse("Request timedout, request id:" + requestObject.getRequestId()); + //response.setProcessInstanceID(requestObject.getProcessInstance().getProcessInstanceId()); + recordEvents(requestObject.getProcessKey(), response, requestObject.getStartTime()); + response.setMessageCode(500); + Response result = Response.status(500).entity(response).build(); + requestObject.getAsynchronousResponse().setResponse(result); + msoLogger.debug("Sending timeout response for request id:" + requestObject.getRequestId() + ":response:" + response); + } catch (InterruptedException e) { + break; + } catch (Exception e) { + msoLogger.debug("WorkflowContextHolder timeout thread caught exception: " + e); + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), + MsoLogger.ErrorCode.UnknownError, "Error in WorkflowContextHolder timeout thread"); + + } + } + + msoLogger.debug("WorkflowContextHolder timeout thread interrupted, quitting"); + } + } + + private static void recordEvents(String processKey, WorkflowResponse response, + long startTime) { + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + logMarker + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse(), "BPMN", MDC.get(processKey), null); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logMarker + + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse()); + + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResource.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResource.java new file mode 100644 index 0000000000..1d90ac401c --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResource.java @@ -0,0 +1,627 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.camunda.bpm.engine.HistoryService; +import org.camunda.bpm.engine.ProcessEngineException; +import org.camunda.bpm.engine.ProcessEngineServices; +import org.camunda.bpm.engine.ProcessEngines; +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.history.HistoricVariableInstance; +import org.camunda.bpm.engine.impl.core.variable.VariableMapImpl; +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.camunda.bpm.engine.variable.VariableMap; +import org.camunda.bpm.engine.variable.Variables; +import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats; + +import org.openecomp.mso.bpmn.core.WorkflowException; +import org.openecomp.mso.logger.MessageEnum; +import org.openecomp.mso.logger.MsoLogger; +import org.slf4j.MDC; + +@Path("/workflow") +public class WorkflowResource { + + private ProcessEngineServices pes4junit = null; + + private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL); + private static final String LOGMARKER = "[WRKFLOW-RESOURCE]"; + + private static final int DEFAULT_WAIT_TIME = 30000; + + @Context + private UriInfo uriInfo = null; + + /** + * Starts the process instance and responds to client synchronously + * If the request does not contain att-mso-service-request-timeout then it waits for the value specified in DEFAULT_WAIT_TIME + * Note: value specified in att-mso-service-request-timeout is in seconds + * During polling time, if there is an exception encountered in the process execution then polling is stopped and the error response is + * returned to the client + * @param processKey + * @param variableMap + * @return + */ + @POST + @Path("/services/{processKey}") + @Produces("application/json") + @Consumes("application/json") + public Response startProcessInstanceByKey(@PathParam("processKey") String processKey, + VariableMapImpl variableMap) { + + Map<String, Object> inputVariables = getInputVariables(variableMap); + setLogContext(processKey, inputVariables); + + WorkflowResponse workflowResponse = new WorkflowResponse(); + long startTime = System.currentTimeMillis(); + ProcessInstance processInstance = null; + + try { + //Kickoff the process + ProcessThread thread = new ProcessThread(inputVariables,processKey,msoLogger); + thread.start(); + + Map<String, Object> responseMap = null; + + //wait for process to be completed + long waitTime = getWaitTime(inputVariables); + long now = System.currentTimeMillis(); + long start = now; + long endTime = start + waitTime; + long pollingInterval = 500; + + // TEMPORARY LOGIC FOR UNIT TEST REFACTORING + // If this is a unit test (method is invoked directly), wait a max + // of 5 seconds after process ended for a result. In production, + // wait up to 60 seconds. + long timeToWaitAfterProcessEnded = uriInfo == null ? 5000 : 60000; + AtomicLong timeProcessEnded = new AtomicLong(0); + boolean endedWithNoResponse = false; + + while (now <= endTime) { + Thread.sleep(pollingInterval); + + now = System.currentTimeMillis(); + + // Increase the polling interval over time + + long elapsed = now - start; + + if (elapsed > 60000) { + pollingInterval = 5000; + } else if (elapsed > 10000) { + pollingInterval = 1000; + } + Exception exception = thread.getException(); + if (exception != null) { + throw new Exception(exception); + } + + processInstance = thread.getProcessInstance(); + + if (processInstance == null) { + msoLogger.debug(LOGMARKER + processKey + " process has not been created yet"); + continue; + } + + String processInstanceId = processInstance.getId(); + workflowResponse.setProcessInstanceID(processInstanceId); + + responseMap = getResponseMap(processInstance, processKey, timeProcessEnded); + + if (responseMap == null) { + msoLogger.debug(LOGMARKER + processKey + " has not produced a response yet"); + + if (timeProcessEnded.longValue() != 0) { + long elapsedSinceEnded = System.currentTimeMillis() - timeProcessEnded.longValue(); + + if (elapsedSinceEnded > timeToWaitAfterProcessEnded) { + endedWithNoResponse = true; + break; + } + } + } else { + processResponseMap(workflowResponse, responseMap); + recordEvents(processKey, workflowResponse, startTime); + return Response.status(workflowResponse.getMessageCode()).entity(workflowResponse).build(); + } + } + + //if we dont get response after waiting then send timeout response + + String state; + String processInstanceId; + + if (processInstance == null) { + processInstanceId = "N/A"; + state = "NOT STARTED"; + } else { + processInstanceId = processInstance.getProcessInstanceId(); + state = isProcessEnded(processInstanceId) ? "ENDED" : "NOT ENDED"; + } + + workflowResponse.setMessage("Fail"); + if (endedWithNoResponse) { + workflowResponse.setResponse("Process ended without producing a response"); + } else { + workflowResponse.setResponse("Request timed out, process state: " + state); + } + workflowResponse.setProcessInstanceID(processInstanceId); + recordEvents(processKey, workflowResponse, startTime); + workflowResponse.setMessageCode(500); + return Response.status(500).entity(workflowResponse).build(); + } catch (Exception ex) { + msoLogger.debug(LOGMARKER + "Exception in startProcessInstance by key"); + ex.printStackTrace(); + workflowResponse.setMessage("Fail" ); + workflowResponse.setResponse("Error occurred while executing the process: " + ex.getMessage()); + if (processInstance != null) workflowResponse.setProcessInstanceID(processInstance.getId()); + + msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "BPMN", MDC.get(processKey), + MsoLogger.ErrorCode.UnknownError, LOGMARKER + workflowResponse.getMessage() + + " for processKey: " + processKey + " with response: " + workflowResponse.getResponse()); + + workflowResponse.setMessageCode(500); + recordEvents(processKey, workflowResponse, startTime); + return Response.status(500).entity(workflowResponse).build(); + } + } + + /** + * Returns the wait time, this is used by the resource on how long it should wait to send a response + * If none specified DEFAULT_WAIT_TIME is used + * @param inputVariables + * @return + */ + private int getWaitTime(Map<String, Object> inputVariables) + { + String timeout = inputVariables.get("att-mso-service-request-timeout") == null + ? null : inputVariables.get("att-mso-service-request-timeout").toString(); + + if (timeout != null) { + try { + return Integer.parseInt(timeout)*1000; + } catch (NumberFormatException nex) { + msoLogger.debug("Invalid input for att-mso-service-request-timeout"); + } + } + return DEFAULT_WAIT_TIME; + } + + private void recordEvents(String processKey, WorkflowResponse response, long startTime) { + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse(), "BPMN", MDC.get(processKey), null); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse()); + } + + private void setLogContext(String processKey, Map<String, Object> inputVariables) { + MsoLogger.setServiceName("MSO." + processKey); + if (inputVariables != null) { + MsoLogger.setLogContext(getValueFromInputVariables(inputVariables, "att-mso-request-id"), + getValueFromInputVariables(inputVariables, "att-mso-service-instance-id")); + } + } + + private String getValueFromInputVariables(Map<String,Object> inputVariables, String key) { + Object value = inputVariables.get(key); + if (value == null) { + return "N/A"; + } else { + return value.toString(); + } + } + + /** + * Checks to see if the specified process is ended. + * @param processInstanceId the process instance ID + * @return true if the process is ended + */ + private boolean isProcessEnded(String processInstanceId) { + ProcessEngineServices pes = getProcessEngineServices(); + return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ; + } + + private void processResponseMap(WorkflowResponse workflowResponse, Map<String, Object> responseMap) { + Object object = responseMap.get("Response"); + String response = object == null ? null : String.valueOf(object); + if(response == null){ + object = responseMap.get("WorkflowResponse"); + response = object == null ? null : String.valueOf(object); + } + + workflowResponse.setResponse(response); + + object = responseMap.get("ResponseCode"); + String responseCode = object == null ? null : String.valueOf(object); + + try { + workflowResponse.setMessageCode(Integer.parseInt(responseCode)); + } catch(NumberFormatException nex) { + msoLogger.debug(LOGMARKER + "Failed to parse ResponseCode: " + responseCode); + workflowResponse.setMessageCode(-1); + } + + Object status = responseMap.get("Status"); + + if ("Success".equalsIgnoreCase(String.valueOf(status))) { + workflowResponse.setMessage("Success"); + } else if ("Fail".equalsIgnoreCase(String.valueOf(status))) { + workflowResponse.setMessage("Fail"); + } else { + msoLogger.debug(LOGMARKER + "Unrecognized Status: " + responseCode); + workflowResponse.setMessage("Fail"); + } + } + + /** + * @version 1.0 + * Triggers the workflow in a separate thread + */ + private class ProcessThread extends Thread { + private final Map<String,Object> inputVariables; + private final String processKey; + private final MsoLogger msoLogger; + private final String businessKey; + private ProcessInstance processInstance = null; + private Exception exception = null; + + public ProcessThread(Map<String, Object> inputVariables, String processKey, MsoLogger msoLogger) { + this.inputVariables = inputVariables; + this.processKey = processKey; + this.msoLogger = msoLogger; + this.businessKey = UUID.randomUUID().toString(); + } + + /** + * If an exception occurs when starting the process instance, it may + * be obtained by calling this method. Note that exceptions are only + * recorded while the process is executing in its original thread. + * Once a process is suspended, exception recording stops. + * @return the exception, or null if none has occurred + */ + public Exception getException() { + return exception; + } + + + public ProcessInstance getProcessInstance() { + return this.processInstance; + } + + /** + * Sets the process instance exception. + * @param exception the exception + */ + private void setException(Exception exception) { + this.exception = exception; + } + + public void run() { + setLogContext(processKey, inputVariables); + + long startTime = System.currentTimeMillis(); + + try { + msoLogger.debug(LOGMARKER + "***Received MSO startProcessInstanceByKey with processKey:" + + processKey + " and variables: " + inputVariables); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, LOGMARKER + + "Call to MSO workflow/services in Camunda. Received MSO startProcessInstanceByKey with" + + " processKey:" + processKey + + " businessKey:" + businessKey + + " variables: " + inputVariables); + + RuntimeService runtimeService = getProcessEngineServices().getRuntimeService(); + + // Note that this method doesn't return until the process suspends + // itself or finishes. We provide a business key so we can identify + // the process instance immediately. + processInstance = runtimeService.startProcessInstanceByKey( + processKey, inputVariables); + + } catch (Exception e) { + msoLogger.debug(LOGMARKER + "ProcessThread caught an exception executing " + + processKey + ": " + e); + setException(e); + } + } + + } + + private Map<String, Object> getInputVariables(VariableMapImpl variableMap) { + VariableMap inputVariables = Variables.createVariables(); + @SuppressWarnings("unchecked") + Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables"); + for (String key : vMap.keySet()) { //variabe name vn + @SuppressWarnings("unchecked") + Map<String, Object> valueMap = (Map<String,Object>)vMap.get(key); //value, type + inputVariables.putValueTyped(key, Variables + .objectValue(valueMap.get("value")) + .serializationDataFormat(SerializationDataFormats.JAVA) // tells the engine to use java serialization for persisting the value + .create()); + } + return inputVariables; + } + + /** + * Attempts to get a response map from the specified process instance. + * @return the response map, or null if it is unavailable + */ + private Map<String, Object> getResponseMap(ProcessInstance processInstance, + String processKey, AtomicLong timeProcessEnded) { + + String responseMapVariable = processKey + "ResponseMap"; + String processInstanceId = processInstance.getId(); + + // Query the runtime service to see if a response map is ready. + +/* RuntimeService runtimeService = getProcessEngineServices().getRuntimeService(); + List<Execution> executions = runtimeService.createExecutionQuery() + .processInstanceId(processInstanceId).list(); + + for (Execution execution : executions) { + @SuppressWarnings("unchecked") + Map<String, Object> responseMap = (Map<String, Object>) + getVariableFromExecution(runtimeService, execution.getId(), + responseMapVariable); + + if (responseMap != null) { + msoLogger.debug(LOGMARKER + "Obtained " + responseMapVariable + + " from process " + processInstanceId + " execution " + + execution.getId()); + return responseMap; + } + } +*/ + //Querying history seem to return consistent results compared to querying the runtime service + + boolean alreadyEnded = timeProcessEnded.longValue() != 0; + + if (alreadyEnded || isProcessEnded(processInstance.getId())) { + if (!alreadyEnded) { + timeProcessEnded.set(System.currentTimeMillis()); + } + + // Query the history service to see if a response map exists. + + HistoryService historyService = getProcessEngineServices().getHistoryService(); + @SuppressWarnings("unchecked") + Map<String, Object> responseMap = (Map<String, Object>) + getVariableFromHistory(historyService, processInstance.getId(), + responseMapVariable); + + if (responseMap != null) { + msoLogger.debug(LOGMARKER + "Obtained " + responseMapVariable + + " from process " + processInstanceId + " history"); + return responseMap; + } + + // Query the history service for old-style response variables. + + String prefix = (String) getVariableFromHistory(historyService, processInstanceId, "prefix"); + + if (prefix != null) { + + // Check for 'WorkflowResponse' variable + Object workflowResponseObject = getVariableFromHistory(historyService, processInstanceId, "WorkflowResponse"); + String workflowResponse = workflowResponseObject == null ? null : String.valueOf(workflowResponseObject); + msoLogger.debug(LOGMARKER + "WorkflowResponse: " + workflowResponse); + + if (workflowResponse != null) { + Object responseCodeObject = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode"); + String responseCode = responseCodeObject == null ? null : String.valueOf(responseCodeObject); + msoLogger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode); + responseMap = new HashMap<String, Object>(); + responseMap.put("WorkflowResponse", workflowResponse); + responseMap.put("ResponseCode", responseCode); + responseMap.put("Status", "Success"); + return responseMap; + } + + + // Check for 'WorkflowException' variable + WorkflowException workflowException = null; + String workflowExceptionText = null; + + Object workflowExceptionObject = getVariableFromHistory(historyService, processInstanceId, "WorkflowException"); + if(workflowExceptionObject != null) { + if(workflowExceptionObject instanceof WorkflowException) { + workflowException = (WorkflowException) workflowExceptionObject; + workflowExceptionText = workflowException.toString(); + responseMap = new HashMap<String, Object>(); + responseMap.put("WorkflowException", workflowExceptionText); + responseMap.put("ResponseCode", workflowException.getErrorCode()); + responseMap.put("Status", "Fail"); + return responseMap; + } + else if (workflowExceptionObject instanceof String) { + Object object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode"); + String responseCode = object == null ? null : String.valueOf(object); + workflowExceptionText = (String) workflowExceptionObject; + responseMap = new HashMap<String, Object>(); + responseMap.put("WorkflowException", workflowExceptionText); + responseMap.put("ResponseCode", responseCode); + responseMap.put("Status", "Fail"); + return responseMap; + } + + } + msoLogger.debug(LOGMARKER + "WorkflowException: " + workflowExceptionText); + + // BEGIN LEGACY SUPPORT. TODO: REMOVE THIS CODE + Object object = getVariableFromHistory(historyService, processInstanceId, processKey + "Response"); + String response = object == null ? null : String.valueOf(object); + msoLogger.debug(LOGMARKER + processKey + "Response: " + response); + + if (response != null) { + object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode"); + String responseCode = object == null ? null : String.valueOf(object); + msoLogger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode); + responseMap = new HashMap<String, Object>(); + responseMap.put("Response", response); + responseMap.put("ResponseCode", responseCode); + responseMap.put("Status", "Success"); + return responseMap; + } + + object = getVariableFromHistory(historyService, processInstanceId, prefix + "ErrorResponse"); + String errorResponse = object == null ? null : String.valueOf(object); + msoLogger.debug(LOGMARKER + prefix + "ErrorResponse: " + errorResponse); + + if (errorResponse != null) { + object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode"); + String responseCode = object == null ? null : String.valueOf(object); + msoLogger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode); + responseMap = new HashMap<String, Object>(); + responseMap.put("Response", errorResponse); + responseMap.put("ResponseCode", responseCode); + responseMap.put("Status", "Fail"); + return responseMap; + } + // END LEGACY SUPPORT. TODO: REMOVE THIS CODE + } + } + return null; + } + + /** + * Gets a variable value from the specified execution. + * @return the variable value, or null if the variable could not be + * obtained + */ + private Object getVariableFromExecution(RuntimeService runtimeService, + String executionId, String variableName) { + try { + return runtimeService.getVariable(executionId, variableName); + } catch (ProcessEngineException e) { + // Most likely cause is that the execution no longer exists. + msoLogger.debug("Error retrieving execution " + executionId + + " variable " + variableName + ": " + e); + return null; + } + } + + /** + * Gets a variable value from specified historical process instance. + * @return the variable value, or null if the variable could not be + * obtained + */ + private Object getVariableFromHistory(HistoryService historyService, + String processInstanceId, String variableName) { + try { + HistoricVariableInstance v = historyService.createHistoricVariableInstanceQuery() + .processInstanceId(processInstanceId).variableName(variableName).singleResult(); + return v == null ? null : v.getValue(); + } catch (Exception e) { + msoLogger.debug("Error retrieving process " + processInstanceId + + " variable " + variableName + " from history: " + e); + return null; + } + } + + @POST + @Path("/services/{processKey}/{processInstanceId}") + @Produces("application/json") + @Consumes("application/json") + public WorkflowResponse getProcessVariables(@PathParam("processKey") String processKey, @PathParam("processInstanceId") String processInstanceId) { + //TODO filter only set of variables + WorkflowResponse response = new WorkflowResponse(); + + long startTime = System.currentTimeMillis(); + try { + ProcessEngineServices engine = getProcessEngineServices(); + List<HistoricVariableInstance> variables = engine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list(); + Map<String,String> variablesMap = new HashMap<String,String>(); + for (HistoricVariableInstance variableInstance: variables) { + variablesMap.put(variableInstance.getName(), variableInstance.getValue().toString()); + } + + msoLogger.debug(LOGMARKER + "***Received MSO getProcessVariables with processKey:" + processKey + " and variables: " + variablesMap.toString()); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, LOGMARKER + + "Call to MSO workflow/services in Camunda. Received MSO getProcessVariables with processKey:" + + processKey + " and variables: " + + variablesMap.toString()); + + + response.setVariables(variablesMap); + response.setMessage("Success"); + response.setResponse("Successfully retrieved the variables"); + response.setProcessInstanceID(processInstanceId); + + msoLogger.debug(LOGMARKER + response.getMessage() + " for processKey: " + processKey + " with response: " + response.getResponse()); + } catch (Exception ex) { + response.setMessage("Fail"); + response.setResponse("Failed to retrieve the variables," + ex.getMessage()); + response.setProcessInstanceID(processInstanceId); + + msoLogger.error (MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "BPMN", MDC.get(processKey), MsoLogger.ErrorCode.UnknownError, LOGMARKER + + response.getMessage() + + " for processKey: " + + processKey + + " with response: " + + response.getResponse()); + + } + + msoLogger.recordMetricEvent ( startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse(), "BPMN", MDC.get(processKey), null); + + msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, + LOGMARKER + response.getMessage() + " for processKey: " + + processKey + " with response: " + response.getResponse()); + + return response; + } + + private ProcessEngineServices getProcessEngineServices() { + if (pes4junit == null) { + return ProcessEngines.getDefaultProcessEngine(); + } else { + return pes4junit; + } + } + + public void setProcessEngineServices4junit(ProcessEngineServices pes) { + pes4junit = pes; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResourceApplication.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResourceApplication.java new file mode 100644 index 0000000000..1e1a20ce45 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResourceApplication.java @@ -0,0 +1,55 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + + +/** + * @version 1.0 + * RESTeasy workflow application which wires synchronous and asynchronous response + * + */ +@ApplicationPath("/") +public class WorkflowResourceApplication extends Application { + private Set<Object> singletons = new HashSet<Object>(); + private Set<Class<?>> classes = new HashSet<Class<?>>(); + + public WorkflowResourceApplication() { + singletons.add(new WorkflowResource()); + singletons.add(new WorkflowAsyncResource()); + singletons.add(new VnfAdapterRestNotifyResource()); + } + + @Override + public Set<Class<?>> getClasses() { + return classes; + } + + @Override + public Set<Object> getSingletons() { + return singletons; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResponse.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResponse.java new file mode 100644 index 0000000000..37cc676b32 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/gamma/workflow/service/WorkflowResponse.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.gamma.workflow.service; + +import java.util.Map; + +/** + * @version 1.0 + * Synchronous workflow response bean + * + */ +public class WorkflowResponse { + + private String response; + private String message; + private String processInstanceID; + private Map<String,String> variables; + private int messageCode; + + public String getResponse() { + return response; + } + public void setResponse(String response) { + this.response = response; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + public String getProcessInstanceID() { + return processInstanceID; + } + public void setProcessInstanceID(String pID) { + this.processInstanceID = pID; + } + public Map<String, String> getVariables() { + return variables; + } + public void setVariables(Map<String, String> variables) { + this.variables = variables; + } + public void setMessageCode(int messageCode) { + this.messageCode = messageCode; + } + + public int getMessageCode() { + return messageCode; + } +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoHandler.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoHandler.java new file mode 100644 index 0000000000..66b170eb75 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoHandler.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.util; + +import java.security.GeneralSecurityException; + +public class CryptoHandler implements ICryptoHandler { + + private static String msoKey = "aa3871669d893c7fb8abbcda31b88b4f"; + //private static String msoAaiPwd = "mso0206"; + private static String msoAaiEncryptedPwd = "C1FC4A39E16419DD41DFC1212843F440"; + + public String getMsoAaiPassword() { + try { + return CryptoUtils.decrypt(msoAaiEncryptedPwd, msoKey); + } catch (GeneralSecurityException e) { + return null; + } + } + + + public String encryptMsoPassword(String plainMsoPwd) { + try { + return CryptoUtils.encrypt(plainMsoPwd, msoKey); + } catch (GeneralSecurityException e) { + return null; + } + } + + + public String decryptMsoPassword(String encryptedPwd) { + try { + return CryptoUtils.decrypt(encryptedPwd, msoKey); + } catch (GeneralSecurityException e) { + return null; + } + } + +} diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoUtils.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoUtils.java new file mode 100644 index 0000000000..e8b91bc776 --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/CryptoUtils.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.util; + + +import java.security.GeneralSecurityException; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; + +// import com.att.util.common.RttpBasicAuth; + +//Need to add BPM error handler + +/** + * CryptoUtils adapted from RTTP client. + * + */ +public class CryptoUtils { + + public static final String AES = "AES"; + + /** + * encrypt a value and generate a keyfile + * if the keyfile is not found then a new one is created + * @throws GeneralSecurityException + * @throws IOException + */ + public static String encrypt(String value, String keyString) throws GeneralSecurityException + { + SecretKeySpec sks = getSecretKeySpec(keyString); + Cipher cipher = Cipher.getInstance(CryptoUtils.AES); + cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters()); + byte[] encrypted = cipher.doFinal(value.getBytes()); + return byteArrayToHexString(encrypted); + } + + /** + * decrypt a value + * @throws GeneralSecurityException + * @throws IOException + */ + public static String decrypt(String message, String keyString) throws GeneralSecurityException + { + SecretKeySpec sks = getSecretKeySpec(keyString); + Cipher cipher = Cipher.getInstance(CryptoUtils.AES); + cipher.init(Cipher.DECRYPT_MODE, sks); + byte[] decrypted = cipher.doFinal(hexStringToByteArray(message)); + return new String(decrypted); + } + + private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException + { + byte [] key = hexStringToByteArray(keyString); + SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES); + return sks; + } + + + private static String byteArrayToHexString(byte[] b){ + StringBuffer sb = new StringBuffer(b.length * 2); + for (int i = 0; i < b.length; i++){ + int v = b[i] & 0xff; + if (v < 16) { + sb.append('0'); + } + sb.append(Integer.toHexString(v)); + } + return sb.toString().toUpperCase(); + } + + private static byte[] hexStringToByteArray(String s) { + byte[] b = new byte[s.length() / 2]; + for (int i = 0; i < b.length; i++){ + int index = i * 2; + int v = Integer.parseInt(s.substring(index, index + 2), 16); + b[i] = (byte)v; + } + return b; + } + + /** + * Not Used... + * + * Call Rttp utility jar to encrypt pwd + * @param clearPassword + * @return + * @throws GeneralSecurityException + * @throws Exception + * + public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException { + try { + return RttpBasicAuth.encrypt(clearPassword); + } catch (Exception e) { + // wrap generic exception + throw new GeneralSecurityException(e); + } + } + */ +} + diff --git a/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/ICryptoHandler.java b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/ICryptoHandler.java new file mode 100644 index 0000000000..1ef96b683d --- /dev/null +++ b/bpmn/MSOGammaBPMN/src/main/java/org/openecomp/mso/bpmn/util/ICryptoHandler.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * OPENECOMP - MSO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.mso.bpmn.util; + +public interface ICryptoHandler { + public String getMsoAaiPassword(); + public String encryptMsoPassword(String plainPwd); + public String decryptMsoPassword(String encryptedPwd); +} |