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.participant.intermediary.handler;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.List;
28 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
29 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
30 import org.onap.policy.common.endpoints.event.comm.TopicSink;
31 import org.onap.policy.common.endpoints.event.comm.TopicSource;
32 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.utils.services.ServiceManagerContainer;
34 import org.springframework.context.event.ContextClosedEvent;
35 import org.springframework.context.event.ContextRefreshedEvent;
36 import org.springframework.context.event.EventListener;
37 import org.springframework.stereotype.Component;
40 * This class activates the Participant Intermediary together with all its handlers.
43 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
45 private static final String[] MSG_TYPE_NAMES = {"messageType"};
47 // Topics from which the participant receives and to which the participant sends messages
48 private List<TopicSink> topicSinks;
49 private List<TopicSource> topicSources;
51 private ParticipantHandler participantHandler;
54 private final MessageTypeDispatcher msgDispatcher;
57 * Instantiate the activator for participant.
59 * @param parameters the ParticipantParameters
60 * @param participantHandler the ParticipantHandler
61 * @param publishers list of Publishers
62 * @param listeners list of Listeners
64 public <T> IntermediaryActivator(final ParticipantParameters parameters,
65 ParticipantHandler participantHandler, List<Publisher> publishers,
66 List<Listener<T>> listeners) {
67 this.participantHandler = participantHandler;
69 topicSinks = TopicEndpointManager.getManager()
70 .addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks());
72 topicSources = TopicEndpointManager.getManager()
73 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
75 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
78 addAction("Topic endpoint management",
79 () -> TopicEndpointManager.getManager().start(),
80 () -> TopicEndpointManager.getManager().shutdown());
82 publishers.forEach(publisher ->
83 addAction("Publisher " + publisher.getClass().getSimpleName(),
84 () -> publisher.active(topicSinks),
87 listeners.forEach(listener ->
88 addAction("Listener " + listener.getClass().getSimpleName(),
89 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
90 () -> msgDispatcher.unregister(listener.getType())));
92 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
97 * Handle ContextRefreshEvent.
99 * @param ctxRefreshedEvent ContextRefreshedEvent
102 public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
105 sendParticipantRegister();
110 * Handle ContextClosedEvent.
112 * @param ctxClosedEvent ContextClosedEvent
115 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
117 sendParticipantDeregister();
122 private void sendParticipantRegister() {
123 participantHandler.sendParticipantRegister();
126 private void sendParticipantDeregister() {
127 participantHandler.sendParticipantDeregister();
131 * Registers the dispatcher with the topic source(s).
133 private void registerMsgDispatcher() {
134 for (final TopicSource source : topicSources) {
135 source.register(msgDispatcher);
140 * Unregisters the dispatcher from the topic source(s).
142 private void unregisterMsgDispatcher() {
143 for (final TopicSource source : topicSources) {
144 source.unregister(msgDispatcher);
149 public void close() throws IOException {