2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.services.onappf;
25 import java.util.List;
28 import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener;
29 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
30 import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener;
31 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
32 import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException;
33 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
34 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
35 import org.onap.policy.apex.services.onappf.rest.HealthCheckRestControllerV1;
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.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.services.Registry;
43 import org.onap.policy.common.utils.services.ServiceManager;
44 import org.onap.policy.common.utils.services.ServiceManagerException;
45 import org.onap.policy.models.pdp.enums.PdpMessageType;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * This class activates the ApexStarter as a complete service together with all its handlers.
53 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
55 public class ApexStarterActivator {
57 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterActivator.class);
59 private final ApexStarterParameterGroup parameterGroup;
60 private final List<TopicSink> topicSinks; // topics to which apex-pdp sends pdp status
61 private final List<TopicSource> topicSources; // topics to which apex-pdp listens to for messages from pap.
62 private static final String[] MSG_TYPE_NAMES = { "messageName" };
65 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
67 private final MessageTypeDispatcher msgDispatcher;
70 * Used to manage the services.
72 private final ServiceManager manager;
75 * The ApexStarter REST API server.
77 private RestServer restServer;
80 @Setter(lombok.AccessLevel.PRIVATE)
81 private volatile boolean alive = false;
84 private List<ToscaConceptIdentifier> supportedPolicyTypes;
87 private final String instanceId;
90 * Instantiate the activator for onappf PDP-A.
92 * @param apexStarterParameterGroup the parameters for the onappf PDP-A service
94 public ApexStarterActivator(final ApexStarterParameterGroup apexStarterParameterGroup) {
96 topicSinks = TopicEndpointManager.getManager()
97 .addTopicSinks(apexStarterParameterGroup.getTopicParameterGroup().getTopicSinks());
99 topicSources = TopicEndpointManager.getManager()
100 .addTopicSources(apexStarterParameterGroup.getTopicParameterGroup().getTopicSources());
102 instanceId = NetworkUtil.genUniqueName("apex");
103 LOGGER.debug("ApexStarterActivator initializing with instance id: {}", instanceId);
105 this.parameterGroup = apexStarterParameterGroup;
106 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
107 } catch (final RuntimeException e) {
108 throw new ApexStarterRunTimeException(e);
111 final var pdpUpdateListener = new PdpUpdateListener();
112 final var pdpStateChangeListener = new PdpStateChangeListener();
113 final var pdpMessageHandler = new PdpMessageHandler();
114 supportedPolicyTypes =
115 pdpMessageHandler.getSupportedPolicyTypesFromParameters(apexStarterParameterGroup.getPdpStatusParameters());
118 this.manager = new ServiceManager()
120 () -> TopicEndpointManager.getManager().start(),
121 () -> TopicEndpointManager.getManager().shutdown())
122 .addAction("set alive",
123 () -> setAlive(true),
124 () -> setAlive(false))
125 .addAction("register pdp status context object",
126 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_OBJECT,
127 pdpMessageHandler.createPdpStatusFromParameters(instanceId,
128 apexStarterParameterGroup.getPdpStatusParameters())),
129 () -> Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_OBJECT))
130 .addAction("topic sinks",
131 () -> Registry.register(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS, topicSinks),
132 () -> Registry.unregister(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS))
133 .addAction("Pdp Status publisher",
134 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
135 new PdpStatusPublisher(topicSinks,
136 apexStarterParameterGroup.getPdpStatusParameters().getTimeIntervalMs())),
137 this::stopAndRemovePdpStatusPublisher)
138 .addAction("Register pdp update listener",
139 () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), pdpUpdateListener),
140 () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name()))
141 .addAction("Register pdp state change request dispatcher",
142 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), pdpStateChangeListener),
143 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()))
144 .addAction("Message Dispatcher",
145 this::registerMsgDispatcher,
146 this::unregisterMsgDispatcher)
147 .addAction("Create REST server",
148 () -> restServer = new RestServer(apexStarterParameterGroup.getRestServerParameters(),
149 List.of(), List.of(HealthCheckRestControllerV1.class)),
150 () -> restServer = null)
151 .addAction("Rest Server",
152 () -> restServer.start(),
153 () -> restServer.stop());
159 * Method to stop and unregister the pdp status publisher.
161 private void stopAndRemovePdpStatusPublisher() {
162 final var pdpStatusPublisher =
163 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
164 pdpStatusPublisher.terminate();
165 Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
169 * Initialize ApexStarter service.
171 * @throws ApexStarterException on errors in initializing the service
173 public void initialize() throws ApexStarterException {
175 throw new IllegalStateException("activator already initialized");
179 LOGGER.debug("ApexStarter starting as a service . . .");
181 LOGGER.debug("ApexStarter started as a service");
182 } catch (final ServiceManagerException exp) {
183 LOGGER.error("ApexStarter service startup failed");
184 throw new ApexStarterException(exp.getMessage(), exp);
189 * Terminate ApexStarter.
191 * @throws ApexStarterException on errors in terminating the service
193 public void terminate() throws ApexStarterException {
195 throw new IllegalStateException("activator is not running");
198 final var pdpStatusPublisher =
199 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
200 // send a final heartbeat with terminated status
201 pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus());
203 Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR);
204 } catch (final ServiceManagerException exp) {
205 LOGGER.error("ApexStarter termination failed");
206 throw new ApexStarterException(exp.getMessage(), exp);
211 * Registers the dispatcher with the topic source(s).
213 private void registerMsgDispatcher() {
214 for (final TopicSource source : topicSources) {
215 source.register(msgDispatcher);
220 * Unregisters the dispatcher from the topic source(s).
222 private void unregisterMsgDispatcher() {
223 for (final TopicSource source : topicSources) {
224 source.unregister(msgDispatcher);