2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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.apex.services.onappf.rest;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
27 import org.onap.policy.apex.services.onappf.parameters.RestServerParameters;
28 import org.onap.policy.common.capabilities.Startable;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
30 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
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 services-onappf rest server.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class ApexStarterRestServer implements Startable {
41 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class);
43 private List<HttpServletServer> servers = new ArrayList<>();
45 private RestServerParameters restServerParameters;
48 * Constructor for instantiating ApexStarterRestServer.
50 * @param restServerParameters the rest server parameters
52 public ApexStarterRestServer(final RestServerParameters restServerParameters) {
53 this.restServerParameters = restServerParameters;
60 public boolean start() {
62 servers = HttpServletServer.factory.build(getServerProperties());
63 for (final HttpServletServer server : servers) {
65 server.addFilterClass(null, ApexStarterAafFilter.class.getName());
69 } catch (final Exception exp) {
70 LOGGER.error("Failed to start services-onappf http server", exp);
77 * Creates the server properties object using restServerParameters.
79 * @return the properties object
81 private Properties getServerProperties() {
82 final Properties props = new Properties();
83 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
86 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
88 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
89 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
90 Integer.toString(restServerParameters.getPort()));
91 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
92 HealthCheckRestControllerV1.class.getName());
93 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
94 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
95 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
96 restServerParameters.getUserName());
97 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
98 restServerParameters.getPassword());
99 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
100 String.valueOf(restServerParameters.isHttps()));
101 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
102 String.valueOf(restServerParameters.isAaf()));
103 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
104 GsonMessageBodyHandler.class.getName());
112 public boolean stop() {
113 for (final HttpServletServer server : servers) {
116 } catch (final Exception exp) {
117 LOGGER.error("Failed to stop services-onappf http server", exp);
127 public void shutdown() {
135 public boolean isAlive() {
136 return !servers.isEmpty();
143 public String toString() {
144 final StringBuilder builder = new StringBuilder();
145 builder.append("ApexStarterRestServer [servers=");
146 builder.append(servers);
148 return builder.toString();