754bf2887ca149716493cdb60f9233d6223b8be6
[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.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
27 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
28 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
29 import org.onap.policy.common.endpoints.event.comm.TopicSink;
30 import org.onap.policy.common.endpoints.event.comm.TopicSource;
31 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
32 import org.onap.policy.common.utils.services.ServiceManagerContainer;
33 import org.springframework.context.event.ContextClosedEvent;
34 import org.springframework.context.event.ContextRefreshedEvent;
35 import org.springframework.context.event.EventListener;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * This class activates the Participant Intermediary together with all its handlers.
40  */
41 @Component
42 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
43
44     private static final String[] MSG_TYPE_NAMES = {"messageType"};
45
46     // Topics from which the participant receives and to which the participant sends messages
47     private List<TopicSink> topicSinks;
48     private List<TopicSource> topicSources;
49
50     ParticipantIntermediaryApi participantIntermediaryApi;
51
52     private final MessageTypeDispatcher msgDispatcher;
53
54     /**
55      * Instantiate the activator for participant.
56      *
57      * @param parameters the ParticipantParameters
58      * @param publishers list of Publishers
59      * @param listeners list of Listeners
60      */
61     public IntermediaryActivator(final ParticipantParameters parameters,
62             ParticipantIntermediaryApi participantIntermediaryApi, List<Publisher> publishers,
63             List<Listener> listeners) {
64         this.participantIntermediaryApi = participantIntermediaryApi;
65
66         topicSinks = TopicEndpointManager.getManager()
67                 .addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks());
68
69         topicSources = TopicEndpointManager.getManager()
70                 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
71
72         msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
73
74         // @formatter:off
75         addAction("Topic endpoint management",
76                 () -> TopicEndpointManager.getManager().start(),
77                 () -> TopicEndpointManager.getManager().shutdown());
78
79         publishers.forEach(publisher ->
80             addAction("Publisher " + publisher.getClass().getSimpleName(),
81                 () -> publisher.active(topicSinks),
82                 publisher::stop));
83
84         listeners.forEach(listener ->
85             addAction("Listener " + listener.getClass().getSimpleName(),
86                     () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
87                     () -> msgDispatcher.unregister(listener.getType())));
88
89         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
90         // @formatter:on
91     }
92
93     /**
94      * Handle ContextRefreshEvent.
95      *
96      * @param ctxRefreshedEvent ContextRefreshedEvent
97      */
98     @EventListener
99     public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
100         if (!isAlive()) {
101             start();
102             sendParticipantRegister();
103         }
104     }
105
106     /**
107      * Handle ContextClosedEvent.
108      *
109      * @param ctxClosedEvent ContextClosedEvent
110      */
111     @EventListener
112     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
113         if (isAlive()) {
114             sendParticipantDeregister();
115             stop();
116         }
117     }
118
119     private void sendParticipantRegister() {
120         participantIntermediaryApi.sendParticipantRegister();
121     }
122
123     private void sendParticipantDeregister() {
124         participantIntermediaryApi.sendParticipantDeregister();
125     }
126
127     /**
128      * Registers the dispatcher with the topic source(s).
129      */
130     private void registerMsgDispatcher() {
131         for (final TopicSource source : topicSources) {
132             source.register(msgDispatcher);
133         }
134     }
135
136     /**
137      * Unregisters the dispatcher from the topic source(s).
138      */
139     private void unregisterMsgDispatcher() {
140         for (final TopicSource source : topicSources) {
141             source.unregister(msgDispatcher);
142         }
143     }
144
145     @Override
146     public void close() throws IOException {
147         if (isAlive()) {
148             super.shutdown();
149         }
150     }
151 }