|
| 1 | +/* |
| 2 | + * Copyright 2024 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import io.grpc.StatusOr; |
| 22 | +import io.grpc.xds.XdsClusterResource.CdsUpdate; |
| 23 | +import io.grpc.xds.XdsEndpointResource.EdsUpdate; |
| 24 | +import io.grpc.xds.XdsListenerResource.LdsUpdate; |
| 25 | +import io.grpc.xds.XdsRouteConfigureResource.RdsUpdate; |
| 26 | +import java.io.Closeable; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents the xDS configuration tree for a specified Listener. |
| 33 | + */ |
| 34 | +public class XdsConfig { |
| 35 | + final LdsUpdate listener; |
| 36 | + final RdsUpdate route; |
| 37 | + final Map<String, StatusOr<XdsClusterConfig>> clusters; |
| 38 | + private final int hashCode; |
| 39 | + |
| 40 | + XdsConfig(LdsUpdate listener, RdsUpdate route, Map<String, StatusOr<XdsClusterConfig>> clusters) { |
| 41 | + this.listener = listener; |
| 42 | + this.route = route; |
| 43 | + this.clusters = clusters; |
| 44 | + |
| 45 | + hashCode = Objects.hash(listener, route, clusters); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean equals(Object obj) { |
| 50 | + if (!(obj instanceof XdsConfig)) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + XdsConfig o = (XdsConfig) obj; |
| 55 | + |
| 56 | + return Objects.equals(listener, o.listener) && Objects.equals(route, o.route) |
| 57 | + && Objects.equals(clusters, o.clusters); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int hashCode() { |
| 62 | + return hashCode; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + StringBuilder builder = new StringBuilder(); |
| 68 | + builder.append("XdsConfig{listener=").append(listener) |
| 69 | + .append(", route=").append(route) |
| 70 | + .append(", clusters={").append(clusters).append("}}"); |
| 71 | + return builder.toString(); |
| 72 | + } |
| 73 | + |
| 74 | + public static class XdsClusterConfig { |
| 75 | + final String clusterName; |
| 76 | + final CdsUpdate clusterResource; |
| 77 | + final StatusOr<EdsUpdate> endpoint; |
| 78 | + |
| 79 | + XdsClusterConfig(String clusterName, CdsUpdate clusterResource, |
| 80 | + StatusOr<EdsUpdate> endpoint) { |
| 81 | + this.clusterName = clusterName; |
| 82 | + this.clusterResource = clusterResource; |
| 83 | + this.endpoint = endpoint; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int hashCode() { |
| 88 | + return clusterName.hashCode() + clusterResource.hashCode() + endpoint.hashCode(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object obj) { |
| 93 | + if (!(obj instanceof XdsClusterConfig)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + XdsClusterConfig o = (XdsClusterConfig) obj; |
| 97 | + return Objects.equals(clusterName, o.clusterName) |
| 98 | + && Objects.equals(clusterResource, o.clusterResource) |
| 99 | + && Objects.equals(endpoint, o.endpoint); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String toString() { |
| 104 | + StringBuilder builder = new StringBuilder(); |
| 105 | + builder.append("XdsClusterConfig{clusterName=").append(clusterName) |
| 106 | + .append(", clusterResource=").append(clusterResource) |
| 107 | + .append(", endpoint=").append(endpoint).append("}"); |
| 108 | + return builder.toString(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + static class XdsConfigBuilder { |
| 113 | + private LdsUpdate listener; |
| 114 | + private RdsUpdate route; |
| 115 | + private Map<String, StatusOr<XdsClusterConfig>> clusters = new HashMap<>(); |
| 116 | + |
| 117 | + XdsConfigBuilder setListener(LdsUpdate listener) { |
| 118 | + this.listener = listener; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + XdsConfigBuilder setRoute(RdsUpdate route) { |
| 123 | + this.route = route; |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + XdsConfigBuilder addCluster(String name, StatusOr<XdsClusterConfig> clusterConfig) { |
| 128 | + clusters.put(name, clusterConfig); |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + XdsConfig build() { |
| 133 | + checkNotNull(listener, "listener"); |
| 134 | + checkNotNull(route, "route"); |
| 135 | + return new XdsConfig(listener, route, clusters); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public interface XdsClusterSubscriptionRegistry { |
| 140 | + Closeable subscribeToCluster(String clusterName); |
| 141 | + } |
| 142 | +} |
0 commit comments