Modify xacml-pdp to use RestServer from common
[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 java.util.Properties;
26 import lombok.Getter;
27 import lombok.Setter;
28 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
29 import org.onap.policy.common.endpoints.event.comm.TopicSource;
30 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
31 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
32 import org.onap.policy.common.endpoints.http.server.RestServer;
33 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
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
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      * @param topicProperties properties used to configure the topics
81      */
82     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
83         LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
84
85         TopicEndpointManager.getManager().addTopicSinks(topicProperties);
86         TopicEndpointManager.getManager().addTopicSources(topicProperties);
87
88         final XacmlPdpHearbeatPublisher heartbeat;
89         final TopicSinkClient sinkClient;
90         final XacmlState state;
91         final RestServer restServer;
92
93         try {
94             XacmlPdpApplicationManager appmgr =
95                             new XacmlPdpApplicationManager(Paths.get(xacmlPdpParameterGroup.getApplicationPath()));
96             XacmlPdpApplicationManager.setCurrent(appmgr);
97
98             XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
99             XacmlPdpStatisticsManager.setCurrent(stats);
100             stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
101             stats.setTotalPolicyCount(appmgr.getPolicyCount());
102
103             state = new XacmlState(appmgr);
104
105             this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
106             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
107
108             sinkClient = new TopicSinkClient(TOPIC);
109             heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
110
111             /*
112              * since the dispatcher isn't registered with the topic yet, we can go ahead
113              * and register the listeners with it.
114              */
115             msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
116                             new XacmlPdpStateChangeListener(sinkClient, state));
117             msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
118                             new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
119
120             restServer = new RestServer(xacmlPdpParameterGroup.getRestServerParameters(), XacmlPdpAafFilter.class,
121                                 XacmlPdpRestController.class);
122
123         } catch (RuntimeException | TopicSinkClientException e) {
124             throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
125         }
126
127         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
128
129         // @formatter:off
130         addAction("XACML PDP parameters",
131             () -> ParameterService.register(xacmlPdpParameterGroup),
132             () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
133
134         addAction("Message Dispatcher",
135             this::registerMsgDispatcher,
136             this::unregisterMsgDispatcher);
137
138         addAction("topics",
139             TopicEndpointManager.getManager()::start,
140             TopicEndpointManager.getManager()::shutdown);
141
142         addAction("Terminate PDP",
143             () -> { },
144             () -> sendTerminateMessage(sinkClient, state));
145         // initial heart beats act as registration messages
146         addAction("Heartbeat Publisher",
147             heartbeat::start,
148             heartbeat::terminate);
149
150         addAction("REST server",
151             restServer::start,
152             restServer::stop);
153
154         // @formatter:on
155     }
156
157     /*
158      * Method used to send a terminate message to the PAP.
159      */
160     private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
161         PdpStatus terminateStatus = state.terminatePdpMessage();
162         sinkClient.send(terminateStatus);
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 : TopicEndpointManager.getManager().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 : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
206             source.unregister(msgDispatcher);
207         }
208     }
209 }