891dab9ae79ba51fbc58294ae3de8fb6a5224a10
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.config.messaging;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.stream.Stream;
27 import javax.ws.rs.core.Response.Status;
28 import lombok.Getter;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
30 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
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.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 array of Publishers
57      * @param listeners array of Listeners
58      * @throws ControlLoopRuntimeException if the activator does not start
59      */
60     public MessageDispatcherActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup, Publisher[] publishers,
61             Listener[] listeners) {
62         topicSinks = TopicEndpointManager.getManager()
63                 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
64
65         topicSources = TopicEndpointManager.getManager()
66                 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
67
68         try {
69             msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
70         } catch (final RuntimeException e) {
71             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
72                     "topic message dispatcher failed to start", e);
73         }
74
75         // @formatter:off
76         addAction("Topic endpoint management",
77                 () -> TopicEndpointManager.getManager().start(),
78                 () -> TopicEndpointManager.getManager().shutdown());
79
80         Stream.of(publishers).forEach(publisher ->
81             addAction("Publisher " + publisher.getClass().getSimpleName(),
82                 () -> publisher.active(topicSinks),
83                 () -> publisher.stop()));
84
85         Stream.of(listeners).forEach(listener ->
86             addAction("Listener " + listener.getClass().getSimpleName(),
87                     () -> msgDispatcher.register(listener.getType(), listener.getScoListener()),
88                     () -> msgDispatcher.unregister(listener.getType())));
89
90         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
91         // @formatter:on
92     }
93
94     /**
95      * Registers the dispatcher with the topic source(s).
96      */
97     private void registerMsgDispatcher() {
98         for (final TopicSource source : topicSources) {
99             source.register(msgDispatcher);
100         }
101     }
102
103     /**
104      * Unregisters the dispatcher from the topic source(s).
105      */
106     private void unregisterMsgDispatcher() {
107         for (final TopicSource source : topicSources) {
108             source.unregister(msgDispatcher);
109         }
110     }
111
112     /**
113      * Start Manager after the application is Started.
114      *
115      * @param cre Refreshed Event
116      */
117     @EventListener
118     public void handleContextStart(ContextRefreshedEvent cre) {
119         if (!isAlive()) {
120             start();
121         }
122     }
123
124     @Override
125     public void close() throws IOException {
126         if (isAlive()) {
127             stop();
128         }
129     }
130 }