2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
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.pap.main.startstop;
24 import java.util.Arrays;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
29 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
30 import org.onap.policy.common.endpoints.event.comm.TopicSource;
31 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
32 import org.onap.policy.common.endpoints.http.server.RestServer;
33 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
34 import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher;
35 import org.onap.policy.common.parameters.ParameterService;
36 import org.onap.policy.common.utils.services.Registry;
37 import org.onap.policy.common.utils.services.ServiceManagerContainer;
38 import org.onap.policy.models.pap.concepts.PolicyNotification;
39 import org.onap.policy.models.pdp.concepts.PdpMessage;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.enums.PdpMessageType;
42 import org.onap.policy.pap.main.PapConstants;
43 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
44 import org.onap.policy.pap.main.PolicyPapRuntimeException;
45 import org.onap.policy.pap.main.comm.PdpHeartbeatListener;
46 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
47 import org.onap.policy.pap.main.comm.Publisher;
48 import org.onap.policy.pap.main.comm.TimerManager;
49 import org.onap.policy.pap.main.notification.PolicyNotifier;
50 import org.onap.policy.pap.main.parameters.PapParameterGroup;
51 import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams;
52 import org.onap.policy.pap.main.rest.HealthCheckRestControllerV1;
53 import org.onap.policy.pap.main.rest.PapAafFilter;
54 import org.onap.policy.pap.main.rest.PapStatisticsManager;
55 import org.onap.policy.pap.main.rest.PdpGroupCreateOrUpdateControllerV1;
56 import org.onap.policy.pap.main.rest.PdpGroupDeleteControllerV1;
57 import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;
58 import org.onap.policy.pap.main.rest.PdpGroupHealthCheckControllerV1;
59 import org.onap.policy.pap.main.rest.PdpGroupQueryControllerV1;
60 import org.onap.policy.pap.main.rest.PdpGroupStateChangeControllerV1;
61 import org.onap.policy.pap.main.rest.PolicyComponentsHealthCheckControllerV1;
62 import org.onap.policy.pap.main.rest.PolicyComponentsHealthCheckProvider;
63 import org.onap.policy.pap.main.rest.PolicyStatusControllerV1;
64 import org.onap.policy.pap.main.rest.PolicyUndeployerImpl;
65 import org.onap.policy.pap.main.rest.StatisticsRestControllerV1;
68 * This class activates Policy Administration (PAP) as a complete service together with all its controllers, listeners &
71 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
73 public class PapActivator extends ServiceManagerContainer {
74 private static final String[] MSG_TYPE_NAMES = { "messageName" };
75 private static final String[] REQ_ID_NAMES = { "response", "responseTo" };
78 * Max number of heat beats that can be missed before PAP removes a PDP.
80 private static final int MAX_MISSED_HEARTBEATS = 3;
82 private final PapParameterGroup papParameterGroup;
85 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then dispatches them to
86 * {@link #reqIdDispatcher}.
88 private final MessageTypeDispatcher msgDispatcher;
91 * Listens for {@link PdpStatus} messages and then routes them to the listener associated with the ID of the
92 * originating request.
94 private final RequestIdDispatcher<PdpStatus> reqIdDispatcher;
97 * Listener for anonymous {@link PdpStatus} messages either for registration or heartbeat.
99 private final PdpHeartbeatListener pdpHeartbeatListener;
102 * Instantiate the activator for policy pap as a complete service.
104 * @param papParameterGroup the parameters for the pap service
106 public PapActivator(final PapParameterGroup papParameterGroup) {
109 TopicEndpointManager.getManager().addTopics(papParameterGroup.getTopicParameterGroup());
112 this.papParameterGroup = papParameterGroup;
113 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
114 this.reqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES);
115 this.pdpHeartbeatListener = new PdpHeartbeatListener(papParameterGroup.getPdpParameters());
117 } catch (final RuntimeException e) {
118 throw new PolicyPapRuntimeException(e);
121 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
123 final var pdpUpdateLock = new Object();
124 final var pdpParams = papParameterGroup.getPdpParameters();
125 final AtomicReference<Publisher<PdpMessage>> pdpPub = new AtomicReference<>();
126 final AtomicReference<Publisher<PolicyNotification>> notifyPub = new AtomicReference<>();
127 final AtomicReference<TimerManager> pdpUpdTimers = new AtomicReference<>();
128 final AtomicReference<TimerManager> pdpStChgTimers = new AtomicReference<>();
129 final AtomicReference<ScheduledExecutorService> pdpExpirationTimer = new AtomicReference<>();
130 final AtomicReference<PolicyModelsProviderFactoryWrapper> daoFactory = new AtomicReference<>();
131 final AtomicReference<PdpModifyRequestMap> requestMap = new AtomicReference<>();
132 final AtomicReference<RestServer> restServer = new AtomicReference<>();
133 final AtomicReference<PolicyNotifier> notifier = new AtomicReference<>();
136 addAction("PAP parameters",
137 () -> ParameterService.register(papParameterGroup),
138 () -> ParameterService.deregister(papParameterGroup.getName()));
140 addAction("DAO Factory",
141 () -> daoFactory.set(new PolicyModelsProviderFactoryWrapper(
142 papParameterGroup.getDatabaseProviderParameters())),
143 () -> daoFactory.get().close());
145 addAction("DAO Factory registration",
146 () -> Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daoFactory.get()),
147 () -> Registry.unregister(PapConstants.REG_PAP_DAO_FACTORY));
149 addAction("Pdp Heartbeat Listener",
150 () -> reqIdDispatcher.register(pdpHeartbeatListener),
151 () -> reqIdDispatcher.unregister(pdpHeartbeatListener));
153 addAction("Request ID Dispatcher",
154 () -> msgDispatcher.register(PdpMessageType.PDP_STATUS.name(), this.reqIdDispatcher),
155 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATUS.name()));
157 addAction("Message Dispatcher",
158 this::registerMsgDispatcher,
159 this::unregisterMsgDispatcher);
162 TopicEndpointManager.getManager()::start,
163 TopicEndpointManager.getManager()::shutdown);
165 addAction("PAP statistics",
166 () -> Registry.register(PapConstants.REG_STATISTICS_MANAGER, new PapStatisticsManager()),
167 () -> Registry.unregister(PapConstants.REG_STATISTICS_MANAGER));
169 addAction("PDP publisher",
171 pdpPub.set(new Publisher<>(PapConstants.TOPIC_POLICY_PDP_PAP));
172 startThread(pdpPub.get());
174 () -> pdpPub.get().stop());
176 addAction("Policy Notification publisher",
178 notifyPub.set(new Publisher<>(PapConstants.TOPIC_POLICY_NOTIFICATION));
179 startThread(notifyPub.get());
180 notifier.set(new PolicyNotifier(notifyPub.get(), daoFactory.get()));
182 () -> notifyPub.get().stop());
184 addAction("Policy Notifier",
185 () -> Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier.get()),
186 () -> Registry.unregister(PapConstants.REG_POLICY_NOTIFIER));
188 addAction("PDP update timers",
190 pdpUpdTimers.set(new TimerManager("update", pdpParams.getUpdateParameters().getMaxWaitMs()));
191 startThread(pdpUpdTimers.get());
193 () -> pdpUpdTimers.get().stop());
195 addAction("PDP state-change timers",
197 pdpStChgTimers.set(new TimerManager("state-change", pdpParams.getUpdateParameters().getMaxWaitMs()));
198 startThread(pdpStChgTimers.get());
200 () -> pdpStChgTimers.get().stop());
202 addAction("PDP modification lock",
203 () -> Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, pdpUpdateLock),
204 () -> Registry.unregister(PapConstants.REG_PDP_MODIFY_LOCK));
206 addAction("PDP modification requests",
208 requestMap.set(new PdpModifyRequestMap(
209 PdpModifyRequestMapParams.builder()
210 .maxPdpAgeMs(MAX_MISSED_HEARTBEATS * pdpParams.getHeartBeatMs())
211 .daoFactory(daoFactory.get())
212 .modifyLock(pdpUpdateLock)
214 .policyNotifier(notifier.get())
215 .pdpPublisher(pdpPub.get())
216 .responseDispatcher(reqIdDispatcher)
217 .stateChangeTimers(pdpStChgTimers.get())
218 .updateTimers(pdpUpdTimers.get())
220 Registry.register(PapConstants.REG_PDP_MODIFY_MAP, requestMap.get());
222 // now that it's registered, we can attach a "policy undeploy" provider
223 requestMap.get().setPolicyUndeployer(new PolicyUndeployerImpl());
225 () -> Registry.unregister(PapConstants.REG_PDP_MODIFY_MAP));
227 addAction("PDP expiration timer",
229 long frequencyMs = pdpParams.getHeartBeatMs();
230 pdpExpirationTimer.set(Executors.newScheduledThreadPool(1));
231 pdpExpirationTimer.get().scheduleWithFixedDelay(
232 requestMap.get()::removeExpiredPdps,
235 TimeUnit.MILLISECONDS);
237 () -> pdpExpirationTimer.get().shutdownNow());
239 addAction("PAP client executor",
241 PolicyComponentsHealthCheckProvider.initializeClientHealthCheckExecutorService(papParameterGroup,
242 HttpClientFactoryInstance.getClientFactory()),
243 PolicyComponentsHealthCheckProvider::cleanup);
245 addAction("REST server",
247 var server = new RestServer(papParameterGroup.getRestServerParameters(), PapAafFilter.class,
248 HealthCheckRestControllerV1.class,
249 StatisticsRestControllerV1.class,
250 PdpGroupCreateOrUpdateControllerV1.class,
251 PdpGroupDeployControllerV1.class,
252 PdpGroupDeleteControllerV1.class,
253 PdpGroupStateChangeControllerV1.class,
254 PdpGroupQueryControllerV1.class,
255 PdpGroupHealthCheckControllerV1.class,
256 PolicyStatusControllerV1.class,
257 PolicyComponentsHealthCheckControllerV1.class);
258 restServer.set(server);
259 restServer.get().start();
261 () -> restServer.get().stop());
266 * Starts a background thread.
268 * @param runner function to run in the background
270 private void startThread(final Runnable runner) {
271 final var thread = new Thread(runner);
272 thread.setDaemon(true);
278 * Get the parameters used by the activator.
280 * @return the parameters of the activator
282 public PapParameterGroup getParameterGroup() {
283 return papParameterGroup;
287 * Registers the dispatcher with the topic source(s).
289 private void registerMsgDispatcher() {
290 for (final TopicSource source : TopicEndpointManager.getManager()
291 .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
292 source.register(msgDispatcher);
297 * Unregisters the dispatcher from the topic source(s).
299 private void unregisterMsgDispatcher() {
300 for (final TopicSource source : TopicEndpointManager.getManager()
301 .getTopicSources(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP))) {
302 source.unregister(msgDispatcher);