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 org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
28 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
29 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
30 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
31 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
33 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
35 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantHealthCheckListener;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStateChangeListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
38 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
39 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
40 import org.onap.policy.common.endpoints.event.comm.TopicSink;
41 import org.onap.policy.common.endpoints.event.comm.TopicSource;
42 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
43 import org.onap.policy.common.endpoints.listeners.ScoListener;
44 import org.onap.policy.common.utils.services.ServiceManagerContainer;
47 * This class activates the Participant Intermediary together with all its handlers.
49 public class IntermediaryActivator extends ServiceManagerContainer {
50 // Name of the message type for messages on topics
51 private static final String[] MSG_TYPE_NAMES = {"messageType"};
54 private final ParticipantIntermediaryParameters parameters;
56 // Topics from which the participant receives and to which the participant sends messages
57 private List<TopicSink> topicSinks;
58 private List<TopicSource> topicSources;
60 // The participant handler for this intermediary
61 final AtomicReference<ParticipantHandler> participantHandler = new AtomicReference<>();
64 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
66 private final MessageTypeDispatcher msgDispatcher;
69 * Instantiate the activator for participant.
71 * @param parameters the parameters for the participant intermediary
72 * @throws ControlLoopRuntimeException when the activation fails
74 public IntermediaryActivator(final ParticipantIntermediaryParameters parameters) {
75 this.parameters = parameters;
78 TopicEndpointManager.getManager().addTopicSinks(parameters.getClampControlLoopTopics().getTopicSinks());
81 TopicEndpointManager.getManager().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(participantHandler.get()));
135 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_HEALTH_CHECK.name(),
136 (ScoListener<ParticipantHealthCheck>) new ParticipantHealthCheckListener(participantHandler.get()));
137 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_STATE_CHANGE.name(),
138 (ScoListener<ParticipantControlLoopStateChange>) new ControlLoopStateChangeListener(
139 participantHandler.get()));
140 msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_UPDATE.name(),
141 (ScoListener<ParticipantControlLoopUpdate>) new ControlLoopUpdateListener(participantHandler.get()));
142 for (final TopicSource source : topicSources) {
143 source.register(msgDispatcher);
148 * Unregisters the dispatcher from the topic source(s).
150 private void unregisterMsgDispatcher() {
151 for (final TopicSource source : topicSources) {
152 source.unregister(msgDispatcher);
157 * Return the participant handler.
159 * @return the participant handler
161 public ParticipantHandler getParticipantHandler() {
162 return participantHandler.get();