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;
24 import javax.ws.rs.core.Response.Status;
25 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
26 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
27 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
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.parameters.ParameterService;
31 import org.onap.policy.common.utils.services.ServiceManagerContainer;
34 * This class activates the control loop runtime component as a complete service together with all its controllers,
35 * listeners & handlers.
37 public class ClRuntimeActivator extends ServiceManagerContainer {
38 // Name of the message type for messages on topics
39 private static final String[] MSG_TYPE_NAMES = {"messageType"};
41 private final ClRuntimeParameterGroup clRuntimeParameterGroup;
43 // Topics from which the application receives and to which the application sends messages
44 private List<TopicSource> topicSources;
47 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
49 private final MessageTypeDispatcher msgDispatcher;
52 * Instantiate the activator for the control loop runtime as a complete service.
54 * @param clRuntimeParameterGroup the parameters for the control loop runtime service
56 public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup) {
58 if (clRuntimeParameterGroup == null || !clRuntimeParameterGroup.isValid()) {
59 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, "ParameterGroup not valid");
62 this.clRuntimeParameterGroup = clRuntimeParameterGroup;
64 topicSources = TopicEndpointManager.getManager()
65 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
68 msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
69 } catch (final RuntimeException e) {
70 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
71 "topic message dispatcher failed to start", e);
75 addAction("Control loop runtime parameters",
76 () -> ParameterService.register(clRuntimeParameterGroup),
77 () -> ParameterService.deregister(clRuntimeParameterGroup.getName()));
79 addAction("Topic endpoint management",
80 () -> TopicEndpointManager.getManager().start(),
81 () -> TopicEndpointManager.getManager().shutdown());
83 addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
85 clRuntimeParameterGroup.getRestServerParameters().setName(clRuntimeParameterGroup.getName());
90 * Registers the dispatcher with the topic source(s).
92 private void registerMsgDispatcher() {
93 for (final TopicSource source : topicSources) {
94 source.register(msgDispatcher);
99 * Unregisters the dispatcher from the topic source(s).
101 private void unregisterMsgDispatcher() {
102 for (final TopicSource source : topicSources) {
103 source.unregister(msgDispatcher);
108 * Get the parameters used by the activator.
110 * @return the parameters of the activator
112 public ClRuntimeParameterGroup getParameterGroup() {
113 return clRuntimeParameterGroup;