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
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.controlloop.runtime.main.startstop;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
30 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
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;
38 * This class activates the control loop runtime component as a complete service together with all its controllers,
39 * listeners & handlers.
41 public class ClRuntimeActivator extends ServiceManagerContainer implements Closeable {
42 // Name of the message type for messages on topics
43 private static final String[] MSG_TYPE_NAMES = {"messageType"};
46 private final ClRuntimeParameterGroup parameterGroup;
48 // Topics from which the application receives and to which the application sends messages
49 private List<TopicSink> topicSinks;
50 private List<TopicSource> topicSources;
53 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
55 private final MessageTypeDispatcher msgDispatcher;
58 * Instantiate the activator for the control loop runtime as a complete service.
60 * @param clRuntimeParameterGroup the parameters for the control loop runtime service
61 * @param supervisionHandler SupervisionHandler
62 * @throws ControlLoopRuntimeException if the activator does not start
64 public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup,
65 SupervisionHandler supervisionHandler) {
66 this.parameterGroup = clRuntimeParameterGroup;
68 topicSinks = TopicEndpointManager.getManager()
69 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
71 topicSources = TopicEndpointManager.getManager()
72 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
75 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
76 } catch (final RuntimeException e) {
77 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
78 "topic message dispatcher failed to start", e);
82 addAction("Topic endpoint management",
83 () -> TopicEndpointManager.getManager().start(),
84 () -> TopicEndpointManager.getManager().shutdown());
86 addAction("Supervision Providers", () -> supervisionHandler.startProviders(),
87 () -> supervisionHandler.stopProviders());
88 addAction("Supervision Listeners", () -> supervisionHandler.startAndRegisterListeners(msgDispatcher),
89 () -> supervisionHandler.stopAndUnregisterListeners(msgDispatcher));
90 addAction("Supervision Publishers", () -> supervisionHandler.startAndRegisterPublishers(topicSinks),
91 () -> supervisionHandler.stopAndUnregisterPublishers());
93 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
98 * Registers the dispatcher with the topic source(s).
100 private void registerMsgDispatcher() {
101 for (final TopicSource source : topicSources) {
102 source.register(msgDispatcher);
107 * Unregisters the dispatcher from the topic source(s).
109 private void unregisterMsgDispatcher() {
110 for (final TopicSource source : topicSources) {
111 source.unregister(msgDispatcher);
116 public void close() throws IOException {