dd0cf30a89735642cf960fd7684349ff1fd1f82a
[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.participant.intermediary.comm.ControlLoopUpdateListener;
30 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStatusPublisher;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.event.comm.TopicSource;
35 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
36 import org.onap.policy.common.utils.services.ServiceManagerContainer;
37
38 /**
39  * This class activates the Participant Intermediary together with all its handlers.
40  */
41 public class IntermediaryActivator extends ServiceManagerContainer {
42     // Name of the message type for messages on topics
43     private static final String[] MSG_TYPE_NAMES = {"messageType"};
44
45     @Getter
46     private final ParticipantIntermediaryParameters parameters;
47
48     // Topics from which the participant receives and to which the participant sends messages
49     private List<TopicSink> topicSinks;
50     private List<TopicSource> topicSources;
51
52     // The participant handler for this intermediary
53     final AtomicReference<ParticipantHandler> participantHandler = new AtomicReference<>();
54
55     /**
56      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
57      */
58     private final MessageTypeDispatcher msgDispatcher;
59
60     /**
61      * Instantiate the activator for participant.
62      *
63      * @param parameters the parameters for the participant intermediary
64      */
65     public IntermediaryActivator(final ParticipantIntermediaryParameters parameters) {
66         this.parameters = parameters;
67
68         topicSinks =
69                 TopicEndpointManager.getManager().addTopicSinks(parameters.getClampControlLoopTopics().getTopicSinks());
70
71         topicSources = TopicEndpointManager.getManager()
72                 .addTopicSources(parameters.getClampControlLoopTopics().getTopicSources());
73
74         try {
75             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
76         } catch (final RuntimeException e) {
77             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
78                     "topic message dispatcher failed to start", e);
79         }
80
81         // @formatter:off
82         final AtomicReference<ParticipantStatusPublisher>     statusPublisher                = new AtomicReference<>();
83         final AtomicReference<ControlLoopUpdateListener>      controlLoopUpdateListener      = new AtomicReference<>();
84
85         addAction("Topic endpoint management",
86             () -> TopicEndpointManager.getManager().start(),
87             () -> TopicEndpointManager.getManager().shutdown());
88
89         addAction("Participant Status Publisher",
90             () -> statusPublisher.set(new ParticipantStatusPublisher(topicSinks)),
91             () -> statusPublisher.get().close());
92
93         addAction("Participant Handler",
94             () -> participantHandler.set(new ParticipantHandler(parameters, statusPublisher.get())),
95             () -> participantHandler.get().close());
96
97         addAction("Control Loop Update Listener",
98             () -> controlLoopUpdateListener.set(new ControlLoopUpdateListener(participantHandler.get())),
99             () -> controlLoopUpdateListener.get().close());
100
101         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
102         // @formatter:on
103     }
104
105     /**
106      * Registers the dispatcher with the topic source(s).
107      */
108     private void registerMsgDispatcher() {
109         for (final TopicSource source : topicSources) {
110             source.register(msgDispatcher);
111         }
112     }
113
114     /**
115      * Unregisters the dispatcher from the topic source(s).
116      */
117     private void unregisterMsgDispatcher() {
118         for (final TopicSource source : topicSources) {
119             source.unregister(msgDispatcher);
120         }
121     }
122
123     /**
124      * Return the participant handler.
125      */
126     public ParticipantHandler getParticipantHandler() {
127         return participantHandler.get();
128     }
129 }