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;
25 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
26 import org.onap.policy.common.endpoints.event.comm.TopicSource;
27 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
28 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
29 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
30 import org.onap.policy.common.parameters.ParameterService;
31 import org.onap.policy.common.utils.services.ServiceManagerContainer;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.concepts.PdpUpdate;
34 import org.onap.policy.models.pdp.enums.PdpMessageType;
35 import org.onap.policy.models.pdp.enums.PdpState;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
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.parameters.XacmlPdpParameterGroup;
42 import org.onap.policy.pdpx.main.rest.XacmlPdpRestServer;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * This class wraps a distributor so that it can be activated as a complete service together with
48 * all its xacml pdp and forwarding handlers.
50 public class XacmlPdpActivator extends ServiceManagerContainer {
52 // The logger for this class
53 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
55 private static final String[] MSG_TYPE_NAMES = {"messageName"};
56 private static final String TOPIC = "POLICY-PDP-PAP";
58 // The parameters of this policy xacml pdp activator
59 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
62 * The XACML PDP REST API server.
64 private XacmlPdpRestServer restServer;
67 * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
68 * dispatches them to {@link #pdpUpdateListener}.
70 private final MessageTypeDispatcher msgDispatcher;
73 * Listens for {@link PdpUpdate} messages and then routes them to the listener associated with the
74 * ID of the originating request.
76 private final XacmlPdpStateChangeListener pdpStateChangeListener;
79 * The current activator.
81 private static XacmlPdpActivator current = null;
83 private volatile boolean alive = false;
85 private XacmlPdpPapRegistration register;
87 private XacmlPdpMessage message;
90 * Instantiate the activator for policy xacml pdp as a complete service.
92 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
93 * @param topicProperties properties used to configure the topics
95 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
96 TopicEndpoint.manager.addTopicSinks(topicProperties);
97 TopicEndpoint.manager.addTopicSources(topicProperties);
100 TopicSinkClient sinkClient = new TopicSinkClient(TOPIC);
101 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
102 this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
103 this.pdpStateChangeListener = new XacmlPdpStateChangeListener(sinkClient);
104 this.register = new XacmlPdpPapRegistration(sinkClient);
105 this.message = new XacmlPdpMessage();
106 } catch (RuntimeException | TopicSinkClientException e) {
107 throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
110 xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
113 addAction("XACML PDP parameters", () -> ParameterService.register(xacmlPdpParameterGroup),
114 () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
116 addAction("Request ID Dispatcher",
117 () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), this.pdpStateChangeListener),
118 () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()));
120 addAction("Message Dispatcher",
121 () -> registerMsgDispatcher(),
122 () -> unregisterMsgDispatcher());
125 () -> TopicEndpoint.manager.start(),
126 () -> TopicEndpoint.manager.shutdown());
128 addAction("Create REST server",
130 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
131 xacmlPdpParameterGroup.getApplicationPath());
137 addAction("REST server",
138 () -> restServer.start(),
139 () -> restServer.stop());
141 addAction("set alive", () -> setAlive(true), () -> setAlive(false));
143 addAction("Initial Registration with PAP",
145 register.pdpRegistration(message.formatStatusMessage(PdpState.PASSIVE));
148 register.pdpRegistration(message.formatStatusMessage(PdpState.TERMINATED));
156 * Get the parameters used by the activator.
158 * @return the parameters of the activator
160 public XacmlPdpParameterGroup getParameterGroup() {
161 return xacmlPdpParameterGroup;
165 * Method to register the parameters to Common Parameter Service.
167 * @param xacmlPdpParameterGroup the xacml pdp parameter group
169 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
170 ParameterService.register(xacmlPdpParameterGroup);
174 * Method to deregister the parameters from Common Parameter Service.
176 * @param xacmlPdpParameterGroup the xacml pdp parameter group
178 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
179 ParameterService.deregister(xacmlPdpParameterGroup.getName());
183 * Registers the dispatcher with the topic source(s).
185 private void registerMsgDispatcher() {
186 for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
187 source.register(msgDispatcher);
192 * Unregisters the dispatcher from the topic source(s).
194 private void unregisterMsgDispatcher() {
195 for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
196 source.unregister(msgDispatcher);
201 * Returns the alive status of xacml pdp service.
206 public boolean isAlive() {
211 * Change the alive status of xacml pdp service.
213 * @param status the status
215 private void setAlive(final boolean status) {
219 public static XacmlPdpActivator getCurrent() {
223 public static void setCurrent(XacmlPdpActivator current) {
224 XacmlPdpActivator.current = current;