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.List;
25 import java.util.concurrent.atomic.AtomicReference;
26 import javax.ws.rs.core.Response.Status;
27 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
28 import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationHandler;
29 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
30 import org.onap.policy.clamp.controlloop.runtime.main.rest.ControlLoopAafFilter;
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.http.server.RestServer;
35 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.common.utils.services.ServiceManagerContainer;
40 * This class activates the control loop runtime component as a complete service together with all its controllers,
41 * listeners & handlers.
43 public class ClRuntimeActivator extends ServiceManagerContainer {
44 // Name of the message type for messages on topics
45 private static final String[] MSG_TYPE_NAMES = {"messageType"};
47 private final ClRuntimeParameterGroup clRuntimeParameterGroup;
49 // Topics from which the application receives and to which the application sends messages
50 private List<TopicSink> topicSinks;
51 private List<TopicSource> topicSources;
54 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
56 private final MessageTypeDispatcher msgDispatcher;
59 * Instantiate the activator for the control loop runtime as a complete service.
61 * @param clRuntimeParameterGroup the parameters for the control loop runtime service
63 public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup) {
65 if (clRuntimeParameterGroup == null || !clRuntimeParameterGroup.isValid()) {
66 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, "ParameterGroup not valid");
69 this.clRuntimeParameterGroup = clRuntimeParameterGroup;
71 topicSinks = TopicEndpointManager.getManager()
72 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
74 topicSources = TopicEndpointManager.getManager()
75 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
78 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
79 } catch (final RuntimeException e) {
80 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
81 "topic message dispatcher failed to start", e);
84 final AtomicReference<InstantiationHandler> instantiationHandler = new AtomicReference<>();
85 final AtomicReference<RestServer> restServer = new AtomicReference<>();
88 addAction("Control loop runtime parameters",
89 () -> ParameterService.register(clRuntimeParameterGroup),
90 () -> ParameterService.deregister(clRuntimeParameterGroup.getName()));
92 addAction("Topic endpoint management",
93 () -> TopicEndpointManager.getManager().start(),
94 () -> TopicEndpointManager.getManager().shutdown());
96 addAction("Instantiation Handler",
97 () -> instantiationHandler.set(new InstantiationHandler(clRuntimeParameterGroup)),
98 () -> instantiationHandler.get().close());
100 addAction("Providers",
101 () -> instantiationHandler.get().startProviders(),
102 () -> instantiationHandler.get().stopProviders());
104 addAction("Listeners",
105 () -> instantiationHandler.get().startAndRegisterListeners(msgDispatcher),
106 () -> instantiationHandler.get().stopAndUnregisterListeners(msgDispatcher));
108 addAction("Publishers",
109 () -> instantiationHandler.get().startAndRegisterPublishers(topicSinks),
110 () -> instantiationHandler.get().stopAndUnregisterPublishers());
112 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
114 clRuntimeParameterGroup.getRestServerParameters().setName(clRuntimeParameterGroup.getName());
116 addAction("REST server",
118 Set<Class<?>> providerClasses = instantiationHandler.get().getProviderClasses();
120 RestServer server = new RestServer(clRuntimeParameterGroup.getRestServerParameters(),
121 ControlLoopAafFilter.class,
122 providerClasses.toArray(new Class<?>[providerClasses.size()]));
124 restServer.set(server);
125 restServer.get().start();
127 () -> restServer.get().stop());
132 * Registers the dispatcher with the topic source(s).
134 private void registerMsgDispatcher() {
135 for (final TopicSource source : topicSources) {
136 source.register(msgDispatcher);
141 * Unregisters the dispatcher from the topic source(s).
143 private void unregisterMsgDispatcher() {
144 for (final TopicSource source : topicSources) {
145 source.unregister(msgDispatcher);
150 * Get the parameters used by the activator.
152 * @return the parameters of the activator
154 public ClRuntimeParameterGroup getParameterGroup() {
155 return clRuntimeParameterGroup;