diff options
author | 2019-07-17 09:43:08 -0400 | |
---|---|---|
committer | 2019-07-18 16:04:53 -0400 | |
commit | 14d66fcf742b8a15867ac0b9b82176b0958f0ae6 (patch) | |
tree | 172af7e3fc3b09593f41141f941f48fb3101c5e6 /main/src/main | |
parent | 120b41703b98380476ee022a98ce05da997305a6 (diff) |
Modify policy/api to use RestServer from common
This also entailed removing the local copy of RestServerParameters.
Change-Id: Ie5c581ab70aee60844d1660fe0a61290de6470ec
Issue-ID: POLICY-1652
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/main')
4 files changed, 9 insertions, 340 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java index 9727b239..baa0040d 100644 --- a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java +++ b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterGroup.java @@ -20,6 +20,7 @@ package org.onap.policy.api.main.parameters; +import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; diff --git a/main/src/main/java/org/onap/policy/api/main/parameters/RestServerParameters.java b/main/src/main/java/org/onap/policy/api/main/parameters/RestServerParameters.java deleted file mode 100644 index 0a9894ea..00000000 --- a/main/src/main/java/org/onap/policy/api/main/parameters/RestServerParameters.java +++ /dev/null @@ -1,180 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.api.main.parameters; - -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; -import org.onap.policy.common.utils.validation.ParameterValidationUtils; - -/** - * Class to hold all parameters needed for api rest server. - * - */ -public class RestServerParameters implements ParameterGroup { - private String name; - private String host; - private int port; - private String userName; - private String password; - private boolean https; - private boolean aaf; - - /** - * Constructor for instantiating RestServerParameters. - * - * @param host the host name - * @param port the port - * @param userName the user name - * @param password the password - */ - public RestServerParameters(final String host, final int port, final String userName, final String password) { - super(); - this.host = host; - this.port = port; - this.userName = userName; - this.password = password; - this.https = false; - this.aaf = false; - } - - /** - * Constructor for instantiating RestServerParameters. - * - * @param host the host name - * @param port the port - * @param userName the user name - * @param password the password - * @param https the https - * @param aaf the aaf - */ - public RestServerParameters(final String host, final int port, final String userName, final String password, - final boolean https, final boolean aaf) { - super(); - this.host = host; - this.port = port; - this.userName = userName; - this.password = password; - this.https = https; - this.aaf = aaf; - } - - /** - * Return the name of this RestServerParameters instance. - * - * @return name the name of this RestServerParameters - */ - @Override - public String getName() { - return name; - } - - /** - * Return the host of this RestServerParameters instance. - * - * @return the host - */ - public String getHost() { - return host; - } - - /** - * Return the port of this RestServerParameters instance. - * - * @return the port - */ - public int getPort() { - return port; - } - - /** - * Return the user name of this RestServerParameters instance. - * - * @return the userName - */ - public String getUserName() { - return userName; - } - - /** - * Return the password of this RestServerParameters instance. - * - * @return the password - */ - public String getPassword() { - return password; - } - - /** - * Return the https flag of this RestServerParameters instance. - * @return the https - */ - public boolean isHttps() { - return https; - } - - /** - * Return the aaf flag of this RestServerParameters instance. - * @return the aaf - */ - public boolean isAaf() { - return aaf; - } - - /** - * Set the name of this RestServerParameters instance. - * - * @param name the name to set - */ - public void setName(final String name) { - this.name = name; - } - - /** - * Validate the rest server parameters. - * - * @return the result of the validation - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (!ParameterValidationUtils.validateStringParameter(host)) { - validationResult.setResult("host", ValidationStatus.INVALID, - "must be a non-blank string containing hostname/ipaddress of the api rest server"); - } - if (!ParameterValidationUtils.validateStringParameter(userName)) { - validationResult.setResult("userName", ValidationStatus.INVALID, - "must be a non-blank string containing userName for api rest server credentials"); - } - if (!ParameterValidationUtils.validateStringParameter(password)) { - validationResult.setResult("password", ValidationStatus.INVALID, - "must be a non-blank string containing password for api rest server credentials"); - } - if (!ParameterValidationUtils.validateIntParameter(port)) { - validationResult.setResult("port", ValidationStatus.INVALID, - "must be a positive integer containing port of the api rest server"); - } - return validationResult; - } -} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestServer.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestServer.java deleted file mode 100644 index 49e1041e..00000000 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestServer.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.api.main.rest; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import org.onap.policy.api.main.parameters.RestServerParameters; -import org.onap.policy.api.main.rest.aaf.AafApiFilter; -import org.onap.policy.common.capabilities.Startable; -import org.onap.policy.common.endpoints.http.server.HttpServletServer; -import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; -import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * Class to manage life cycle of api rest server. - * - */ -public class ApiRestServer implements Startable { - - private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class); - - private List<HttpServletServer> servers = new ArrayList<>(); - - private RestServerParameters restServerParameters; - - /** - * Constructor for instantiating ApiRestServer. - * - * @param restServerParameters the rest server parameters - */ - public ApiRestServer(final RestServerParameters restServerParameters) { - this.restServerParameters = restServerParameters; - } - - /** - * {@inheritDoc}. - */ - @Override - public boolean start() { - try { - servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties()); - for (HttpServletServer server : servers) { - if (server.isAaf()) { - server.addFilterClass(null, AafApiFilter.class.getName()); - } - server.start(); - } - } catch (final Exception exp) { - LOGGER.error("Failed to start api http server", exp); - return false; - } - return true; - } - - /** - * Creates the server properties object using restServerParameters. - * - * @return the properties object - */ - private Properties getServerProperties() { - final Properties props = new Properties(); - final String svcpfx = - PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName(); - - props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName()); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost()); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, - Integer.toString(restServerParameters.getPort())); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, - String.join(",", LegacyApiRestController.class.getName(), - ApiRestController.class.getName())); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false"); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true"); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, - restServerParameters.getUserName()); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, - restServerParameters.getPassword()); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, - String.valueOf(restServerParameters.isHttps())); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, - String.valueOf(restServerParameters.isAaf())); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, - GsonMessageBodyHandler.class.getName()); - - return props; - } - - /** - * {@inheritDoc}. - */ - @Override - public boolean stop() { - for (final HttpServletServer server : servers) { - try { - server.stop(); - } catch (final Exception exp) { - LOGGER.error("Failed to stop api http server", exp); - } - } - return true; - } - - /** - * {@inheritDoc}. - */ - @Override - public void shutdown() { - stop(); - } - - /** - * {@inheritDoc}. - */ - @Override - public boolean isAlive() { - return !servers.isEmpty(); - } - - /** - * {@inheritDoc}. - */ - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("ApiRestServer [servers="); - builder.append(servers); - builder.append("]"); - return builder.toString(); - } - -} diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java index dc61f3de..9708d5ff 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiActivator.java @@ -25,7 +25,10 @@ package org.onap.policy.api.main.startstop; import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.ApiParameterGroup; -import org.onap.policy.api.main.rest.ApiRestServer; +import org.onap.policy.api.main.rest.ApiRestController; +import org.onap.policy.api.main.rest.LegacyApiRestController; +import org.onap.policy.api.main.rest.aaf.AafApiFilter; +import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.parameters.ParameterService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +45,7 @@ public class ApiActivator { private static boolean alive = false; - private ApiRestServer restServer; + private RestServer restServer; /** * Instantiate the activator for policy api as a complete service. @@ -73,7 +76,9 @@ public class ApiActivator { */ private void startApiRestServer() throws PolicyApiException { apiParameterGroup.getRestServerParameters().setName(apiParameterGroup.getName()); - restServer = new ApiRestServer(apiParameterGroup.getRestServerParameters()); + restServer = new RestServer(apiParameterGroup.getRestServerParameters(), AafApiFilter.class, + LegacyApiRestController.class, + ApiRestController.class); if (!restServer.start()) { throw new PolicyApiException( "Failed to start api rest server. Check log for more details..."); |