2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
30 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
31 import org.onap.policy.common.gson.GsonMessageBodyHandler;
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 final RestServerParameters restServerParameters;
50 * Constructor for instantiating XacmlPdpRestServer.
52 * @param restServerParameters the rest server parameters
54 public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
55 this.restServerParameters = restServerParameters;
62 public boolean start() {
64 LOGGER.info("Starting XacmlPdpRestServer...");
67 // Get the server properties
69 servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
71 // Start all the servers
73 for (final HttpServletServer server : servers) {
75 server.addFilterClass(null, XacmlPdpAafFilter.class.getName());
79 LOGGER.info("servers are started");
80 } catch (final Exception exp) {
81 LOGGER.error("Failed to start xacml pdp http server", exp);
88 * Creates the server properties object using restServerParameters.
90 * @return the properties object
92 private Properties getServerProperties() {
93 final Properties props = new Properties();
94 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
95 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
96 restServerParameters.getHost());
97 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
98 Integer.toString(restServerParameters.getPort()));
99 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
100 XacmlPdpRestController.class.getName());
101 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
102 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
103 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
104 restServerParameters.getUserName());
105 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
106 restServerParameters.getPassword());
107 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
108 String.valueOf(restServerParameters.isHttps()));
109 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
110 String.valueOf(restServerParameters.isAaf()));
111 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
112 GsonMessageBodyHandler.class.getName());
120 public boolean stop() {
121 for (final HttpServletServer server : servers) {
124 } catch (final Exception exp) {
125 LOGGER.error("Failed to stop xacml pdp http server", exp);
135 public void shutdown() {
143 public boolean isAlive() {
144 return !servers.isEmpty();
151 public String toString() {
152 final StringBuilder builder = new StringBuilder();
153 builder.append("XacmlPdpRestServer [servers=");
154 builder.append(servers);
156 return builder.toString();