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;
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.endpoints.properties.PolicyEndPointProperties;
32 import org.onap.policy.common.gson.GsonMessageBodyHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class to manage life cycle of services-onappf rest server.
39 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
41 public class ApexStarterRestServer implements Startable {
42 private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class);
44 private List<HttpServletServer> servers = new ArrayList<>();
46 private RestServerParameters restServerParameters;
49 * Constructor for instantiating ApexStarterRestServer.
51 * @param restServerParameters the rest server parameters
53 public ApexStarterRestServer(final RestServerParameters restServerParameters) {
54 this.restServerParameters = restServerParameters;
61 public boolean start() {
63 servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
64 for (final HttpServletServer server : servers) {
66 server.addFilterClass(null, ApexStarterAafFilter.class.getName());
70 } catch (final Exception exp) {
71 LOGGER.error("Failed to start services-onappf http server", exp);
78 * Creates the server properties object using restServerParameters.
80 * @return the properties object
82 private Properties getServerProperties() {
83 final Properties props = new Properties();
84 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
87 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
89 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
90 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
91 Integer.toString(restServerParameters.getPort()));
92 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
93 HealthCheckRestControllerV1.class.getName());
94 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
95 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
96 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
97 restServerParameters.getUserName());
98 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
99 restServerParameters.getPassword());
100 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
101 String.valueOf(restServerParameters.isHttps()));
102 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
103 String.valueOf(restServerParameters.isAaf()));
104 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
105 GsonMessageBodyHandler.class.getName());
113 public boolean stop() {
114 for (final HttpServletServer server : servers) {
117 } catch (final Exception exp) {
118 LOGGER.error("Failed to stop services-onappf http server", exp);
128 public void shutdown() {
136 public boolean isAlive() {
137 return !servers.isEmpty();
144 public String toString() {
145 final StringBuilder builder = new StringBuilder();
146 builder.append("ApexStarterRestServer [servers=");
147 builder.append(servers);
149 return builder.toString();