2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.apex.services.onappf.rest;
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.endpoints.http.server.HttpServletServerFactoryInstance;
31 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
32 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
33 import org.onap.policy.common.gson.GsonMessageBodyHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Class to manage life cycle of services-onappf rest server.
40 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
42 public class ApexStarterRestServer implements Startable {
43 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class);
45 private List<HttpServletServer> servers = new ArrayList<>();
47 private RestServerParameters restServerParameters;
50 * Constructor for instantiating ApexStarterRestServer.
52 * @param restServerParameters the rest server parameters
54 public ApexStarterRestServer(final RestServerParameters restServerParameters) {
55 this.restServerParameters = restServerParameters;
62 public boolean start() {
64 servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
65 for (final HttpServletServer server : servers) {
67 server.addFilterClass(null, ApexStarterAafFilter.class.getName());
71 } catch (final Exception exp) {
72 LOGGER.error("Failed to start services-onappf http server", exp);
79 * Creates the server properties object using restServerParameters.
81 * @return the properties object
83 private Properties getServerProperties() {
84 final Properties props = new Properties();
85 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
88 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
90 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
91 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
92 Integer.toString(restServerParameters.getPort()));
93 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
94 HealthCheckRestControllerV1.class.getName());
95 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
96 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
97 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
98 restServerParameters.getUserName());
99 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
100 restServerParameters.getPassword());
101 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
102 String.valueOf(restServerParameters.isHttps()));
103 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
104 String.valueOf(restServerParameters.isAaf()));
105 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
106 GsonMessageBodyHandler.class.getName());
114 public boolean stop() {
115 for (final HttpServletServer server : servers) {
118 } catch (final Exception exp) {
119 LOGGER.error("Failed to stop services-onappf http server", exp);
129 public void shutdown() {
137 public boolean isAlive() {
138 return !servers.isEmpty();
145 public String toString() {
146 final StringBuilder builder = new StringBuilder();
147 builder.append("ApexStarterRestServer [servers=");
148 builder.append(servers);
150 return builder.toString();