From f287f0fe68eea6da03a46f98e2504f21bfcc8e3e Mon Sep 17 00:00:00 2001 From: Vlad Arama Date: Mon, 1 Apr 2024 10:57:32 -0400 Subject: [PATCH] trace-server: implement `IdentifierService` Signed-off-by: Vlad Arama --- .../tests/services/IdentifierServiceTest.java | 54 +++++ .../stubs/webapp/TestWebApplication.java | 2 + .../rest/core/model/ServerInfoResponse.java | 76 +++++++ .../rest/core/services/EndpointConstants.java | 1 + .../rest/core/services/IdentifierService.java | 87 ++++++++ .../core/services/ServerInfoResponseImpl.java | 187 ++++++++++++++++++ .../services/TraceServerOpenApiResource.java | 2 + .../rest/core/webapp/WebApplication.java | 2 + 8 files changed, 411 insertions(+) create mode 100644 trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/services/IdentifierServiceTest.java create mode 100644 trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/model/ServerInfoResponse.java create mode 100644 trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/IdentifierService.java create mode 100644 trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/ServerInfoResponseImpl.java diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/services/IdentifierServiceTest.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/services/IdentifierServiceTest.java new file mode 100644 index 000000000..2705544d3 --- /dev/null +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/services/IdentifierServiceTest.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2024 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License 2.0 which + * accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ + +package org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.services; + +import static org.junit.Assert.assertNotNull; + +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.IdentifierService; +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.ServerInfoResponseImpl; +import org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests.utils.RestServerTest; +import org.junit.Test; + +/** + * Test the {@link IdentifierService} + * + * @author Vlad Arama + */ +public class IdentifierServiceTest extends RestServerTest { + + /** + * Test basic operations on the Identifier Service + */ + @Test + public void testIdentifier() { + WebTarget application = getApplicationEndpoint(); + WebTarget identifierEndpoint = application.path("identifier"); + + Response response = identifierEndpoint.request(MediaType.APPLICATION_JSON) + .get(); + ServerInfoResponseImpl responseValues = response.readEntity(ServerInfoResponseImpl.class); + + assertNotNull("Server version should not be null", responseValues.getVersion()); + assertNotNull("OS should not be null", responseValues.getOs()); + assertNotNull("OS Architecture should not be null", responseValues.getOsArch()); + assertNotNull("OS Version should not be null", responseValues.getOsVersion()); + assertNotNull("CPU count should not be null", responseValues.getCpuCount()); + assertNotNull("Max memory should not be null", responseValues.getMaxMemory()); + assertNotNull("Product ID should not be null", responseValues.getProductId()); + + } + +} diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/stubs/webapp/TestWebApplication.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/stubs/webapp/TestWebApplication.java index 17e6837d2..0a5e92773 100644 --- a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/stubs/webapp/TestWebApplication.java +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core.tests/src/org/eclipse/tracecompass/incubator/trace/server/jersey/rest/core/tests/stubs/webapp/TestWebApplication.java @@ -15,6 +15,7 @@ import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.ExperimentManagerService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.FilterService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.HealthService; +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.IdentifierService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.TraceManagerService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.CORSFilter; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.webapp.JacksonObjectMapperProvider; @@ -46,6 +47,7 @@ protected void registerResourcesAndMappers(ResourceConfig rc) { rc.register(TestDataProviderService.class); rc.register(FilterService.class); rc.register(HealthService.class); + rc.register(IdentifierService.class); rc.register(ConfigurationManagerService.class); rc.register(CORSFilter.class); rc.register(JacksonObjectMapperProvider.class); diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/model/ServerInfoResponse.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/model/ServerInfoResponse.java new file mode 100644 index 000000000..15abdfe6d --- /dev/null +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/model/ServerInfoResponse.java @@ -0,0 +1,76 @@ +/********************************************************************** + * Copyright (c) 2024 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License 2.0 which + * accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + **********************************************************************/ +package org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * Contributes to the model used for TSP swagger-core annotations. + * + * @author Vlad Arama + */ +@Schema(description = "System Information Response") +public interface ServerInfoResponse { + + /** + * @return Version in the format Major.Minor.Micro + */ + @Schema(required = true, description = "Version in the format Major.Minor.Micro") + String getVersion(); + + /** + * @return Build time or qualifier of the server version, if available + */ + @Schema(description = "Build time or qualifier of the server version, if available") + String getBuildTime(); + + /** + * @return Operating system name + */ + @Schema(required = true, description = "Operating system name") + String getOs(); + + /** + * @return Architecture of the operating system + */ + @Schema(description = "Architecture of the operating system") + String getOsArch(); + + /** + * @return Operating system version + */ + @Schema(description = "Operating system version") + String getOsVersion(); + + /** + * @return Number of CPUs available + */ + @Schema(description = "Number of CPUs available") + int getCpuCount(); + + /** + * @return Maximum memory available to the JVM in bytes + */ + @Schema(description = "Maximum memory available to the JVM in bytes") + long getMaxMemory(); + + /** + * @return Name of the launcher used, if available + */ + @Schema(description = "Name of the launcher used, if available") + String getLauncherName(); + + /** + * @return Product identifier for the trace server + */ + @Schema(required = true, description = "Product identifier for the trace server") + String getProductId(); +} diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/EndpointConstants.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/EndpointConstants.java index e14505411..870b93717 100644 --- a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/EndpointConstants.java +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/EndpointConstants.java @@ -83,6 +83,7 @@ public final class EndpointConstants { static final String DIA = "Diagnostic"; //$NON-NLS-1$ static final String DT = "Data Tree"; //$NON-NLS-1$ static final String EXP = "Experiments"; //$NON-NLS-1$ + static final String IDF = "Identifier"; //$NON-NLS-1$ static final String STY = "Styles"; //$NON-NLS-1$ static final String TGR = "TimeGraph"; //$NON-NLS-1$ static final String TRA = "Traces"; //$NON-NLS-1$ diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/IdentifierService.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/IdentifierService.java new file mode 100644 index 000000000..1634cc056 --- /dev/null +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/IdentifierService.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2024 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License 2.0 which + * accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services; + +import org.eclipse.core.runtime.IProduct; +import org.eclipse.core.runtime.Platform; +import org.osgi.framework.Version; +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.model.ServerInfoResponse; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; + +/** + * + * Service to identify important information regarding the trace server and the + * system it is running on. + * + * @author Vlad Arama + * + */ +@Path("/identifier") +@Tag(name = EndpointConstants.IDF) +public class IdentifierService { + private static final String OS_NAME = "os.name"; //$NON-NLS-1$ + private static final String OS_ARCH = "os.arch"; //$NON-NLS-1$ + private static final String OS_VERSION = "os.version"; //$NON-NLS-1$ + private static final String PRODUCT_ID = "eclipse.product"; //$NON-NLS-1$ + private static final String LAUNCHER_NAME = "eclipse.launcher.name"; //$NON-NLS-1$ + private static final String QUALIFIER = "qualifier"; //$NON-NLS-1$ + private static final String SEPARATOR = "."; //$NON-NLS-1$ + private static final String HARD_CODED_VERSION = "0.8.1"; //$NON-NLS-1$ + + /** + * Getter returning important information about the system, including server + * version, OS, CPU count, memory size, launcher name and product id. + * + * @return A JSON response containing the system's details. + */ + @GET + @Produces(MediaType.APPLICATION_JSON) + @Operation(summary = "Retrieves system and server information", responses = { + @ApiResponse(responseCode = "200", description = "Successfully retrieved the system and server information", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ServerInfoResponse.class))) + }) + public Response getSystemInfo() { + IProduct product = Platform.getProduct(); + ServerInfoResponseImpl response = new ServerInfoResponseImpl(); + + response.setOs(System.getProperty(OS_NAME)); + response.setOsArch(System.getProperty(OS_ARCH)); + response.setOsVersion(System.getProperty(OS_VERSION)); + response.setCpuCount(Runtime.getRuntime().availableProcessors()); + response.setMaxMemory(Runtime.getRuntime().maxMemory()); + response.setProductId(System.getProperty(PRODUCT_ID)); + response.setLauncherName(System.getProperty(LAUNCHER_NAME)); + + if (product != null) { + Version version = product.getDefiningBundle().getVersion(); + response.setVersion(version.getMajor() + SEPARATOR + version.getMicro() + SEPARATOR + version.getMinor()); + String qualifier = version.getQualifier(); + if (!QUALIFIER.equalsIgnoreCase(qualifier) && qualifier != null) { + response.setBuildTime(qualifier); + } + } else { + response.setVersion(HARD_CODED_VERSION); + } + + return Response.ok(response).build(); + } + +} diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/ServerInfoResponseImpl.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/ServerInfoResponseImpl.java new file mode 100644 index 000000000..742362fbd --- /dev/null +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/ServerInfoResponseImpl.java @@ -0,0 +1,187 @@ +/******************************************************************************* + * Copyright (c) 2024 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License 2.0 which + * accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ + +package org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services; + +import com.fasterxml.jackson.annotation.JsonInclude; + +/** + * Implementation of the ServerInfoResponse interface + * + * @author Vlad Arama + */ +public class ServerInfoResponseImpl { + private String version; + @JsonInclude(JsonInclude.Include.NON_NULL) // Makes build time optional + private String buildTime; + private String os; + private String osArch; + private String osVersion; + private int cpuCount; + private long maxMemory; + @JsonInclude(JsonInclude.Include.NON_NULL) // Makes launcher name optional + private String launcherName; + private String productId; + + /** + * @return Version in the format Major.Minor.Micro + */ + public String getVersion() { + return version; + } + + /** + * @return Build time or qualifier of the server version, if available + */ + public String getBuildTime() { + return buildTime; + } + + /** + * @return Operating system name + */ + public String getOs() { + return os; + } + + /** + * @return Architecture of the operating system + */ + public String getOsArch() { + return osArch; + } + + /** + * @return Operating system version + */ + public String getOsVersion() { + return osVersion; + } + + /** + * @return Number of CPUs available + */ + public int getCpuCount() { + return cpuCount; + } + + /** + * @return Maximum memory available to the JVM in bytes + */ + public long getMaxMemory() { + return maxMemory; + } + + /** + * @return Name of the launcher used, if available + */ + public String getLauncherName() { + return launcherName; + } + + /** + * @return Product identifier for the software + */ + public String getProductId() { + return productId; + } + + /** + * Set Version + * + * @param version + * Version of the server + */ + public void setVersion(String version) { + this.version = version; + } + + /** + * Set Build Time + * + * @param buildTime + * Time of build + */ + public void setBuildTime(String buildTime) { + this.buildTime = buildTime; + } + + /** + * Set OS + * + * @param os + * Operating System + */ + public void setOs(String os) { + this.os = os; + } + + /** + * Set OS Architecture + * + * @param osArch + * Architecture of the OS + */ + public void setOsArch(String osArch) { + this.osArch = osArch; + } + + /** + * Set OS Version + * + * @param osVersion + * Version of the OS + */ + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + /** + * Set CPU Count + * + * @param cpuCount + * Number of CPUs + */ + public void setCpuCount(int cpuCount) { + this.cpuCount = cpuCount; + } + + /** + * Set Max Memory + * + * @param maxMemory + * Maximum memory available + */ + public void setMaxMemory(long maxMemory) { + this.maxMemory = maxMemory; + } + + /** + * Set Launcher Name + * + * @param launcherName + * Name of the launcher + */ + public void setLauncherName(String launcherName) { + this.launcherName = launcherName; + } + + /** + * Set Product ID + * + * @param productId + * Product ID + */ + public void setProductId(String productId) { + this.productId = productId; + } + +} diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/TraceServerOpenApiResource.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/TraceServerOpenApiResource.java index 329aa24f6..215e32d98 100644 --- a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/TraceServerOpenApiResource.java +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/services/TraceServerOpenApiResource.java @@ -18,6 +18,7 @@ import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.DT; import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.EMAIL; import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.EXP; +import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.IDF; import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.LICENSE; import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.LICENSE_URL; import static org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.EndpointConstants.SERVER; @@ -50,6 +51,7 @@ @Tag(name = DIA, description = "Retrieve the server's status."), @Tag(name = DT, description = "Query data tree models (e.g. for statistics)."), @Tag(name = EXP, description = "Manage experiments on your server; an experiment represents a collection of traces, which can produce output models."), + @Tag(name = IDF, description = "Retrieve information about the server and the system it is running on."), @Tag(name = STY, description = "Retrieve styles for different outputs."), @Tag(name = TGR, description = "Query Time Graph models."), @Tag(name = TRA, description = "Manage physical traces on your server."), diff --git a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/webapp/WebApplication.java b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/webapp/WebApplication.java index f6a82f751..7a901855e 100644 --- a/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/webapp/WebApplication.java +++ b/trace-server/org.eclipse.tracecompass.incubator.trace.server.jersey.rest.core/src/org/eclipse/tracecompass/incubator/internal/trace/server/jersey/rest/core/webapp/WebApplication.java @@ -31,6 +31,7 @@ import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.ExperimentManagerService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.FilterService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.HealthService; +import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.IdentifierService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.TraceManagerService; import org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services.TraceServerOpenApiResource; import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; @@ -137,6 +138,7 @@ protected void registerResourcesAndMappers(ResourceConfig rc) { rc.register(DataProviderService.class); rc.register(FilterService.class); rc.register(HealthService.class); + rc.register(IdentifierService.class); rc.register(CORSFilter.class); rc.register(JacksonObjectMapperProvider.class); EncodingFilter.enableFor(rc, GZipEncoder.class);