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.util.Arrays;
24 import java.util.Properties;
26 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
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.listeners.MessageTypeDispatcher;
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.onap.policy.common.utils.services.ServiceManagerContainer;
33 import org.onap.policy.models.pdp.concepts.PdpStatus;
34 import org.onap.policy.models.pdp.concepts.PdpUpdate;
35 import org.onap.policy.models.pdp.enums.PdpMessageType;
36 import org.onap.policy.models.pdp.enums.PdpState;
37 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
38 import org.onap.policy.pdpx.main.comm.XacmlPdpMessage;
39 import org.onap.policy.pdpx.main.comm.XacmlPdpPapRegistration;
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.XacmlPdpRestServer;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * This class wraps a distributor so that it can be activated as a complete service together with
49 * all its xacml pdp and forwarding handlers.
51 public class XacmlPdpActivator extends ServiceManagerContainer {
53 // The logger for this class
54 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
56 private static final String[] MSG_TYPE_NAMES = {"messageName"};
57 private static final String TOPIC = "POLICY-PDP-PAP";
59 // The parameters of this policy xacml pdp activator
60 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
63 * The XACML PDP REST API server.
65 private XacmlPdpRestServer restServer;
68 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
69 * dispatches them to appropriate listener.
71 private final MessageTypeDispatcher msgDispatcher;
74 * Listens for {@link PdpStateChange} messages from the PAP.
76 private final XacmlPdpStateChangeListener pdpStateChangeListener;
79 * Listens for {@link PdpUpdate} messages from the PAP.
81 private final XacmlPdpUpdateListener pdpUpdateListener;
84 * The current activator.
86 private static XacmlPdpActivator current = null;
88 private volatile boolean alive = false;
90 private XacmlPdpPapRegistration register;
92 private XacmlPdpMessage message;
95 * Instantiate the activator for policy xacml pdp as a complete service.
97 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
98 * @param topicProperties properties used to configure the topics
100 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
101 LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
103 TopicEndpoint.manager.addTopicSinks(topicProperties);
104 TopicEndpoint.manager.addTopicSources(topicProperties);
107 final TopicSinkClient sinkClient = new TopicSinkClient(TOPIC);
108 this.message = new XacmlPdpMessage();
109 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
110 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
111 this.pdpStateChangeListener = new XacmlPdpStateChangeListener(sinkClient, message);
112 this.pdpUpdateListener = new XacmlPdpUpdateListener(sinkClient, message);
113 this.register = new XacmlPdpPapRegistration(sinkClient);
114 } catch (RuntimeException | TopicSinkClientException e) {
115 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
118 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
121 addAction("XACML PDP parameters", () -> ParameterService.register(xacmlPdpParameterGroup),
122 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
124 addAction("PdpStateChange Dispatcher",
125 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), this.pdpStateChangeListener),
126 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()));
128 addAction("PdpUpdate Dispatcher",
129 () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), this.pdpUpdateListener),
130 () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name()));
132 addAction("Message Dispatcher",
133 () -> registerMsgDispatcher(),
134 () -> unregisterMsgDispatcher());
137 () -> TopicEndpoint.manager.start(),
138 () -> TopicEndpoint.manager.shutdown());
140 addAction("Create REST server",
142 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
143 xacmlPdpParameterGroup.getApplicationPath());
149 addAction("REST server",
150 () -> restServer.start(),
151 () -> restServer.stop());
153 addAction("set alive", () -> setAlive(true), () -> setAlive(false));
155 addAction("Initial Registration with PAP",
157 register.pdpRegistration(message.formatInitialStatusMessage(PdpState.PASSIVE));
160 register.pdpRegistration(message.formatInitialStatusMessage(PdpState.TERMINATED));
168 * Get the parameters used by the activator.
170 * @return the parameters of the activator
172 public XacmlPdpParameterGroup getParameterGroup() {
173 return xacmlPdpParameterGroup;
177 * Method to register the parameters to Common Parameter Service.
179 * @param xacmlPdpParameterGroup the xacml pdp parameter group
181 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
182 ParameterService.register(xacmlPdpParameterGroup);
186 * Method to deregister the parameters from Common Parameter Service.
188 * @param xacmlPdpParameterGroup the xacml pdp parameter group
190 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
191 ParameterService.deregister(xacmlPdpParameterGroup.getName());
195 * Registers the dispatcher with the topic source(s).
197 private void registerMsgDispatcher() {
198 for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
199 source.register(msgDispatcher);
204 * Unregisters the dispatcher from the topic source(s).
206 private void unregisterMsgDispatcher() {
207 for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
208 source.unregister(msgDispatcher);
213 * Returns the alive status of xacml pdp service.
218 public boolean isAlive() {
223 * Change the alive status of xacml pdp service.
225 * @param status the status
227 private void setAlive(final boolean status) {
231 public static XacmlPdpActivator getCurrent() {
235 public static void setCurrent(XacmlPdpActivator current) {
236 XacmlPdpActivator.current = current;