2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
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.pdpx.main.startstop;
23 import java.nio.file.Paths;
24 import java.util.Arrays;
27 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
28 import org.onap.policy.common.endpoints.event.comm.TopicSource;
29 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
30 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
31 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.services.ServiceManagerContainer;
34 import org.onap.policy.models.pdp.concepts.PdpStatus;
35 import org.onap.policy.models.pdp.enums.PdpMessageType;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
37 import org.onap.policy.pdpx.main.XacmlState;
38 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
39 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
40 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpUpdateListener;
41 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
42 import org.onap.policy.pdpx.main.rest.XacmlPdpAafFilter;
43 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpRestController;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * This class wraps a distributor so that it can be activated as a complete service together with
51 * all its xacml pdp and forwarding handlers.
53 public class XacmlPdpActivator extends ServiceManagerContainer {
55 // The logger for this class
56 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
58 private static final String[] MSG_TYPE_NAMES = {"messageName"};
59 private static final String TOPIC = "POLICY-PDP-PAP";
63 private static XacmlPdpActivator current = null;
64 private final XacmlPdpRestServer restServer;
66 // The parameters of this policy xacml pdp activator
67 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
70 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
71 * dispatches them to appropriate listener.
73 private final MessageTypeDispatcher msgDispatcher;
76 * Instantiate the activator for policy xacml pdp as a complete service.
78 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
80 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
81 LOGGER.info("Activator initializing using {}", xacmlPdpParameterGroup);
83 TopicEndpointManager.getManager().addTopics(xacmlPdpParameterGroup.getTopicParameterGroup());
85 final XacmlPdpHearbeatPublisher heartbeat;
86 final TopicSinkClient sinkClient;
87 final XacmlState state;
90 XacmlPdpApplicationManager appmgr =
91 new XacmlPdpApplicationManager(Paths.get(xacmlPdpParameterGroup.getApplicationPath()),
92 xacmlPdpParameterGroup.getPolicyApiParameters());
93 XacmlPdpApplicationManager.setCurrent(appmgr);
95 XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
96 XacmlPdpStatisticsManager.setCurrent(stats);
97 stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
98 stats.setTotalPolicyCount(appmgr.getPolicyCount());
100 state = new XacmlState(appmgr, xacmlPdpParameterGroup.getPdpGroup(), xacmlPdpParameterGroup.getPdpType());
102 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
103 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
105 sinkClient = new TopicSinkClient(TOPIC);
106 heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
109 * since the dispatcher isn't registered with the topic yet, we can go ahead
110 * and register the listeners with it.
112 msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
113 new XacmlPdpStateChangeListener(sinkClient, state));
114 msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
115 new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
117 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
118 XacmlPdpAafFilter.class, XacmlPdpRestController.class);
120 } catch (RuntimeException | TopicSinkClientException e) {
121 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
124 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
127 addAction("XACML PDP parameters",
128 () -> ParameterService.register(xacmlPdpParameterGroup),
129 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
131 addAction("Message Dispatcher",
132 this::registerMsgDispatcher,
133 this::unregisterMsgDispatcher);
136 TopicEndpointManager.getManager()::start,
137 TopicEndpointManager.getManager()::shutdown);
139 addAction("Terminate PDP",
141 () -> sendTerminateMessage(sinkClient, state));
143 // initial heart beats act as registration messages
144 addAction("Heartbeat Publisher",
146 heartbeat::terminate);
148 addAction("REST Server",
156 * Method used to send a terminate message to the PAP.
158 private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
159 PdpStatus terminateStatus = state.terminatePdpMessage();
160 sinkClient.send(terminateStatus);
164 * Get the parameters used by the activator.
166 * @return the parameters of the activator
168 public XacmlPdpParameterGroup getParameterGroup() {
169 return xacmlPdpParameterGroup;
173 * Method to register the parameters to Common Parameter Service.
175 * @param xacmlPdpParameterGroup the xacml pdp parameter group
177 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
178 ParameterService.register(xacmlPdpParameterGroup);
182 * Method to deregister the parameters from Common Parameter Service.
184 * @param xacmlPdpParameterGroup the xacml pdp parameter group
186 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
187 ParameterService.deregister(xacmlPdpParameterGroup.getName());
191 * Registers the dispatcher with the topic source(s).
193 private void registerMsgDispatcher() {
194 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
195 source.register(msgDispatcher);
200 * Unregisters the dispatcher from the topic source(s).
202 private void unregisterMsgDispatcher() {
203 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
204 source.unregister(msgDispatcher);
209 * Start the xacmlpdp rest controller.
211 public void startXacmlRestController() {
212 if (isXacmlRestControllerAlive()) {
213 LOGGER.info("Xacml rest controller already running");
220 * Stop the xacmlpdp rest controller.
222 public void stopXacmlRestController() {
223 if (isXacmlRestControllerAlive()) {
226 LOGGER.info("Xacml rest controller already stopped");
230 public boolean isXacmlRestControllerAlive() {
231 return restServer.isAlive();