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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
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;
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;
37 * Class to manage life cycle of xacml pdp rest server.
40 public class XacmlPdpRestServer implements Startable {
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);
46 private List<HttpServletServer> servers = new ArrayList<>();
48 private RestServerParameters restServerParameters;
50 private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
53 * Constructor for instantiating XacmlPdpRestServer.
55 * @param restServerParameters the rest server parameters
57 public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
58 this.restServerParameters = restServerParameters;
65 public boolean start() {
68 // Look for existing policy types loaded into the system
70 locateExistingPolicyTypes();
72 // Get the server properties
74 servers = HttpServletServer.factory.build(getServerProperties());
76 // Start all the servers
78 for (final HttpServletServer server : servers) {
80 server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
84 } catch (final Exception exp) {
85 LOGGER.error("Failed to start xacml pdp http server", exp);
92 * Creates the server properties object using restServerParameters.
94 * @return the properties object
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()));
118 private void locateExistingPolicyTypes() {
122 applicationLoader = ServiceLoader.load(XacmlApplicationServiceProvider.class);
124 // Iterate through them
126 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
127 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
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());
137 LOGGER.debug("{}", strDump);
139 // Update statistics manager
141 XacmlPdpStatisticsManager.setTotalPolicyTypesCount(types);
148 public boolean stop() {
149 for (final HttpServletServer server : servers) {
152 } catch (final Exception exp) {
153 LOGGER.error("Failed to stop xacml pdp http server", exp);
163 public void shutdown() {
171 public boolean isAlive() {
172 return !servers.isEmpty();
179 public String toString() {
180 final StringBuilder builder = new StringBuilder();
181 builder.append("XacmlPdpRestServer [servers=");
182 builder.append(servers);
184 return builder.toString();