diff options
Diffstat (limited to 'main/src')
11 files changed, 46 insertions, 412 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..."); diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java index f6266de5..32c44602 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java @@ -25,6 +25,12 @@ package org.onap.policy.api.main.parameters; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.onap.policy.common.endpoints.parameters.RestServerParameters; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.models.provider.PolicyModelsProviderParameters; @@ -41,12 +47,6 @@ public class CommonTestData { */ private static final String REST_SERVER_PORT = "6969"; - private static final String REST_SERVER_PASSWORD = "zb!XztG34"; - private static final String REST_SERVER_USER = "healthcheck"; - private static final String REST_SERVER_HOST = "0.0.0.0"; - private static final boolean REST_SERVER_HTTPS = false; - private static final boolean REST_SERVER_AAF = false; - private static final String PROVIDER_GROUP_NAME = "PolicyProviderParameterGroup"; private static final String PROVIDER_IMPL = "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"; private static final String DATABASE_DRIVER = "org.h2.Driver"; @@ -55,6 +55,8 @@ public class CommonTestData { private static final String DATABASE_PASSWORD = "P01icY"; private static final String PERSISTENCE_UNIT = "ToscaConceptTest"; + private Coder coder = new StandardCoder(); + /** * Returns an instance of RestServerParameters for test cases. * @@ -63,14 +65,15 @@ public class CommonTestData { * @return the RestServerParameters object */ public RestServerParameters getRestServerParameters(final boolean isEmpty, int port) { - final RestServerParameters restServerParameters; - if (!isEmpty) { - restServerParameters = new RestServerParameters(REST_SERVER_HOST, port, REST_SERVER_USER, - REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF); - } else { - restServerParameters = new RestServerParameters(null, 0, null, null, false, false); + String fileName = "src/test/resources/parameters/" + + (isEmpty ? "RestServerParametersEmpty" : "RestServerParameters") + ".json"; + try { + String text = new String(Files.readAllBytes(new File(fileName).toPath()), StandardCharsets.UTF_8); + text = text.replace("6969", String.valueOf(port)); + return coder.decode(text, RestServerParameters.class); + } catch (CoderException | IOException e) { + throw new RuntimeException("cannot read/decode " + fileName, e); } - return restServerParameters; } /** diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java index b8f27e94..c12847a7 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.models.provider.PolicyModelsProviderParameters; @@ -98,7 +99,7 @@ public class TestApiParameterGroup { final GroupValidationResult validationResult = apiParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() - .contains("\"org.onap.policy.api.main.parameters.RestServerParameters\" INVALID, " + .contains("\"org.onap.policy.common.endpoints.parameters.RestServerParameters\" INVALID, " + "parameter group has status INVALID")); } diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestRestServerParameters.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestRestServerParameters.java deleted file mode 100644 index 15323a4d..00000000 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestRestServerParameters.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ - * Copyright (C) 2019 IBM. - * ================================================================================ - * 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 static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; - -public class TestRestServerParameters { - - private RestServerParameters restServerParameters; - - @Before - public void setUp() { - restServerParameters = new RestServerParameters("host", 22, "userName", "password"); - } - - @Test - public void testGetMethods() { - assertEquals("userName", restServerParameters.getUserName()); - assertEquals("host", restServerParameters.getHost()); - assertEquals(22, restServerParameters.getPort()); - assertEquals("password", restServerParameters.getPassword()); - } -} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 6d49efd2..103eb4ca 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -34,7 +34,6 @@ import java.lang.reflect.Modifier; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.Properties; - import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; @@ -45,7 +44,6 @@ import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; @@ -53,8 +51,9 @@ import org.junit.After; import org.junit.Test; import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.CommonTestData; -import org.onap.policy.api.main.parameters.RestServerParameters; import org.onap.policy.api.main.startstop.Main; +import org.onap.policy.common.endpoints.http.server.RestServer; +import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.gson.GsonMessageBodyHandler; import org.onap.policy.common.utils.coder.StandardCoder; @@ -139,7 +138,7 @@ public class TestApiRestServer { private static final String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; private static final CommonTestData COMMON_TEST_DATA = new CommonTestData(); private Main main; - private ApiRestServer restServer; + private RestServer restServer; private StandardCoder standardCoder = new StandardCoder(); private int port; @@ -199,14 +198,14 @@ public class TestApiRestServer { port = NetworkUtil.allocPort(); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false, port); restServerParams.setName(CommonTestData.API_GROUP_NAME); - restServer = new ApiRestServer(restServerParams); + restServer = new RestServer(restServerParams, null, ApiRestController.class); try { restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report); assertTrue(restServer.isAlive()); - assertTrue(restServer.toString().startsWith("ApiRestServer [servers=")); + assertTrue(restServer.toString().startsWith("RestServer [servers=")); } catch (final Exception exp) { LOGGER.error("testHealthCheckFailure failed", exp); fail("Test should not throw an exception"); @@ -252,7 +251,7 @@ public class TestApiRestServer { port = NetworkUtil.allocPort(); final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false, port); restServerParams.setName(CommonTestData.API_GROUP_NAME); - restServer = new ApiRestServer(restServerParams); + restServer = new RestServer(restServerParams, null, ApiRestController.class); try { restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); diff --git a/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt b/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt index 2ab9c1b4..eca803ea 100644 --- a/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt +++ b/main/src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt @@ -1,7 +1,7 @@ validation error(s) on parameters from "parameters/ApiConfigParameters_InvalidRestServerParameters.json" parameter group "ApiGroup" type "org.onap.policy.api.main.parameters.ApiParameterGroup" INVALID, parameter group has status INVALID - parameter group "null" type "org.onap.policy.api.main.parameters.RestServerParameters" INVALID, parameter group has status INVALID - field "host" type "java.lang.String" value "" INVALID, must be a non-blank string containing hostname/ipaddress of the api rest server - field "port" type "int" value "-1" INVALID, must be a positive integer containing port of the api rest server - field "userName" type "java.lang.String" value "" INVALID, must be a non-blank string containing userName for api rest server credentials - field "password" type "java.lang.String" value "" INVALID, must be a non-blank string containing password for api rest server credentials + parameter group "RestServerParameters" type "org.onap.policy.common.endpoints.parameters.RestServerParameters" INVALID, parameter group has status INVALID + field "host" type "java.lang.String" value "" INVALID, must be a non-blank string + field "port" type "int" value "-1" INVALID, must be >= 1 + field "userName" type "java.lang.String" value "" INVALID, must be a non-blank string + field "password" type "java.lang.String" value "" INVALID, must be a non-blank string
\ No newline at end of file diff --git a/main/src/test/resources/parameters/RestServerParameters.json b/main/src/test/resources/parameters/RestServerParameters.json new file mode 100644 index 00000000..86aee276 --- /dev/null +++ b/main/src/test/resources/parameters/RestServerParameters.json @@ -0,0 +1,6 @@ +{ + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34" +} diff --git a/main/src/test/resources/parameters/RestServerParametersEmpty.json b/main/src/test/resources/parameters/RestServerParametersEmpty.json new file mode 100644 index 00000000..e02aef27 --- /dev/null +++ b/main/src/test/resources/parameters/RestServerParametersEmpty.json @@ -0,0 +1,2 @@ +{
+}
\ No newline at end of file |