be2fa1a302384a7b5d96027c05b292e971b2c231
[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 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;
46
47 /**
48  * This class activates the Participant Intermediary together with all its handlers.
49  */
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"};
53
54     @Getter
55     private final ParticipantIntermediaryParameters parameters;
56
57     // Topics from which the participant receives and to which the participant sends messages
58     private List<TopicSink> topicSinks;
59     private List<TopicSource> topicSources;
60
61     // The participant handler for this intermediary
62     final AtomicReference<ParticipantHandler> participantHandler = new AtomicReference<>();
63
64     /**
65      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
66      */
67     private final MessageTypeDispatcher msgDispatcher;
68
69     /**
70      * Instantiate the activator for participant.
71      *
72      * @param parameters the parameters for the participant intermediary
73      */
74     public IntermediaryActivator(final ParticipantIntermediaryParameters parameters) {
75         this.parameters = parameters;
76
77         topicSinks =
78                 TopicEndpointManager.getManager().addTopicSinks(parameters.getClampControlLoopTopics().getTopicSinks());
79
80         topicSources = TopicEndpointManager.getManager()
81                 .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(
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);
147         }
148     }
149
150     /**
151      * Unregisters the dispatcher from the topic source(s).
152      */
153     private void unregisterMsgDispatcher() {
154         for (final TopicSource source : topicSources) {
155             source.unregister(msgDispatcher);
156         }
157     }
158
159     /**
160      * Return the participant handler.
161      */
162     public ParticipantHandler getParticipantHandler() {
163         return participantHandler.get();
164     }
165 }