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