284094bc42a65771c87b6cf51fc738da8e0090f0
[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.runtime.config.messaging;
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.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
30 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
31 import org.onap.policy.common.endpoints.event.comm.TopicSink;
32 import org.onap.policy.common.endpoints.event.comm.TopicSource;
33 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
34 import org.onap.policy.common.utils.services.ServiceManagerContainer;
35 import org.springframework.context.event.ContextClosedEvent;
36 import org.springframework.context.event.ContextRefreshedEvent;
37 import org.springframework.context.event.EventListener;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class MessageDispatcherActivator extends ServiceManagerContainer implements Closeable {
42
43     private static final String[] MSG_TYPE_NAMES = {"messageType"};
44
45     // Topics from which the application receives and to which the application sends messages
46     private List<TopicSink> topicSinks;
47     private List<TopicSource> topicSources;
48
49     @Getter
50     private final MessageTypeDispatcher msgDispatcher;
51
52     /**
53      * Constructor.
54      *
55      * @param clRuntimeParameterGroup the parameters for the control loop runtime service
56      * @param publishers list of Publishers
57      * @param listeners list of Listeners
58      * @throws ControlLoopRuntimeException if the activator does not start
59      */
60     public <T> MessageDispatcherActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup,
61                     List<Publisher> publishers, List<Listener<T>> listeners) {
62         topicSinks = TopicEndpointManager.getManager()
63                 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
64
65         topicSources = TopicEndpointManager.getManager()
66                 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
67
68         msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
69
70         // @formatter:off
71         addAction("Topic endpoint management",
72             () -> TopicEndpointManager.getManager().start(),
73             () -> TopicEndpointManager.getManager().shutdown());
74
75         publishers.forEach(publisher ->
76             addAction("Publisher " + publisher.getClass().getSimpleName(),
77                 () -> publisher.active(topicSinks),
78                 publisher::stop));
79
80         listeners.forEach(listener ->
81             addAction("Listener " + listener.getClass().getSimpleName(),
82                 () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
83                 () -> msgDispatcher.unregister(listener.getType())));
84
85         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
86         // @formatter:on
87     }
88
89     /**
90      * Registers the dispatcher with the topic source(s).
91      */
92     private void registerMsgDispatcher() {
93         for (final TopicSource source : topicSources) {
94             source.register(msgDispatcher);
95         }
96     }
97
98     /**
99      * Unregisters the dispatcher from the topic source(s).
100      */
101     private void unregisterMsgDispatcher() {
102         for (final TopicSource source : topicSources) {
103             source.unregister(msgDispatcher);
104         }
105     }
106
107     /**
108      * Start Manager after the application is Started.
109      *
110      * @param cre Refreshed Event
111      */
112     @EventListener
113     public void handleContextStart(ContextRefreshedEvent cre) {
114         if (!isAlive()) {
115             start();
116         }
117     }
118
119     /**
120      * Handle ContextClosedEvent.
121      *
122      * @param ctxClosedEvent ContextClosedEvent
123      */
124     @EventListener
125     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
126         if (isAlive()) {
127             stop();
128         }
129     }
130
131     @Override
132     public void close() throws IOException {
133         if (isAlive()) {
134             super.shutdown();
135         }
136     }
137 }