8ac4d684dfe4df7a3b1c0c330a0d435d00979b50
[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 javax.ws.rs.core.Response.Status;
25 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
26 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
27 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
28 import org.onap.policy.common.endpoints.event.comm.TopicSource;
29 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.common.utils.services.ServiceManagerContainer;
32
33 /**
34  * This class activates the control loop runtime component as a complete service together with all its controllers,
35  * listeners & handlers.
36  */
37 public class ClRuntimeActivator extends ServiceManagerContainer {
38     // Name of the message type for messages on topics
39     private static final String[] MSG_TYPE_NAMES = {"messageType"};
40
41     private final ClRuntimeParameterGroup clRuntimeParameterGroup;
42
43     // Topics from which the application receives and to which the application sends messages
44     private List<TopicSource> topicSources;
45
46     /**
47      * Listens for messages on the topic, decodes them into a message, and then dispatches them.
48      */
49     private final MessageTypeDispatcher msgDispatcher;
50
51     /**
52      * Instantiate the activator for the control loop runtime as a complete service.
53      *
54      * @param clRuntimeParameterGroup the parameters for the control loop runtime service
55      */
56     public ClRuntimeActivator(final ClRuntimeParameterGroup clRuntimeParameterGroup) {
57
58         if (clRuntimeParameterGroup == null || !clRuntimeParameterGroup.isValid()) {
59             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, "ParameterGroup not valid");
60         }
61
62         this.clRuntimeParameterGroup = clRuntimeParameterGroup;
63
64         topicSources = TopicEndpointManager.getManager()
65                 .addTopicSources(clRuntimeParameterGroup.getTopicParameterGroup().getTopicSources());
66
67         try {
68             msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
69         } catch (final RuntimeException e) {
70             throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
71                     "topic message dispatcher failed to start", e);
72         }
73
74         // @formatter:off
75         addAction("Control loop runtime parameters",
76             () -> ParameterService.register(clRuntimeParameterGroup),
77             () -> ParameterService.deregister(clRuntimeParameterGroup.getName()));
78
79         addAction("Topic endpoint management",
80             () -> TopicEndpointManager.getManager().start(),
81             () -> TopicEndpointManager.getManager().shutdown());
82
83         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
84
85         clRuntimeParameterGroup.getRestServerParameters().setName(clRuntimeParameterGroup.getName());
86         // @formatter:on
87     }
88
89     /**
90      * Registers the dispatcher with the topic source(s).
91      */
92     private void registerMsgDispatcher() {
93         for (final TopicSource source : topicSources) {
94             source.register(msgDispatcher);
95         }
96     }
97
98     /**
99      * Unregisters the dispatcher from the topic source(s).
100      */
101     private void unregisterMsgDispatcher() {
102         for (final TopicSource source : topicSources) {
103             source.unregister(msgDispatcher);
104         }
105     }
106
107     /**
108      * Get the parameters used by the activator.
109      *
110      * @return the parameters of the activator
111      */
112     public ClRuntimeParameterGroup getParameterGroup() {
113         return clRuntimeParameterGroup;
114     }
115 }