d544915dddd1490c8c0de35b883656436abad8e7
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.participant.intermediary.handler;
23
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.util.List;
27 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
28 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
29 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
30 import org.onap.policy.common.endpoints.event.comm.TopicSink;
31 import org.onap.policy.common.endpoints.event.comm.TopicSource;
32 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.utils.services.ServiceManagerContainer;
34 import org.springframework.context.event.ContextClosedEvent;
35 import org.springframework.context.event.ContextRefreshedEvent;
36 import org.springframework.context.event.EventListener;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * This class activates the Participant Intermediary together with all its handlers.
41  */
42 @Component
43 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
44
45     private static final String[] MSG_TYPE_NAMES = {"messageType"};
46
47     // Topics from which the participant receives and to which the participant sends messages
48     private List<TopicSink> topicSinks;
49     private List<TopicSource> topicSources;
50
51     ParticipantIntermediaryApi participantIntermediaryApi;
52
53     private final MessageTypeDispatcher msgDispatcher;
54
55     /**
56      * Instantiate the activator for participant.
57      *
58      * @param parameters the ParticipantParameters
59      * @param publishers list of Publishers
60      * @param listeners list of Listeners
61      */
62     public <T> IntermediaryActivator(final ParticipantParameters parameters,
63             ParticipantIntermediaryApi participantIntermediaryApi, List<Publisher> publishers,
64             List<Listener<T>> listeners) {
65         this.participantIntermediaryApi = participantIntermediaryApi;
66
67         topicSinks = TopicEndpointManager.getManager()
68                 .addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks());
69
70         topicSources = TopicEndpointManager.getManager()
71                 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
72
73         msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
74
75         // @formatter:off
76         addAction("Topic endpoint management",
77                 () -> TopicEndpointManager.getManager().start(),
78                 () -> TopicEndpointManager.getManager().shutdown());
79
80         publishers.forEach(publisher ->
81             addAction("Publisher " + publisher.getClass().getSimpleName(),
82                 () -> publisher.active(topicSinks),
83                 publisher::stop));
84
85         listeners.forEach(listener ->
86             addAction("Listener " + listener.getClass().getSimpleName(),
87                     () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
88                     () -> msgDispatcher.unregister(listener.getType())));
89
90         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
91         // @formatter:on
92     }
93
94     /**
95      * Handle ContextRefreshEvent.
96      *
97      * @param ctxRefreshedEvent ContextRefreshedEvent
98      */
99     @EventListener
100     public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
101         if (!isAlive()) {
102             start();
103             sendParticipantRegister();
104         }
105     }
106
107     /**
108      * Handle ContextClosedEvent.
109      *
110      * @param ctxClosedEvent ContextClosedEvent
111      */
112     @EventListener
113     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
114         if (isAlive()) {
115             sendParticipantDeregister();
116             stop();
117         }
118     }
119
120     private void sendParticipantRegister() {
121         participantIntermediaryApi.sendParticipantRegister();
122     }
123
124     private void sendParticipantDeregister() {
125         participantIntermediaryApi.sendParticipantDeregister();
126     }
127
128     /**
129      * Registers the dispatcher with the topic source(s).
130      */
131     private void registerMsgDispatcher() {
132         for (final TopicSource source : topicSources) {
133             source.register(msgDispatcher);
134         }
135     }
136
137     /**
138      * Unregisters the dispatcher from the topic source(s).
139      */
140     private void unregisterMsgDispatcher() {
141         for (final TopicSource source : topicSources) {
142             source.unregister(msgDispatcher);
143         }
144     }
145
146     @Override
147     public void close() throws IOException {
148         if (isAlive()) {
149             super.shutdown();
150         }
151     }
152 }