Xacml PDP Register/Unregister Changes
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / XacmlPdpActivator.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.startstop;
22
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;
45
46 /**
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.
49  */
50 public class XacmlPdpActivator extends ServiceManagerContainer {
51
52     // The logger for this class
53     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
54
55     private static final String[] MSG_TYPE_NAMES = {"messageName"};
56     private static final String TOPIC = "POLICY-PDP-PAP";
57
58     // The parameters of this policy xacml pdp activator
59     private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
60
61     /**
62      * The XACML PDP REST API server.
63      */
64     private XacmlPdpRestServer restServer;
65
66     /**
67      * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
68      * dispatches them to {@link #pdpUpdateListener}.
69      */
70     private final MessageTypeDispatcher msgDispatcher;
71
72     /**
73      * Listens for {@link PdpUpdate} messages and then routes them to the listener associated with the
74      * ID of the originating request.
75      */
76     private final XacmlPdpStateChangeListener pdpStateChangeListener;
77
78     /**
79      * The current activator.
80      */
81     private static XacmlPdpActivator current = null;
82
83     private volatile boolean alive = false;
84
85     private XacmlPdpPapRegistration register;
86
87     private XacmlPdpMessage message;
88
89     /**
90      * Instantiate the activator for policy xacml pdp as a complete service.
91      *
92      * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
93      * @param topicProperties properties used to configure the topics
94      */
95     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
96         TopicEndpoint.manager.addTopicSinks(topicProperties);
97         TopicEndpoint.manager.addTopicSources(topicProperties);
98
99         try {
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);
108         }
109
110         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
111
112         // @formatter:off
113         addAction("XACML PDP parameters", () -> ParameterService.register(xacmlPdpParameterGroup),
114             () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
115
116         addAction("Request ID Dispatcher",
117             () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), this.pdpStateChangeListener),
118             () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name()));
119
120         addAction("Message Dispatcher",
121             () -> registerMsgDispatcher(),
122             () -> unregisterMsgDispatcher());
123
124         addAction("topics",
125             () -> TopicEndpoint.manager.start(),
126             () -> TopicEndpoint.manager.shutdown());
127
128         addAction("Create REST server",
129             () -> {
130                 restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
131                         xacmlPdpParameterGroup.getApplicationPath());
132             },
133             () -> {
134                 restServer = null;
135             });
136
137         addAction("REST server",
138             () -> restServer.start(),
139             () -> restServer.stop());
140
141         addAction("set alive", () -> setAlive(true), () -> setAlive(false));
142
143         addAction("Initial Registration with PAP",
144             () -> {
145                 register.pdpRegistration(message.formatStatusMessage(PdpState.PASSIVE));
146             },
147             () -> {
148                 register.pdpRegistration(message.formatStatusMessage(PdpState.TERMINATED));
149             });
150         // @formatter:on
151
152         current = this;
153     }
154
155     /**
156      * Get the parameters used by the activator.
157      *
158      * @return the parameters of the activator
159      */
160     public XacmlPdpParameterGroup getParameterGroup() {
161         return xacmlPdpParameterGroup;
162     }
163
164     /**
165      * Method to register the parameters to Common Parameter Service.
166      *
167      * @param xacmlPdpParameterGroup the xacml pdp parameter group
168      */
169     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
170         ParameterService.register(xacmlPdpParameterGroup);
171     }
172
173     /**
174      * Method to deregister the parameters from Common Parameter Service.
175      *
176      * @param xacmlPdpParameterGroup the xacml pdp parameter group
177      */
178     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
179         ParameterService.deregister(xacmlPdpParameterGroup.getName());
180     }
181
182     /**
183      * Registers the dispatcher with the topic source(s).
184      */
185     private void registerMsgDispatcher() {
186         for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
187             source.register(msgDispatcher);
188         }
189     }
190
191     /**
192      * Unregisters the dispatcher from the topic source(s).
193      */
194     private void unregisterMsgDispatcher() {
195         for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
196             source.unregister(msgDispatcher);
197         }
198     }
199
200     /**
201      * Returns the alive status of xacml pdp service.
202      *
203      * @return the alive
204      */
205     @Override
206     public boolean isAlive() {
207         return alive;
208     }
209
210     /**
211      * Change the alive status of xacml pdp service.
212      *
213      * @param status the status
214      */
215     private void setAlive(final boolean status) {
216         alive = status;
217     }
218
219     public static XacmlPdpActivator getCurrent() {
220         return current;
221     }
222
223     public static void setCurrent(XacmlPdpActivator current) {
224         XacmlPdpActivator.current = current;
225     }
226 }