2a881d45994eaa7cdeb27e94ac040b31d23178af
[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.TopicEndpoint;
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.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.XacmlPdpApplicationManager;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpRestServer;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class wraps a distributor so that it can be activated as a complete service together with
51  * all its xacml pdp and forwarding handlers.
52  */
53 public class XacmlPdpActivator extends ServiceManagerContainer {
54
55     // The logger for this class
56     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
57
58     private static final String[] MSG_TYPE_NAMES = {"messageName"};
59     private static final String TOPIC = "POLICY-PDP-PAP";
60
61     @Getter
62     @Setter
63     private static XacmlPdpActivator current = null;
64
65     // The parameters of this policy xacml pdp activator
66     private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
67
68     /**
69      * The XACML PDP REST API server.
70      */
71     private XacmlPdpRestServer restServer;
72
73     /**
74      * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then
75      * dispatches them to appropriate listener.
76      */
77     private final MessageTypeDispatcher msgDispatcher;
78
79     /**
80      * Instantiate the activator for policy xacml pdp as a complete service.
81      *
82      * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
83      * @param topicProperties properties used to configure the topics
84      */
85     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
86         LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
87
88         TopicEndpoint.manager.addTopicSinks(topicProperties);
89         TopicEndpoint.manager.addTopicSources(topicProperties);
90
91         XacmlPdpHearbeatPublisher heartbeat;
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
102             XacmlState state = new XacmlState(appmgr);
103
104             this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
105             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
106
107             TopicSinkClient sinkClient = new TopicSinkClient(TOPIC);
108             heartbeat = new XacmlPdpHearbeatPublisher(sinkClient, state);
109
110             /*
111              * since the dispatcher isn't registered with the topic yet, we can go ahead
112              * and register the listeners with it.
113              */
114             msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(),
115                             new XacmlPdpStateChangeListener(sinkClient, state));
116             msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(),
117                             new XacmlPdpUpdateListener(sinkClient, state, heartbeat, appmgr));
118
119         } catch (RuntimeException | TopicSinkClientException e) {
120             throw new PolicyXacmlPdpRuntimeException(e.getMessage(), e);
121         }
122
123         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
124
125         // @formatter:off
126         addAction("XACML PDP parameters",
127             () -> ParameterService.register(xacmlPdpParameterGroup),
128             () -> ParameterService.deregister(xacmlPdpParameterGroup.getName()));
129
130         addAction("Message Dispatcher",
131             this::registerMsgDispatcher,
132             this::unregisterMsgDispatcher);
133
134         addAction("topics",
135             TopicEndpoint.manager::start,
136             TopicEndpoint.manager::shutdown);
137
138         // initial heart beats act as registration messages
139         addAction("Heartbeat Publisher",
140             heartbeat::start,
141             heartbeat::terminate);
142
143         addAction("Create REST server",
144             () -> restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters()),
145             () -> restServer = null);
146
147         addAction("REST server",
148             () -> restServer.start(),
149             () -> restServer.stop());
150         // @formatter:on
151     }
152
153     /**
154      * Get the parameters used by the activator.
155      *
156      * @return the parameters of the activator
157      */
158     public XacmlPdpParameterGroup getParameterGroup() {
159         return xacmlPdpParameterGroup;
160     }
161
162     /**
163      * Method to register the parameters to Common Parameter Service.
164      *
165      * @param xacmlPdpParameterGroup the xacml pdp parameter group
166      */
167     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
168         ParameterService.register(xacmlPdpParameterGroup);
169     }
170
171     /**
172      * Method to deregister the parameters from Common Parameter Service.
173      *
174      * @param xacmlPdpParameterGroup the xacml pdp parameter group
175      */
176     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
177         ParameterService.deregister(xacmlPdpParameterGroup.getName());
178     }
179
180     /**
181      * Registers the dispatcher with the topic source(s).
182      */
183     private void registerMsgDispatcher() {
184         for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
185             source.register(msgDispatcher);
186         }
187     }
188
189     /**
190      * Unregisters the dispatcher from the topic source(s).
191      */
192     private void unregisterMsgDispatcher() {
193         for (TopicSource source : TopicEndpoint.manager.getTopicSources(Arrays.asList(TOPIC))) {
194             source.unregister(msgDispatcher);
195         }
196     }
197 }