2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.services.onappf;
23 import java.util.List;
24 import java.util.Properties;
29 import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener;
30 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
31 import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener;
32 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
33 import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException;
34 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
35 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
36 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
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.listeners.MessageTypeDispatcher;
40 import org.onap.policy.common.utils.services.Registry;
41 import org.onap.policy.common.utils.services.ServiceManager;
42 import org.onap.policy.common.utils.services.ServiceManagerException;
43 import org.onap.policy.models.pdp.enums.PdpMessageType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * This class activates the ApexStarter as a complete service together with all its handlers.
50 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
52 public class ApexStarterActivator {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterActivator.class);
55 private final ApexStarterParameterGroup apexStarterParameterGroup;
56 private List<TopicSink> topicSinks;// topics to which apex-pdp sends pdp status
57 private List<TopicSource> topicSources; // topics to which apex-pdp listens to for messages from pap.
58 private static final String[] MSG_TYPE_NAMES = { "messageName" };
60 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
62 private final MessageTypeDispatcher msgDispatcher;
65 * Used to manage the services.
67 private ServiceManager manager;
70 @Setter(lombok.AccessLevel.PRIVATE)
71 private volatile boolean alive = false;
73 public ApexStarterActivator(final ApexStarterParameterGroup apexStarterParameterGroup,
74 final Properties topicProperties) {
76 topicSinks = TopicEndpoint.manager.addTopicSinks(topicProperties);
77 topicSources = TopicEndpoint.manager.addTopicSources(topicProperties);
79 // TODO: instanceId currently set as a random string, could be fetched from actual deployment
80 final int random = (int) (Math.random() * 100);
81 final String instanceId = "apex_" + random;
82 LOGGER.debug("ApexStarterActivator initializing with instance id:" + instanceId);
84 this.apexStarterParameterGroup = apexStarterParameterGroup;
85 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
86 } catch (final RuntimeException e) {
87 throw new ApexStarterRunTimeException(e);
90 final PdpUpdateListener pdpUpdateListener = new PdpUpdateListener();
91 final PdpStateChangeListener pdpStateChangeListener = new PdpStateChangeListener();
93 this.manager = new ServiceManager()
95 () -> TopicEndpoint.manager.start(),
96 () -> TopicEndpoint.manager.shutdown())
97 .addAction("set alive",
99 () -> setAlive(false))
100 .addAction("register pdp status context object",
101 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_OBJECT,
102 new PdpMessageHandler().createPdpStatusFromParameters(instanceId,
103 apexStarterParameterGroup.getPdpStatusParameters())),
104 () -> Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_OBJECT))
105 .addAction("topic sinks",
106 () -> Registry.register(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS, topicSinks),
107 () -> Registry.unregister(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS))
108 .addAction("Pdp Status publisher",
109 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
110 new PdpStatusPublisher(topicSinks,
111 apexStarterParameterGroup.getPdpStatusParameters().getTimeIntervalMs())),
112 () -> stopAndRemovePdpStatusPublisher())
113 .addAction("Register pdp update listener",
114 () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), pdpUpdateListener),
115 () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name()))
116 .addAction("Register pdp state change request dispatcher",
117 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), pdpStateChangeListener),
118 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()))
119 .addAction("Message Dispatcher",
120 () -> registerMsgDispatcher(),
121 () -> unregisterMsgDispatcher());
126 * Method to stop and unregister the pdp status publisher.
128 private void stopAndRemovePdpStatusPublisher() {
129 final PdpStatusPublisher pdpStatusPublisher =
130 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
131 pdpStatusPublisher.terminate();
132 Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
136 * Initialize ApexStarter service.
138 * @throws ApexStarterException on errors in initializing the service
140 public void initialize() throws ApexStarterException {
142 throw new IllegalStateException("activator already initialized");
146 LOGGER.debug("ApexStarter starting as a service . . .");
148 LOGGER.debug("ApexStarter started as a service");
149 } catch (final ServiceManagerException exp) {
150 LOGGER.error("ApexStarter service startup failed");
151 throw new ApexStarterException(exp.getMessage(), exp);
156 * Terminate ApexStarter.
158 * @throws ApexStarterException on errors in terminating the service
160 public void terminate() throws ApexStarterException {
162 throw new IllegalStateException("activator is not running");
165 final PdpStatusPublisher pdpStatusPublisher =
166 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
167 // send a final heartbeat with terminated status
168 pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus());
170 Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR);
171 } catch (final ServiceManagerException exp) {
172 LOGGER.error("ApexStarter termination failed");
173 throw new ApexStarterException(exp.getMessage(), exp);
178 * Get the parameters used by the activator.
180 * @return the parameters of the activator
182 public ApexStarterParameterGroup getParameterGroup() {
183 return apexStarterParameterGroup;
187 * Registers the dispatcher with the topic source(s).
189 private void registerMsgDispatcher() {
190 for (final TopicSource source : topicSources) {
191 source.register(msgDispatcher);
196 * Unregisters the dispatcher from the topic source(s).
198 private void unregisterMsgDispatcher() {
199 for (final TopicSource source : topicSources) {
200 source.unregister(msgDispatcher);