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.participant.intermediary.handler;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
27 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
28 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
29 import org.onap.policy.common.endpoints.event.comm.TopicSink;
30 import org.onap.policy.common.endpoints.event.comm.TopicSource;
31 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
32 import org.onap.policy.common.utils.services.ServiceManagerContainer;
33 import org.springframework.context.event.ContextClosedEvent;
34 import org.springframework.context.event.ContextRefreshedEvent;
35 import org.springframework.context.event.EventListener;
36 import org.springframework.stereotype.Component;
39 * This class activates the Participant Intermediary together with all its handlers.
42 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
44 private static final String[] MSG_TYPE_NAMES = {"messageType"};
46 // Topics from which the participant receives and to which the participant sends messages
47 private List<TopicSink> topicSinks;
48 private List<TopicSource> topicSources;
50 ParticipantIntermediaryApi participantIntermediaryApi;
52 private final MessageTypeDispatcher msgDispatcher;
55 * Instantiate the activator for participant.
57 * @param parameters the ParticipantParameters
58 * @param publishers list of Publishers
59 * @param listeners list of Listeners
61 public IntermediaryActivator(final ParticipantParameters parameters,
62 ParticipantIntermediaryApi participantIntermediaryApi, List<Publisher> publishers,
63 List<Listener> listeners) {
64 this.participantIntermediaryApi = participantIntermediaryApi;
66 topicSinks = TopicEndpointManager.getManager()
67 .addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks());
69 topicSources = TopicEndpointManager.getManager()
70 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
72 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
75 addAction("Topic endpoint management",
76 () -> TopicEndpointManager.getManager().start(),
77 () -> TopicEndpointManager.getManager().shutdown());
79 publishers.forEach(publisher ->
80 addAction("Publisher " + publisher.getClass().getSimpleName(),
81 () -> publisher.active(topicSinks),
84 listeners.forEach(listener ->
85 addAction("Listener " + listener.getClass().getSimpleName(),
86 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
87 () -> msgDispatcher.unregister(listener.getType())));
89 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
94 * Handle ContextRefreshEvent.
96 * @param ctxRefreshedEvent ContextRefreshedEvent
99 public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
102 sendParticipantRegister();
107 * Handle ContextClosedEvent.
109 * @param ctxClosedEvent ContextClosedEvent
112 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
114 sendParticipantDeregister();
119 private void sendParticipantRegister() {
120 participantIntermediaryApi.sendParticipantRegister();
123 private void sendParticipantDeregister() {
124 participantIntermediaryApi.sendParticipantDeregister();
128 * Registers the dispatcher with the topic source(s).
130 private void registerMsgDispatcher() {
131 for (final TopicSource source : topicSources) {
132 source.register(msgDispatcher);
137 * Unregisters the dispatcher from the topic source(s).
139 private void unregisterMsgDispatcher() {
140 for (final TopicSource source : topicSources) {
141 source.unregister(msgDispatcher);
146 public void close() throws IOException {