From 6ace00d3f867fdcfbd412c207765d26d78b26c74 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 7 Feb 2023 14:15:28 +0100 Subject: Use switch expression --- .../controller/restapi/billing/CsvResponse.java | 5 +++++ .../ChangeManagementApiHandler.java | 19 +++++++----------- .../configserver/ConfigServerApiHandler.java | 16 +++++---------- .../restapi/deployment/BadgeApiHandler.java | 8 ++++---- .../controller/restapi/deployment/Badges.java | 23 ++++++++++------------ .../restapi/deployment/CliApiHandler.java | 8 ++++---- 6 files changed, 35 insertions(+), 44 deletions(-) (limited to 'controller-server/src') diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/CsvResponse.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/CsvResponse.java index 3643626761d..e97a51e58a2 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/CsvResponse.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/CsvResponse.java @@ -9,7 +9,11 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.List; +/** + * @author ogronnesby + */ class CsvResponse extends HttpResponse { + private final String[] header; private final List rows; @@ -31,4 +35,5 @@ class CsvResponse extends HttpResponse { public String getContentType() { return "text/csv; encoding=utf-8"; } + } diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/changemanagement/ChangeManagementApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/changemanagement/ChangeManagementApiHandler.java index 5148577880f..6d19316efd7 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/changemanagement/ChangeManagementApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/changemanagement/ChangeManagementApiHandler.java @@ -46,18 +46,13 @@ public class ChangeManagementApiHandler extends AuditLoggingRequestHandler { @Override public HttpResponse auditAndHandle(HttpRequest request) { try { - switch (request.getMethod()) { - case GET: - return get(request); - case POST: - return post(request); - case PATCH: - return patch(request); - case DELETE: - return delete(request); - default: - return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is unsupported"); - } + return switch (request.getMethod()) { + case GET -> get(request); + case POST -> post(request); + case PATCH -> patch(request); + case DELETE -> delete(request); + default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is unsupported"); + }; } catch (IllegalArgumentException e) { return ErrorResponse.badRequest(Exceptions.toMessageString(e)); } catch (RuntimeException e) { diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/configserver/ConfigServerApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/configserver/ConfigServerApiHandler.java index e764fed4653..3c832dc8873 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/configserver/ConfigServerApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/configserver/ConfigServerApiHandler.java @@ -55,17 +55,11 @@ public class ConfigServerApiHandler extends AuditLoggingRequestHandler { @Override public HttpResponse auditAndHandle(HttpRequest request) { try { - switch (request.getMethod()) { - case GET: - return get(request); - case POST: - case PUT: - case DELETE: - case PATCH: - return proxy(request); - default: - return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is unsupported"); - } + return switch (request.getMethod()) { + case GET -> get(request); + case POST, PUT, DELETE, PATCH -> proxy(request); + default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is unsupported"); + }; } catch (IllegalArgumentException e) { return ErrorResponse.badRequest(Exceptions.toMessageString(e)); } catch (RuntimeException e) { diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java index 74a28276c79..c6eaf5abef7 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java @@ -52,10 +52,10 @@ public class BadgeApiHandler extends ThreadedHttpRequestHandler { public HttpResponse handle(HttpRequest request) { Method method = request.getMethod(); try { - switch (method) { - case GET: return get(request); - default: return ErrorResponse.methodNotAllowed("Method '" + method + "' is unsupported"); - } + return switch (method) { + case GET -> get(request); + default -> ErrorResponse.methodNotAllowed("Method '" + method + "' is unsupported"); + }; } catch (IllegalArgumentException|IllegalStateException e) { return ErrorResponse.badRequest(Exceptions.toMessageString(e)); } catch (RuntimeException e) { diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/Badges.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/Badges.java index eed12f61a76..814241a765c 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/Badges.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/Badges.java @@ -2,7 +2,6 @@ package com.yahoo.vespa.hosted.controller.restapi.deployment; import com.yahoo.config.provision.ApplicationId; -import com.yahoo.config.provision.SystemName; import com.yahoo.slime.ArrayTraverser; import com.yahoo.slime.SlimeUtils; import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType; @@ -51,18 +50,16 @@ public class Badges { } static String colorOf(Run run, Optional previous) { - switch (run.status()) { - case running: switch (previous.orElse(RunStatus.success)) { - case success: return "url(#run-on-success)"; - case aborted: - case noTests: return "url(#run-on-warning)"; - default: return "url(#run-on-failure)"; - } - case success: return success; - case aborted: - case noTests: return warning; - default: return failure; - } + return switch (run.status()) { + case running -> switch (previous.orElse(RunStatus.success)) { + case success -> "url(#run-on-success)"; + case aborted, noTests -> "url(#run-on-warning)"; + default -> "url(#run-on-failure)"; + }; + case success -> success; + case aborted, noTests -> warning; + default -> failure; + }; } static String nameOf(JobType type) { diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java index 336352e931a..c67d0d04938 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/CliApiHandler.java @@ -34,10 +34,10 @@ public class CliApiHandler extends ThreadedHttpRequestHandler { @Override public HttpResponse handle(HttpRequest request) { try { - switch (request.getMethod()) { - case GET: return get(request); - default: return ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported"); - } + return switch (request.getMethod()) { + case GET -> get(request); + default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported"); + }; } catch (IllegalArgumentException e) { return ErrorResponse.badRequest(Exceptions.toMessageString(e)); -- cgit v1.2.3 From cc4f287050c918e06ce7d0a0e9c4b729c6b99fcd Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 7 Feb 2023 14:15:49 +0100 Subject: Remove unused field --- .../vespa/hosted/controller/restapi/filter/AthenzRoleFilter.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'controller-server/src') diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/AthenzRoleFilter.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/AthenzRoleFilter.java index e23f0205e5a..c25502ab9bf 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/AthenzRoleFilter.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/filter/AthenzRoleFilter.java @@ -6,7 +6,6 @@ import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.Payload; import com.yahoo.component.annotation.Inject; import com.yahoo.config.provision.ApplicationName; -import com.yahoo.config.provision.SystemName; import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.zone.ZoneId; import com.yahoo.jdisc.Response; @@ -33,7 +32,6 @@ import com.yahoo.vespa.hosted.controller.tenant.Tenant; import com.yahoo.yolean.Exceptions; import java.net.URI; -import java.security.Principal; import java.security.cert.X509Certificate; import java.time.Instant; import java.util.ArrayList; @@ -64,7 +62,6 @@ public class AthenzRoleFilter extends JsonSecurityRequestFilterBase { private final AthenzFacade athenz; private final TenantController tenants; private final ExecutorService executor; - private final SystemName systemName; private final ZoneRegistry zones; @Inject @@ -72,7 +69,6 @@ public class AthenzRoleFilter extends JsonSecurityRequestFilterBase { this.athenz = new AthenzFacade(athenzClientFactory); this.tenants = controller.tenants(); this.executor = Executors.newCachedThreadPool(); - this.systemName = controller.system(); this.zones = controller.zoneRegistry(); } -- cgit v1.2.3 From aecea8e8755fd6d367a6d7df57b341314a583864 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 7 Feb 2023 14:22:47 +0100 Subject: Remove duplicated class --- container-core/abi-spec.json | 1 + .../com/yahoo/container/jdisc/EmptyResponse.java | 5 +++++ .../restapi/application/ApplicationApiHandler.java | 2 +- .../restapi/application/EmptyResponse.java | 20 -------------------- .../restapi/deployment/DeploymentApiHandler.java | 2 +- .../controller/restapi/user/UserApiHandler.java | 2 +- 6 files changed, 9 insertions(+), 23 deletions(-) delete mode 100644 controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/EmptyResponse.java (limited to 'controller-server/src') diff --git a/container-core/abi-spec.json b/container-core/abi-spec.json index 5f86db1d385..3d5b9e8d59e 100644 --- a/container-core/abi-spec.json +++ b/container-core/abi-spec.json @@ -538,6 +538,7 @@ ], "methods" : [ "public void (int)", + "public void ()", "public void render(java.io.OutputStream)" ], "fields" : [ ] diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/EmptyResponse.java b/container-core/src/main/java/com/yahoo/container/jdisc/EmptyResponse.java index 4477b7319b8..4d207d7afaf 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/EmptyResponse.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/EmptyResponse.java @@ -15,6 +15,11 @@ public class EmptyResponse extends HttpResponse { super(status); } + public EmptyResponse() { + this(200); + } + + @Override public void render(OutputStream outputStream) throws IOException { // NOP } diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java index bc7e3f0e7d0..54d219a2a6d 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/ApplicationApiHandler.java @@ -29,6 +29,7 @@ import com.yahoo.config.provision.ZoneEndpoint.AllowedUrn; import com.yahoo.config.provision.zone.RoutingMethod; import com.yahoo.config.provision.zone.ZoneId; import com.yahoo.container.handler.metrics.JsonResponse; +import com.yahoo.container.jdisc.EmptyResponse; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; @@ -50,7 +51,6 @@ import com.yahoo.slime.SlimeUtils; import com.yahoo.text.Text; import com.yahoo.vespa.athenz.api.OAuthCredentials; import com.yahoo.vespa.athenz.client.zms.ZmsClientException; -import com.yahoo.vespa.flags.Flags; import com.yahoo.vespa.hosted.controller.Application; import com.yahoo.vespa.hosted.controller.Controller; import com.yahoo.vespa.hosted.controller.Instance; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/EmptyResponse.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/EmptyResponse.java deleted file mode 100644 index dbd4250669c..00000000000 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/EmptyResponse.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.controller.restapi.application; - -import com.yahoo.container.jdisc.HttpResponse; - -import java.io.OutputStream; - -/** - * @author bratseth - */ -public class EmptyResponse extends HttpResponse { - - public EmptyResponse() { - super(200); - } - - @Override - public void render(OutputStream stream) {} - -} diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java index 6946efdbe2a..759b2366229 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/DeploymentApiHandler.java @@ -5,6 +5,7 @@ import com.yahoo.component.Version; import com.yahoo.config.application.api.DeploymentInstanceSpec; import com.yahoo.config.application.api.DeploymentSpec; import com.yahoo.config.provision.ApplicationId; +import com.yahoo.container.jdisc.EmptyResponse; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; @@ -25,7 +26,6 @@ import com.yahoo.vespa.hosted.controller.deployment.Run; import com.yahoo.vespa.hosted.controller.deployment.RunStatus; import com.yahoo.vespa.hosted.controller.deployment.Versions; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses; -import com.yahoo.vespa.hosted.controller.restapi.application.EmptyResponse; import com.yahoo.vespa.hosted.controller.versions.DeploymentStatistics; import com.yahoo.vespa.hosted.controller.versions.VespaVersion; import com.yahoo.yolean.Exceptions; diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/user/UserApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/user/UserApiHandler.java index c2e926f777b..a9787e28113 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/user/UserApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/user/UserApiHandler.java @@ -4,6 +4,7 @@ package com.yahoo.vespa.hosted.controller.restapi.user; import com.yahoo.component.annotation.Inject; import com.yahoo.config.provision.ApplicationName; import com.yahoo.config.provision.TenantName; +import com.yahoo.container.jdisc.EmptyResponse; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; @@ -37,7 +38,6 @@ import com.yahoo.vespa.hosted.controller.api.role.SimplePrincipal; import com.yahoo.vespa.hosted.controller.api.role.TenantRole; import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId; import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses; -import com.yahoo.vespa.hosted.controller.restapi.application.EmptyResponse; import com.yahoo.vespa.hosted.controller.tenant.Tenant; import com.yahoo.yolean.Exceptions; -- cgit v1.2.3 From d701848461f719eba635917199725b3b228fa7bc Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 7 Feb 2023 14:24:12 +0100 Subject: Convert to records --- .../notification/FormattedNotification.java | 33 +++--------- .../controller/notification/Notification.java | 62 +++++++--------------- 2 files changed, 26 insertions(+), 69 deletions(-) (limited to 'controller-server/src') diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/FormattedNotification.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/FormattedNotification.java index 8a6243a7224..5e36d6d6499 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/FormattedNotification.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/FormattedNotification.java @@ -9,32 +9,13 @@ import java.util.Objects; * * @author enygaard */ -public class FormattedNotification { - private final String prettyType; - private final String messagePrefix; - private final URI uri; - private final Notification notification; +public record FormattedNotification(Notification notification, String prettyType, String messagePrefix, URI uri) { - public FormattedNotification(Notification notification, String prettyType, String messagePrefix, URI uri) { - this.prettyType = Objects.requireNonNull(prettyType); - this.messagePrefix = Objects.requireNonNull(messagePrefix); - this.uri = Objects.requireNonNull(uri); - this.notification = Objects.requireNonNull(notification); + public FormattedNotification { + Objects.requireNonNull(prettyType); + Objects.requireNonNull(messagePrefix); + Objects.requireNonNull(uri); + Objects.requireNonNull(notification); } - public String prettyType() { - return prettyType; - } - - public String messagePrefix() { - return messagePrefix; - } - - public URI uri() { - return uri; - } - - public Notification notification() { - return notification; - } -} \ No newline at end of file +} diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/Notification.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/Notification.java index da423995b7e..53450783c8e 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/Notification.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/Notification.java @@ -13,12 +13,7 @@ import java.util.Objects; * * @author freva */ -public class Notification { - private final Instant at; - private final Type type; - private final Level level; - private final NotificationSource source; - private final List messages; +public record Notification(Instant at, com.yahoo.vespa.hosted.controller.notification.Notification.Type type, com.yahoo.vespa.hosted.controller.notification.Notification.Level level, NotificationSource source, List messages) { public Notification(Instant at, Type type, Level level, NotificationSource source, List messages) { this.at = Objects.requireNonNull(at, "at cannot be null"); @@ -29,37 +24,6 @@ public class Notification { if (messages.size() < 1) throw new IllegalArgumentException("messages cannot be empty"); } - public Instant at() { return at; } - public Type type() { return type; } - public Level level() { return level; } - public NotificationSource source() { return source; } - public List messages() { return messages; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Notification that = (Notification) o; - return at.equals(that.at) && type == that.type && level == that.level && - source.equals(that.source) && messages.equals(that.messages); - } - - @Override - public int hashCode() { - return Objects.hash(at, type, level, source, messages); - } - - @Override - public String toString() { - return "Notification{" + - "at=" + at + - ", type=" + type + - ", level=" + level + - ", source=" + source + - ", messages=" + messages + - '}'; - } - public enum Level { // Must be ordered in order of importance info, warning, error @@ -67,22 +31,34 @@ public class Notification { public enum Type { - /** Related to contents of application package, e.g., usage of deprecated features/syntax */ + /** + * Related to contents of application package, e.g., usage of deprecated features/syntax + */ applicationPackage, - /** Related to contents of application package detectable by the controller on submission */ + /** + * Related to contents of application package detectable by the controller on submission + */ submission, - /** Related to contents of application test package, e.g., mismatch between deployment spec and provided tests */ + /** + * Related to contents of application test package, e.g., mismatch between deployment spec and provided tests + */ testPackage, - /** Related to deployment of application, e.g., system test failure, node allocation failure, internal errors, etc. */ + /** + * Related to deployment of application, e.g., system test failure, node allocation failure, internal errors, etc. + */ deployment, - /** Application cluster is (near) external feed blocked */ + /** + * Application cluster is (near) external feed blocked + */ feedBlock, - /** Application cluster is reindexing document(s) */ + /** + * Application cluster is reindexing document(s) + */ reindex } -- cgit v1.2.3 From 6f5f4183b0ed20d54d4bb708813603228c1a180b Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Tue, 7 Feb 2023 14:28:19 +0100 Subject: Remove unnecessary inner class --- .../vespa/hosted/controller/application/SystemApplication.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'controller-server/src') diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/SystemApplication.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/SystemApplication.java index f4a1001d9ff..56b7ffd01f9 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/SystemApplication.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/SystemApplication.java @@ -33,7 +33,7 @@ public enum SystemApplication { tenantHost(InfrastructureApplication.TENANT_HOST); /** The tenant owning all system applications */ - public static final TenantName TENANT = TenantName.from(Constants.TENANT_NAME); + public static final TenantName TENANT = TenantName.from("hosted-vespa"); private final InfrastructureApplication application; private final List dependencies; @@ -95,8 +95,4 @@ public enum SystemApplication { return Text.format("system application %s of type %s", id(), nodeType()); } - private static class Constants { - private static final String TENANT_NAME = "hosted-vespa"; - } - } -- cgit v1.2.3