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.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.parameters.ParameterService;
34 import org.onap.policy.common.utils.services.ServiceManagerContainer;
35 import org.onap.policy.models.pdp.concepts.PdpStatus;
36 import org.onap.policy.models.pdp.enums.PdpMessageType;
37 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
38 import org.onap.policy.pdpx.main.XacmlState;
39 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
40 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
41 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpUpdateListener;
42 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
43 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpRestServer;
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;
65 // The parameters of this policy xacml pdp activator
66 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
69 * The XACML PDP REST API server.
71 private XacmlPdpRestServer restServer;
74 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
75 * dispatches them to appropriate listener.
77 private final MessageTypeDispatcher msgDispatcher;
80 * Instantiate the activator for policy xacml pdp as a complete service.
82 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
83 * @param topicProperties properties used to configure the topics
85 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
86 LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
88 TopicEndpointManager.getManager().addTopicSinks(topicProperties);
89 TopicEndpointManager.getManager().addTopicSources(topicProperties);
91 final XacmlPdpHearbeatPublisher heartbeat;
92 final TopicSinkClient sinkClient;
93 final XacmlState state;
96 XacmlPdpApplicationManager appmgr =
97 new XacmlPdpApplicationManager(Paths.get(xacmlPdpParameterGroup.getApplicationPath()));
98 XacmlPdpApplicationManager.setCurrent(appmgr);
100 XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
101 XacmlPdpStatisticsManager.setCurrent(stats);
102 stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
103 stats.setTotalPolicyCount(appmgr.getPolicyCount());
105 state = new XacmlState(appmgr);
107 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
108 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
110 sinkClient = new TopicSinkClient(TOPIC);
111 heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
114 * since the dispatcher isn't registered with the topic yet, we can go ahead
115 * and register the listeners with it.
117 msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
118 new XacmlPdpStateChangeListener(sinkClient, state));
119 msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
120 new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
122 } catch (RuntimeException | TopicSinkClientException e) {
123 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
126 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
129 addAction("XACML PDP parameters",
130 () -> ParameterService.register(xacmlPdpParameterGroup),
131 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
133 addAction("Message Dispatcher",
134 this::registerMsgDispatcher,
135 this::unregisterMsgDispatcher);
138 TopicEndpointManager.getManager()::start,
139 TopicEndpointManager.getManager()::shutdown);
141 addAction("Terminate PDP",
143 () -> sendTerminateMessage(sinkClient, state));
144 // initial heart beats act as registration messages
145 addAction("Heartbeat Publisher",
147 heartbeat::terminate);
149 addAction("Create REST server",
150 () -> restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters()),
151 () -> restServer = null);
153 addAction("REST server",
154 () -> restServer.start(),
155 () -> restServer.stop());
161 * Method used to send a terminate message to the PAP.
163 private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
164 PdpStatus terminateStatus = state.terminatePdpMessage();
165 sinkClient.send(terminateStatus);
169 * Get the parameters used by the activator.
171 * @return the parameters of the activator
173 public XacmlPdpParameterGroup getParameterGroup() {
174 return xacmlPdpParameterGroup;
178 * Method to register the parameters to Common Parameter Service.
180 * @param xacmlPdpParameterGroup the xacml pdp parameter group
182 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
183 ParameterService.register(xacmlPdpParameterGroup);
187 * Method to deregister the parameters from Common Parameter Service.
189 * @param xacmlPdpParameterGroup the xacml pdp parameter group
191 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
192 ParameterService.deregister(xacmlPdpParameterGroup.getName());
196 * Registers the dispatcher with the topic source(s).
198 private void registerMsgDispatcher() {
199 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
200 source.register(msgDispatcher);
205 * Unregisters the dispatcher from the topic source(s).
207 private void unregisterMsgDispatcher() {
208 for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
209 source.unregister(msgDispatcher);