2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.ApexStarterAafFilter;
36 import org.onap.policy.apex.services.onappf.rest.HealthCheckRestControllerV1;
37 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
38 import org.onap.policy.common.endpoints.event.comm.TopicSink;
39 import org.onap.policy.common.endpoints.event.comm.TopicSource;
40 import org.onap.policy.common.endpoints.http.server.RestServer;
41 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.common.utils.services.Registry;
44 import org.onap.policy.common.utils.services.ServiceManager;
45 import org.onap.policy.common.utils.services.ServiceManagerException;
46 import org.onap.policy.models.pdp.enums.PdpMessageType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * This class activates the ApexStarter as a complete service together with all its handlers.
54 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
56 public class ApexStarterActivator {
58 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterActivator.class);
60 private final ApexStarterParameterGroup parameterGroup;
61 private List<TopicSink> topicSinks; // topics to which apex-pdp sends pdp status
62 private List<TopicSource> topicSources; // topics to which apex-pdp listens to for messages from pap.
63 private static final String[] MSG_TYPE_NAMES = { "messageName" };
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;
76 * The ApexStarter REST API server.
78 private RestServer restServer;
81 @Setter(lombok.AccessLevel.PRIVATE)
82 private volatile boolean alive = false;
85 private List<ToscaConceptIdentifier> supportedPolicyTypes;
88 private final String instanceId;
91 * Instantiate the activator for onappf PDP-A.
93 * @param apexStarterParameterGroup the parameters for the onappf PDP-A service
95 public ApexStarterActivator(final ApexStarterParameterGroup apexStarterParameterGroup) {
97 topicSinks = TopicEndpointManager.getManager()
98 .addTopicSinks(apexStarterParameterGroup.getTopicParameterGroup().getTopicSinks());
100 topicSources = TopicEndpointManager.getManager()
101 .addTopicSources(apexStarterParameterGroup.getTopicParameterGroup().getTopicSources());
103 instanceId = NetworkUtil.genUniqueName("apex");
104 LOGGER.debug("ApexStarterActivator initializing with instance id: {}", instanceId);
106 this.parameterGroup = apexStarterParameterGroup;
107 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
108 } catch (final RuntimeException e) {
109 throw new ApexStarterRunTimeException(e);
112 final PdpUpdateListener pdpUpdateListener = new PdpUpdateListener();
113 final PdpStateChangeListener pdpStateChangeListener = new PdpStateChangeListener();
114 final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler();
115 supportedPolicyTypes =
116 pdpMessageHandler.getSupportedPolicyTypesFromParameters(apexStarterParameterGroup.getPdpStatusParameters());
119 this.manager = new ServiceManager()
121 () -> TopicEndpointManager.getManager().start(),
122 () -> TopicEndpointManager.getManager().shutdown())
123 .addAction("set alive",
124 () -> setAlive(true),
125 () -> setAlive(false))
126 .addAction("register pdp status context object",
127 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_OBJECT,
128 pdpMessageHandler.createPdpStatusFromParameters(instanceId,
129 apexStarterParameterGroup.getPdpStatusParameters())),
130 () -> Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_OBJECT))
131 .addAction("topic sinks",
132 () -> Registry.register(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS, topicSinks),
133 () -> Registry.unregister(ApexStarterConstants.REG_APEX_PDP_TOPIC_SINKS))
134 .addAction("Pdp Status publisher",
135 () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
136 new PdpStatusPublisher(topicSinks,
137 apexStarterParameterGroup.getPdpStatusParameters().getTimeIntervalMs())),
138 this::stopAndRemovePdpStatusPublisher)
139 .addAction("Register pdp update listener",
140 () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), pdpUpdateListener),
141 () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name()))
142 .addAction("Register pdp state change request dispatcher",
143 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), pdpStateChangeListener),
144 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()))
145 .addAction("Message Dispatcher",
146 this::registerMsgDispatcher,
147 this::unregisterMsgDispatcher)
148 .addAction("Create REST server",
149 () -> restServer = new RestServer(apexStarterParameterGroup.getRestServerParameters(),
150 ApexStarterAafFilter.class, HealthCheckRestControllerV1.class),
151 () -> restServer = null)
152 .addAction("Rest Server",
153 () -> restServer.start(),
154 () -> restServer.stop());
160 * Method to stop and unregister the pdp status publisher.
162 private void stopAndRemovePdpStatusPublisher() {
163 final PdpStatusPublisher pdpStatusPublisher =
164 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
165 pdpStatusPublisher.terminate();
166 Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER);
170 * Initialize ApexStarter service.
172 * @throws ApexStarterException on errors in initializing the service
174 public void initialize() throws ApexStarterException {
176 throw new IllegalStateException("activator already initialized");
180 LOGGER.debug("ApexStarter starting as a service . . .");
182 LOGGER.debug("ApexStarter started as a service");
183 } catch (final ServiceManagerException exp) {
184 LOGGER.error("ApexStarter service startup failed");
185 throw new ApexStarterException(exp.getMessage(), exp);
190 * Terminate ApexStarter.
192 * @throws ApexStarterException on errors in terminating the service
194 public void terminate() throws ApexStarterException {
196 throw new IllegalStateException("activator is not running");
199 final PdpStatusPublisher pdpStatusPublisher =
200 Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class);
201 // send a final heartbeat with terminated status
202 pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus());
204 Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR);
205 } catch (final ServiceManagerException exp) {
206 LOGGER.error("ApexStarter termination failed");
207 throw new ApexStarterException(exp.getMessage(), exp);
212 * Registers the dispatcher with the topic source(s).
214 private void registerMsgDispatcher() {
215 for (final TopicSource source : topicSources) {
216 source.register(msgDispatcher);
221 * Unregisters the dispatcher from the topic source(s).
223 private void unregisterMsgDispatcher() {
224 for (final TopicSource source : topicSources) {
225 source.unregister(msgDispatcher);