2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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;
25 import java.util.Properties;
28 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
29 import org.onap.policy.common.endpoints.event.comm.TopicSource;
30 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
31 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
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.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;
67 // The parameters of this policy xacml pdp activator
68 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
71 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
72 * dispatches them to appropriate listener.
74 private final MessageTypeDispatcher msgDispatcher;
77 * Instantiate the activator for policy xacml pdp as a complete service.
79 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
80 * @param topicProperties properties used to configure the topics
82 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
83 LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
85 TopicEndpointManager.getManager().addTopicSinks(topicProperties);
86 TopicEndpointManager.getManager().addTopicSources(topicProperties);
88 final XacmlPdpHearbeatPublisher heartbeat;
89 final TopicSinkClient sinkClient;
90 final XacmlState state;
91 final RestServer restServer;
94 XacmlPdpApplicationManager appmgr =
95 new XacmlPdpApplicationManager(Paths.get(xacmlPdpParameterGroup.getApplicationPath()));
96 XacmlPdpApplicationManager.setCurrent(appmgr);
98 XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
99 XacmlPdpStatisticsManager.setCurrent(stats);
100 stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
101 stats.setTotalPolicyCount(appmgr.getPolicyCount());
103 state = new XacmlState(appmgr);
105 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
106 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
108 sinkClient = new TopicSinkClient(TOPIC);
109 heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
112 * since the dispatcher isn't registered with the topic yet, we can go ahead
113 * and register the listeners with it.
115 msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
116 new XacmlPdpStateChangeListener(sinkClient, state));
117 msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
118 new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
120 restServer = new RestServer(xacmlPdpParameterGroup.getRestServerParameters(), XacmlPdpAafFilter.class,
121 XacmlPdpRestController.class);
123 } catch (RuntimeException | TopicSinkClientException e) {
124 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
127 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
130 addAction("XACML PDP parameters",
131 () -> ParameterService.register(xacmlPdpParameterGroup),
132 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
134 addAction("Message Dispatcher",
135 this::registerMsgDispatcher,
136 this::unregisterMsgDispatcher);
139 TopicEndpointManager.getManager()::start,
140 TopicEndpointManager.getManager()::shutdown);
142 addAction("Terminate PDP",
144 () -> sendTerminateMessage(sinkClient, state));
145 // initial heart beats act as registration messages
146 addAction("Heartbeat Publisher",
148 heartbeat::terminate);
150 addAction("REST server",
158 * Method used to send a terminate message to the PAP.
160 private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
161 PdpStatus terminateStatus = state.terminatePdpMessage();
162 sinkClient.send(terminateStatus);
166 * Get the parameters used by the activator.
168 * @return the parameters of the activator
170 public XacmlPdpParameterGroup getParameterGroup() {
171 return xacmlPdpParameterGroup;
175 * Method to register the parameters to Common Parameter Service.
177 * @param xacmlPdpParameterGroup the xacml pdp parameter group
179 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
180 ParameterService.register(xacmlPdpParameterGroup);
184 * Method to deregister the parameters from Common Parameter Service.
186 * @param xacmlPdpParameterGroup the xacml pdp parameter group
188 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
189 ParameterService.deregister(xacmlPdpParameterGroup.getName());
193 * Registers the dispatcher with the topic source(s).
195 private void registerMsgDispatcher() {
196 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
197 source.register(msgDispatcher);
202 * Unregisters the dispatcher from the topic source(s).
204 private void unregisterMsgDispatcher() {
205 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
206 source.unregister(msgDispatcher);