f3b7a54e524c4afd325447fd1587ae238ea1e3f9
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpRestServer.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.rest;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.ServiceLoader;
28
29 import org.onap.policy.common.capabilities.Startable;
30 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
31 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
32 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Class to manage life cycle of xacml pdp rest server.
38  *
39  */
40 public class XacmlPdpRestServer implements Startable {
41
42     private static final String SEPARATOR = ".";
43     private static final String HTTP_SERVER_SERVICES = "http.server.services";
44     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
45
46     private List<HttpServletServer> servers = new ArrayList<>();
47
48     private RestServerParameters restServerParameters;
49
50     private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
51
52     /**
53      * Constructor for instantiating XacmlPdpRestServer.
54      *
55      * @param restServerParameters the rest server parameters
56      */
57     public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
58         this.restServerParameters = restServerParameters;
59     }
60
61     /**
62      * {@inheritDoc}.
63      */
64     @Override
65     public boolean start() {
66         try {
67             //
68             // Look for existing policy types loaded into the system
69             //
70             locateExistingPolicyTypes();
71             //
72             // Get the server properties
73             //
74             servers = HttpServletServer.factory.build(getServerProperties());
75             //
76             // Start all the servers
77             //
78             for (final HttpServletServer server : servers) {
79                 if (server.isAaf()) {
80                     server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
81                 }
82                 server.start();
83             }
84         } catch (final Exception exp) {
85             LOGGER.error("Failed to start xacml pdp http server", exp);
86             return false;
87         }
88         return true;
89     }
90
91     /**
92      * Creates the server properties object using restServerParameters.
93      *
94      * @return the properties object
95      */
96     private Properties getServerProperties() {
97         final Properties props = new Properties();
98         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
99         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
100                 restServerParameters.getHost());
101         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
102                 Integer.toString(restServerParameters.getPort()));
103         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
104                 XacmlPdpRestController.class.getCanonicalName());
105         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
106         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
107         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
108                 restServerParameters.getUserName());
109         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
110                 restServerParameters.getPassword());
111         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
112                 String.valueOf(restServerParameters.isHttps()));
113         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
114                 String.valueOf(restServerParameters.isAaf()));
115         return props;
116     }
117
118     private void locateExistingPolicyTypes() {
119         //
120         // Load service
121         //
122         applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
123         //
124         // Iterate through them
125         //
126         StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
127         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
128         long types = 0;
129         while (iterator.hasNext()) {
130             XacmlApplicationServiceProvider application = iterator.next();
131             strDump.append(application.applicationName());
132             strDump.append(" supports ");
133             strDump.append(application.supportedPolicyTypes());
134             types += application.supportedPolicyTypes().size();
135             strDump.append(System.lineSeparator());
136         }
137         LOGGER.debug("{}", strDump);
138         //
139         // Update statistics manager
140         //
141         XacmlPdpStatisticsManager.setTotalPolicyTypesCount(types);
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public boolean stop() {
149         for (final HttpServletServer server : servers) {
150             try {
151                 server.shutdown();
152             } catch (final Exception exp) {
153                 LOGGER.error("Failed to stop xacml pdp http server", exp);
154             }
155         }
156         return true;
157     }
158
159     /**
160      * {@inheritDoc}.
161      */
162     @Override
163     public void shutdown() {
164         stop();
165     }
166
167     /**
168      * {@inheritDoc}.
169      */
170     @Override
171     public boolean isAlive() {
172         return !servers.isEmpty();
173     }
174
175     /**
176      * {@inheritDoc}.
177      */
178     @Override
179     public String toString() {
180         final StringBuilder builder = new StringBuilder();
181         builder.append("XacmlPdpRestServer [servers=");
182         builder.append(servers);
183         builder.append("]");
184         return builder.toString();
185     }
186
187 }