Wait for pdp-pap topic in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / XacmlPdpActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021 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 lombok.Getter;
24 import lombok.Setter;
25 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
26 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClient;
27 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
28 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
29 import org.onap.policy.common.endpoints.http.client.HttpClient;
30 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
31 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
32 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
33 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.common.utils.services.ServiceManagerContainer;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
37 import org.onap.policy.models.pdp.enums.PdpMessageType;
38 import org.onap.policy.pdpx.main.PolicyXacmlPdpRuntimeException;
39 import org.onap.policy.pdpx.main.XacmlState;
40 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
41 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpStateChangeListener;
42 import org.onap.policy.pdpx.main.comm.listeners.XacmlPdpUpdateListener;
43 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpAafFilter;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpRestController;
47 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * This class wraps a distributor so that it can be activated as a complete service together with
53  * all its xacml pdp and forwarding handlers.
54  */
55 public class XacmlPdpActivator extends ServiceManagerContainer {
56
57     // The logger for this class
58     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
59
60     private static final String[] MSG_TYPE_NAMES = {"messageName"};
61     private static final String TOPIC = "POLICY-PDP-PAP";
62
63     @Getter
64     @Setter
65     private static XacmlPdpActivator current = null;
66     private final XacmlPdpRestServer restServer;
67
68     // The parameters of this policy xacml pdp activator
69     private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
70
71     /**
72      * POLICY-PDP-PAP client.
73      */
74     private BidirectionalTopicClient topicClient;
75
76     /**
77      * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
78      * dispatches them to appropriate listener.
79      */
80     private final MessageTypeDispatcher msgDispatcher;
81
82     /**
83      * Instantiate the activator for policy xacml pdp as a complete service.
84      *
85      * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
86      */
87     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
88         LOGGER.info("Activator initializing using {}", xacmlPdpParameterGroup);
89
90         RestClientParameters apiClientParams = xacmlPdpParameterGroup.getPolicyApiParameters();
91
92         TopicEndpointManager.getManager().addTopics(xacmlPdpParameterGroup.getTopicParameterGroup());
93
94         final XacmlPdpHearbeatPublisher heartbeat;
95         final TopicSinkClient sinkClient;
96         final XacmlState state;
97
98         try {
99             HttpClient apiClient = HttpClientFactoryInstance.getClientFactory().build(apiClientParams);
100
101             var appmgr = new XacmlPdpApplicationManager(xacmlPdpParameterGroup.getApplicationParameters(),
102                                             apiClient);
103             XacmlPdpApplicationManager.setCurrent(appmgr);
104
105             var stats = new XacmlPdpStatisticsManager();
106             XacmlPdpStatisticsManager.setCurrent(stats);
107             stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
108             stats.setTotalPolicyCount(appmgr.getPolicyCount());
109
110             state = new XacmlState(appmgr, xacmlPdpParameterGroup.getPdpGroup(), xacmlPdpParameterGroup.getPdpType());
111
112             this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
113             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
114
115             topicClient = new BidirectionalTopicClient(TOPIC, TOPIC);
116             sinkClient = new TopicSinkClient(topicClient.getSink());
117
118             heartbeat = new XacmlPdpHearbeatPublisher(topicClient,
119                             xacmlPdpParameterGroup.getProbeHeartbeatTopicSec() * 1000, state);
120
121             /*
122              * since the dispatcher isn't registered with the topic yet, we can go ahead
123              * and register the listeners with it.
124              */
125             msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
126                             new XacmlPdpStateChangeListener(sinkClient, state));
127             msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
128                             new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
129
130             restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters(),
131                     XacmlPdpAafFilter.class, XacmlPdpRestController.class);
132
133         } catch (RuntimeException | HttpClientConfigException | BidirectionalTopicClientException e) {
134             throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
135         }
136
137         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
138
139         // @formatter:off
140         addAction("XACML PDP parameters",
141             () -> ParameterService.register(xacmlPdpParameterGroup),
142             () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
143
144         addAction("Message Dispatcher",
145             this::registerMsgDispatcher,
146             this::unregisterMsgDispatcher);
147
148         addAction("topics",
149             TopicEndpointManager.getManager()::start,
150             TopicEndpointManager.getManager()::shutdown);
151
152         addAction("Terminate PDP",
153             () -> { },
154             () -> sendTerminateMessage(sinkClient, state));
155
156         // initial heart beats act as registration messages
157         addAction("Heartbeat Publisher",
158             heartbeat::start,
159             heartbeat::terminate);
160
161         addAction("REST Server",
162             restServer::start,
163             restServer::stop);
164
165         // @formatter:on
166     }
167
168     /*
169      * Method used to send a terminate message to the PAP.
170      */
171     private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
172         PdpStatus terminateStatus = state.terminatePdpMessage();
173         sinkClient.send(terminateStatus);
174     }
175
176     /**
177      * Get the parameters used by the activator.
178      *
179      * @return the parameters of the activator
180      */
181     public XacmlPdpParameterGroup getParameterGroup() {
182         return xacmlPdpParameterGroup;
183     }
184
185     /**
186      * Method to register the parameters to Common Parameter Service.
187      *
188      * @param xacmlPdpParameterGroup the xacml pdp parameter group
189      */
190     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
191         ParameterService.register(xacmlPdpParameterGroup);
192     }
193
194     /**
195      * Method to deregister the parameters from Common Parameter Service.
196      *
197      * @param xacmlPdpParameterGroup the xacml pdp parameter group
198      */
199     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
200         ParameterService.deregister(xacmlPdpParameterGroup.getName());
201     }
202
203     /**
204      * Registers the dispatcher with the topic source(s).
205      */
206     private void registerMsgDispatcher() {
207         topicClient.getSource().register(msgDispatcher);
208     }
209
210     /**
211      * Unregisters the dispatcher from the topic source(s).
212      */
213     private void unregisterMsgDispatcher() {
214         topicClient.getSource().unregister(msgDispatcher);
215     }
216
217     /**
218      * Start the xacmlpdp rest controller.
219      */
220     public void startXacmlRestController() {
221         if (isXacmlRestControllerAlive()) {
222             LOGGER.info("Xacml rest controller already running");
223         } else {
224             restServer.start();
225         }
226     }
227
228     /**
229      * Stop the xacmlpdp rest controller.
230      */
231     public void stopXacmlRestController() {
232         if (isXacmlRestControllerAlive()) {
233             restServer.stop();
234         } else {
235             LOGGER.info("Xacml rest controller already stopped");
236         }
237     }
238
239     public boolean isXacmlRestControllerAlive() {
240         return restServer.isAlive();
241     }
242 }