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.util.HashSet;
24 import java.util.List;
26 import java.util.concurrent.atomic.AtomicReference;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler;
30 import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningHandler;
31 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationHandler;
32 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
33 import org.onap.policy.clamp.controlloop.runtime.main.rest.ControlLoopAafFilter;
34 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
35 import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler;
36 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
37 import org.onap.policy.common.endpoints.event.comm.TopicSink;
38 import org.onap.policy.common.endpoints.event.comm.TopicSource;
39 import org.onap.policy.common.endpoints.http.server.RestServer;
40 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
41 import org.onap.policy.common.parameters.ParameterService;
42 import org.onap.policy.common.utils.services.ServiceManagerContainer;
45 * This class activates the control loop runtime component as a complete service together with all its controllers,
46 * listeners & handlers.
48 public class ClRuntimeActivator extends ServiceManagerContainer {
49 // Name of the message type for messages on topics
50 private static final String[] MSG_TYPE_NAMES = {"messageType"};
52 private final ClRuntimeParameterGroup clRuntimeParameterGroup;
54 // Topics from which the application receives and to which the application sends messages
55 private List<TopicSink> topicSinks;
56 private List<TopicSource> topicSources;
59 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
61 private final MessageTypeDispatcher msgDispatcher;
64 * Instantiate the activator for the control loop runtime as a complete service.
66 * @param clRuntimeParameterGroup the parameters for the control loop runtime service
67 * @throws ControlLoopRuntimeException if the activator does not start
69 public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup) {
71 if (clRuntimeParameterGroup == null || !clRuntimeParameterGroup.isValid()) {
72 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, "ParameterGroup not valid");
75 this.clRuntimeParameterGroup = clRuntimeParameterGroup;
77 topicSinks = TopicEndpointManager.getManager()
78 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
80 topicSources = TopicEndpointManager.getManager()
81 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
84 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
85 } catch (final RuntimeException e) {
86 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
87 "topic message dispatcher failed to start", e);
90 final AtomicReference<ControlLoopHandler> commissioningHandler = new AtomicReference<>();
91 final AtomicReference<ControlLoopHandler> instantiationHandler = new AtomicReference<>();
92 final AtomicReference<ControlLoopHandler> supervisionHandler = new AtomicReference<>();
93 final AtomicReference<ControlLoopHandler> monitoringHandler = new AtomicReference<>();
94 final AtomicReference<RestServer> restServer = new AtomicReference<>();
97 addAction("Control loop runtime parameters",
98 () -> ParameterService.register(clRuntimeParameterGroup),
99 () -> ParameterService.deregister(clRuntimeParameterGroup.getName()));
101 addAction("Topic endpoint management",
102 () -> TopicEndpointManager.getManager().start(),
103 () -> TopicEndpointManager.getManager().shutdown());
105 addAction("Commissioning Handler",
106 () -> commissioningHandler.set(new CommissioningHandler(clRuntimeParameterGroup)),
107 () -> commissioningHandler.get().close());
109 addAction("Instantiation Handler",
110 () -> instantiationHandler.set(new InstantiationHandler(clRuntimeParameterGroup)),
111 () -> instantiationHandler.get().close());
113 addAction("Supervision Handler",
114 () -> supervisionHandler.set(new SupervisionHandler(clRuntimeParameterGroup)),
115 () -> supervisionHandler.get().close());
117 addAction("Monitoring Handler",
118 () -> monitoringHandler.set(new MonitoringHandler(clRuntimeParameterGroup)),
119 () -> monitoringHandler.get().close());
121 addHandlerActions("Commissioning", commissioningHandler);
122 addHandlerActions("Instantiation", instantiationHandler);
123 addHandlerActions("Supervision", supervisionHandler);
124 addHandlerActions("Monitoring", monitoringHandler);
126 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
128 clRuntimeParameterGroup.getRestServerParameters().setName(clRuntimeParameterGroup.getName());
130 addAction("REST server",
132 Set<Class<?>> providerClasses = new HashSet<>();
133 providerClasses.addAll(commissioningHandler.get().getProviderClasses());
134 providerClasses.addAll(instantiationHandler.get().getProviderClasses());
135 providerClasses.addAll(supervisionHandler.get().getProviderClasses());
136 providerClasses.addAll(monitoringHandler.get().getProviderClasses());
138 var server = new RestServer(clRuntimeParameterGroup.getRestServerParameters(),
139 ControlLoopAafFilter.class,
140 providerClasses.toArray(new Class<?>[providerClasses.size()]));
142 restServer.set(server);
143 restServer.get().start();
145 () -> restServer.get().stop());
149 private void addHandlerActions(final String name, final AtomicReference<ControlLoopHandler> handler) {
150 addAction(name + " Providers",
151 () -> handler.get().startProviders(),
152 () -> handler.get().stopProviders());
153 addAction(name + " Listeners",
154 () -> handler.get().startAndRegisterListeners(msgDispatcher),
155 () -> handler.get().stopAndUnregisterListeners(msgDispatcher));
156 addAction(name + " Publishers",
157 () -> handler.get().startAndRegisterPublishers(topicSinks),
158 () -> handler.get().stopAndUnregisterPublishers());
162 * Registers the dispatcher with the topic source(s).
164 private void registerMsgDispatcher() {
165 for (final TopicSource source : topicSources) {
166 source.register(msgDispatcher);
171 * Unregisters the dispatcher from the topic source(s).
173 private void unregisterMsgDispatcher() {
174 for (final TopicSource source : topicSources) {
175 source.unregister(msgDispatcher);
180 * Get the parameters used by the activator.
182 * @return the parameters of the activator
184 public ClRuntimeParameterGroup getParameterGroup() {
185 return clRuntimeParameterGroup;