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