a4238a9c4076273c9d98b6b508406ed98820a3ad
[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.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
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;
43
44 /**
45  * This class activates the control loop runtime component as a complete service together with all its controllers,
46  * listeners & handlers.
47  */
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"};
51
52     private final ClRuntimeParameterGroup clRuntimeParameterGroup;
53
54     // Topics from which the application receives and to which the application sends messages
55     private List<TopicSink> topicSinks;
56     private List<TopicSource> topicSources;
57
58     /**
59      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
60      */
61     private final MessageTypeDispatcher msgDispatcher;
62
63     /**
64      * Instantiate the activator for the control loop runtime as a complete service.
65      *
66      * @param clRuntimeParameterGroup the parameters for the control loop runtime service
67      */
68     public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup) {
69
70         if (clRuntimeParameterGroup == null || !clRuntimeParameterGroup.isValid()) {
71             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, "ParameterGroup not valid");
72         }
73
74         this.clRuntimeParameterGroup = clRuntimeParameterGroup;
75
76         topicSinks = TopicEndpointManager.getManager()
77                 .addTopicSinks(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSinks());
78
79         topicSources = TopicEndpointManager.getManager()
80                 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
81
82         try {
83             msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
84         } catch (final RuntimeException e) {
85             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
86                     "topic message dispatcher failed to start", e);
87         }
88
89         final AtomicReference<ControlLoopHandler> commissioningHandler = new AtomicReference<>();
90         final AtomicReference<ControlLoopHandler> instantiationHandler = new AtomicReference<>();
91         final AtomicReference<ControlLoopHandler> supervisionHandler = new AtomicReference<>();
92         final AtomicReference<ControlLoopHandler> monitoringHandler = new AtomicReference<>();
93         final AtomicReference<RestServer> restServer = new AtomicReference<>();
94
95         // @formatter:off
96         addAction("Control loop runtime parameters",
97             () -> ParameterService.register(clRuntimeParameterGroup),
98             () -> ParameterService.deregister(clRuntimeParameterGroup.getName()));
99
100         addAction("Topic endpoint management",
101             () -> TopicEndpointManager.getManager().start(),
102             () -> TopicEndpointManager.getManager().shutdown());
103
104         addAction("Commissioning Handler",
105                 () -> commissioningHandler.set(new CommissioningHandler(clRuntimeParameterGroup)),
106                 () -> commissioningHandler.get().close());
107
108         addAction("Instantiation Handler",
109             () -> instantiationHandler.set(new InstantiationHandler(clRuntimeParameterGroup)),
110             () -> instantiationHandler.get().close());
111
112         addAction("Supervision Handler",
113             () -> supervisionHandler.set(new SupervisionHandler(clRuntimeParameterGroup)),
114             () -> supervisionHandler.get().close());
115
116         addAction("Monitoring Handler",
117             () -> monitoringHandler.set(new MonitoringHandler(clRuntimeParameterGroup)),
118             () -> monitoringHandler.get().close());
119
120         addHandlerActions("Commissioning", commissioningHandler);
121         addHandlerActions("Instantiation", instantiationHandler);
122         addHandlerActions("Supervision", supervisionHandler);
123         addHandlerActions("Monitoring", monitoringHandler);
124
125         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
126
127         clRuntimeParameterGroup.getRestServerParameters().setName(clRuntimeParameterGroup.getName());
128
129         addAction("REST server",
130             () -> {
131                 Set<Class<?>> providerClasses = new HashSet<>();
132                 providerClasses.addAll(commissioningHandler.get().getProviderClasses());
133                 providerClasses.addAll(instantiationHandler.get().getProviderClasses());
134                 providerClasses.addAll(supervisionHandler.get().getProviderClasses());
135                 providerClasses.addAll(monitoringHandler.get().getProviderClasses());
136
137                 RestServer server = new RestServer(clRuntimeParameterGroup.getRestServerParameters(),
138                         ControlLoopAafFilter.class,
139                         providerClasses.toArray(new Class<?>[providerClasses.size()]));
140
141                 restServer.set(server);
142                 restServer.get().start();
143             },
144             () -> restServer.get().stop());
145         // @formatter:on
146     }
147
148     private void addHandlerActions(final String name, final AtomicReference<ControlLoopHandler> handler) {
149         addAction(name + " Providers",
150             () -> handler.get().startProviders(),
151             () -> handler.get().stopProviders());
152         addAction(name + " Listeners",
153             () -> handler.get().startAndRegisterListeners(msgDispatcher),
154             () -> handler.get().stopAndUnregisterListeners(msgDispatcher));
155         addAction(name + " Publishers",
156             () -> handler.get().startAndRegisterPublishers(topicSinks),
157             () -> handler.get().stopAndUnregisterPublishers());
158     }
159
160     /**
161      * Registers the dispatcher with the topic source(s).
162      */
163     private void registerMsgDispatcher() {
164         for (final TopicSource source : topicSources) {
165             source.register(msgDispatcher);
166         }
167     }
168
169     /**
170      * Unregisters the dispatcher from the topic source(s).
171      */
172     private void unregisterMsgDispatcher() {
173         for (final TopicSource source : topicSources) {
174             source.unregister(msgDispatcher);
175         }
176     }
177
178     /**
179      * Get the parameters used by the activator.
180      *
181      * @return the parameters of the activator
182      */
183     public ClRuntimeParameterGroup getParameterGroup() {
184         return clRuntimeParameterGroup;
185     }
186 }