18
18
19
19
import com .google .common .annotations .VisibleForTesting ;
20
20
import com .google .common .base .MoreObjects ;
21
- import com .google .common .base .Objects ;
21
+ import com .google .common .base .Optional ;
22
22
import com .google .common .collect .ImmutableList ;
23
23
import io .envoyproxy .envoy .type .FractionalPercent ;
24
24
import io .envoyproxy .envoy .type .FractionalPercent .DenominatorType ;
27
27
import java .util .ArrayList ;
28
28
import java .util .Collections ;
29
29
import java .util .List ;
30
+ import java .util .Objects ;
31
+ import javax .annotation .Nullable ;
30
32
31
33
/**
32
34
* Defines gRPC data types for Envoy protobuf messages used in xDS protocol. Each data type has
@@ -96,14 +98,14 @@ public boolean equals(Object o) {
96
98
return false ;
97
99
}
98
100
Locality locality = (Locality ) o ;
99
- return Objects .equal (region , locality .region )
100
- && Objects .equal (zone , locality .zone )
101
- && Objects .equal (subzone , locality .subzone );
101
+ return Objects .equals (region , locality .region )
102
+ && Objects .equals (zone , locality .zone )
103
+ && Objects .equals (subzone , locality .subzone );
102
104
}
103
105
104
106
@ Override
105
107
public int hashCode () {
106
- return Objects .hashCode (region , zone , subzone );
108
+ return Objects .hash (region , zone , subzone );
107
109
}
108
110
109
111
@ Override
@@ -169,12 +171,12 @@ public boolean equals(Object o) {
169
171
LocalityLbEndpoints that = (LocalityLbEndpoints ) o ;
170
172
return localityWeight == that .localityWeight
171
173
&& priority == that .priority
172
- && Objects .equal (endpoints , that .endpoints );
174
+ && Objects .equals (endpoints , that .endpoints );
173
175
}
174
176
175
177
@ Override
176
178
public int hashCode () {
177
- return Objects .hashCode (endpoints , localityWeight , priority );
179
+ return Objects .hash (endpoints , localityWeight , priority );
178
180
}
179
181
180
182
@ Override
@@ -247,13 +249,13 @@ public boolean equals(Object o) {
247
249
}
248
250
LbEndpoint that = (LbEndpoint ) o ;
249
251
return loadBalancingWeight == that .loadBalancingWeight
250
- && Objects .equal (eag , that .eag )
252
+ && Objects .equals (eag , that .eag )
251
253
&& isHealthy == that .isHealthy ;
252
254
}
253
255
254
256
@ Override
255
257
public int hashCode () {
256
- return Objects .hashCode (eag , loadBalancingWeight , isHealthy );
258
+ return Objects .hash (eag , loadBalancingWeight , isHealthy );
257
259
}
258
260
259
261
@ Override
@@ -267,7 +269,7 @@ public String toString() {
267
269
}
268
270
269
271
/**
270
- * See corresponding Enovy proto message {@link
272
+ * See corresponding Envoy proto message {@link
271
273
* io.envoyproxy.envoy.api.v2.ClusterLoadAssignment.Policy.DropOverload}.
272
274
*/
273
275
static final class DropOverload {
@@ -323,12 +325,12 @@ public boolean equals(Object o) {
323
325
return false ;
324
326
}
325
327
DropOverload that = (DropOverload ) o ;
326
- return dropsPerMillion == that .dropsPerMillion && Objects .equal (category , that .category );
328
+ return dropsPerMillion == that .dropsPerMillion && Objects .equals (category , that .category );
327
329
}
328
330
329
331
@ Override
330
332
public int hashCode () {
331
- return Objects .hashCode (category , dropsPerMillion );
333
+ return Objects .hash (category , dropsPerMillion );
332
334
}
333
335
334
336
@ Override
@@ -339,4 +341,239 @@ public String toString() {
339
341
.toString ();
340
342
}
341
343
}
344
+
345
+ /** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.Route}. */
346
+ static final class Route {
347
+ private final RouteMatch routeMatch ;
348
+ @ Nullable
349
+ private final RouteAction routeAction ;
350
+
351
+ @ VisibleForTesting
352
+ Route (RouteMatch routeMatch , @ Nullable RouteAction routeAction ) {
353
+ this .routeMatch = routeMatch ;
354
+ this .routeAction = routeAction ;
355
+ }
356
+
357
+ RouteMatch getRouteMatch () {
358
+ return routeMatch ;
359
+ }
360
+
361
+ Optional <RouteAction > getRouteAction () {
362
+ return Optional .fromNullable (routeAction );
363
+ }
364
+
365
+ @ Override
366
+ public boolean equals (Object o ) {
367
+ if (this == o ) {
368
+ return true ;
369
+ }
370
+ if (o == null || getClass () != o .getClass ()) {
371
+ return false ;
372
+ }
373
+ Route route = (Route ) o ;
374
+ return Objects .equals (routeMatch , route .routeMatch )
375
+ && Objects .equals (routeAction , route .routeAction );
376
+ }
377
+
378
+ @ Override
379
+ public int hashCode () {
380
+ return Objects .hash (routeMatch , routeAction );
381
+ }
382
+
383
+ @ Override
384
+ public String toString () {
385
+ return MoreObjects .toStringHelper (this )
386
+ .add ("routeMatch" , routeMatch )
387
+ .add ("routeAction" , routeAction )
388
+ .toString ();
389
+ }
390
+
391
+ static Route fromEnvoyProtoRoute (io .envoyproxy .envoy .api .v2 .route .Route proto ) {
392
+ RouteMatch routeMatch = RouteMatch .fromEnvoyProtoRouteMatch (proto .getMatch ());
393
+ RouteAction routeAction = null ;
394
+ if (proto .hasRoute ()) {
395
+ routeAction = RouteAction .fromEnvoyProtoRouteAction (proto .getRoute ());
396
+ }
397
+ return new Route (routeMatch , routeAction );
398
+ }
399
+ }
400
+
401
+ /** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.RouteMatch}. */
402
+ static final class RouteMatch {
403
+ private final String prefix ;
404
+ private final String path ;
405
+ private final boolean hasRegex ;
406
+
407
+ @ VisibleForTesting
408
+ RouteMatch (String prefix , String path , boolean hasRegex ) {
409
+ this .prefix = prefix ;
410
+ this .path = path ;
411
+ this .hasRegex = hasRegex ;
412
+ }
413
+
414
+ String getPrefix () {
415
+ return prefix ;
416
+ }
417
+
418
+ String getPath () {
419
+ return path ;
420
+ }
421
+
422
+ boolean hasRegex () {
423
+ return hasRegex ;
424
+ }
425
+
426
+ @ Override
427
+ public boolean equals (Object o ) {
428
+ if (this == o ) {
429
+ return true ;
430
+ }
431
+ if (o == null || getClass () != o .getClass ()) {
432
+ return false ;
433
+ }
434
+ RouteMatch that = (RouteMatch ) o ;
435
+ return hasRegex == that .hasRegex
436
+ && Objects .equals (prefix , that .prefix )
437
+ && Objects .equals (path , that .path );
438
+ }
439
+
440
+ @ Override
441
+ public int hashCode () {
442
+ return Objects .hash (prefix , path , hasRegex );
443
+ }
444
+
445
+ @ Override
446
+ public String toString () {
447
+ return MoreObjects .toStringHelper (this )
448
+ .add ("prefix" , prefix )
449
+ .add ("path" , path )
450
+ .add ("hasRegex" , hasRegex )
451
+ .toString ();
452
+ }
453
+
454
+ private static RouteMatch fromEnvoyProtoRouteMatch (
455
+ io .envoyproxy .envoy .api .v2 .route .RouteMatch proto ) {
456
+ return new RouteMatch (
457
+ proto .getPrefix (), proto .getPath (), !proto .getRegex ().isEmpty () || proto .hasSafeRegex ());
458
+ }
459
+ }
460
+
461
+ /** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.RouteAction}. */
462
+ static final class RouteAction {
463
+ private final String cluster ;
464
+ private final String clusterHeader ;
465
+ private final List <ClusterWeight > weightedCluster ;
466
+
467
+ @ VisibleForTesting
468
+ RouteAction (String cluster , String clusterHeader , List <ClusterWeight > weightedCluster ) {
469
+ this .cluster = cluster ;
470
+ this .clusterHeader = clusterHeader ;
471
+ this .weightedCluster = Collections .unmodifiableList (weightedCluster );
472
+ }
473
+
474
+ String getCluster () {
475
+ return cluster ;
476
+ }
477
+
478
+ String getClusterHeader () {
479
+ return clusterHeader ;
480
+ }
481
+
482
+ List <ClusterWeight > getWeightedCluster () {
483
+ return weightedCluster ;
484
+ }
485
+
486
+ @ Override
487
+ public boolean equals (Object o ) {
488
+ if (this == o ) {
489
+ return true ;
490
+ }
491
+ if (o == null || getClass () != o .getClass ()) {
492
+ return false ;
493
+ }
494
+ RouteAction that = (RouteAction ) o ;
495
+ return Objects .equals (cluster , that .cluster )
496
+ && Objects .equals (clusterHeader , that .clusterHeader )
497
+ && Objects .equals (weightedCluster , that .weightedCluster );
498
+ }
499
+
500
+ @ Override
501
+ public int hashCode () {
502
+ return Objects .hash (cluster , clusterHeader , weightedCluster );
503
+ }
504
+
505
+ @ Override
506
+ public String toString () {
507
+ return MoreObjects .toStringHelper (this )
508
+ .add ("cluster" , cluster )
509
+ .add ("clusterHeader" , clusterHeader )
510
+ .add ("weightedCluster" , weightedCluster )
511
+ .toString ();
512
+ }
513
+
514
+ private static RouteAction fromEnvoyProtoRouteAction (
515
+ io .envoyproxy .envoy .api .v2 .route .RouteAction proto ) {
516
+ List <ClusterWeight > weightedCluster = new ArrayList <>();
517
+ List <io .envoyproxy .envoy .api .v2 .route .WeightedCluster .ClusterWeight > clusterWeights
518
+ = proto .getWeightedClusters ().getClustersList ();
519
+ for (io .envoyproxy .envoy .api .v2 .route .WeightedCluster .ClusterWeight clusterWeight
520
+ : clusterWeights ) {
521
+ weightedCluster .add (ClusterWeight .fromEnvoyProtoClusterWeight (clusterWeight ));
522
+ }
523
+ return new RouteAction (proto .getCluster (), proto .getClusterHeader (), weightedCluster );
524
+ }
525
+ }
526
+
527
+ /**
528
+ * See corresponding Envoy proto message {@link
529
+ * io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight}.
530
+ */
531
+ static final class ClusterWeight {
532
+ private final String name ;
533
+ private final int weight ;
534
+
535
+ @ VisibleForTesting
536
+ ClusterWeight (String name , int weight ) {
537
+ this .name = name ;
538
+ this .weight = weight ;
539
+ }
540
+
541
+ String getName () {
542
+ return name ;
543
+ }
544
+
545
+ int getWeight () {
546
+ return weight ;
547
+ }
548
+
549
+ @ Override
550
+ public boolean equals (Object o ) {
551
+ if (this == o ) {
552
+ return true ;
553
+ }
554
+ if (o == null || getClass () != o .getClass ()) {
555
+ return false ;
556
+ }
557
+ ClusterWeight that = (ClusterWeight ) o ;
558
+ return weight == that .weight && Objects .equals (name , that .name );
559
+ }
560
+
561
+ @ Override
562
+ public int hashCode () {
563
+ return Objects .hash (name , weight );
564
+ }
565
+
566
+ @ Override
567
+ public String toString () {
568
+ return MoreObjects .toStringHelper (this )
569
+ .add ("name" , name )
570
+ .add ("weight" , weight )
571
+ .toString ();
572
+ }
573
+
574
+ private static ClusterWeight fromEnvoyProtoClusterWeight (
575
+ io .envoyproxy .envoy .api .v2 .route .WeightedCluster .ClusterWeight proto ) {
576
+ return new ClusterWeight (proto .getName (), proto .getWeight ().getValue ());
577
+ }
578
+ }
342
579
}
0 commit comments