1   /*
2    * Copyright 2023 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.xds.internal;
18  
19  import java.util.Map;
20  
21  import com.google.common.base.MoreObjects;
22  import com.google.common.base.Objects;
23  
24  import io.envoyproxy.controlplane.cache.Resources.ResourceType;
25  import io.envoyproxy.controlplane.cache.SnapshotResources;
26  import io.envoyproxy.controlplane.cache.VersionedResource;
27  import io.envoyproxy.controlplane.cache.v3.Snapshot;
28  import io.envoyproxy.envoy.config.cluster.v3.Cluster;
29  import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment;
30  import io.envoyproxy.envoy.config.listener.v3.Listener;
31  import io.envoyproxy.envoy.config.route.v3.RouteConfiguration;
32  import io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.Secret;
33  
34  final class CentralDogmaSnapshot extends Snapshot {
35  
36      private final SnapshotResources<Cluster> clusters;
37      private final SnapshotResources<ClusterLoadAssignment> endpoints;
38      private final SnapshotResources<Listener> listeners;
39      private final SnapshotResources<RouteConfiguration> routes;
40      private final SnapshotResources<Secret> secrets;
41  
42      CentralDogmaSnapshot(SnapshotResources<Cluster> clusters,
43                           SnapshotResources<ClusterLoadAssignment> endpoints,
44                           SnapshotResources<Listener> listeners, SnapshotResources<RouteConfiguration> routes,
45                           SnapshotResources<Secret> secrets) {
46          this.clusters = clusters;
47          this.endpoints = endpoints;
48          this.listeners = listeners;
49          this.routes = routes;
50          this.secrets = secrets;
51      }
52  
53      @Override
54      public SnapshotResources<Cluster> clusters() {
55          return clusters;
56      }
57  
58      @Override
59      public SnapshotResources<ClusterLoadAssignment> endpoints() {
60          return endpoints;
61      }
62  
63      @Override
64      public SnapshotResources<Listener> listeners() {
65          return listeners;
66      }
67  
68      @Override
69      public SnapshotResources<RouteConfiguration> routes() {
70          return routes;
71      }
72  
73      @Override
74      public SnapshotResources<Secret> secrets() {
75          return secrets;
76      }
77  
78      @Override
79      public Map<String, VersionedResource<?>> versionedResources(ResourceType resourceType) {
80          // Have to override this method because of the type inference.
81          return super.versionedResources(resourceType);
82      }
83  
84      @Override
85      public boolean equals(Object o) {
86          if (this == o) {
87              return true;
88          }
89          if (!(o instanceof CentralDogmaSnapshot)) {
90              return false;
91          }
92          final CentralDogmaSnapshot that = (CentralDogmaSnapshot) o;
93          return Objects.equal(clusters, that.clusters) &&
94                 Objects.equal(endpoints, that.endpoints) &&
95                 Objects.equal(listeners, that.listeners) &&
96                 Objects.equal(routes, that.routes) &&
97                 Objects.equal(secrets, that.secrets);
98      }
99  
100     @Override
101     public int hashCode() {
102         return Objects.hashCode(clusters, endpoints, listeners, routes, secrets);
103     }
104 
105     @Override
106     public String toString() {
107         return MoreObjects.toStringHelper(this)
108                           .add("clusters", clusters)
109                           .add("endpoints", endpoints)
110                           .add("listeners", listeners)
111                           .add("routes", routes)
112                           .add("secrets", secrets)
113                           .toString();
114     }
115 }