2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021,2024 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.acm.runtime.config.messaging;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.function.UnaryOperator;
28 import java.util.stream.Collectors;
30 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
31 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
32 import org.onap.policy.common.endpoints.event.comm.Topic;
33 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.TopicSource;
36 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
37 import org.onap.policy.common.utils.services.ServiceManagerContainer;
38 import org.springframework.context.event.ContextClosedEvent;
39 import org.springframework.context.event.ContextRefreshedEvent;
40 import org.springframework.context.event.EventListener;
41 import org.springframework.stereotype.Component;
44 public class MessageDispatcherActivator extends ServiceManagerContainer implements Closeable {
46 private static final String[] MSG_TYPE_NAMES = {"messageType"};
48 // Topics from which the application receives and to which the application sends messages
49 private List<TopicSink> topicSinks;
50 private List<TopicSource> topicSources;
53 private final MessageTypeDispatcher msgDispatcher;
58 * @param acRuntimeParameterGroup the parameters for the automation composition runtime service
59 * @param publishers list of Publishers
60 * @param listeners list of Listeners
61 * @throws AutomationCompositionRuntimeException if the activator does not start
63 public <T> MessageDispatcherActivator(final AcRuntimeParameterGroup acRuntimeParameterGroup,
64 List<Publisher> publishers, List<Listener<T>> listeners) {
65 topicSinks = TopicEndpointManager.getManager()
66 .addTopicSinks(acRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
68 topicSources = TopicEndpointManager.getManager()
69 .addTopicSources(acRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
71 var topics = acRuntimeParameterGroup.getTopics();
73 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
75 var topicMap = topicSinks.stream()
76 .collect(Collectors.toMap(Topic::getTopic, UnaryOperator.identity()));
80 addAction("Topic endpoint management",
81 () -> TopicEndpointManager.getManager().start(),
82 () -> TopicEndpointManager.getManager().shutdown());
84 publishers.forEach(publisher ->
85 addAction("Publisher " + publisher.getClass().getSimpleName(),
86 () -> publisher.active(publisher.isDefaultTopic() ? topicMap.get(topics.getOperationTopic())
87 : topicMap.get(topics.getSyncTopic())),
90 listeners.forEach(listener ->
91 addAction("Listener " + listener.getClass().getSimpleName(),
92 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
93 () -> msgDispatcher.unregister(listener.getType())));
95 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
100 * Registers the dispatcher with the topic source(s).
102 private void registerMsgDispatcher() {
103 for (final var source : topicSources) {
104 source.register(msgDispatcher);
109 * Unregisters the dispatcher from the topic source(s).
111 private void unregisterMsgDispatcher() {
112 for (final var source : topicSources) {
113 source.unregister(msgDispatcher);
118 * Start Manager after the application is Started.
120 * @param cre Refreshed Event
123 public void handleContextStart(ContextRefreshedEvent cre) {
130 * Handle ContextClosedEvent.
132 * @param ctxClosedEvent ContextClosedEvent
135 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
142 public void close() throws IOException {