Use new addTopic() method 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 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
66     // The parameters of this policy xacml pdp activator
67     private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
68
69     /**
70      * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
71      * dispatches them to appropriate listener.
72      */
73     private final MessageTypeDispatcher msgDispatcher;
74
75     /**
76      * Instantiate the activator for policy xacml pdp as a complete service.
77      *
78      * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
79      */
80     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
81         LOGGER.info("Activator initializing using {}", xacmlPdpParameterGroup);
82
83         TopicEndpointManager.getManager().addTopics(xacmlPdpParameterGroup.getTopicParameterGroup());
84
85         final XacmlPdpHearbeatPublisher heartbeat;
86         final TopicSinkClient sinkClient;
87         final XacmlState state;
88         final RestServer restServer;
89
90         try {
91             XacmlPdpApplicationManager appmgr =
92                             new XacmlPdpApplicationManager(Paths.get(xacmlPdpParameterGroup.getApplicationPath()));
93             XacmlPdpApplicationManager.setCurrent(appmgr);
94
95             XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
96             XacmlPdpStatisticsManager.setCurrent(stats);
97             stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
98             stats.setTotalPolicyCount(appmgr.getPolicyCount());
99
100             state = new XacmlState(appmgr);
101
102             this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
103             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
104
105             sinkClient = new TopicSinkClient(TOPIC);
106             heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
107
108             /*
109              * since the dispatcher isn't registered with the topic yet, we can go ahead
110              * and register the listeners with it.
111              */
112             msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
113                             new XacmlPdpStateChangeListener(sinkClient, state));
114             msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
115                             new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
116
117             restServer = new RestServer(xacmlPdpParameterGroup.getRestServerParameters(), XacmlPdpAafFilter.class,
118                                 XacmlPdpRestController.class);
119
120         } catch (RuntimeException | TopicSinkClientException e) {
121             throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
122         }
123
124         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
125
126         // @formatter:off
127         addAction("XACML PDP parameters",
128             () -> ParameterService.register(xacmlPdpParameterGroup),
129             () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
130
131         addAction("Message Dispatcher",
132             this::registerMsgDispatcher,
133             this::unregisterMsgDispatcher);
134
135         addAction("topics",
136             TopicEndpointManager.getManager()::start,
137             TopicEndpointManager.getManager()::shutdown);
138
139         addAction("Terminate PDP",
140             () -> { },
141             () -> sendTerminateMessage(sinkClient, state));
142         // initial heart beats act as registration messages
143         addAction("Heartbeat Publisher",
144             heartbeat::start,
145             heartbeat::terminate);
146
147         addAction("REST server",
148             restServer::start,
149             restServer::stop);
150
151         // @formatter:on
152     }
153
154     /*
155      * Method used to send a terminate message to the PAP.
156      */
157     private void sendTerminateMessage(TopicSinkClient sinkClient, XacmlState state) {
158         PdpStatus terminateStatus = state.terminatePdpMessage();
159         sinkClient.send(terminateStatus);
160     }
161
162     /**
163      * Get the parameters used by the activator.
164      *
165      * @return the parameters of the activator
166      */
167     public XacmlPdpParameterGroup getParameterGroup() {
168         return xacmlPdpParameterGroup;
169     }
170
171     /**
172      * Method to register the parameters to Common Parameter Service.
173      *
174      * @param xacmlPdpParameterGroup the xacml pdp parameter group
175      */
176     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
177         ParameterService.register(xacmlPdpParameterGroup);
178     }
179
180     /**
181      * Method to deregister the parameters from Common Parameter Service.
182      *
183      * @param xacmlPdpParameterGroup the xacml pdp parameter group
184      */
185     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
186         ParameterService.deregister(xacmlPdpParameterGroup.getName());
187     }
188
189     /**
190      * Registers the dispatcher with the topic source(s).
191      */
192     private void registerMsgDispatcher() {
193         for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
194             source.register(msgDispatcher);
195         }
196     }
197
198     /**
199      * Unregisters the dispatcher from the topic source(s).
200      */
201     private void unregisterMsgDispatcher() {
202         for (TopicSource source : TopicEndpointManager.getManager().getTopicSources(Arrays.asList(TOPIC))) {
203             source.unregister(msgDispatcher);
204         }
205     }
206 }