3eae27267f408c1139f69a611534ff7384aef910
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.intermediary.handler;
22
23 import java.util.List;
24 import java.util.concurrent.atomic.AtomicReference;
25 import javax.ws.rs.core.Response.Status;
26 import lombok.Getter;
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;
45
46 /**
47  * This class activates the Participant Intermediary together with all its handlers.
48  */
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"};
52
53     @Getter
54     private final ParticipantIntermediaryParameters parameters;
55
56     // Topics from which the participant receives and to which the participant sends messages
57     private List<TopicSink> topicSinks;
58     private List<TopicSource> topicSources;
59
60     // The participant handler for this intermediary
61     final AtomicReference<ParticipantHandler> participantHandler = new AtomicReference<>();
62
63     /**
64      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
65      */
66     private final MessageTypeDispatcher msgDispatcher;
67
68     /**
69      * Instantiate the activator for participant.
70      *
71      * @param parameters the parameters for the participant intermediary
72      * @throws ControlLoopRuntimeException when the activation fails
73      */
74     public IntermediaryActivator(final ParticipantIntermediaryParameters parameters) {
75         this.parameters = parameters;
76
77         topicSinks =
78             TopicEndpointManager.getManager().addTopicSinks(parameters.getClampControlLoopTopics().getTopicSinks());
79
80         topicSources =
81             TopicEndpointManager.getManager().addTopicSources(parameters.getClampControlLoopTopics().getTopicSources());
82
83         try {
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);
88         }
89
90         // @formatter:off
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<>();
96
97         addAction("Topic endpoint management",
98             () -> TopicEndpointManager.getManager().start(),
99             () -> TopicEndpointManager.getManager().shutdown());
100
101         addAction("Participant Status Publisher",
102             () -> statusPublisher.set(new ParticipantStatusPublisher(topicSinks)),
103             () -> statusPublisher.get().close());
104
105         addAction("Participant Handler",
106             () -> participantHandler.set(new ParticipantHandler(parameters, statusPublisher.get())),
107             () -> participantHandler.get().close());
108
109         addAction("Participant State Change Listener",
110             () -> participantStateChangeListener.set(new ParticipantStateChangeListener(participantHandler.get())),
111             () -> participantStateChangeListener.get().close());
112
113         addAction("Participant Health Check Listener",
114             () -> participantHealthCheckListener.set(new ParticipantHealthCheckListener(participantHandler.get())),
115             () -> participantHealthCheckListener.get().close());
116
117         addAction("Control Loop State Change Listener",
118             () -> controlLoopStateChangeListener.set(new ControlLoopStateChangeListener(participantHandler.get())),
119             () -> controlLoopStateChangeListener.get().close());
120
121         addAction("Control Loop Update Listener",
122             () -> controlLoopUpdateListener.set(new ControlLoopUpdateListener(participantHandler.get())),
123             () -> controlLoopUpdateListener.get().close());
124
125         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
126         // @formatter:on
127     }
128
129     /**
130      * Registers the dispatcher with the topic source(s).
131      */
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);
144         }
145     }
146
147     /**
148      * Unregisters the dispatcher from the topic source(s).
149      */
150     private void unregisterMsgDispatcher() {
151         for (final TopicSource source : topicSources) {
152             source.unregister(msgDispatcher);
153         }
154     }
155
156     /**
157      * Return the participant handler.
158      *
159      * @return the participant handler
160      */
161     public ParticipantHandler getParticipantHandler() {
162         return participantHandler.get();
163     }
164 }