ac3c72e529dd575b68b9383ae33ffa1a7240daa2
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 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.acm.element.handler;
22
23 import java.io.IOException;
24 import java.util.List;
25 import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementMessageType;
26 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
27 import org.onap.policy.common.endpoints.event.comm.TopicSink;
28 import org.onap.policy.common.endpoints.event.comm.TopicSource;
29 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
30 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
31 import org.onap.policy.common.utils.services.ServiceManagerContainer;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.event.ContextClosedEvent;
35 import org.springframework.context.event.EventListener;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * This class activates the Kafka together with all its handlers.
40  */
41 @Component
42 public class MessageActivator extends ServiceManagerContainer implements AutoCloseable {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(MessageActivator.class);
45
46     private static final String[] MSG_TYPE_NAMES = { "messageType" };
47
48     // Topics from which the AC element receives and sends messages
49     private List<TopicSink> topicSinks;
50     private List<TopicSource> topicSources;
51
52     private final MessageListener listener;
53     private final MessagePublisher publisher;
54
55     private MessageTypeDispatcher msgDispatcher;
56
57     /**
58      * Constructor.
59      *
60      * @param listener  MessageListener
61      * @param publisher MessagePublisher
62      */
63     public MessageActivator(MessageListener listener, MessagePublisher publisher) {
64         super();
65         this.listener = listener;
66         this.publisher = publisher;
67         msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
68     }
69
70     /**
71      * Activate publisher and listener messages.
72      *
73      * @param parameters TopicParameterGroup
74      */
75     public void activate(final TopicParameterGroup parameters) {
76         topicSinks = TopicEndpointManager.getManager().addTopicSinks(parameters.getTopicSinks());
77         topicSources = TopicEndpointManager.getManager().addTopicSources(parameters.getTopicSources());
78
79         // @formatter:off
80         addAction("Topic endpoint management",
81             () -> TopicEndpointManager.getManager().start(),
82             () -> TopicEndpointManager.getManager().shutdown());
83
84         addAction("Message Publisher",
85             () -> publisher.active(topicSinks), publisher::stop);
86
87
88         addAction("Message Listener",
89             () -> msgDispatcher.register(ElementMessageType.STATUS.name(), listener),
90             () -> msgDispatcher.unregister(ElementMessageType.STATUS.name()));
91
92         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
93         // @formatter:on
94
95         start();
96         LOGGER.info("Kafka configuration initialised successfully");
97     }
98
99     /**
100      * Handle ContextClosedEvent.
101      *
102      * @param ctxClosedEvent ContextClosedEvent
103      */
104     @EventListener
105     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
106         deactivate();
107     }
108
109     /**
110      * Deactivate publisher and listener messages.
111      */
112     public void deactivate() {
113         if (isAlive()) {
114             stop();
115         }
116     }
117
118     /**
119      * Registers the dispatcher with the topic source(s).
120      */
121     private void registerMsgDispatcher() {
122         for (final TopicSource source : topicSources) {
123             source.register(msgDispatcher);
124         }
125     }
126
127     /**
128      * Unregisters the dispatcher from the topic source(s).
129      */
130     private void unregisterMsgDispatcher() {
131         for (final TopicSource source : topicSources) {
132             source.unregister(msgDispatcher);
133         }
134     }
135
136     @Override
137     public void close() throws IOException {
138         if (isAlive()) {
139             super.shutdown();
140             LOGGER.info("Kafka configuration is uninitialised.");
141         }
142     }
143 }