0144f669cc34878aa17cb2e598bbff02a3d51b7a
[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 lombok.Getter;
28 import org.onap.policy.clamp.acm.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     private ParticipantHandler participantHandler;
52
53     @Getter
54     private final MessageTypeDispatcher msgDispatcher;
55
56     /**
57      * Instantiate the activator for participant.
58      *
59      * @param parameters the ParticipantParameters
60      * @param participantHandler the ParticipantHandler
61      * @param publishers list of Publishers
62      * @param listeners list of Listeners
63      */
64     public <T> IntermediaryActivator(final ParticipantParameters parameters, ParticipantHandler participantHandler,
65         List<Publisher> publishers, List<Listener<T>> listeners) {
66         this.participantHandler = participantHandler;
67
68         topicSinks = TopicEndpointManager.getManager().addTopicSinks(
69             parameters.getIntermediaryParameters().getClampAutomationCompositionTopics().getTopicSinks());
70
71         topicSources = TopicEndpointManager.getManager().addTopicSources(
72             parameters.getIntermediaryParameters().getClampAutomationCompositionTopics().getTopicSources());
73
74         msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
75
76         // @formatter:off
77         addAction("Topic endpoint management",
78             () -> TopicEndpointManager.getManager().start(),
79             () -> TopicEndpointManager.getManager().shutdown());
80
81         publishers.forEach(publisher ->
82             addAction("Publisher " + publisher.getClass().getSimpleName(),
83                 () -> publisher.active(topicSinks),
84                 publisher::stop));
85
86         listeners.forEach(listener ->
87             addAction("Listener " + listener.getClass().getSimpleName(),
88                 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
89                 () -> msgDispatcher.unregister(listener.getType())));
90
91         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
92         // @formatter:on
93     }
94
95     /**
96      * Handle ContextRefreshEvent.
97      *
98      * @param ctxRefreshedEvent ContextRefreshedEvent
99      */
100     @EventListener
101     public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
102         if (!isAlive()) {
103             start();
104             sendParticipantRegister();
105         }
106     }
107
108     /**
109      * Handle ContextClosedEvent.
110      *
111      * @param ctxClosedEvent ContextClosedEvent
112      */
113     @EventListener
114     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
115         if (isAlive()) {
116             sendParticipantDeregister();
117             stop();
118         }
119     }
120
121     private void sendParticipantRegister() {
122         participantHandler.sendParticipantRegister();
123     }
124
125     private void sendParticipantDeregister() {
126         participantHandler.sendParticipantDeregister();
127     }
128
129     /**
130      * Registers the dispatcher with the topic source(s).
131      */
132     private void registerMsgDispatcher() {
133         for (final TopicSource source : topicSources) {
134             source.register(msgDispatcher);
135         }
136     }
137
138     /**
139      * Unregisters the dispatcher from the topic source(s).
140      */
141     private void unregisterMsgDispatcher() {
142         for (final TopicSource source : topicSources) {
143             source.unregister(msgDispatcher);
144         }
145     }
146
147     @Override
148     public void close() throws IOException {
149         if (isAlive()) {
150             super.shutdown();
151         }
152     }
153 }