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.models.messages.dmaap.participant.ParticipantMessageType;
27 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
28 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
29 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantHealthCheckListener;
30 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStateChangeListener;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
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.ApplicationContext;
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 * This class activates the Participant Intermediary together with all its handlers.
46 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
48 private final ApplicationContext applicationContext;
50 // Topics from which the participant receives and to which the participant sends messages
51 private List<TopicSource> topicSources;
54 * Instantiate the activator for participant.
56 * @param applicationContext ApplicationContext
57 * @param parameters the ParticipantParameters
59 public IntermediaryActivator(final ApplicationContext applicationContext, final ParticipantParameters parameters) {
60 this.applicationContext = applicationContext;
62 topicSources = TopicEndpointManager.getManager()
63 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
67 addAction("Topic endpoint management",
68 () -> TopicEndpointManager.getManager().start(),
69 () -> TopicEndpointManager.getManager().shutdown());
71 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
76 * Handle ContextRefreshEvent.
78 * @param ctxRefreshedEvent ContextRefreshedEvent
81 public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
88 * Handle ContextClosedEvent.
90 * @param ctxClosedEvent ContextClosedEvent
93 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
100 * Registers the dispatcher with the topic source(s).
102 private void registerMsgDispatcher() {
103 MessageTypeDispatcher msgDispatcher = applicationContext.getBean(MessageTypeDispatcher.class);
105 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATE_CHANGE.name(),
106 applicationContext.getBean(ParticipantStateChangeListener.class));
108 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_HEALTH_CHECK.name(),
109 applicationContext.getBean(ParticipantHealthCheckListener.class));
111 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_STATE_CHANGE.name(),
112 applicationContext.getBean(ControlLoopStateChangeListener.class));
114 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_UPDATE.name(),
115 applicationContext.getBean(ControlLoopUpdateListener.class));
117 for (final TopicSource source : topicSources) {
118 source.register(msgDispatcher);
123 * Unregisters the dispatcher from the topic source(s).
125 private void unregisterMsgDispatcher() {
126 MessageTypeDispatcher msgDispatcher = applicationContext.getBean(MessageTypeDispatcher.class);
128 for (final TopicSource source : topicSources) {
129 source.unregister(msgDispatcher);
134 public void close() throws IOException {