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.util.List;
24 import java.util.concurrent.atomic.AtomicReference;
25 import javax.ws.rs.core.Response.Status;
27 import lombok.experimental.Delegate;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantHealthCheckListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStateChangeListener;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
40 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
42 import org.onap.policy.common.endpoints.event.comm.TopicSource;
43 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
44 import org.onap.policy.common.endpoints.listeners.ScoListener;
45 import org.onap.policy.common.utils.services.ServiceManagerContainer;
48 * This class activates the Participant Intermediary together with all its handlers.
50 public class IntermediaryActivator extends ServiceManagerContainer {
51 // Name of the message type for messages on topics
52 private static final String[] MSG_TYPE_NAMES = {"messageType"};
55 private final ParticipantIntermediaryParameters parameters;
57 // Topics from which the participant receives and to which the participant sends messages
58 private List<TopicSink> topicSinks;
59 private List<TopicSource> topicSources;
61 // The participant handler for this intermediary
62 final AtomicReference<ParticipantHandler> participantHandler = new AtomicReference<>();
65 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
67 private final MessageTypeDispatcher msgDispatcher;
70 * Instantiate the activator for participant.
72 * @param parameters the parameters for the participant intermediary
74 public IntermediaryActivator(final ParticipantIntermediaryParameters parameters) {
75 this.parameters = parameters;
78 TopicEndpointManager.getManager().addTopicSinks(parameters.getClampControlLoopTopics().getTopicSinks());
80 topicSources = TopicEndpointManager.getManager()
81 .addTopicSources(parameters.getClampControlLoopTopics().getTopicSources());
84 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
85 } catch (final RuntimeException e) {
86 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
87 "topic message dispatcher failed to start", e);
91 final AtomicReference<ParticipantStatusPublisher> statusPublisher = new AtomicReference<>();
92 final AtomicReference<ParticipantStateChangeListener> participantStateChangeListener = new AtomicReference<>();
93 final AtomicReference<ParticipantHealthCheckListener> participantHealthCheckListener = new AtomicReference<>();
94 final AtomicReference<ControlLoopStateChangeListener> controlLoopStateChangeListener = new AtomicReference<>();
95 final AtomicReference<ControlLoopUpdateListener> controlLoopUpdateListener = new AtomicReference<>();
97 addAction("Topic endpoint management",
98 () -> TopicEndpointManager.getManager().start(),
99 () -> TopicEndpointManager.getManager().shutdown());
101 addAction("Participant Status Publisher",
102 () -> statusPublisher.set(new ParticipantStatusPublisher(topicSinks)),
103 () -> statusPublisher.get().close());
105 addAction("Participant Handler",
106 () -> participantHandler.set(new ParticipantHandler(parameters, statusPublisher.get())),
107 () -> participantHandler.get().close());
109 addAction("Participant State Change Listener",
110 () -> participantStateChangeListener.set(new ParticipantStateChangeListener(participantHandler.get())),
111 () -> participantStateChangeListener.get().close());
113 addAction("Participant Health Check Listener",
114 () -> participantHealthCheckListener.set(new ParticipantHealthCheckListener(participantHandler.get())),
115 () -> participantHealthCheckListener.get().close());
117 addAction("Control Loop State Change Listener",
118 () -> controlLoopStateChangeListener.set(new ControlLoopStateChangeListener(participantHandler.get())),
119 () -> controlLoopStateChangeListener.get().close());
121 addAction("Control Loop Update Listener",
122 () -> controlLoopUpdateListener.set(new ControlLoopUpdateListener(participantHandler.get())),
123 () -> controlLoopUpdateListener.get().close());
125 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
130 * Registers the dispatcher with the topic source(s).
132 private void registerMsgDispatcher() {
133 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATE_CHANGE.name(),
134 (ScoListener<ParticipantStateChange>) new ParticipantStateChangeListener(
135 participantHandler.get()));
136 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_HEALTH_CHECK.name(),
137 (ScoListener<ParticipantHealthCheck>) new ParticipantHealthCheckListener(
138 participantHandler.get()));
139 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_STATE_CHANGE.name(),
140 (ScoListener<ParticipantControlLoopStateChange>) new ControlLoopStateChangeListener(
141 participantHandler.get()));
142 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_UPDATE.name(),
143 (ScoListener<ParticipantControlLoopUpdate>) new ControlLoopUpdateListener(
144 participantHandler.get()));
145 for (final TopicSource source : topicSources) {
146 source.register(msgDispatcher);
151 * Unregisters the dispatcher from the topic source(s).
153 private void unregisterMsgDispatcher() {
154 for (final TopicSource source : topicSources) {
155 source.unregister(msgDispatcher);
160 * Return the participant handler.
162 public ParticipantHandler getParticipantHandler() {
163 return participantHandler.get();