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