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