2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.config.messaging;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.List;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
30 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
31 import org.onap.policy.common.endpoints.event.comm.TopicSink;
32 import org.onap.policy.common.endpoints.event.comm.TopicSource;
33 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
34 import org.onap.policy.common.utils.services.ServiceManagerContainer;
35 import org.springframework.context.event.ContextClosedEvent;
36 import org.springframework.context.event.ContextRefreshedEvent;
37 import org.springframework.context.event.EventListener;
38 import org.springframework.stereotype.Component;
41 public class MessageDispatcherActivator extends ServiceManagerContainer implements Closeable {
43 private static final String[] MSG_TYPE_NAMES = {"messageType"};
45 // Topics from which the application receives and to which the application sends messages
46 private List<TopicSink> topicSinks;
47 private List<TopicSource> topicSources;
50 private final MessageTypeDispatcher msgDispatcher;
55 * @param clRuntimeParameterGroup the parameters for the control loop runtime service
56 * @param publishers list of Publishers
57 * @param listeners list of Listeners
58 * @throws ControlLoopRuntimeException if the activator does not start
60 public <T> MessageDispatcherActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup,
61 List<Publisher> publishers, List<Listener<T>> listeners) {
62 topicSinks = TopicEndpointManager.getManager()
63 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
65 topicSources = TopicEndpointManager.getManager()
66 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
68 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
71 addAction("Topic endpoint management",
72 () -> TopicEndpointManager.getManager().start(),
73 () -> TopicEndpointManager.getManager().shutdown());
75 publishers.forEach(publisher ->
76 addAction("Publisher " + publisher.getClass().getSimpleName(),
77 () -> publisher.active(topicSinks),
80 listeners.forEach(listener ->
81 addAction("Listener " + listener.getClass().getSimpleName(),
82 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
83 () -> msgDispatcher.unregister(listener.getType())));
85 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
90 * Registers the dispatcher with the topic source(s).
92 private void registerMsgDispatcher() {
93 for (final TopicSource source : topicSources) {
94 source.register(msgDispatcher);
99 * Unregisters the dispatcher from the topic source(s).
101 private void unregisterMsgDispatcher() {
102 for (final TopicSource source : topicSources) {
103 source.unregister(msgDispatcher);
108 * Start Manager after the application is Started.
110 * @param cre Refreshed Event
113 public void handleContextStart(ContextRefreshedEvent cre) {
120 * Handle ContextClosedEvent.
122 * @param ctxClosedEvent ContextClosedEvent
125 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
132 public void close() throws IOException {