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.acm.participant.intermediary.handler;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Timer;
28 import java.util.TimerTask;
30 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
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.ContextClosedEvent;
37 import org.springframework.context.event.ContextRefreshedEvent;
38 import org.springframework.context.event.EventListener;
39 import org.springframework.stereotype.Component;
42 * This class activates the Participant Intermediary together with all its handlers.
45 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
47 private static final String[] MSG_TYPE_NAMES = {"messageType"};
49 // Topics from which the participant receives and to which the participant sends messages
50 private final List<TopicSink> topicSinks;
51 private final List<TopicSource> topicSources;
53 private final ParticipantHandler participantHandler;
56 private final MessageTypeDispatcher msgDispatcher;
59 * Instantiate the activator for participant.
61 * @param parameters the ParticipantParameters
62 * @param participantHandler the ParticipantHandler
63 * @param publishers list of Publishers
64 * @param listeners list of Listeners
66 public <T> IntermediaryActivator(final ParticipantParameters parameters, ParticipantHandler participantHandler,
67 List<Publisher> publishers, List<Listener<T>> listeners) {
68 this.participantHandler = participantHandler;
70 topicSinks = TopicEndpointManager.getManager().addTopicSinks(
71 parameters.getIntermediaryParameters().getClampAutomationCompositionTopics().getTopicSinks());
73 topicSources = TopicEndpointManager.getManager().addTopicSources(
74 parameters.getIntermediaryParameters().getClampAutomationCompositionTopics().getTopicSources());
76 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
79 addAction("Topic endpoint management",
80 () -> TopicEndpointManager.getManager().start(),
81 () -> TopicEndpointManager.getManager().shutdown());
83 listeners.forEach(listener ->
84 addAction("Listener " + listener.getClass().getSimpleName(),
85 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
86 () -> msgDispatcher.unregister(listener.getType())));
88 publishers.forEach(publisher ->
89 addAction("Publisher " + publisher.getClass().getSimpleName(),
90 () -> publisher.active(topicSinks),
93 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
98 * Handle ContextRefreshEvent.
100 * @param ctxRefreshedEvent ContextRefreshedEvent
103 public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
106 var task = new TimerTask() {
109 new Thread(participantHandler::sendParticipantRegister).start();
112 new Timer().schedule(task, 5000);
117 * Handle ContextClosedEvent.
119 * @param ctxClosedEvent ContextClosedEvent
122 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
124 sendParticipantDeregister();
129 private void sendParticipantDeregister() {
130 participantHandler.sendParticipantDeregister();
134 * Registers the dispatcher with the topic source(s).
136 private void registerMsgDispatcher() {
137 for (final TopicSource source : topicSources) {
138 source.register(msgDispatcher);
143 * Unregisters the dispatcher from the topic source(s).
145 private void unregisterMsgDispatcher() {
146 for (final TopicSource source : topicSources) {
147 source.unregister(msgDispatcher);
152 public void close() throws IOException {