Revert "Leave xacml-pdp REST server always running"
[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         // initial heart beats act as registration messages
156         addAction("Heartbeat Publisher",
157             heartbeat::start,
158             heartbeat::terminate);
159
160         // @formatter:on
161     }
162
163     /*
164      * Method used to send a terminate message to the PAP.
165      */
166     private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
167         PdpStatus terminateStatus = state.terminatePdpMessage();
168         sinkClient.send(terminateStatus);
169     }
170
171     /**
172      * Get the parameters used by the activator.
173      *
174      * @return the parameters of the activator
175      */
176     public XacmlPdpParameterGroup getParameterGroup() {
177         return xacmlPdpParameterGroup;
178     }
179
180     /**
181      * Method to register the parameters to Common Parameter Service.
182      *
183      * @param xacmlPdpParameterGroup the xacml pdp parameter group
184      */
185     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
186         ParameterService.register(xacmlPdpParameterGroup);
187     }
188
189     /**
190      * Method to deregister the parameters from Common Parameter Service.
191      *
192      * @param xacmlPdpParameterGroup the xacml pdp parameter group
193      */
194     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
195         ParameterService.deregister(xacmlPdpParameterGroup.getName());
196     }
197
198     /**
199      * Registers the dispatcher with the topic source(s).
200      */
201     private void registerMsgDispatcher() {
202         topicClient.getSource().register(msgDispatcher);
203     }
204
205     /**
206      * Unregisters the dispatcher from the topic source(s).
207      */
208     private void unregisterMsgDispatcher() {
209         topicClient.getSource().unregister(msgDispatcher);
210     }
211
212     /**
213      * Start the xacmlpdp rest controller.
214      */
215     public void startXacmlRestController() {
216         if (isXacmlRestControllerAlive()) {
217             LOGGER.info("Xacml rest controller already running");
218         } else {
219             restServer.start();
220         }
221     }
222
223     /**
224      * Stop the xacmlpdp rest controller.
225      */
226     public void stopXacmlRestController() {
227         if (isXacmlRestControllerAlive()) {
228             restServer.stop();
229         } else {
230             LOGGER.info("Xacml rest controller already stopped");
231         }
232     }
233
234     public boolean isXacmlRestControllerAlive() {
235         return restServer.isAlive();
236     }
237 }