2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.config.messaging;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.stream.Stream;
27 import javax.ws.rs.core.Response.Status;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
30 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
31 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
32 import org.onap.policy.common.endpoints.event.comm.TopicSink;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
35 import org.onap.policy.common.utils.services.ServiceManagerContainer;
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 array of Publishers
57 * @param listeners array of Listeners
58 * @throws ControlLoopRuntimeException if the activator does not start
60 public MessageDispatcherActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup, Publisher[] publishers,
61 Listener[] listeners) {
62 topicSinks = TopicEndpointManager.getManager()
63 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
65 topicSources = TopicEndpointManager.getManager()
66 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
69 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
70 } catch (final RuntimeException e) {
71 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
72 "topic message dispatcher failed to start", e);
76 addAction("Topic endpoint management",
77 () -> TopicEndpointManager.getManager().start(),
78 () -> TopicEndpointManager.getManager().shutdown());
80 Stream.of(publishers).forEach(publisher ->
81 addAction("Publisher " + publisher.getClass().getSimpleName(),
82 () -> publisher.active(topicSinks),
83 () -> publisher.stop()));
85 Stream.of(listeners).forEach(listener ->
86 addAction("Listener " + listener.getClass().getSimpleName(),
87 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
88 () -> msgDispatcher.unregister(listener.getType())));
90 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
95 * Registers the dispatcher with the topic source(s).
97 private void registerMsgDispatcher() {
98 for (final TopicSource source : topicSources) {
99 source.register(msgDispatcher);
104 * Unregisters the dispatcher from the topic source(s).
106 private void unregisterMsgDispatcher() {
107 for (final TopicSource source : topicSources) {
108 source.unregister(msgDispatcher);
113 * Start Manager after the application is Started.
115 * @param cre Refreshed Event
118 public void handleContextStart(ContextRefreshedEvent cre) {
125 public void close() throws IOException {