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