323f76178234817988282218d9f53ce46f8dcaa9
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.main.startstop;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import javax.ws.rs.core.Response.Status;
27 import lombok.Getter;
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;
36
37 /**
38  * This class activates the control loop runtime component as a complete service together with all its controllers,
39  * listeners & handlers.
40  */
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"};
44
45     @Getter
46     private final ClRuntimeParameterGroup parameterGroup;
47
48     // Topics from which the application receives and to which the application sends messages
49     private List<TopicSink> topicSinks;
50     private List<TopicSource> topicSources;
51
52     /**
53      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
54      */
55     private final MessageTypeDispatcher msgDispatcher;
56
57     /**
58      * Instantiate the activator for the control loop runtime as a complete service.
59      *
60      * @param clRuntimeParameterGroup the parameters for the control loop runtime service
61      * @param supervisionHandler SupervisionHandler
62      * @throws ControlLoopRuntimeException if the activator does not start
63      */
64     public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup,
65             SupervisionHandler supervisionHandler) {
66         this.parameterGroup = clRuntimeParameterGroup;
67
68         topicSinks = TopicEndpointManager.getManager()
69                 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
70
71         topicSources = TopicEndpointManager.getManager()
72                 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
73
74         try {
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);
79         }
80
81         // @formatter:off
82         addAction("Topic endpoint management",
83             () -> TopicEndpointManager.getManager().start(),
84             () -> TopicEndpointManager.getManager().shutdown());
85
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());
92
93         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
94         // @formatter:on
95     }
96
97     /**
98      * Registers the dispatcher with the topic source(s).
99      */
100     private void registerMsgDispatcher() {
101         for (final TopicSource source : topicSources) {
102             source.register(msgDispatcher);
103         }
104     }
105
106     /**
107      * Unregisters the dispatcher from the topic source(s).
108      */
109     private void unregisterMsgDispatcher() {
110         for (final TopicSource source : topicSources) {
111             source.unregister(msgDispatcher);
112         }
113     }
114
115     @Override
116     public void close() throws IOException {
117         if (isAlive()) {
118             stop();
119         }
120     }
121 }