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;
25 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
26 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClient;
27 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
28 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
29 import org.onap.policy.common.endpoints.http.client.HttpClient;
30 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
31 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
32 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.common.utils.services.ServiceManagerContainer;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
37 import org.onap.policy.models.pdp.enums.PdpMessageType;
38 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
39 import org.onap.policy.pdpx.main.XacmlState;
40 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
41 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
42 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpUpdateListener;
43 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpAafFilter;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpRestController;
47 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * This class wraps a distributor so that it can be activated as a complete service together with
53 * all its xacml pdp and forwarding handlers.
55 public class XacmlPdpActivator extends ServiceManagerContainer {
57 // The logger for this class
58 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
60 private static final String[] MSG_TYPE_NAMES = {"messageName"};
61 private static final String TOPIC = "POLICY-PDP-PAP";
65 private static XacmlPdpActivator current = null;
66 private final XacmlPdpRestServer restServer;
68 // The parameters of this policy xacml pdp activator
69 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
72 * POLICY-PDP-PAP client.
74 private BidirectionalTopicClient topicClient;
77 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
78 * dispatches them to appropriate listener.
80 private final MessageTypeDispatcher msgDispatcher;
83 * Instantiate the activator for policy xacml pdp as a complete service.
85 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
87 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
88 LOGGER.info("Activator initializing using {}", xacmlPdpParameterGroup);
90 RestClientParameters apiClientParams = xacmlPdpParameterGroup.getPolicyApiParameters();
92 TopicEndpointManager.getManager().addTopics(xacmlPdpParameterGroup.getTopicParameterGroup());
94 final XacmlPdpHearbeatPublisher heartbeat;
95 final TopicSinkClient sinkClient;
96 final XacmlState state;
99 HttpClient apiClient = HttpClientFactoryInstance.getClientFactory().build(apiClientParams);
101 var appmgr = new XacmlPdpApplicationManager(xacmlPdpParameterGroup.getApplicationParameters(),
103 XacmlPdpApplicationManager.setCurrent(appmgr);
105 var stats = new XacmlPdpStatisticsManager();
106 XacmlPdpStatisticsManager.setCurrent(stats);
107 stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
108 stats.setTotalPolicyCount(appmgr.getPolicyCount());
110 state = new XacmlState(appmgr, xacmlPdpParameterGroup.getPdpGroup(), xacmlPdpParameterGroup.getPdpType());
112 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
113 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
115 topicClient = new BidirectionalTopicClient(TOPIC, TOPIC);
116 sinkClient = new TopicSinkClient(topicClient.getSink());
118 heartbeat = new XacmlPdpHearbeatPublisher(topicClient,
119 xacmlPdpParameterGroup.getProbeHeartbeatTopicSec() * 1000, state);
122 * since the dispatcher isn't registered with the topic yet, we can go ahead
123 * and register the listeners with it.
125 msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
126 new XacmlPdpStateChangeListener(sinkClient, state));
127 msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
128 new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
130 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
131 XacmlPdpAafFilter.class, XacmlPdpRestController.class);
133 } catch (RuntimeException | HttpClientConfigException | BidirectionalTopicClientException e) {
134 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
137 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
140 addAction("XACML PDP parameters",
141 () -> ParameterService.register(xacmlPdpParameterGroup),
142 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
144 addAction("Message Dispatcher",
145 this::registerMsgDispatcher,
146 this::unregisterMsgDispatcher);
149 TopicEndpointManager.getManager()::start,
150 TopicEndpointManager.getManager()::shutdown);
152 addAction("Terminate PDP",
154 () -> sendTerminateMessage(sinkClient, state));
156 // initial heart beats act as registration messages
157 addAction("Heartbeat Publisher",
159 heartbeat::terminate);
161 addAction("REST Server",
169 * Method used to send a terminate message to the PAP.
171 private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
172 PdpStatus terminateStatus = state.terminatePdpMessage();
173 sinkClient.send(terminateStatus);
177 * Get the parameters used by the activator.
179 * @return the parameters of the activator
181 public XacmlPdpParameterGroup getParameterGroup() {
182 return xacmlPdpParameterGroup;
186 * Method to register the parameters to Common Parameter Service.
188 * @param xacmlPdpParameterGroup the xacml pdp parameter group
190 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
191 ParameterService.register(xacmlPdpParameterGroup);
195 * Method to deregister the parameters from Common Parameter Service.
197 * @param xacmlPdpParameterGroup the xacml pdp parameter group
199 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
200 ParameterService.deregister(xacmlPdpParameterGroup.getName());
204 * Registers the dispatcher with the topic source(s).
206 private void registerMsgDispatcher() {
207 topicClient.getSource().register(msgDispatcher);
211 * Unregisters the dispatcher from the topic source(s).
213 private void unregisterMsgDispatcher() {
214 topicClient.getSource().unregister(msgDispatcher);
218 * Start the xacmlpdp rest controller.
220 public void startXacmlRestController() {
221 if (isXacmlRestControllerAlive()) {
222 LOGGER.info("Xacml rest controller already running");
229 * Stop the xacmlpdp rest controller.
231 public void stopXacmlRestController() {
232 if (isXacmlRestControllerAlive()) {
235 LOGGER.info("Xacml rest controller already stopped");
239 public boolean isXacmlRestControllerAlive() {
240 return restServer.isAlive();