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