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.util.Arrays;
26 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
27 import org.onap.policy.common.endpoints.event.comm.TopicSource;
28 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
29 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
30 import org.onap.policy.common.endpoints.http.client.HttpClient;
31 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
32 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
33 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
34 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
35 import org.onap.policy.common.parameters.ParameterService;
36 import org.onap.policy.common.utils.services.ServiceManagerContainer;
37 import org.onap.policy.models.pdp.concepts.PdpStatus;
38 import org.onap.policy.models.pdp.enums.PdpMessageType;
39 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
40 import org.onap.policy.pdpx.main.XacmlState;
41 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
42 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
43 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpUpdateListener;
44 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpAafFilter;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
47 import org.onap.policy.pdpx.main.rest.XacmlPdpRestController;
48 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * This class wraps a distributor so that it can be activated as a complete service together with
54 * all its xacml pdp and forwarding handlers.
56 public class XacmlPdpActivator extends ServiceManagerContainer {
58 // The logger for this class
59 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
61 private static final String[] MSG_TYPE_NAMES = {"messageName"};
62 private static final String TOPIC = "POLICY-PDP-PAP";
66 private static XacmlPdpActivator current = null;
67 private final XacmlPdpRestServer restServer;
69 // The parameters of this policy xacml pdp activator
70 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
73 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
74 * dispatches them to appropriate listener.
76 private final MessageTypeDispatcher msgDispatcher;
79 * Instantiate the activator for policy xacml pdp as a complete service.
81 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
83 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
84 LOGGER.info("Activator initializing using {}", xacmlPdpParameterGroup);
86 RestClientParameters apiClientParams = xacmlPdpParameterGroup.getPolicyApiParameters();
88 TopicEndpointManager.getManager().addTopics(xacmlPdpParameterGroup.getTopicParameterGroup());
90 final XacmlPdpHearbeatPublisher heartbeat;
91 final TopicSinkClient sinkClient;
92 final XacmlState state;
95 HttpClient apiClient = HttpClientFactoryInstance.getClientFactory().build(apiClientParams);
97 var appmgr = new XacmlPdpApplicationManager(xacmlPdpParameterGroup.getApplicationParameters(),
99 XacmlPdpApplicationManager.setCurrent(appmgr);
101 var stats = new XacmlPdpStatisticsManager();
102 XacmlPdpStatisticsManager.setCurrent(stats);
103 stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
104 stats.setTotalPolicyCount(appmgr.getPolicyCount());
106 state = new XacmlState(appmgr, xacmlPdpParameterGroup.getPdpGroup(), xacmlPdpParameterGroup.getPdpType());
108 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
109 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
111 sinkClient = new TopicSinkClient(TOPIC);
112 heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
115 * since the dispatcher isn't registered with the topic yet, we can go ahead
116 * and register the listeners with it.
118 msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
119 new XacmlPdpStateChangeListener(sinkClient, state));
120 msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
121 new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
123 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
124 XacmlPdpAafFilter.class, XacmlPdpRestController.class);
126 } catch (RuntimeException | TopicSinkClientException | HttpClientConfigException e) {
127 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
130 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
133 addAction("XACML PDP parameters",
134 () -> ParameterService.register(xacmlPdpParameterGroup),
135 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
137 addAction("Message Dispatcher",
138 this::registerMsgDispatcher,
139 this::unregisterMsgDispatcher);
142 TopicEndpointManager.getManager()::start,
143 TopicEndpointManager.getManager()::shutdown);
145 addAction("Terminate PDP",
147 () -> sendTerminateMessage(sinkClient, state));
148 // initial heart beats act as registration messages
149 addAction("Heartbeat Publisher",
151 heartbeat::terminate);
157 * Method used to send a terminate message to the PAP.
159 private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
160 PdpStatus terminateStatus = state.terminatePdpMessage();
161 sinkClient.send(terminateStatus);
165 * Get the parameters used by the activator.
167 * @return the parameters of the activator
169 public XacmlPdpParameterGroup getParameterGroup() {
170 return xacmlPdpParameterGroup;
174 * Method to register the parameters to Common Parameter Service.
176 * @param xacmlPdpParameterGroup the xacml pdp parameter group
178 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
179 ParameterService.register(xacmlPdpParameterGroup);
183 * Method to deregister the parameters from Common Parameter Service.
185 * @param xacmlPdpParameterGroup the xacml pdp parameter group
187 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
188 ParameterService.deregister(xacmlPdpParameterGroup.getName());
192 * Registers the dispatcher with the topic source(s).
194 private void registerMsgDispatcher() {
195 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
196 source.register(msgDispatcher);
201 * Unregisters the dispatcher from the topic source(s).
203 private void unregisterMsgDispatcher() {
204 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
205 source.unregister(msgDispatcher);
210 * Start the xacmlpdp rest controller.
212 public void startXacmlRestController() {
213 if (isXacmlRestControllerAlive()) {
214 LOGGER.info("Xacml rest controller already running");
221 * Stop the xacmlpdp rest controller.
223 public void stopXacmlRestController() {
224 if (isXacmlRestControllerAlive()) {
227 LOGGER.info("Xacml rest controller already stopped");
231 public boolean isXacmlRestControllerAlive() {
232 return restServer.isAlive();