aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/main
diff options
context:
space:
mode:
authorJorge Hernandez <jorge.hernandez-herrero@att.com>2019-03-04 20:56:21 +0000
committerGerrit Code Review <gerrit@onap.org>2019-03-04 20:56:21 +0000
commite7ad7f65ed1c66b6ec2af6d415a386c931bfe20c (patch)
treed116387dd3add30cb13015a70f56baf5a74f7d7f /policy-endpoints/src/main
parentaee89a9ca97ba75b9cbb7cf5eba545a8e78acc09 (diff)
parent265f24eb2a14ec15f397501212cb7eb887cc1f26 (diff)
Merge "Add various listener classes"
Diffstat (limited to 'policy-endpoints/src/main')
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/JsonListener.java74
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/MessageTypeDispatcher.java98
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/RequestIdDispatcher.java150
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/ScoListener.java91
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/TypedMessageListener.java40
5 files changed, 453 insertions, 0 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/JsonListener.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/JsonListener.java
new file mode 100644
index 00000000..ff8cbc5b
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/JsonListener.java
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.onap.policy.common.endpoints.listeners;
+
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.event.comm.TopicListener;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardCoderObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Listens for messages received on a topic, in JSON format, decodes them into a
+ * {@link StandardCoderObject}, and then offers the objects to the subclass.
+ */
+public abstract class JsonListener implements TopicListener {
+ private static final Logger logger = LoggerFactory.getLogger(JsonListener.class);
+
+ /**
+ * Used to decode the event.
+ */
+ private static final Coder coder = new StandardCoder();
+
+ /**
+ * Constructs the object.
+ */
+ public JsonListener() {
+ super();
+ }
+
+ @Override
+ public void onTopicEvent(CommInfrastructure infra, String topic, String event) {
+ // decode from JSON into a standard object
+ StandardCoderObject sco;
+ try {
+ sco = coder.decode(event, StandardCoderObject.class);
+
+ } catch (CoderException e) {
+ logger.warn("unable to decode: {}", event, e);
+ return;
+ }
+
+ onTopicEvent(infra, topic, sco);
+ }
+
+ /**
+ * Indicates that a standard object was received.
+ *
+ * @param infra infrastructure with which the message was received
+ * @param topic topic on which the message was received
+ * @param sco the standard object that was received
+ */
+ public abstract void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco);
+}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/MessageTypeDispatcher.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/MessageTypeDispatcher.java
new file mode 100644
index 00000000..195a7eec
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/MessageTypeDispatcher.java
@@ -0,0 +1,98 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.onap.policy.common.endpoints.listeners;
+
+import java.util.concurrent.ConcurrentHashMap;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.utils.coder.StandardCoderObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Dispatches standard objects to listeners, based on the message type extracted from the
+ * message. Only one listener may be registered for a given type.
+ */
+public class MessageTypeDispatcher extends JsonListener {
+ private static final Logger logger = LoggerFactory.getLogger(MessageTypeDispatcher.class);
+
+ /**
+ * Name of the message field, which may be hierarchical.
+ */
+ private final String[] messageFieldNames;
+
+ /**
+ * Name of the message field, joined with "." - for logging.
+ */
+ private final String fullMessageFieldName;
+
+ /**
+ * Maps a message type to its listener.
+ */
+ private final ConcurrentHashMap<String, ScoListener<?>> type2listener = new ConcurrentHashMap<>();
+
+ /**
+ * Constructs the object.
+ *
+ * @param messageFieldNames name of the message field, which may be hierarchical
+ */
+ public MessageTypeDispatcher(String... messageFieldNames) {
+ this.messageFieldNames = messageFieldNames;
+ this.fullMessageFieldName = String.join(".", messageFieldNames);
+ }
+
+ /**
+ * Registers a listener for a certain type of message.
+ *
+ * @param type type of message of interest to the listener
+ * @param listener listener to register
+ */
+ public <T> void register(String type, ScoListener<T> listener) {
+ type2listener.put(type, listener);
+ }
+
+ /**
+ * Unregisters the listener associated with the specified message type.
+ *
+ * @param type type of message whose listener is to be unregistered
+ */
+ public void unregister(String type) {
+ type2listener.remove(type);
+ }
+
+ @Override
+ public void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco) {
+ // extract the message type
+ final String type = sco.getString(messageFieldNames);
+ if (type == null) {
+ logger.warn("unable to extract {}: {}", fullMessageFieldName, sco);
+ return;
+ }
+
+ // dispatch the message
+ ScoListener<?> listener = type2listener.get(type);
+ if (listener == null) {
+ logger.info("discarding event of type {}", type);
+ return;
+ }
+
+ listener.onTopicEvent(infra, topic, sco);
+ }
+}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/RequestIdDispatcher.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/RequestIdDispatcher.java
new file mode 100644
index 00000000..9ba73c9b
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/RequestIdDispatcher.java
@@ -0,0 +1,150 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.onap.policy.common.endpoints.listeners;
+
+import com.google.common.base.Strings;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.utils.coder.StandardCoderObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Dispatches messages to listeners based on the request id extracted from the message. A
+ * listener may be registered for a specific request id or for messages that have no
+ * request id (i.e., autonomous messages). Note: only one listener may be registered for a
+ * specific request id.
+ *
+ * @param <T> type of message/POJO this handles
+ */
+public class RequestIdDispatcher<T> extends ScoListener<T> {
+
+ private static final Logger logger = LoggerFactory.getLogger(RequestIdDispatcher.class);
+
+ /**
+ * Name of the request id field, which may be hierarchical.
+ */
+ private final String[] requestIdFieldNames;
+
+ /**
+ * Listeners for autonomous messages.
+ */
+ private final ConcurrentLinkedQueue<TypedMessageListener<T>> listeners = new ConcurrentLinkedQueue<>();
+
+ /**
+ * Listeners for specific request ids.
+ */
+ private final ConcurrentHashMap<String, TypedMessageListener<T>> req2listener = new ConcurrentHashMap<>();
+
+ /**
+ * Constructs the object.
+ *
+ * @param clazz class of message this handles
+ * @param requestIdFieldNames name of the request id field, which may be hierarchical
+ */
+ public RequestIdDispatcher(Class<T> clazz, String... requestIdFieldNames) {
+ super(clazz);
+ this.requestIdFieldNames = requestIdFieldNames;
+ }
+
+ /**
+ * Registers a listener for autonomous messages.
+ *
+ * @param listener listener to be registered
+ */
+ public void register(TypedMessageListener<T> listener) {
+ listeners.add(listener);
+ }
+
+ /**
+ * Registers a listener for a particular request id.
+ *
+ * @param reqid request id of interest
+ * @param listener listener to be registered
+ */
+ public void register(String reqid, TypedMessageListener<T> listener) {
+ if (Strings.isNullOrEmpty(reqid)) {
+ throw new IllegalArgumentException("attempt to register a listener with an empty request id");
+ }
+
+ req2listener.put(reqid, listener);
+ }
+
+ /**
+ * Unregisters a listener for autonomous messages.
+ *
+ * @param listener listener to be unregistered
+ */
+ public void unregister(TypedMessageListener<T> listener) {
+ listeners.remove(listener);
+ }
+
+ /**
+ * Unregisters the listener associated with a particular request id.
+ *
+ * @param reqid request id whose listener is to be unregistered
+ */
+ public void unregister(String reqid) {
+ req2listener.remove(reqid);
+ }
+
+ @Override
+ public void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco, T message) {
+
+ // extract the request id
+ String reqid = sco.getString(requestIdFieldNames);
+
+ // dispatch the message
+ if (Strings.isNullOrEmpty(reqid)) {
+ // it's an autonomous message - offer it to all autonomous listeners
+ for (TypedMessageListener<T> listener : listeners) {
+ offerToListener(infra, topic, message, listener);
+ }
+
+ } else {
+ // it's a response to a particular request
+ offerToListener(infra, topic, message, req2listener.get(reqid));
+ }
+ }
+
+ /**
+ * Offers a message to a listener.
+ *
+ * @param infra infrastructure on which the message was received
+ * @param topic topic on which the message was received
+ * @param msg message that was received
+ * @param listener listener to which the message should be offered, or {@code null}
+ */
+ private void offerToListener(CommInfrastructure infra, String topic, T msg, TypedMessageListener<T> listener) {
+
+ if (listener == null) {
+ return;
+ }
+
+ try {
+ listener.onTopicEvent(infra, topic, msg);
+
+ } catch (RuntimeException e) {
+ logger.warn("listener {} failed to process message: {}", listener, msg, e);
+ }
+ }
+}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/ScoListener.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/ScoListener.java
new file mode 100644
index 00000000..a3d33965
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/ScoListener.java
@@ -0,0 +1,91 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.onap.policy.common.endpoints.listeners;
+
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardCoderObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Listens for receipt of a {@link StandardCoderObject}, translating it into an object of
+ * the appropriate type, and then passing it to the subclass.
+ *
+ * @param <T> type of message/POJO this handles
+ */
+public abstract class ScoListener<T> {
+
+ private static final Logger logger = LoggerFactory.getLogger(ScoListener.class);
+
+ /**
+ * Used to translate the standard object to an object of type "T".
+ */
+ private static final Coder coder = new StandardCoder();
+
+ /**
+ * Class of message this handles.
+ */
+ private final Class<T> clazz;
+
+ /**
+ * Constructs the object.
+ *
+ * @param clazz class of message this handles
+ */
+ public ScoListener(Class<T> clazz) {
+ this.clazz = clazz;
+ }
+
+ /**
+ * Receives an event, translates it into the desired type of object, and passes it to
+ * the subclass.
+ *
+ * @param infra infrastructure with which the message was received
+ * @param topic topic on which the message was received
+ * @param sco event that was received
+ */
+ public void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco) {
+ // translate the event to the desired object type
+ final T msg;
+ try {
+ msg = coder.fromStandard(sco, clazz);
+
+ } catch (CoderException e) {
+ logger.warn("unable to decode {}: {}", clazz.getName(), sco, e);
+ return;
+ }
+
+ onTopicEvent(infra, topic, sco, msg);
+ }
+
+ /**
+ * Indicates that a message was received.
+ *
+ * @param infra infrastructure with which the message was received
+ * @param topic topic on which the message was received
+ * @param sco event that was received
+ * @param message message that was received
+ */
+ public abstract void onTopicEvent(CommInfrastructure infra, String topic, StandardCoderObject sco, T message);
+}
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/TypedMessageListener.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/TypedMessageListener.java
new file mode 100644
index 00000000..ab381205
--- /dev/null
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/listeners/TypedMessageListener.java
@@ -0,0 +1,40 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.onap.policy.common.endpoints.listeners;
+
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+
+/**
+ * Listener for messages of a certain type.
+ *
+ * @param <T> type of message/POJO this handles
+ */
+public interface TypedMessageListener<T> {
+
+ /**
+ * Handles receipt of a message.
+ *
+ * @param infra infrastructure with which the message was received
+ * @param topic topic on which the message was received
+ * @param message message that was received
+ */
+ void onTopicEvent(CommInfrastructure infra, String topic, T message);
+}