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.util.List;
26 import java.util.function.UnaryOperator;
27 import java.util.stream.Collectors;
29 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
30 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
31 import org.onap.policy.common.endpoints.event.comm.Topic;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.event.comm.TopicSource;
35 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
36 import org.onap.policy.common.utils.services.ServiceManagerContainer;
37 import org.springframework.context.event.ContextClosedEvent;
38 import org.springframework.context.event.ContextRefreshedEvent;
39 import org.springframework.context.event.EventListener;
40 import org.springframework.stereotype.Component;
43 public class MessageDispatcherActivator extends ServiceManagerContainer implements Closeable {
45 private static final String[] MSG_TYPE_NAMES = {"messageType"};
47 // Topics from which the application receives and to which the application sends messages
48 private List<TopicSink> topicSinks;
49 private List<TopicSource> topicSources;
52 private final MessageTypeDispatcher msgDispatcher;
57 * @param acRuntimeParameterGroup the parameters for the automation composition runtime service
58 * @param publishers list of Publishers
59 * @param listeners list of Listeners
60 * @throws AutomationCompositionRuntimeException if the activator does not start
62 public <T> MessageDispatcherActivator(final AcRuntimeParameterGroup acRuntimeParameterGroup,
63 List<Publisher> publishers, List<Listener<T>> listeners) {
64 topicSinks = TopicEndpointManager.getManager()
65 .addTopicSinks(acRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
67 topicSources = TopicEndpointManager.getManager()
68 .addTopicSources(acRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
70 var topics = acRuntimeParameterGroup.getTopics();
72 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
74 var topicMap = topicSinks.stream()
75 .collect(Collectors.toMap(Topic::getTopic, UnaryOperator.identity()));
79 addAction("Topic endpoint management",
80 () -> TopicEndpointManager.getManager().start(),
81 () -> TopicEndpointManager.getManager().shutdown());
83 publishers.forEach(publisher ->
84 addAction("Publisher " + publisher.getClass().getSimpleName(),
85 () -> publisher.active(publisher.isDefaultTopic() ? topicMap.get(topics.getOperationTopic())
86 : topicMap.get(topics.getSyncTopic())),
89 listeners.forEach(listener ->
90 addAction("Listener " + listener.getClass().getSimpleName(),
91 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
92 () -> msgDispatcher.unregister(listener.getType())));
94 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
99 * Registers the dispatcher with the topic source(s).
101 private void registerMsgDispatcher() {
102 for (final var source : topicSources) {
103 source.register(msgDispatcher);
108 * Unregisters the dispatcher from the topic source(s).
110 private void unregisterMsgDispatcher() {
111 for (final var source : topicSources) {
112 source.unregister(msgDispatcher);
117 * Start Manager after the application is Started.
119 * @param cre Refreshed Event
122 public void handleContextStart(ContextRefreshedEvent cre) {
129 * Handle ContextClosedEvent.
131 * @param ctxClosedEvent ContextClosedEvent
134 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
141 public void close() {