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() {
67 // Initialize the applications - SEND PROPERTIES
69 XacmlPdpApplicationManager.initializeApplications(Paths.get(applicationPath));
72 // Update statistics manager on the policy types
74 XacmlPdpStatisticsManager.setTotalPolicyTypesCount(XacmlPdpApplicationManager.getPolicyTypeCount());
77 // Get the server properties
79 servers = HttpServletServer.factory.build(getServerProperties());
81 // Start all the servers
83 for (final HttpServletServer server : servers) {
85 server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
89 } catch (final Exception exp) {
90 LOGGER.error("Failed to start xacml pdp http server", exp);
97 * Creates the server properties object using restServerParameters.
99 * @return the properties object
101 private Properties getServerProperties() {
102 final Properties props = new Properties();
103 props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
104 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
105 restServerParameters.getHost());
106 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
107 Integer.toString(restServerParameters.getPort()));
108 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
109 XacmlPdpRestController.class.getCanonicalName());
110 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
111 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
112 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
113 restServerParameters.getUserName());
114 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
115 restServerParameters.getPassword());
116 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
117 String.valueOf(restServerParameters.isHttps()));
118 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
119 String.valueOf(restServerParameters.isAaf()));
120 props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
121 GsonMessageBodyHandler.class.getName());
129 public boolean stop() {
130 for (final HttpServletServer server : servers) {
133 } catch (final Exception exp) {
134 LOGGER.error("Failed to stop xacml pdp http server", exp);
144 public void shutdown() {
152 public boolean isAlive() {
153 return !servers.isEmpty();
160 public String toString() {
161 final StringBuilder builder = new StringBuilder();
162 builder.append("XacmlPdpRestServer [servers=");
163 builder.append(servers);
165 return builder.toString();