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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.element.handler;
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;
39 * This class activates the Kafka together with all its handlers.
42 public class MessageActivator extends ServiceManagerContainer implements AutoCloseable {
44 private static final Logger LOGGER = LoggerFactory.getLogger(MessageActivator.class);
46 private static final String[] MSG_TYPE_NAMES = { "messageType" };
48 // Topics from which the AC element receives and sends messages
49 private List<TopicSink> topicSinks;
50 private List<TopicSource> topicSources;
52 private final MessageListener listener;
53 private final MessagePublisher publisher;
55 private MessageTypeDispatcher msgDispatcher;
60 * @param listener MessageListener
61 * @param publisher MessagePublisher
63 public MessageActivator(MessageListener listener, MessagePublisher publisher) {
65 this.listener = listener;
66 this.publisher = publisher;
67 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
71 * Activate publisher and listener messages.
73 * @param parameters TopicParameterGroup
75 public void activate(final TopicParameterGroup parameters) {
76 topicSinks = TopicEndpointManager.getManager().addTopicSinks(parameters.getTopicSinks());
77 topicSources = TopicEndpointManager.getManager().addTopicSources(parameters.getTopicSources());
80 addAction("Topic endpoint management",
81 () -> TopicEndpointManager.getManager().start(),
82 () -> TopicEndpointManager.getManager().shutdown());
84 addAction("Message Publisher",
85 () -> publisher.active(topicSinks), publisher::stop);
88 addAction("Message Listener",
89 () -> msgDispatcher.register(ElementMessageType.STATUS.name(), listener),
90 () -> msgDispatcher.unregister(ElementMessageType.STATUS.name()));
92 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
96 LOGGER.info("Kafka configuration initialised successfully");
100 * Handle ContextClosedEvent.
102 * @param ctxClosedEvent ContextClosedEvent
105 public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
110 * Deactivate publisher and listener messages.
112 public void deactivate() {
119 * Registers the dispatcher with the topic source(s).
121 private void registerMsgDispatcher() {
122 for (final TopicSource source : topicSources) {
123 source.register(msgDispatcher);
128 * Unregisters the dispatcher from the topic source(s).
130 private void unregisterMsgDispatcher() {
131 for (final TopicSource source : topicSources) {
132 source.unregister(msgDispatcher);
137 public void close() throws IOException {
140 LOGGER.info("Kafka configuration is uninitialised.");