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.nio.file.Paths;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
28 import org.onap.policy.common.capabilities.Startable;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
30 import org.onap.policy.common.gson.GsonMessageBodyHandler;
31 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Class to manage life cycle of xacml pdp rest server.
39 public class XacmlPdpRestServer implements Startable {
41 private static final String SEPARATOR = ".";
42 private static final String HTTP_SERVER_SERVICES = "http.server.services";
43 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
45 private List<HttpServletServer> servers = new ArrayList<>();
47 private RestServerParameters restServerParameters;
48 private String applicationPath;
51 * Constructor for instantiating XacmlPdpRestServer.
53 * @param restServerParameters the rest server parameters
55 public XacmlPdpRestServer(final RestServerParameters restServerParameters, final String applicationPath) {
56 this.restServerParameters = restServerParameters;
57 this.applicationPath = applicationPath;
64 public boolean start() {
66 LOGGER.info("Starting XacmlPdpRestServer...");
68 // Initialize the applications - SEND PROPERTIES
70 XacmlPdpApplicationManager.initializeApplications(Paths.get(applicationPath));
73 // Update statistics manager on the policy types
75 XacmlPdpStatisticsManager.setTotalPolicyTypesCount(XacmlPdpApplicationManager.getPolicyTypeCount());
78 // Get the server properties
80 servers = HttpServletServer.factory.build(getServerProperties());
82 // Start all the servers
84 for (final HttpServletServer server : servers) {
86 server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
90 LOGGER.info("servers are started");
91 } catch (final Exception exp) {
92 LOGGER.error("Failed to start xacml pdp http server", exp);
99 * Creates the server properties object using restServerParameters.
101 * @return the properties object
103 private Properties getServerProperties() {
104 final Properties props = new Properties();
105 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
106 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
107 restServerParameters.getHost());
108 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
109 Integer.toString(restServerParameters.getPort()));
110 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
111 XacmlPdpRestController.class.getCanonicalName());
112 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
113 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
114 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
115 restServerParameters.getUserName());
116 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
117 restServerParameters.getPassword());
118 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
119 String.valueOf(restServerParameters.isHttps()));
120 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
121 String.valueOf(restServerParameters.isAaf()));
122 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
123 GsonMessageBodyHandler.class.getName());
131 public boolean stop() {
132 for (final HttpServletServer server : servers) {
135 } catch (final Exception exp) {
136 LOGGER.error("Failed to stop xacml pdp http server", exp);
146 public void shutdown() {
154 public boolean isAlive() {
155 return !servers.isEmpty();
162 public String toString() {
163 final StringBuilder builder = new StringBuilder();
164 builder.append("XacmlPdpRestServer [servers=");
165 builder.append(servers);
167 return builder.toString();