2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29 import org.onap.policy.api.main.parameters.RestServerParameters;
30 import org.onap.policy.api.main.rest.aaf.AafApiFilter;
31 import org.onap.policy.common.capabilities.Startable;
32 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
33 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
34 import org.onap.policy.common.gson.GsonMessageBodyHandler;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
40 * Class to manage life cycle of api rest server.
43 public class ApiRestServer implements Startable {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class);
47 private List<HttpServletServer> servers = new ArrayList<>();
49 private RestServerParameters restServerParameters;
52 * Constructor for instantiating ApiRestServer.
54 * @param restServerParameters the rest server parameters
56 public ApiRestServer(final RestServerParameters restServerParameters) {
57 this.restServerParameters = restServerParameters;
64 public boolean start() {
66 servers = HttpServletServer.factory.build(getServerProperties());
67 for (HttpServletServer server : servers) {
69 server.addFilterClass(null, AafApiFilter.class.getName());
73 } catch (final Exception exp) {
74 LOGGER.error("Failed to start api http server", exp);
81 * Creates the server properties object using restServerParameters.
83 * @return the properties object
85 private Properties getServerProperties() {
86 final Properties props = new Properties();
88 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
90 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
91 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
92 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
93 Integer.toString(restServerParameters.getPort()));
94 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
95 String.join(",", LegacyApiRestController.class.getName(),
96 ApiRestController.class.getName()));
97 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
98 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
99 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
100 restServerParameters.getUserName());
101 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
102 restServerParameters.getPassword());
103 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
104 String.valueOf(restServerParameters.isHttps()));
105 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
106 String.valueOf(restServerParameters.isAaf()));
107 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
108 GsonMessageBodyHandler.class.getName());
117 public boolean stop() {
118 for (final HttpServletServer server : servers) {
121 } catch (final Exception exp) {
122 LOGGER.error("Failed to stop api http server", exp);
132 public void shutdown() {
140 public boolean isAlive() {
141 return !servers.isEmpty();
148 public String toString() {
149 final StringBuilder builder = new StringBuilder();
150 builder.append("ApiRestServer [servers=");
151 builder.append(servers);
153 return builder.toString();