2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.pdp;
24 import java.util.List;
25 import java.util.Random;
28 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
29 import org.onap.policy.common.endpoints.event.comm.TopicSink;
30 import org.onap.policy.common.endpoints.event.comm.TopicSource;
31 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
32 import org.onap.policy.common.utils.services.Registry;
33 import org.onap.policy.common.utils.services.ServiceManager;
34 import org.onap.policy.common.utils.services.ServiceManagerException;
35 import org.onap.policy.models.pdp.enums.PdpMessageType;
36 import org.onap.policy.models.sim.pdp.comm.PdpStateChangeListener;
37 import org.onap.policy.models.sim.pdp.comm.PdpStatusPublisher;
38 import org.onap.policy.models.sim.pdp.comm.PdpUpdateListener;
39 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
40 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorRunTimeException;
41 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
42 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * This class activates the PdpSimulator as a complete service together with all its handlers.
49 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
51 public class PdpSimulatorActivator {
53 private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorActivator.class);
54 private final PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
55 private List<TopicSink> topicSinks;// topics to which pdp sends pdp status
56 private List<TopicSource> topicSources; // topics to which pdp listens to for messages from pap.
57 private static final String[] MSG_TYPE_NAMES = { "messageName" };
60 * This simulator is only used for testing. Consequently, it is safe to use a simple
61 * random number generator, thus the sonar is disabled.
63 private static final Random RANDOM = new Random(); // NOSONAR
66 * Listens for messages on the topic, decodes them into a message, and then dispatches them.
68 private final MessageTypeDispatcher msgDispatcher;
71 * Used to manage the services.
73 private ServiceManager manager;
77 @Setter(lombok.AccessLevel.PRIVATE)
78 private volatile boolean alive = false;
81 * Instantiate the activator for onappf PDP-A.
83 * @param pdpSimulatorParameterGroup the parameters for the onappf PDP-A service
85 public PdpSimulatorActivator(final PdpSimulatorParameterGroup pdpSimulatorParameterGroup) {
87 topicSinks = TopicEndpointManager.getManager()
88 .addTopicSinks(pdpSimulatorParameterGroup.getTopicParameterGroup().getTopicSinks());
89 topicSources = TopicEndpointManager.getManager()
90 .addTopicSources(pdpSimulatorParameterGroup.getTopicParameterGroup().getTopicSources());
92 final int random = RANDOM.nextInt();
93 final String instanceId = "pdp_" + random;
94 LOGGER.debug("PdpSimulatorActivator initializing with instance id: {}", instanceId);
96 this.pdpSimulatorParameterGroup = pdpSimulatorParameterGroup;
97 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
98 } catch (final RuntimeException e) {
99 throw new PdpSimulatorRunTimeException(e);
102 final PdpUpdateListener pdpUpdateListener = new PdpUpdateListener();
103 final PdpStateChangeListener pdpStateChangeListener = new PdpStateChangeListener();
105 this.manager = new ServiceManager()
107 TopicEndpointManager.getManager()::start,
108 TopicEndpointManager.getManager()::shutdown)
109 .addAction("set alive",
110 () -> setAlive(true),
111 () -> setAlive(false))
112 .addAction("register pdp status context object",
113 () -> Registry.register(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT,
114 new PdpMessageHandler().createPdpStatusFromParameters(instanceId,
115 pdpSimulatorParameterGroup.getPdpStatusParameters())),
116 () -> Registry.unregister(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT))
117 .addAction("topic sinks",
118 () -> Registry.register(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS, topicSinks),
119 () -> Registry.unregister(PdpSimulatorConstants.REG_PDP_TOPIC_SINKS))
120 .addAction("Pdp Status publisher",
121 () -> Registry.register(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER,
122 new PdpStatusPublisher(topicSinks,
123 pdpSimulatorParameterGroup.getPdpStatusParameters().getTimeIntervalMs())),
124 this::stopAndRemovePdpStatusPublisher)
125 .addAction("Register pdp update listener",
126 () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), pdpUpdateListener),
127 () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name()))
128 .addAction("Register pdp state change request dispatcher",
129 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), pdpStateChangeListener),
130 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()))
131 .addAction("Message Dispatcher",
132 this::registerMsgDispatcher,
133 this::unregisterMsgDispatcher);
139 * Method to stop and unregister the pdp status publisher.
141 private void stopAndRemovePdpStatusPublisher() {
142 final PdpStatusPublisher pdpStatusPublisher =
143 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
144 pdpStatusPublisher.terminate();
145 Registry.unregister(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER);
149 * Initialize PdpSimulator service.
151 * @throws PdpSimulatorException on errors in initializing the service
153 public void initialize() throws PdpSimulatorException {
155 throw new IllegalStateException("activator already initialized");
159 LOGGER.debug("PdpSimulator starting as a service . . .");
161 LOGGER.debug("PdpSimulator started as a service");
162 } catch (final ServiceManagerException exp) {
163 LOGGER.error("PdpSimulator service startup failed");
164 throw new PdpSimulatorException(exp.getMessage(), exp);
169 * Terminate PdpSimulator.
171 * @throws PdpSimulatorException on errors in terminating the service
173 public void terminate() throws PdpSimulatorException {
175 throw new IllegalStateException("activator is not running");
178 final PdpStatusPublisher pdpStatusPublisher =
179 Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
180 // send a final heartbeat with terminated status
181 pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus());
183 Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
184 } catch (final ServiceManagerException exp) {
185 LOGGER.error("PdpSimulator termination failed");
186 throw new PdpSimulatorException(exp.getMessage(), exp);
191 * Get the parameters used by the activator.
193 * @return pdpSimulatorParameterGroup the parameters of the activator
195 public PdpSimulatorParameterGroup getParameterGroup() {
196 return pdpSimulatorParameterGroup;
200 * Registers the dispatcher with the topic source(s).
202 private void registerMsgDispatcher() {
203 for (final TopicSource source : topicSources) {
204 source.register(msgDispatcher);
209 * Unregisters the dispatcher from the topic source(s).
211 private void unregisterMsgDispatcher() {
212 for (final TopicSource source : topicSources) {
213 source.unregister(msgDispatcher);