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