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