From 697d02bf4e4188e3040cd987dee97d15f397a35f Mon Sep 17 00:00:00 2001 From: ramverma Date: Fri, 8 Jun 2018 11:56:21 +0100 Subject: Adding plugins-event module to apex-pdp Adding plugins-event module to apex-pdp Fix a minor bug in TextFileUtils Change-Id: I393c5f5809d078850d6669d22759ba9fa1b4f0e6 Issue-ID: POLICY-862 Signed-off-by: ramverma --- .../plugins-event-protocol-jms/pom.xml | 44 ++++++ .../protocol/jms/Apex2JMSObjectEventConverter.java | 148 +++++++++++++++++++++ .../protocol/jms/Apex2JMSTextEventConverter.java | 100 ++++++++++++++ .../jms/JMSObjectEventProtocolParameters.java | 127 ++++++++++++++++++ .../jms/JMSTextEventProtocolParameters.java | 56 ++++++++ .../plugins/event/protocol/jms/package-info.java | 28 ++++ 6 files changed, 503 insertions(+) create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSObjectEventConverter.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSTextEventConverter.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSObjectEventProtocolParameters.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSTextEventProtocolParameters.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/package-info.java (limited to 'plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms') diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml new file mode 100644 index 000000000..98328951e --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-protocol + plugins-event-protocol + 2.0.0-SNAPSHOT + + + plugins-event-protocol-jms + ${project.artifactId} + [${project.parent.artifactId}] Plugins for handling events that are being transported as JMS messages + + + ${project.basedir}/src + + + + + javax.jms + javax.jms-api + 2.0.1 + + + \ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSObjectEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSObjectEventConverter.java new file mode 100644 index 000000000..52c678407 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSObjectEventConverter.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import java.util.ArrayList; +import java.util.List; + +import javax.jms.ObjectMessage; + +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class Apex2JMSObjectEventConverter converts {@link ApexEvent} instances into string instances of + * {@link javax.jms.ObjectMessage} message events for JMS. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class Apex2JMSObjectEventConverter implements ApexEventProtocolConverter { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JMSObjectEventConverter.class); + + // JMS event protocol parameters on the consumer (JMS->Apex) sides + private JMSObjectEventProtocolParameters eventProtocolParameters = null; + + /** + * Constructor to create the Apex to JMS Object converter. + * + * @throws ApexEventException the apex event exception + */ + public Apex2JMSObjectEventConverter() throws ApexEventException {} + + @Override + public void init(final EventProtocolParameters parameters) { + // Check if properties have been set for JMS object event conversion as a consumer. They may not be set because + // JMS may not be in use + // on both sides of Apex + if (!(parameters instanceof JMSObjectEventProtocolParameters)) { + final String errormessage = "specified Event Protocol Parameters properties of type \"" + + parameters.getClass().getCanonicalName() + "\" are not applicable to a " + + Apex2JMSObjectEventConverter.class.getName() + " converter"; + LOGGER.error(errormessage); + } else { + this.eventProtocolParameters = (JMSObjectEventProtocolParameters) parameters; + } + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String, java.lang.Object) + */ + @Override + public List toApexEvent(final String eventName, final Object eventObject) throws ApexEventException { + // Check if this is an ObjectMessage from JMS + if (!(eventObject instanceof ObjectMessage)) { + final String errorMessage = "message \"" + eventObject + "\" received from JMS is not an instance of \"" + + ObjectMessage.class.getCanonicalName() + "\""; + LOGGER.warn(errorMessage); + throw new ApexEventRuntimeException(errorMessage); + } + + // Get the object from the object message + final ObjectMessage objectMessage = (ObjectMessage) eventObject; + Object jmsIncomingObject; + try { + jmsIncomingObject = objectMessage.getObject(); + } catch (final Exception e) { + final String errorMessage = "object contained in message \"" + eventObject + + "\" received from JMS could not be retrieved as a Java object"; + LOGGER.debug(errorMessage, e); + throw new ApexEventRuntimeException(errorMessage, e); + } + + // Check that the consumer parameters for JMS->Apex messaging have been set + if (eventProtocolParameters == null) { + final String errorMessage = + "consumer parameters for JMS events consumed by Apex are not set in the Apex configuration for this engine"; + LOGGER.debug(errorMessage); + throw new ApexEventRuntimeException(errorMessage); + } + + // Create the Apex event + // @formatter:off + final ApexEvent apexEvent = new ApexEvent( + jmsIncomingObject.getClass().getSimpleName() + eventProtocolParameters.getIncomingEventSuffix(), + eventProtocolParameters.getIncomingEventVersion(), + jmsIncomingObject.toString().getClass().getPackage().getName(), + eventProtocolParameters.getIncomingEventSource(), + eventProtocolParameters.getIncomingEventTarget()); + // @formattter:on + + // Set the data on the apex event as the incoming object + apexEvent.put(jmsIncomingObject.getClass().getSimpleName(), jmsIncomingObject); + + // Return the event in a single element + final ArrayList eventList = new ArrayList(); + eventList.add(apexEvent); + return eventList; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.apex.service.engine.event.ApexEvent) + */ + @Override + public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException { + // Check the Apex event + if (apexEvent == null) { + LOGGER.warn("event processing failed, Apex event is null"); + throw new ApexEventException("event processing failed, Apex event is null"); + } + + // Check that the Apex event has a single parameter + if (apexEvent.size() != 1) { + final String errorMessage = "event processing failed, Apex event must have one and only one parameter for JMS Object handling"; + LOGGER.warn(errorMessage); + throw new ApexEventException(errorMessage); + } + + // Return the single object from the Apex event message + return apexEvent.values().iterator().next(); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSTextEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSTextEventConverter.java new file mode 100644 index 000000000..f48843125 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JMSTextEventConverter.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import java.util.List; + +import javax.jms.TextMessage; + +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JSONEventConverter; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class Apex2JMSTextEventConverter converts {@link ApexEvent} instances into string instances of + * {@link javax.jms.TextMessage} message events for JMS. It is a proxy for the built in + * {@link org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JSONEventConverter} plugin. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class Apex2JMSTextEventConverter extends Apex2JSONEventConverter { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JMSTextEventConverter.class); + + /** + * Constructor to create the Apex to JMS Object converter. + * + * @throws ApexEventException the apex event exception + */ + public Apex2JMSTextEventConverter() throws ApexEventException {} + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String, java.lang.Object) + */ + @Override + public List toApexEvent(final String eventName, final Object eventObject) throws ApexEventException { + // Check if this is an TextMessage from JMS + if (!(eventObject instanceof TextMessage)) { + final String errorMessage = "message \"" + eventObject + "\" received from JMS is not an instance of \"" + + TextMessage.class.getCanonicalName() + "\""; + LOGGER.debug(errorMessage); + throw new ApexEventRuntimeException(errorMessage); + } + + // Get the string from the object message + final TextMessage textMessage = (TextMessage) eventObject; + String jmsString; + try { + jmsString = textMessage.getText(); + } catch (final Exception e) { + final String errorMessage = "object contained in message \"" + eventObject + + "\" received from JMS could not be retrieved as a Java String"; + LOGGER.debug(errorMessage, e); + throw new ApexEventRuntimeException(errorMessage, e); + } + + // Use the generic JSON plugin from here + return super.toApexEvent(eventName, jmsString); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.apex.service.engine. + * event. ApexEvent) + */ + @Override + public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException { + // Check the Apex event + if (apexEvent == null) { + LOGGER.warn("event processing failed, Apex event is null"); + throw new ApexEventException("event processing failed, Apex event is null"); + } + + // Return the Apex event as a string object + return super.fromApexEvent(apexEvent); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSObjectEventProtocolParameters.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSObjectEventProtocolParameters.java new file mode 100644 index 000000000..c2baff8e6 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSObjectEventProtocolParameters.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +/** + * Event protocol parameters for JMS Object messages as an event protocol. + * + *

+ * On reception of an a JMS {@link javax.jms.ObjectMessage}, the JMS Object plugin unmarshals the message as follows: + *

    + *
  1. It extracts the Java object from the {@link javax.jms.ObjectMessage} instance. + *
  2. It creates an {@link org.onap.policy.apex.service.engine.event.ApexEvent} instance to hold the java object. + *
  3. It sets the name of the Apex event to be the simple class name of the incoming Java object and appends the value + * of the {@code incomingEventSuffix} parameter to it. + *
  4. It sets the version of the incoming event to the value of the {@code incomingEventVersion} parameter. + *
  5. It sets the name space of the incoming event to be the value of the package of the class of the incoming Java + * object. + *
  6. It sets the source of the incoming event to the value of the {@code incomingEventSource} parameter. + *
  7. It sets the target of the incoming event to the value of the {@code incomingEventTarget} parameter. + *
  8. It puts a single entry into the Apex event map with the the simple class name of the incoming Java object being + * the key of the entry and the actual incoming object as the value of the entry. + *
+ *

+ * When sending an object to JMS, the plugin expects to receive an Apex event with a single entry. The plugin marshals + * the value of that entry to an object that can be sent by JMS as a {@link javax.jms.ObjectMessage} instance. + *

+ * The parameters for this plugin are: + *

    + *
  1. incomingEventSuffix: The suffix to append to the simple name of incoming Java class instances when they are + * encapsulated in Apex events. The parameter defaults to the string value {@code IncomingEvent}. + *
  2. incomingEventVersion: The event version to use for incoming Java class instances when they are encapsulated in + * Apex events. The parameter defaults to the string value {@code 1.0.0}. + *
  3. incomingEventSource: The event source to use for incoming Java class instances when they are encapsulated in Apex + * events. The parameter defaults to the string value {@code JMS}. + *
  4. incomingEventTarget: The event target to use for incoming Java class instances when they are encapsulated in Apex + * events. The parameter defaults to the string value {@code Apex}. + *
+ * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JMSObjectEventProtocolParameters extends EventProtocolParameters { + /** The label of this event protocol. */ + public static final String JMS_OBJECT_EVENT_PROTOCOL_LABEL = "JMSOBJECT"; + + //@formatter:off + // Default parameter values + private static final String DEFAULT_INCOMING_EVENT_SUFFIX = "IncomingEvent"; + private static final String DEFAULT_INCOMING_EVENT_VERSION = "1.0.0"; + private static final String DEFAULT_INCOMING_EVENT_SOURCE = "JMS"; + private static final String DEFAULT_INCOMING_EVENT_TARGET = "Apex"; + + // JMS carrier parameters + private String incomingEventSuffix = DEFAULT_INCOMING_EVENT_SUFFIX; + private String incomingEventVersion = DEFAULT_INCOMING_EVENT_VERSION; + private String incomingEventSource = DEFAULT_INCOMING_EVENT_SOURCE; + private String incomingEventTarget = DEFAULT_INCOMING_EVENT_TARGET; + //@formatter:off + + /** + * Constructor to create a JSON event protocol parameter instance and register the instance with the parameter service. + */ + public JMSObjectEventProtocolParameters() { + super(JMSObjectEventProtocolParameters.class.getCanonicalName()); + + // Set the event protocol properties for the JMS Text event protocol + this.setLabel(JMS_OBJECT_EVENT_PROTOCOL_LABEL); + + // Set the event protocol plugin class + this.setEventProtocolPluginClass(Apex2JMSObjectEventConverter.class.getCanonicalName()); + } + + /** + * Gets the incoming event version. + * + * @return the incoming event version + */ + public String getIncomingEventVersion() { + return incomingEventVersion; + } + + /** + * Gets the incoming event source. + * + * @return the incoming event source + */ + public String getIncomingEventSource() { + return incomingEventSource; + } + + /** + * Gets the incoming event target. + * + * @return the incoming event target + */ + public String getIncomingEventTarget() { + return incomingEventTarget; + } + + /** + * Gets the incoming event suffix. + * + * @return the incoming event suffix + */ + public String getIncomingEventSuffix() { + return incomingEventSuffix; + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSTextEventProtocolParameters.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSTextEventProtocolParameters.java new file mode 100644 index 000000000..8ddc64862 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/JMSTextEventProtocolParameters.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters; + +/** + * Event protocol parameters for JMS Text messages as an event protocol. + *

+ * Text messages received and sent over JMS in ~Text format are assumed to be in a JSON format that Apex can understand. + * Therefore this plugin is a subclass of the built in JSON event protocol plugin. + *

+ * On reception of a JMS {@link javax.jms.TextMessage} message, the JMS Text plugin unmarshals the message the JMS text + * message and passes it to its JSON superclass unmarshaling for processing. + *

+ * When sending an Apex event, the plugin uses its underlying JSON superclass to marshal the event to a JSON string and + * passes that string to the JSON carrier plugin for sending. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JMSTextEventProtocolParameters extends JSONEventProtocolParameters { + /** The label of this event protocol. */ + public static final String JMS_TEXT_EVENT_PROTOCOL_LABEL = "JMSTEXT"; + + /** + * Constructor to create a JSON event protocol parameter instance and register the instance with the parameter + * service. + */ + public JMSTextEventProtocolParameters() { + super(JMSTextEventProtocolParameters.class.getCanonicalName(), JMS_TEXT_EVENT_PROTOCOL_LABEL); + + // Set the event protocol properties for the JMS Text event protocol + this.setLabel(JMS_TEXT_EVENT_PROTOCOL_LABEL); + + // Set the event protocol plugin class + this.setEventProtocolPluginClass(Apex2JMSTextEventConverter.class.getCanonicalName()); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/package-info.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/package-info.java new file mode 100644 index 000000000..92d6d07d4 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Contains implementations of Apex event protocol converter plugins for JMS event protocols that are implementations of + * {@link javax.jms.Message}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; -- cgit 1.2.3-korg