diff options
author | Chenfei Gao <cgao@research.att.com> | 2019-01-31 16:09:09 -0500 |
---|---|---|
committer | Chenfei Gao <cgao@research.att.com> | 2019-02-07 09:19:38 -0500 |
commit | 6ea2e1bb9f7ee330f04fe8e28fd94fbf398a06ec (patch) | |
tree | 9c2f3845f222a31b080929763446ef1cb20858bd /main/src | |
parent | f437d629aac23467dc505d081bbe1634a5ef63b8 (diff) |
Add changes to basic structure of api component
Includes:
1) Changed logging from FlexLogger to slf4j and used the logback.xml aligned with
v1.2 logging requirements.
2) Added https as a configurable parameter of api rest server.
3) Added aaf as a condfigurable parameter of api rest server.
4) Added AafFilter classes for healthcheck and statistics.
5) Added api statistics REST entry point.
6) Created related junit tests.
7) Packaged docker
Issue-ID: POLICY-1270
Change-Id: I638f61361bb052545e8597531ba3d58019bf1f24
Signed-off-by: Chenfei Gao <cgao@research.att.com>
Diffstat (limited to 'main/src')
26 files changed, 1504 insertions, 126 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/PolicyApiException.java b/main/src/main/java/org/onap/policy/api/main/exception/PolicyApiException.java index 8dedc8ad..1fccb06d 100644 --- a/main/src/main/java/org/onap/policy/api/main/PolicyApiException.java +++ b/main/src/main/java/org/onap/policy/api/main/exception/PolicyApiException.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.api.main; +package org.onap.policy.api.main.exception; /** * This exception will be called if an error occurs in policy api external service. diff --git a/main/src/main/java/org/onap/policy/api/main/PolicyApiRuntimeException.java b/main/src/main/java/org/onap/policy/api/main/exception/PolicyApiRuntimeException.java index a9ec07db..ea6b55f6 100644 --- a/main/src/main/java/org/onap/policy/api/main/PolicyApiRuntimeException.java +++ b/main/src/main/java/org/onap/policy/api/main/exception/PolicyApiRuntimeException.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.api.main; +package org.onap.policy.api.main.exception; /** * This runtime exception will be called if a runtime error occurs when using policy api. diff --git a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java index 92923d0d..b660b883 100644 --- a/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java +++ b/main/src/main/java/org/onap/policy/api/main/parameters/ApiParameterHandler.java @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -23,18 +26,19 @@ package org.onap.policy.api.main.parameters; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.FileReader; -import org.onap.policy.api.main.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.startstop.ApiCommandLineArguments; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.GroupValidationResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class handles reading, parsing and validating of policy api parameters from JSON files. */ public class ApiParameterHandler { - private static final Logger LOGGER = FlexLogger.getLogger(ApiParameterHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ApiParameterHandler.class); + /** * Read the parameters from the parameter file. * @@ -55,7 +59,6 @@ public class ApiParameterHandler { } catch (final Exception e) { final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath() + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage(); - LOGGER.error(errorMessage, e); throw new PolicyApiException(errorMessage, e); } 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 index 33ecff37..0a9894ea 100644 --- 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 @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -35,7 +38,9 @@ public class RestServerParameters implements ParameterGroup { private int port; private String userName; private String password; - + private boolean https; + private boolean aaf; + /** * Constructor for instantiating RestServerParameters. * @@ -50,6 +55,29 @@ public class RestServerParameters implements ParameterGroup { 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; } /** @@ -97,6 +125,22 @@ public class RestServerParameters implements ParameterGroup { 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. diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java index 9b4d75df..ca211b67 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -33,16 +36,15 @@ import javax.ws.rs.core.Response; import org.onap.policy.common.endpoints.report.HealthCheckReport; /** - * Class to provide api REST services. - * + * Class to provide REST API services. */ @Path("/") @Api @Produces(MediaType.APPLICATION_JSON) @SwaggerDefinition( - info = @Info(description = "Policy Api Service", version = "v1.0", title = "Policy Api"), + info = @Info(description = "Policy Api Service", version = "v2.0", title = "Policy Api"), consumes = { MediaType.APPLICATION_JSON }, produces = { MediaType.APPLICATION_JSON }, - schemes = { SwaggerDefinition.Scheme.HTTP }, + schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS }, tags = { @Tag(name = "policy-api", description = "Policy Api Service Operations") }) public class ApiRestController { @@ -54,4 +56,14 @@ public class ApiRestController { public Response healthcheck() { return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build(); } + + @GET + @Path("statistics") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Fetch current statistics", + notes = "Provides current statistics of the Policy API component", + response = StatisticsReport.class) + public Response statistics() { + return Response.status(Response.Status.OK).entity(new StatisticsProvider().fetchCurrentStatistics()).build(); + } } 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 index b6d69602..cf27bacb 100644 --- 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 @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -24,10 +27,11 @@ 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.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class to manage life cycle of api rest server. @@ -37,7 +41,8 @@ public class ApiRestServer implements Startable { private static final String SEPARATOR = "."; private static final String HTTP_SERVER_SERVICES = "http.server.services"; - private static final Logger LOGGER = FlexLogger.getLogger(ApiRestServer.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class); private List<HttpServletServer> servers = new ArrayList<>(); @@ -59,7 +64,10 @@ public class ApiRestServer implements Startable { public boolean start() { try { servers = HttpServletServer.factory.build(getServerProperties()); - for (final HttpServletServer server : servers) { + for (HttpServletServer server : servers) { + if (server.isAaf()) { + server.addFilterClass(null, AafApiFilter.class.getCanonicalName()); + } server.start(); } } catch (final Exception exp) { @@ -68,7 +76,7 @@ public class ApiRestServer implements Startable { } return true; } - + /** * Creates the server properties object using restServerParameters. * @@ -89,6 +97,10 @@ public class ApiRestServer implements Startable { restServerParameters.getUserName()); props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password", restServerParameters.getPassword()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https", + String.valueOf(restServerParameters.isHttps())); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf", + String.valueOf(restServerParameters.isAaf())); return props; } diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java new file mode 100644 index 00000000..55c8cee1 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java @@ -0,0 +1,340 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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; + +/** + * Class to hold statistical data for API access. + */ +public class ApiStatisticsManager { + + private static long totalApiCallCount; + private static long apiCallSuccessCount; + private static long apiCallFailureCount; + private static long totalPolicyGetCount; + private static long totalPolicyPostCount; + private static long totalTemplateGetCount; + private static long totalTemplatePostCount; + private static long policyGetSuccessCount; + private static long policyGetFailureCount; + private static long policyPostSuccessCount; + private static long policyPostFailureCount; + private static long templateGetSuccessCount; + private static long templateGetFailureCount; + private static long templatePostSuccessCount; + private static long templatePostFailureCount; + + private ApiStatisticsManager() { + throw new IllegalStateException("Instantiation of the class is not allowed"); + } + + /** + * Method to update the total api call count. + * + * @return the updated value of totalApiCallCount + */ + public static long updateTotalApiCallCount() { + return ++totalApiCallCount; + } + + /** + * Method to update the successful api call count. + * + * @return the updated value of apiCallSuccessCount + */ + public static long updateApiCallSuccessCount() { + return ++apiCallSuccessCount; + } + + /** + * Method to update the failed api call count. + * + * @return the updated value of apiCallFailureCount + */ + public static long updateApiCallFailureCount() { + return ++apiCallFailureCount; + } + + /** + * Method to update the total policy GET count. + * + * @return the updated value of totalPolicyGetCount + */ + public static long updateTotalPolicyGetCount() { + return ++totalPolicyGetCount; + } + + /** + * Method to update the total policy POST count. + * + * @return the updated value of totalPolicyPostCount + */ + public static long updateTotalPolicyPostCount() { + return ++totalPolicyPostCount; + } + + /** + * Method to update the total template GET count. + * + * @return the updated value of totalTemplateGetCount + */ + public static long updateTotalTemplateGetCount() { + return ++totalTemplateGetCount; + } + + /** + * Method to update the total template POST count. + * + * @return the updated value of totalTemplatePostCount + */ + public static long updateTotalTemplatePostCount() { + return ++totalTemplatePostCount; + } + + /** + * Method to update successful policy GET count. + * + * @return the updated value of policyGetSuccessCount + */ + public static long updatePolicyGetSuccessCount() { + return ++policyGetSuccessCount; + } + + /** + * Method to update failed policy GET count. + * + * @return the updated value of policyGetFailureCount + */ + public static long updatePolicyGetFailureCount() { + return ++policyGetFailureCount; + } + + /** + * Method to update successful policy POST count. + * + * @return the updated value of policyPostSuccessCount + */ + public static long updatePolicyPostSuccessCount() { + return ++policyPostSuccessCount; + } + + /** + * Method to update failed policy POST count. + * + * @return the updated value of policyPostFailureCount + */ + public static long updatePolicyPostFailureCount() { + return ++policyPostFailureCount; + } + + /** + * Method to update successful template GET count. + * + * @return the updated value of templateGetSuccessCount + */ + public static long updateTemplateGetSuccessCount() { + return ++templateGetSuccessCount; + } + + /** + * Method to update failed template GET count. + * + * @return the updated value of templateGetFailureCount + */ + public static long updateTemplateGetFailureCount() { + return ++templateGetFailureCount; + } + + /** + * Method to update successful template POST count. + * + * @return the updated value of templatePostSuccessCount + */ + public static long updateTemplatePostSuccessCount() { + return ++templatePostSuccessCount; + } + + /** + * Method to update failed template POST count. + * + * @return the updated value of templatePostFailureCount + */ + public static long updateTemplatePostFailureCount() { + return ++templatePostFailureCount; + } + + /** + * Returns the current value of totalApiCallCount. + * + * @return the totalApiCallCount + */ + public static long getTotalApiCallCount() { + return totalApiCallCount; + } + + /** + * Returns the current value of apiCallSuccessCount. + * + * @return the apiCallSuccessCount + */ + public static long getApiCallSuccessCount() { + return apiCallSuccessCount; + } + + /** + * Returns the current value of apiCallFailureCount. + * + * @return the apiCallFailureCount + */ + public static long getApiCallFailureCount() { + return apiCallFailureCount; + } + + /** + * Returns the current value of totalPolicyGetCount. + * + * @return the totalPolicyGetCount + */ + public static long getTotalPolicyGetCount() { + return totalPolicyGetCount; + } + + /** + * Returns the current value of totalPolicyPostCount. + * + * @return the totalPolicyPostCount + */ + public static long getTotalPolicyPostCount() { + return totalPolicyPostCount; + } + + /** + * Returns the current value of totalTemplateGetCount. + * + * @return the totalTemplateGetCount + */ + public static long getTotalTemplateGetCount() { + return totalTemplateGetCount; + } + + /** + * Returns the current value of totalTemplatePostCount. + * + * @return the totalTemplatePostCount + */ + public static long getTotalTemplatePostCount() { + return totalTemplatePostCount; + } + + /** + * Returns the current value of policyGetSuccessCount. + * + * @return the policyGetSuccessCount + */ + public static long getPolicyGetSuccessCount() { + return policyGetSuccessCount; + } + + /** + * Returns the current value of policyGetFailureCount. + * + * @return the policyGetFailureCount + */ + public static long getPolicyGetFailureCount() { + return policyGetFailureCount; + } + + /** + * Returns the current value of policyPostSuccessCount. + * + * @return the policyPostSuccessCount + */ + public static long getPolicyPostSuccessCount() { + return policyPostSuccessCount; + } + + /** + * Returns the current value of policyPostFailureCount. + * + * @return the policyPostFailureCount + */ + public static long getPolicyPostFailureCount() { + return policyPostFailureCount; + } + + /** + * Returns the current value of templateGetSuccessCount. + * + * @return the templateGetSuccessCount + */ + public static long getTemplateGetSuccessCount() { + return templateGetSuccessCount; + } + + /** + * Returns the current value of templateGetFailureCount. + * + * @return the templateGetFailureCount + */ + public static long getTemplateGetFailureCount() { + return templateGetFailureCount; + } + + /** + * Returns the current value of templatePostSuccessCount. + * + * @return the templatePostSuccessCount + */ + public static long getTemplatePostSuccessCount() { + return templatePostSuccessCount; + } + + /** + * Returns the current value of templatePostFailureCount. + * + * @return the templatePostFailureCount + */ + public static long getTemplatePostFailureCount() { + return templatePostFailureCount; + } + + /** + * Reset all the statistics counts to 0. + */ + public static void resetAllStatistics() { + totalApiCallCount = 0L; + apiCallSuccessCount = 0L; + apiCallFailureCount = 0L; + totalPolicyGetCount = 0L; + totalPolicyPostCount = 0L; + totalTemplateGetCount = 0L; + totalTemplatePostCount = 0L; + policyGetSuccessCount = 0L; + policyGetFailureCount = 0L; + policyPostSuccessCount = 0L; + policyPostFailureCount = 0L; + templateGetSuccessCount = 0L; + templateGetFailureCount = 0L; + templatePostSuccessCount = 0L; + templatePostFailureCount = 0L; + } +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/HealthCheckProvider.java index dd223384..48387336 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/HealthCheckProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/HealthCheckProvider.java @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. diff --git a/main/src/main/java/org/onap/policy/api/main/rest/StatisticsProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/StatisticsProvider.java new file mode 100644 index 00000000..e8f18404 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/StatisticsProvider.java @@ -0,0 +1,59 @@ +/*- +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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 org.onap.policy.api.main.startstop.ApiActivator; + +/** + * Class to fetch API statistics. + */ +public class StatisticsProvider { + + /** + * Return the current API statistics. + * + * @return Report containing API statistics + */ + public StatisticsReport fetchCurrentStatistics() { + final StatisticsReport report = new StatisticsReport(); + report.setCode(ApiActivator.isAlive() ? 200 : 500); + report.setTotalApiCallCount(ApiStatisticsManager.getTotalApiCallCount()); + report.setApiCallSuccessCount(ApiStatisticsManager.getApiCallSuccessCount()); + report.setApiCallFailureCount(ApiStatisticsManager.getApiCallFailureCount()); + report.setTotalPolicyGetCount(ApiStatisticsManager.getTotalPolicyGetCount()); + report.setTotalPolicyPostCount(ApiStatisticsManager.getTotalPolicyPostCount()); + report.setTotalTemplateGetCount(ApiStatisticsManager.getTotalTemplateGetCount()); + report.setTotalTemplatePostCount(ApiStatisticsManager.getTotalTemplatePostCount()); + report.setPolicyGetSuccessCount(ApiStatisticsManager.getPolicyGetSuccessCount()); + report.setPolicyGetFailureCount(ApiStatisticsManager.getPolicyGetFailureCount()); + report.setPolicyPostSuccessCount(ApiStatisticsManager.getPolicyPostSuccessCount()); + report.setPolicyPostFailureCount(ApiStatisticsManager.getPolicyPostFailureCount()); + report.setTemplateGetSuccessCount(ApiStatisticsManager.getTemplateGetSuccessCount()); + report.setTemplateGetFailureCount(ApiStatisticsManager.getTemplateGetFailureCount()); + report.setTemplatePostSuccessCount(ApiStatisticsManager.getTemplatePostSuccessCount()); + report.setTemplatePostFailureCount(ApiStatisticsManager.getTemplatePostFailureCount()); + return report; + } +} + diff --git a/main/src/main/java/org/onap/policy/api/main/rest/StatisticsReport.java b/main/src/main/java/org/onap/policy/api/main/rest/StatisticsReport.java new file mode 100644 index 00000000..03bc9590 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/StatisticsReport.java @@ -0,0 +1,376 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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; + +/** + * Class to represent API statistics report. + */ +public class StatisticsReport { + + private int code; + private long totalApiCallCount; + private long apiCallSuccessCount; + private long apiCallFailureCount; + private long totalPolicyGetCount; + private long totalPolicyPostCount; + private long totalTemplateGetCount; + private long totalTemplatePostCount; + private long policyGetSuccessCount; + private long policyGetFailureCount; + private long policyPostSuccessCount; + private long policyPostFailureCount; + private long templateGetSuccessCount; + private long templateGetFailureCount; + private long templatePostSuccessCount; + private long templatePostFailureCount; + + /** + * Returns the code of this {@link StatisticsReport} instance. + * + * @return the code + */ + public int getCode() { + return code; + } + + /** + * Set code in this {@link StatisticsReport} instance. + * + * @param code the code to set + */ + public void setCode(final int code) { + this.code = code; + } + + /** + * Returns the totalApiCallCount of this {@link StatisticsReport} instance. + * + * @return the totalApiCallCount + */ + public long getTotalApiCallCount() { + return totalApiCallCount; + } + + /** + * Set totalApiCallCount in this {@link StatisticsReport} instance. + * + * @param totalApiCallCount the totalApiCallCount to set + */ + public void setTotalApiCallCount(final long totalApiCallCount) { + this.totalApiCallCount = totalApiCallCount; + } + + /** + * Returns the apiCallSuccessCount of this {@link StatisticsReport} instance. + * + * @return the apiCallSuccessCount + */ + public long getApiCallSuccessCount() { + return apiCallSuccessCount; + } + + /** + * Set apiCallSuccessCount in this {@link StatisticsReport} instance. + * + * @param apiCallSuccessCount the apiCallSuccessCount to set + */ + public void setApiCallSuccessCount(final long apiCallSuccessCount) { + this.apiCallSuccessCount = apiCallSuccessCount; + } + + /** + * Returns the apiCallFailureCount of this {@link StatisticsReport} instance. + * + * @return the apiCallFailureCount + */ + public long getApiCallFailureCount() { + return apiCallFailureCount; + } + + /** + * Set apiCallFailureCount in this {@link StatisticsReport} instance. + * + * @param apiCallFailureCount the apiCallFailureCount to set + */ + public void setApiCallFailureCount(final long apiCallFailureCount) { + this.apiCallFailureCount = apiCallFailureCount; + } + + /** + * Returns the totalPolicyGetCount of this {@link StatisticsReport} instance. + * + * @return the totalPolicyGetCount + */ + public long getTotalPolicyGetCount() { + return totalPolicyGetCount; + } + + /** + * Set totalPolicyGetCount in this {@link StatisticsReport} instance. + * + * @param totalPolicyGetCount the totalPolicyGetCount to set + */ + public void setTotalPolicyGetCount(final long totalPolicyGetCount) { + this.totalPolicyGetCount = totalPolicyGetCount; + } + + /** + * Returns the totalPolicyPostCount of this {@link StatisticsReport} instance. + * + * @return the totalPolicyPostCount + */ + public long getTotalPolicyPostCount() { + return totalPolicyPostCount; + } + + /** + * Set totalPolicyPostCount in this {@link StatisticsReport} instance. + * + * @param totalPolicyPostCount the totalPolicyPostCount to set + */ + public void setTotalPolicyPostCount(final long totalPolicyPostCount) { + this.totalPolicyPostCount = totalPolicyPostCount; + } + + /** + * Returns the totalTemplateGetCount of this {@link StatisticsReport} instance. + * + * @return the totalTemplateGetCount + */ + public long getTotalTemplateGetCount() { + return totalTemplateGetCount; + } + + /** + * Set totalTemplateGetCount in this {@link StatisticsReport} instance. + * + * @param totalTemplateGetCount the totalTemplateGetCount to set + */ + public void setTotalTemplateGetCount(final long totalTemplateGetCount) { + this.totalTemplateGetCount = totalTemplateGetCount; + } + + /** + * Returns the totalTemplatePostCount of this {@link StatisticsReport} instance. + * + * @return the totalTemplatePostCount + */ + public long getTotalTemplatePostCount() { + return totalTemplatePostCount; + } + + /** + * Set totalTemplatePostCount in this {@link StatisticsReport} instance. + * + * @param totalTemplatePostCount the totalTemplatePostCount to set + */ + public void setTotalTemplatePostCount(final long totalTemplatePostCount) { + this.totalTemplatePostCount = totalTemplatePostCount; + } + + /** + * Returns the policyGetSuccessCount of this {@link StatisticsReport} instance. + * + * @return the policyGetSuccessCount + */ + public long getPolicyGetSuccessCount() { + return policyGetSuccessCount; + } + + /** + * Set policyGetSuccessCount in this {@link StatisticsReport} instance. + * + * @param policyGetSuccessCount the policyGetSuccessCount to set + */ + public void setPolicyGetSuccessCount(final long policyGetSuccessCount) { + this.policyGetSuccessCount = policyGetSuccessCount; + } + + /** + * Returns the policyGetFailureCount of this {@link StatisticsReport} instance. + * + * @return the policyGetFailureCount + */ + public long getPolicyGetFailureCount() { + return policyGetFailureCount; + } + + /** + * Set policyGetFailureCount in this {@link StatisticsReport} instance. + * + * @param policyGetFailureCount the policyGetFailureCount to set + */ + public void setPolicyGetFailureCount(final long policyGetFailureCount) { + this.policyGetFailureCount = policyGetFailureCount; + } + + /** + * Returns the policyPostSuccessCount of this {@link StatisticsReport} instance. + * + * @return the policyPostSuccessCount + */ + public long getPolicyPostSuccessCount() { + return policyPostSuccessCount; + } + + /** + * Set policyPostSuccessCount in this {@link StatisticsReport} instance. + * + * @param policyPostSuccessCount the policyPostSuccessCount to set + */ + public void setPolicyPostSuccessCount(final long policyPostSuccessCount) { + this.policyPostSuccessCount = policyPostSuccessCount; + } + + /** + * Returns the policyPostFailureCount of this {@link StatisticsReport} instance. + * + * @return the policyPostFailureCount + */ + public long getPolicyPostFailureCount() { + return policyPostFailureCount; + } + + /** + * Set policyPostFailureCount in this {@link StatisticsReport} instance. + * + * @param policyPostFailureCount the policyPostFailureCount to set + */ + public void setPolicyPostFailureCount(final long policyPostFailureCount) { + this.policyPostFailureCount = policyPostFailureCount; + } + + /** + * Returns the templateGetSuccessCount of this {@link StatisticsReport} instance. + * + * @return the templateGetSuccessCount + */ + public long getTemplateGetSuccessCount() { + return templateGetSuccessCount; + } + + /** + * Set templateGetSuccessCount in this {@link StatisticsReport} instance. + * + * @param templateGetSuccessCount the templateGetSuccessCount to set + */ + public void setTemplateGetSuccessCount(final long templateGetSuccessCount) { + this.templateGetSuccessCount = templateGetSuccessCount; + } + + /** + * Returns the templateGetFailureCount of this {@link StatisticsReport} instance. + * + * @return the templateGetFailureCount + */ + public long getTemplateGetFailureCount() { + return templateGetFailureCount; + } + + /** + * Set templateGetFailureCount in this {@link StatisticsReport} instance. + * + * @param templateGetFailureCount the templateGetFailureCount to set + */ + public void setTemplateGetFailureCount(final long templateGetFailureCount) { + this.templateGetFailureCount = templateGetFailureCount; + } + + /** + * Returns the templatePostSuccessCount of this {@link StatisticsReport} instance. + * + * @return the templatePostSuccessCount + */ + public long getTemplatePostSuccessCount() { + return templatePostSuccessCount; + } + + /** + * Set templatePostSuccessCount in this {@link StatisticsReport} instance. + * + * @param templatePostSuccessCount the templatePostSuccessCount to set + */ + public void setTemplatePostSuccessCount(final long templatePostSuccessCount) { + this.templatePostSuccessCount = templatePostSuccessCount; + } + + /** + * Returns the templatePostFailureCount of this {@link StatisticsReport} instance. + * + * @return the templatePostFailureCount + */ + public long getTemplatePostFailureCount() { + return templatePostFailureCount; + } + + /** + * Set templatePostFailureCount in this {@link StatisticsReport} instance. + * + * @param templatePostFailureCount the templatePostFailureCount to set + */ + public void setTemplatePostFailureCount(final long templatePostFailureCount) { + this.templatePostFailureCount = templatePostFailureCount; + } + + /** + * {@inheritDoc}}. + */ + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("StatisticsReport [code="); + builder.append(getCode()); + builder.append(", totalApiCallCount="); + builder.append(getTotalApiCallCount()); + builder.append(", apiCallSuccessCount="); + builder.append(getApiCallSuccessCount()); + builder.append(", apiCallFailureCount="); + builder.append(getApiCallFailureCount()); + builder.append(", totalPolicyGetCount="); + builder.append(getTotalPolicyGetCount()); + builder.append(", totalPolicyPostCount="); + builder.append(getTotalPolicyPostCount()); + builder.append(", totalTemplateGetCount="); + builder.append(getTotalTemplateGetCount()); + builder.append(", totalTemplatePostCount="); + builder.append(getTotalTemplatePostCount()); + builder.append(", policyGetSuccessCount="); + builder.append(getPolicyGetSuccessCount()); + builder.append(", policyGetFailureCount="); + builder.append(getPolicyGetFailureCount()); + builder.append(", policyPostSuccessCount="); + builder.append(getPolicyPostSuccessCount()); + builder.append(", policyPostFailureCount="); + builder.append(getPolicyPostFailureCount()); + builder.append(", templateGetSuccessCount="); + builder.append(getTemplateGetSuccessCount()); + builder.append(", templateGetFailureCount="); + builder.append(getTemplateGetFailureCount()); + builder.append(", templatePostSuccessCount="); + builder.append(getTemplatePostSuccessCount()); + builder.append(", templatePostFailureCount="); + builder.append(getTemplatePostFailureCount()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/main/src/main/java/org/onap/policy/api/main/rest/aaf/AafApiFilter.java b/main/src/main/java/org/onap/policy/api/main/rest/aaf/AafApiFilter.java new file mode 100644 index 00000000..dea12ee7 --- /dev/null +++ b/main/src/main/java/org/onap/policy/api/main/rest/aaf/AafApiFilter.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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.aaf; + +import org.onap.policy.common.endpoints.http.server.aaf.AafGranularAuthFilter; + +/** + * Api AAF authorization filter. + */ +public class AafApiFilter extends AafGranularAuthFilter { + + public static final String AAF_NODETYPE = "policy-api"; + public static final String AAF_ROOT_PERMISSION = DEFAULT_NAMESPACE + "." + AAF_NODETYPE; + + @Override + public String getPermissionTypeRoot() { + return AAF_ROOT_PERMISSION; + } +} 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 1f7b05aa..4cd98849 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 @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -20,22 +23,21 @@ package org.onap.policy.api.main.startstop; -import org.onap.policy.api.main.PolicyApiException; +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.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class wraps a distributor so that it can be activated as a complete service together with all its api * and forwarding handlers. */ public class ApiActivator { - // The logger for this class - private static final Logger LOGGER = FlexLogger.getLogger(ApiActivator.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(ApiActivator.class); - // The parameters of this policy api activator private final ApiParameterGroup apiParameterGroup; private static boolean alive = false; @@ -91,8 +93,7 @@ public class ApiActivator { // Stop the api rest server restServer.stop(); } catch (final Exception exp) { - LOGGER.error("Policy api service termination failed", exp); - throw new PolicyApiException(exp.getMessage(), exp); + throw new PolicyApiException("Policy api service termination failed", exp); } } diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiCommandLineArguments.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiCommandLineArguments.java index f165b16f..f6c1c781 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiCommandLineArguments.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiCommandLineArguments.java @@ -31,11 +31,10 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -import org.onap.policy.api.main.PolicyApiException; -import org.onap.policy.api.main.PolicyApiRuntimeException; +import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiRuntimeException; import org.onap.policy.common.utils.resources.ResourceUtils; - /** * This class reads and handles command line parameters for the policy api main program. */ diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/Main.java b/main/src/main/java/org/onap/policy/api/main/startstop/Main.java index 00d36873..97d53ade 100644 --- a/main/src/main/java/org/onap/policy/api/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/api/main/startstop/Main.java @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -21,18 +24,19 @@ package org.onap.policy.api.main.startstop; import java.util.Arrays; -import org.onap.policy.api.main.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.api.main.parameters.ApiParameterHandler; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class initiates ONAP Policy Framework policy api. * */ public class Main { - private static final Logger LOGGER = FlexLogger.getLogger(Main.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); // The policy api Activator that activates the policy api service private ApiActivator activator; @@ -47,7 +51,7 @@ public class Main { */ public Main(final String[] args) { final String argumentString = Arrays.toString(args); - LOGGER.info("Starting policy api service with arguments - " + argumentString); + LOGGER.info("Starting policy api service with arguments - {}", argumentString); // Check the arguments final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); @@ -81,8 +85,7 @@ public class Main { try { activator.initialize(); } catch (final PolicyApiException e) { - LOGGER.error("start of policy api service failed, used parameters are " + Arrays.toString(args), - e); + LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e); return; } 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 f86c8570..8ff2f98e 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 @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -30,21 +33,23 @@ public class CommonTestData { private static final String REST_SERVER_USER = "healthcheck"; private static final int REST_SERVER_PORT = 6969; 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; public static final String API_GROUP_NAME = "ApiGroup"; /** - * Returns an instance of ReceptionHandlerParameters for test cases. + * Returns an instance of RestServerParameters for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not - * @return the restServerParameters object + * @return the RestServerParameters object */ public RestServerParameters getRestServerParameters(final boolean isEmpty) { final RestServerParameters restServerParameters; if (!isEmpty) { restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER, - REST_SERVER_PASSWORD); + REST_SERVER_PASSWORD, REST_SERVER_HTTPS, REST_SERVER_AAF); } else { - restServerParameters = new RestServerParameters(null, 0, null, null); + restServerParameters = new RestServerParameters(null, 0, null, null, false, false); } 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 7a59dad6..93fbfc64 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 @@ -1,6 +1,9 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * 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. @@ -47,6 +50,8 @@ public class TestApiParameterGroup { apiParameters.getRestServerParameters().getUserName()); assertEquals(restServerParameters.getPassword(), apiParameters.getRestServerParameters().getPassword()); + assertEquals(restServerParameters.isHttps(), apiParameters.getRestServerParameters().isHttps()); + assertEquals(restServerParameters.isAaf(), apiParameters.getRestServerParameters().isAaf()); assertEquals(CommonTestData.API_GROUP_NAME, apiParameters.getName()); } diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java index 7a065240..71d8c357 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * 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. @@ -29,7 +30,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Test; -import org.onap.policy.api.main.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.startstop.ApiCommandLineArguments; /** @@ -39,9 +40,7 @@ import org.onap.policy.api.main.startstop.ApiCommandLineArguments; public class TestApiParameterHandler { @Test public void testParameterHandlerNoParameterFile() throws PolicyApiException { - final String[] noArgumentString = - { "-c", "parameters/NoParameterFile.json" }; - + final String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" }; final ApiCommandLineArguments noArguments = new ApiCommandLineArguments(); noArguments.parse(noArgumentString); @@ -55,9 +54,7 @@ public class TestApiParameterHandler { @Test public void testParameterHandlerEmptyParameters() throws PolicyApiException { - final String[] emptyArgumentString = - { "-c", "parameters/EmptyParameters.json" }; - + final String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" }; final ApiCommandLineArguments emptyArguments = new ApiCommandLineArguments(); emptyArguments.parse(emptyArgumentString); @@ -71,9 +68,7 @@ public class TestApiParameterHandler { @Test public void testParameterHandlerBadParameters() throws PolicyApiException { - final String[] badArgumentString = - { "-c", "parameters/BadParameters.json" }; - + final String[] badArgumentString = { "-c", "parameters/BadParameters.json" }; final ApiCommandLineArguments badArguments = new ApiCommandLineArguments(); badArguments.parse(badArgumentString); @@ -89,9 +84,7 @@ public class TestApiParameterHandler { @Test public void testParameterHandlerInvalidParameters() throws PolicyApiException { - final String[] invalidArgumentString = - { "-c", "parameters/InvalidParameters.json" }; - + final String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" }; final ApiCommandLineArguments invalidArguments = new ApiCommandLineArguments(); invalidArguments.parse(invalidArgumentString); @@ -107,9 +100,7 @@ public class TestApiParameterHandler { @Test public void testParameterHandlerNoParameters() throws PolicyApiException { - final String[] noArgumentString = - { "-c", "parameters/NoParameters.json" }; - + final String[] noArgumentString = { "-c", "parameters/NoParameters.json" }; final ApiCommandLineArguments noArguments = new ApiCommandLineArguments(); noArguments.parse(noArgumentString); @@ -127,24 +118,18 @@ public class TestApiParameterHandler { @Test public void testParameterHandlerMinumumParameters() throws PolicyApiException { - final String[] minArgumentString = - { "-c", "parameters/MinimumParameters.json" }; - + final String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" }; final ApiCommandLineArguments minArguments = new ApiCommandLineArguments(); minArguments.parse(minArgumentString); - final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(minArguments); assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName()); } @Test public void testApiParameterGroup() throws PolicyApiException { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters.json" }; - + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); arguments.parse(apiConfigParameters); - final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments); assertTrue(arguments.checkSetConfigurationFilePath()); assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName()); @@ -152,9 +137,7 @@ public class TestApiParameterHandler { @Test public void testApiParameterGroup_InvalidName() throws PolicyApiException { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters_InvalidName.json" }; - + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_InvalidName.json" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); arguments.parse(apiConfigParameters); @@ -170,9 +153,8 @@ public class TestApiParameterHandler { @Test public void testApiParameterGroup_InvalidRestServerParameters() throws PolicyApiException, IOException { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" }; - + final String[] apiConfigParameters = + { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); arguments.parse(apiConfigParameters); @@ -189,8 +171,7 @@ public class TestApiParameterHandler { @Test public void testApiVersion() throws PolicyApiException { - final String[] apiConfigParameters = - { "-v" }; + final String[] apiConfigParameters = { "-v" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); final String version = arguments.parse(apiConfigParameters); assertTrue(version.startsWith("ONAP Policy Framework Api Service")); @@ -198,8 +179,7 @@ public class TestApiParameterHandler { @Test public void testApiHelp() throws PolicyApiException { - final String[] apiConfigParameters = - { "-h" }; + final String[] apiConfigParameters = { "-h" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); final String help = arguments.parse(apiConfigParameters); assertTrue(help.startsWith("usage:")); @@ -207,8 +187,7 @@ public class TestApiParameterHandler { @Test public void testApiInvalidOption() throws PolicyApiException { - final String[] apiConfigParameters = - { "-d" }; + final String[] apiConfigParameters = { "-d" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); try { arguments.parse(apiConfigParameters); 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 ad63d0f1..1ea42e0e 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * 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. @@ -22,6 +23,9 @@ package org.onap.policy.api.main.rest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -32,13 +36,14 @@ import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.Test; -import org.onap.policy.api.main.PolicyApiException; +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.report.HealthCheckReport; -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class to perform unit test of HealthCheckMonitor. @@ -46,7 +51,7 @@ import org.onap.policy.common.logging.flexlogger.Logger; */ public class TestApiRestServer { - private static final Logger LOGGER = FlexLogger.getLogger(TestApiRestServer.class); + private static final Logger LOGGER = LoggerFactory.getLogger(TestApiRestServer.class); private static final String NOT_ALIVE = "not alive"; private static final String ALIVE = "alive"; private static final String SELF = "self"; @@ -55,10 +60,15 @@ public class TestApiRestServer { @Test public void testHealthCheckSuccess() throws PolicyApiException, InterruptedException { final String reportString = "Report [name=Policy API, url=self, healthy=true, code=200, message=alive]"; - final Main main = startApiService(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); - stopApiService(main); + try { + final Main main = startApiService(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); + stopApiService(main); + } catch (final Exception exp) { + LOGGER.error("testHealthCheckSuccess failed", exp); + fail("Test should not throw an exception"); + } } @Test @@ -67,12 +77,17 @@ public class TestApiRestServer { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.API_GROUP_NAME); final ApiRestServer restServer = new ApiRestServer(restServerParams); - restServer.start(); - final HealthCheckReport report = performHealthCheck(); - validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); - assertTrue(restServer.isAlive()); - assertTrue(restServer.toString().startsWith("ApiRestServer [servers=")); - restServer.shutdown(); + try { + restServer.start(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("ApiRestServer [servers=")); + restServer.shutdown(); + } catch (final Exception exp) { + LOGGER.error("testHealthCheckFailure failed", exp); + fail("Test should not throw an exception"); + } } private Main startApiService() { @@ -84,8 +99,7 @@ public class TestApiRestServer { main.shutdown(); } - private HealthCheckReport performHealthCheck() throws InterruptedException { - HealthCheckReport response = null; + private HealthCheckReport performHealthCheck() throws InterruptedException, IOException { final ClientConfig clientConfig = new ClientConfig(); final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); @@ -95,14 +109,11 @@ public class TestApiRestServer { final WebTarget webTarget = client.target("http://localhost:6969/healthcheck"); final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); - while (response == null) { - try { - response = invocationBuilder.get(HealthCheckReport.class); - } catch (final Exception exp) { - LOGGER.info("the server is not started yet. We will retry again"); - } + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); } - return response; + return invocationBuilder.get(HealthCheckReport.class); } private void validateReport(final String name, final String url, final boolean healthy, final int code, diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatistics.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatistics.java new file mode 100644 index 00000000..5d535678 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatistics.java @@ -0,0 +1,152 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +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.utils.network.NetworkUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to perform unit test of {@link ApiRestController}. + */ +public class TestApiStatistics { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestApiStatistics.class); + + @Test + public void testApiStatistics_200() throws PolicyApiException, InterruptedException { + try { + final Main main = startApiService(); + StatisticsReport report = getApiStatistics(); + validateReport(report, 0, 200); + updateApiStatistics(); + report = getApiStatistics(); + validateReport(report, 1, 200); + stopApiService(main); + ApiStatisticsManager.resetAllStatistics(); + } catch (final Exception exp) { + LOGGER.error("testApiStatistics_200 failed", exp); + fail("Test should not throw an exception"); + } + } + + @Test + public void testApiStatistics_500() throws InterruptedException { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.API_GROUP_NAME); + + final ApiRestServer restServer = new ApiRestServer(restServerParams); + try { + restServer.start(); + final StatisticsReport report = getApiStatistics(); + validateReport(report, 0, 500); + restServer.shutdown(); + ApiStatisticsManager.resetAllStatistics(); + } catch (final Exception exp) { + LOGGER.error("testApiStatistics_500 failed", exp); + fail("Test should not throw an exception"); + } + } + + + private Main startApiService() { + final String[] distributionConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; + return new Main(distributionConfigParameters); + } + + private void stopApiService(final Main main) throws PolicyApiException { + main.shutdown(); + } + + private StatisticsReport getApiStatistics() throws InterruptedException, IOException { + final ClientConfig clientConfig = new ClientConfig(); + + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + clientConfig.register(feature); + + final Client client = ClientBuilder.newClient(clientConfig); + final WebTarget webTarget = client.target("http://localhost:6969/statistics"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); + } + return invocationBuilder.get(StatisticsReport.class); + } + + private void updateApiStatistics() { + ApiStatisticsManager.updateTotalApiCallCount(); + ApiStatisticsManager.updateApiCallSuccessCount(); + ApiStatisticsManager.updateApiCallFailureCount(); + ApiStatisticsManager.updateTotalPolicyGetCount(); + ApiStatisticsManager.updateTotalPolicyPostCount(); + ApiStatisticsManager.updateTotalTemplateGetCount(); + ApiStatisticsManager.updateTotalTemplatePostCount(); + ApiStatisticsManager.updatePolicyGetSuccessCount(); + ApiStatisticsManager.updatePolicyGetFailureCount(); + ApiStatisticsManager.updatePolicyPostSuccessCount(); + ApiStatisticsManager.updatePolicyPostFailureCount(); + ApiStatisticsManager.updateTemplateGetSuccessCount(); + ApiStatisticsManager.updateTemplateGetFailureCount(); + ApiStatisticsManager.updateTemplatePostSuccessCount(); + ApiStatisticsManager.updateTemplatePostFailureCount(); + } + + private void validateReport(final StatisticsReport report, final int count, final int code) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalApiCallCount()); + assertEquals(count, report.getApiCallSuccessCount()); + assertEquals(count, report.getApiCallFailureCount()); + assertEquals(count, report.getTotalPolicyGetCount()); + assertEquals(count, report.getTotalPolicyPostCount()); + assertEquals(count, report.getTotalTemplateGetCount()); + assertEquals(count, report.getTotalTemplatePostCount()); + assertEquals(count, report.getPolicyGetSuccessCount()); + assertEquals(count, report.getPolicyGetFailureCount()); + assertEquals(count, report.getPolicyPostSuccessCount()); + assertEquals(count, report.getPolicyPostFailureCount()); + assertEquals(count, report.getTemplateGetSuccessCount()); + assertEquals(count, report.getTemplateGetFailureCount()); + assertEquals(count, report.getTemplatePostSuccessCount()); + assertEquals(count, report.getTemplatePostFailureCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsApiRestServer.java new file mode 100644 index 00000000..d080350a --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsApiRestServer.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +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; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Test; +import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.startstop.Main; +import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to perform unit test of https set on API REST server. + */ +public class TestHttpsApiRestServer { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsApiRestServer.class); + private static final String ALIVE = "alive"; + private static final String SELF = "self"; + private static final String NAME = "Policy API"; + private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + + @Test + public void testHttpsHealthCheckSuccess() { + final String reportString = "Report [name=Policy API, url=self, healthy=true, code=200, message=alive]"; + try { + final Main main = startApiService(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); + stopApiService(main); + } catch (final Exception exp) { + LOGGER.error("testHttpsHealthCheckSuccess failed", exp); + fail("Test should not throw an exception"); + } + } + + private Main startApiService() { + final Properties systemProps = System.getProperties(); + systemProps.put("javax.net.ssl.keyStore", KEYSTORE); + systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); + System.setProperties(systemProps); + + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_Https.json" }; + return new Main(apiConfigParameters); + } + + private void stopApiService(final Main main) throws PolicyApiException { + main.shutdown(); + } + + private HealthCheckReport performHealthCheck() throws Exception { + + final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + + @Override + public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { + } + + @Override + public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { + } + } }; + + final SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, noopTrustManager, new SecureRandom()); + final ClientBuilder clientBuilder = + ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); + final Client client = clientBuilder.build(); + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + client.register(feature); + + final WebTarget webTarget = client.target("https://localhost:6969/healthcheck"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); + } + return invocationBuilder.get(HealthCheckReport.class); + } + + private void validateReport(final String name, final String url, final boolean healthy, final int code, + final String message, final String reportString, final HealthCheckReport report) { + assertEquals(name, report.getName()); + assertEquals(url, report.getUrl()); + assertEquals(healthy, report.isHealthy()); + assertEquals(code, report.getCode()); + assertEquals(message, report.getMessage()); + assertEquals(reportString, report.toString()); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsStatisticApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsStatisticApiRestServer.java new file mode 100644 index 00000000..6b992442 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestHttpsStatisticApiRestServer.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +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; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Test; +import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.startstop.Main; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to perform unit test of API statistics via https set on API REST server. + */ +public class TestHttpsStatisticApiRestServer { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestHttpsStatisticApiRestServer.class); + private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + + @Test + public void testHttpsApiStatistic() + throws PolicyApiException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { + final String reportString = "StatisticsReport [code=200, totalApiCallCount=0, apiCallSuccessCount=0, " + + "apiCallFailureCount=0, " + "totalPolicyGetCount=0, totalPolicyPostCount=0, " + + "totalTemplateGetCount=0, totalTemplatePostCount=0, " + + "policyGetSuccessCount=0, policyGetFailureCount=0, " + + "policyPostSuccessCount=0, policyPostFailureCount=0, " + + "templateGetSuccessCount=0, templateGetFailureCount=0, " + + "templatePostSuccessCount=0, templatePostFailureCount=0]"; + try { + final Main main = startApiService(); + final StatisticsReport report = performStatisticCheck(); + validateReport(200, 0, reportString, report); + stopApiService(main); + } catch (final Exception exp) { + LOGGER.error("testHttpsApiStatistic failed", exp); + fail("Test should not throw an exception"); + } + } + + private Main startApiService() { + final Properties systemProps = System.getProperties(); + systemProps.put("javax.net.ssl.keyStore", KEYSTORE); + systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); + System.setProperties(systemProps); + + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_Https.json" }; + return new Main(apiConfigParameters); + } + + private void stopApiService(final Main main) throws PolicyApiException { + main.shutdown(); + } + + private StatisticsReport performStatisticCheck() throws Exception { + + final TrustManager[] noopTrustManager = new TrustManager[] { new X509TrustManager() { + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + + @Override + public void checkClientTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { + } + + @Override + public void checkServerTrusted(final java.security.cert.X509Certificate[] certs, final String authType) { + } + } }; + + final SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, noopTrustManager, new SecureRandom()); + final ClientBuilder clientBuilder = + ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); + final Client client = clientBuilder.build(); + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + client.register(feature); + + final WebTarget webTarget = client.target("https://localhost:6969/statistics"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("cannot connect to port 6969"); + } + return invocationBuilder.get(StatisticsReport.class); + } + + private void validateReport(final int code, final int count, + final String reportString, final StatisticsReport report) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalApiCallCount()); + assertEquals(count, report.getApiCallSuccessCount()); + assertEquals(count, report.getApiCallFailureCount()); + assertEquals(count, report.getTotalPolicyGetCount()); + assertEquals(count, report.getTotalPolicyPostCount()); + assertEquals(count, report.getTotalTemplateGetCount()); + assertEquals(count, report.getTotalTemplatePostCount()); + assertEquals(count, report.getPolicyGetSuccessCount()); + assertEquals(count, report.getPolicyGetFailureCount()); + assertEquals(count, report.getPolicyPostSuccessCount()); + assertEquals(count, report.getPolicyPostFailureCount()); + assertEquals(count, report.getTemplateGetSuccessCount()); + assertEquals(count, report.getTemplateGetFailureCount()); + assertEquals(count, report.getTemplatePostSuccessCount()); + assertEquals(count, report.getTemplatePostFailureCount()); + assertEquals(reportString, report.toString()); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestStatisticsReport.java b/main/src/test/java/org/onap/policy/api/main/rest/TestStatisticsReport.java new file mode 100644 index 00000000..75b89c15 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestStatisticsReport.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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 com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of {@link StatisticsReport}. + */ +public class TestStatisticsReport { + + @Test + public void testStatisticsReport() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterMustExistRule()) + .with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(StatisticsReport.class.getPackage().getName(), + new FilterClassName(StatisticsReport.class.getName())); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java index e5ab8101..01cd77c6 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * 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. @@ -24,7 +25,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.onap.policy.api.main.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.api.main.parameters.ApiParameterHandler; import org.onap.policy.api.main.parameters.CommonTestData; @@ -38,14 +39,9 @@ public class TestApiActivator { @Test public void testApiActivator() throws PolicyApiException { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters.json" }; - - final ApiCommandLineArguments arguments = - new ApiCommandLineArguments(apiConfigParameters); - + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; + final ApiCommandLineArguments arguments = new ApiCommandLineArguments(apiConfigParameters); final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments); - final ApiActivator activator = new ApiActivator(parGroup); activator.initialize(); assertTrue(activator.getParameterGroup().isValid()); diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java index 47725349..2f68e852 100644 --- a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * 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. @@ -24,7 +25,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.onap.policy.api.main.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.parameters.CommonTestData; /** @@ -35,8 +36,7 @@ public class TestMain { @Test public void testMain() throws PolicyApiException { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters.json" }; + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" }; final Main main = new Main(apiConfigParameters); assertTrue(main.getParameters().isValid()); assertEquals(CommonTestData.API_GROUP_NAME, main.getParameters().getName()); @@ -45,31 +45,27 @@ public class TestMain { @Test public void testMain_NoArguments() { - final String[] apiConfigParameters = - {}; + final String[] apiConfigParameters = {}; final Main main = new Main(apiConfigParameters); assertTrue(main.getParameters() == null); } @Test public void testMain_InvalidArguments() { - final String[] apiConfigParameters = - { "parameters/ApiConfigParameters.json" }; + final String[] apiConfigParameters = { "parameters/ApiConfigParameters.json" }; final Main main = new Main(apiConfigParameters); assertTrue(main.getParameters() == null); } @Test public void testMain_Help() { - final String[] apiConfigParameters = - { "-h" }; + final String[] apiConfigParameters = { "-h" }; Main.main(apiConfigParameters); } @Test public void testMain_InvalidParameters() { - final String[] apiConfigParameters = - { "-c", "parameters/ApiConfigParameters_InvalidName.json" }; + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_InvalidName.json" }; final Main main = new Main(apiConfigParameters); assertTrue(main.getParameters() == null); } diff --git a/main/src/test/resources/parameters/ApiConfigParameters_Https.json b/main/src/test/resources/parameters/ApiConfigParameters_Https.json new file mode 100644 index 00000000..ec732132 --- /dev/null +++ b/main/src/test/resources/parameters/ApiConfigParameters_Https.json @@ -0,0 +1,10 @@ +{ + "name":"ApiGroup", + "restServerParameters":{ + "host":"0.0.0.0", + "port":6969, + "userName":"healthcheck", + "password":"zb!XztG34", + "https":true + } +} diff --git a/main/src/test/resources/ssl/policy-keystore b/main/src/test/resources/ssl/policy-keystore Binary files differnew file mode 100644 index 00000000..7d2b1ecc --- /dev/null +++ b/main/src/test/resources/ssl/policy-keystore |