forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnvoyProtoData.java
865 lines (769 loc) · 26.8 KB
/
EnvoyProtoData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/*
* Copyright 2019 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.xds;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.ImmutableList;
import com.google.re2j.Pattern;
import com.google.re2j.PatternSyntaxException;
import io.envoyproxy.envoy.type.v3.FractionalPercent;
import io.envoyproxy.envoy.type.v3.FractionalPercent.DenominatorType;
import io.grpc.EquivalentAddressGroup;
import io.grpc.xds.RouteMatch.FractionMatcher;
import io.grpc.xds.RouteMatch.HeaderMatcher;
import io.grpc.xds.RouteMatch.PathMatcher;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* Defines gRPC data types for Envoy protobuf messages used in xDS protocol. Each data type has
* the same name as Envoy's corresponding protobuf message, but only with fields used by gRPC.
*
* <p>Each data type should define a {@code fromEnvoyProtoXXX} static method to convert an Envoy
* proto message to an instance of that data type.
*
* <p>For data types that need to be sent as protobuf messages, a {@code toEnvoyProtoXXX} instance
* method is defined to convert an instance to Envoy proto message.
*
* <p>Data conversion should follow the invariant: converted data is guaranteed to be valid for
* gRPC. If the protobuf message contains invalid data, the conversion should fail and no object
* should be instantiated.
*/
final class EnvoyProtoData {
// Prevent instantiation.
private EnvoyProtoData() {
}
static final class StructOrError<T> {
/**
* Returns a {@link StructOrError} for the successfully converted data object.
*/
static <T> StructOrError<T> fromStruct(T struct) {
return new StructOrError<>(struct);
}
/**
* Returns a {@link StructOrError} for the failure to convert the data object.
*/
static <T> StructOrError<T> fromError(String errorDetail) {
return new StructOrError<>(errorDetail);
}
private final String errorDetail;
private final T struct;
private StructOrError(T struct) {
this.struct = checkNotNull(struct, "struct");
this.errorDetail = null;
}
private StructOrError(String errorDetail) {
this.struct = null;
this.errorDetail = checkNotNull(errorDetail, "errorDetail");
}
/**
* Returns struct if exists, otherwise null.
*/
@Nullable
public T getStruct() {
return struct;
}
/**
* Returns error detail if exists, otherwise null.
*/
@Nullable
String getErrorDetail() {
return errorDetail;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StructOrError<?> that = (StructOrError<?>) o;
return Objects.equals(errorDetail, that.errorDetail) && Objects.equals(struct, that.struct);
}
@Override
public int hashCode() {
return Objects.hash(errorDetail, struct);
}
@Override
public String toString() {
if (struct != null) {
return MoreObjects.toStringHelper(this)
.add("struct", struct)
.toString();
} else {
assert errorDetail != null;
return MoreObjects.toStringHelper(this)
.add("error", errorDetail)
.toString();
}
}
}
/**
* See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.core.Locality}.
*/
static final class Locality {
private final String region;
private final String zone;
private final String subZone;
Locality(String region, String zone, String subZone) {
this.region = region;
this.zone = zone;
this.subZone = subZone;
}
static Locality fromEnvoyProtoLocality(io.envoyproxy.envoy.config.core.v3.Locality locality) {
return new Locality(
/* region = */ locality.getRegion(),
/* zone = */ locality.getZone(),
/* subZone = */ locality.getSubZone());
}
@VisibleForTesting
static Locality fromEnvoyProtoLocalityV2(io.envoyproxy.envoy.api.v2.core.Locality locality) {
return new Locality(
/* region = */ locality.getRegion(),
/* zone = */ locality.getZone(),
/* subZone = */ locality.getSubZone());
}
io.envoyproxy.envoy.api.v2.core.Locality toEnvoyProtoLocality() {
return io.envoyproxy.envoy.api.v2.core.Locality.newBuilder()
.setRegion(region)
.setZone(zone)
.setSubZone(subZone)
.build();
}
String getRegion() {
return region;
}
String getZone() {
return zone;
}
String getSubZone() {
return subZone;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Locality locality = (Locality) o;
return Objects.equals(region, locality.region)
&& Objects.equals(zone, locality.zone)
&& Objects.equals(subZone, locality.subZone);
}
@Override
public int hashCode() {
return Objects.hash(region, zone, subZone);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("region", region)
.add("zone", zone)
.add("subZone", subZone)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.endpoint.LocalityLbEndpoints}.
*/
static final class LocalityLbEndpoints {
private final List<LbEndpoint> endpoints;
private final int localityWeight;
private final int priority;
/** Must only be used for testing. */
@VisibleForTesting
LocalityLbEndpoints(List<LbEndpoint> endpoints, int localityWeight, int priority) {
this.endpoints = endpoints;
this.localityWeight = localityWeight;
this.priority = priority;
}
static LocalityLbEndpoints fromEnvoyProtoLocalityLbEndpoints(
io.envoyproxy.envoy.config.endpoint.v3.LocalityLbEndpoints proto) {
List<LbEndpoint> endpoints = new ArrayList<>(proto.getLbEndpointsCount());
for (io.envoyproxy.envoy.config.endpoint.v3.LbEndpoint endpoint :
proto.getLbEndpointsList()) {
endpoints.add(LbEndpoint.fromEnvoyProtoLbEndpoint(endpoint));
}
return
new LocalityLbEndpoints(
endpoints,
proto.getLoadBalancingWeight().getValue(),
proto.getPriority());
}
@VisibleForTesting
static LocalityLbEndpoints fromEnvoyProtoLocalityLbEndpointsV2(
io.envoyproxy.envoy.api.v2.endpoint.LocalityLbEndpoints proto) {
List<LbEndpoint> endpoints = new ArrayList<>(proto.getLbEndpointsCount());
for (io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint endpoint : proto.getLbEndpointsList()) {
endpoints.add(LbEndpoint.fromEnvoyProtoLbEndpointV2(endpoint));
}
return
new LocalityLbEndpoints(
endpoints,
proto.getLoadBalancingWeight().getValue(),
proto.getPriority());
}
List<LbEndpoint> getEndpoints() {
return Collections.unmodifiableList(endpoints);
}
int getLocalityWeight() {
return localityWeight;
}
int getPriority() {
return priority;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LocalityLbEndpoints that = (LocalityLbEndpoints) o;
return localityWeight == that.localityWeight
&& priority == that.priority
&& Objects.equals(endpoints, that.endpoints);
}
@Override
public int hashCode() {
return Objects.hash(endpoints, localityWeight, priority);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("endpoints", endpoints)
.add("localityWeight", localityWeight)
.add("priority", priority)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint}.
*/
static final class LbEndpoint {
private final EquivalentAddressGroup eag;
private final int loadBalancingWeight;
private final boolean isHealthy;
@VisibleForTesting
LbEndpoint(String address, int port, int loadBalancingWeight, boolean isHealthy) {
this(
new EquivalentAddressGroup(
new InetSocketAddress(address, port)),
loadBalancingWeight, isHealthy);
}
@VisibleForTesting
LbEndpoint(EquivalentAddressGroup eag, int loadBalancingWeight, boolean isHealthy) {
this.eag = eag;
this.loadBalancingWeight = loadBalancingWeight;
this.isHealthy = isHealthy;
}
static LbEndpoint fromEnvoyProtoLbEndpoint(
io.envoyproxy.envoy.config.endpoint.v3.LbEndpoint proto) {
io.envoyproxy.envoy.config.core.v3.SocketAddress socketAddress =
proto.getEndpoint().getAddress().getSocketAddress();
InetSocketAddress addr =
new InetSocketAddress(socketAddress.getAddress(), socketAddress.getPortValue());
return new LbEndpoint(
new EquivalentAddressGroup(ImmutableList.<java.net.SocketAddress>of(addr)),
proto.getLoadBalancingWeight().getValue(),
proto.getHealthStatus() == io.envoyproxy.envoy.config.core.v3.HealthStatus.HEALTHY
|| proto.getHealthStatus()
== io.envoyproxy.envoy.config.core.v3.HealthStatus.UNKNOWN);
}
private static LbEndpoint fromEnvoyProtoLbEndpointV2(
io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint proto) {
io.envoyproxy.envoy.api.v2.core.SocketAddress socketAddress =
proto.getEndpoint().getAddress().getSocketAddress();
InetSocketAddress addr =
new InetSocketAddress(socketAddress.getAddress(), socketAddress.getPortValue());
return new LbEndpoint(
new EquivalentAddressGroup(ImmutableList.<java.net.SocketAddress>of(addr)),
proto.getLoadBalancingWeight().getValue(),
proto.getHealthStatus() == io.envoyproxy.envoy.api.v2.core.HealthStatus.HEALTHY
|| proto.getHealthStatus() == io.envoyproxy.envoy.api.v2.core.HealthStatus.UNKNOWN);
}
EquivalentAddressGroup getAddress() {
return eag;
}
int getLoadBalancingWeight() {
return loadBalancingWeight;
}
boolean isHealthy() {
return isHealthy;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LbEndpoint that = (LbEndpoint) o;
return loadBalancingWeight == that.loadBalancingWeight
&& Objects.equals(eag, that.eag)
&& isHealthy == that.isHealthy;
}
@Override
public int hashCode() {
return Objects.hash(eag, loadBalancingWeight, isHealthy);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("eag", eag)
.add("loadBalancingWeight", loadBalancingWeight)
.add("isHealthy", isHealthy)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.ClusterLoadAssignment.Policy.DropOverload}.
*/
static final class DropOverload {
private final String category;
private final int dropsPerMillion;
/** Must only be used for testing. */
@VisibleForTesting
DropOverload(String category, int dropsPerMillion) {
this.category = category;
this.dropsPerMillion = dropsPerMillion;
}
static DropOverload fromEnvoyProtoDropOverload(
io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment.Policy.DropOverload proto) {
FractionalPercent percent = proto.getDropPercentage();
int numerator = percent.getNumerator();
DenominatorType type = percent.getDenominator();
switch (type) {
case TEN_THOUSAND:
numerator *= 100;
break;
case HUNDRED:
numerator *= 10_000;
break;
case MILLION:
break;
case UNRECOGNIZED:
default:
throw new IllegalArgumentException("Unknown denominator type of " + percent);
}
if (numerator > 1_000_000) {
numerator = 1_000_000;
}
return new DropOverload(proto.getCategory(), numerator);
}
@VisibleForTesting
static DropOverload fromEnvoyProtoDropOverloadV2(
io.envoyproxy.envoy.api.v2.ClusterLoadAssignment.Policy.DropOverload proto) {
io.envoyproxy.envoy.type.FractionalPercent percent = proto.getDropPercentage();
int numerator = percent.getNumerator();
io.envoyproxy.envoy.type.FractionalPercent.DenominatorType type = percent.getDenominator();
switch (type) {
case TEN_THOUSAND:
numerator *= 100;
break;
case HUNDRED:
numerator *= 10_000;
break;
case MILLION:
break;
case UNRECOGNIZED:
default:
throw new IllegalArgumentException("Unknown denominator type of " + percent);
}
if (numerator > 1_000_000) {
numerator = 1_000_000;
}
return new DropOverload(proto.getCategory(), numerator);
}
String getCategory() {
return category;
}
int getDropsPerMillion() {
return dropsPerMillion;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DropOverload that = (DropOverload) o;
return dropsPerMillion == that.dropsPerMillion && Objects.equals(category, that.category);
}
@Override
public int hashCode() {
return Objects.hash(category, dropsPerMillion);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("category", category)
.add("dropsPerMillion", dropsPerMillion)
.toString();
}
}
/** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.Route}. */
static final class Route {
private final RouteMatch routeMatch;
private final RouteAction routeAction;
@VisibleForTesting
Route(RouteMatch routeMatch, @Nullable RouteAction routeAction) {
this.routeMatch = routeMatch;
this.routeAction = routeAction;
}
RouteMatch getRouteMatch() {
return routeMatch;
}
RouteAction getRouteAction() {
return routeAction;
}
// TODO(chengyuanzhang): delete and do not use after routing feature is always ON.
boolean isDefaultRoute() {
// For backward compatibility, all the other matchers are ignored.
String prefix = routeMatch.getPathMatch().getPrefix();
if (prefix != null) {
return prefix.isEmpty() || prefix.equals("/");
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Route route = (Route) o;
return Objects.equals(routeMatch, route.routeMatch)
&& Objects.equals(routeAction, route.routeAction);
}
@Override
public int hashCode() {
return Objects.hash(routeMatch, routeAction);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("routeMatch", routeMatch)
.add("routeAction", routeAction)
.toString();
}
@Nullable
static StructOrError<Route> fromEnvoyProtoRoute(
io.envoyproxy.envoy.config.route.v3.Route proto) {
StructOrError<RouteMatch> routeMatch = convertEnvoyProtoRouteMatch(proto.getMatch());
if (routeMatch == null) {
return null;
}
if (routeMatch.getErrorDetail() != null) {
return StructOrError.fromError(
"Invalid route [" + proto.getName() + "]: " + routeMatch.getErrorDetail());
}
StructOrError<RouteAction> routeAction;
switch (proto.getActionCase()) {
case ROUTE:
routeAction = RouteAction.fromEnvoyProtoRouteAction(proto.getRoute());
break;
case REDIRECT:
return StructOrError.fromError("Unsupported action type: redirect");
case DIRECT_RESPONSE:
return StructOrError.fromError("Unsupported action type: direct_response");
case FILTER_ACTION:
return StructOrError.fromError("Unsupported action type: filter_action");
case ACTION_NOT_SET:
default:
return StructOrError.fromError("Unknown action type: " + proto.getActionCase());
}
if (routeAction == null) {
return null;
}
if (routeAction.getErrorDetail() != null) {
return StructOrError.fromError(
"Invalid route [" + proto.getName() + "]: " + routeAction.getErrorDetail());
}
return StructOrError.fromStruct(new Route(routeMatch.getStruct(), routeAction.getStruct()));
}
@VisibleForTesting
@SuppressWarnings("deprecation")
@Nullable
static StructOrError<RouteMatch> convertEnvoyProtoRouteMatch(
io.envoyproxy.envoy.config.route.v3.RouteMatch proto) {
if (proto.getQueryParametersCount() != 0) {
return null;
}
if (proto.hasCaseSensitive() && !proto.getCaseSensitive().getValue()) {
return StructOrError.fromError("Unsupported match option: case insensitive");
}
StructOrError<PathMatcher> pathMatch = convertEnvoyProtoPathMatcher(proto);
if (pathMatch.getErrorDetail() != null) {
return StructOrError.fromError(pathMatch.getErrorDetail());
}
FractionMatcher fractionMatch = null;
if (proto.hasRuntimeFraction()) {
StructOrError<FractionMatcher> parsedFraction =
convertEnvoyProtoFraction(proto.getRuntimeFraction().getDefaultValue());
if (parsedFraction.getErrorDetail() != null) {
return StructOrError.fromError(parsedFraction.getErrorDetail());
}
fractionMatch = parsedFraction.getStruct();
}
List<HeaderMatcher> headerMatchers = new ArrayList<>();
for (io.envoyproxy.envoy.config.route.v3.HeaderMatcher hmProto : proto.getHeadersList()) {
StructOrError<HeaderMatcher> headerMatcher = convertEnvoyProtoHeaderMatcher(hmProto);
if (headerMatcher.getErrorDetail() != null) {
return StructOrError.fromError(headerMatcher.getErrorDetail());
}
headerMatchers.add(headerMatcher.getStruct());
}
return StructOrError.fromStruct(
new RouteMatch(
pathMatch.getStruct(), Collections.unmodifiableList(headerMatchers), fractionMatch));
}
@SuppressWarnings("deprecation")
private static StructOrError<PathMatcher> convertEnvoyProtoPathMatcher(
io.envoyproxy.envoy.config.route.v3.RouteMatch proto) {
String path = null;
String prefix = null;
Pattern safeRegEx = null;
switch (proto.getPathSpecifierCase()) {
case PREFIX:
prefix = proto.getPrefix();
break;
case PATH:
path = proto.getPath();
break;
case SAFE_REGEX:
String rawPattern = proto.getSafeRegex().getRegex();
try {
safeRegEx = Pattern.compile(rawPattern);
} catch (PatternSyntaxException e) {
return StructOrError.fromError("Malformed safe regex pattern: " + e.getMessage());
}
break;
case PATHSPECIFIER_NOT_SET:
default:
return StructOrError.fromError("Unknown path match type");
}
return StructOrError.fromStruct(new PathMatcher(path, prefix, safeRegEx));
}
private static StructOrError<FractionMatcher> convertEnvoyProtoFraction(
io.envoyproxy.envoy.type.v3.FractionalPercent proto) {
int numerator = proto.getNumerator();
int denominator = 0;
switch (proto.getDenominator()) {
case HUNDRED:
denominator = 100;
break;
case TEN_THOUSAND:
denominator = 10_000;
break;
case MILLION:
denominator = 1_000_000;
break;
case UNRECOGNIZED:
default:
return StructOrError.fromError(
"Unrecognized fractional percent denominator: " + proto.getDenominator());
}
return StructOrError.fromStruct(new FractionMatcher(numerator, denominator));
}
@VisibleForTesting
@SuppressWarnings("deprecation")
static StructOrError<HeaderMatcher> convertEnvoyProtoHeaderMatcher(
io.envoyproxy.envoy.config.route.v3.HeaderMatcher proto) {
String exactMatch = null;
Pattern safeRegExMatch = null;
HeaderMatcher.Range rangeMatch = null;
Boolean presentMatch = null;
String prefixMatch = null;
String suffixMatch = null;
switch (proto.getHeaderMatchSpecifierCase()) {
case EXACT_MATCH:
exactMatch = proto.getExactMatch();
break;
case SAFE_REGEX_MATCH:
String rawPattern = proto.getSafeRegexMatch().getRegex();
try {
safeRegExMatch = Pattern.compile(rawPattern);
} catch (PatternSyntaxException e) {
return StructOrError.fromError(
"HeaderMatcher [" + proto.getName() + "] contains malformed safe regex pattern: "
+ e.getMessage());
}
break;
case RANGE_MATCH:
rangeMatch =
new HeaderMatcher.Range(
proto.getRangeMatch().getStart(), proto.getRangeMatch().getEnd());
break;
case PRESENT_MATCH:
presentMatch = proto.getPresentMatch();
break;
case PREFIX_MATCH:
prefixMatch = proto.getPrefixMatch();
break;
case SUFFIX_MATCH:
suffixMatch = proto.getSuffixMatch();
break;
case HEADERMATCHSPECIFIER_NOT_SET:
default:
return StructOrError.fromError("Unknown header matcher type");
}
return StructOrError.fromStruct(
new HeaderMatcher(
proto.getName(), exactMatch, safeRegExMatch, rangeMatch, presentMatch,
prefixMatch, suffixMatch, proto.getInvertMatch()));
}
}
/** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.RouteAction}. */
static final class RouteAction {
// Exactly one of the following fields is non-null.
@Nullable
private final String cluster;
@Nullable
private final List<ClusterWeight> weightedClusters;
@VisibleForTesting
RouteAction(@Nullable String cluster, @Nullable List<ClusterWeight> weightedClusters) {
this.cluster = cluster;
this.weightedClusters = weightedClusters;
}
@Nullable
String getCluster() {
return cluster;
}
@Nullable
List<ClusterWeight> getWeightedCluster() {
return weightedClusters;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RouteAction that = (RouteAction) o;
return Objects.equals(cluster, that.cluster)
&& Objects.equals(weightedClusters, that.weightedClusters);
}
@Override
public int hashCode() {
return Objects.hash(cluster, weightedClusters);
}
@Override
public String toString() {
ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
if (cluster != null) {
toStringHelper.add("cluster", cluster);
}
if (weightedClusters != null) {
toStringHelper.add("weightedClusters", weightedClusters);
}
return toStringHelper.toString();
}
@Nullable
@VisibleForTesting
static StructOrError<RouteAction> fromEnvoyProtoRouteAction(
io.envoyproxy.envoy.config.route.v3.RouteAction proto) {
String cluster = null;
List<ClusterWeight> weightedClusters = null;
switch (proto.getClusterSpecifierCase()) {
case CLUSTER:
cluster = proto.getCluster();
break;
case CLUSTER_HEADER:
return null;
case WEIGHTED_CLUSTERS:
List<io.envoyproxy.envoy.config.route.v3.WeightedCluster.ClusterWeight> clusterWeights
= proto.getWeightedClusters().getClustersList();
weightedClusters = new ArrayList<>();
for (io.envoyproxy.envoy.config.route.v3.WeightedCluster.ClusterWeight clusterWeight
: clusterWeights) {
weightedClusters.add(ClusterWeight.fromEnvoyProtoClusterWeight(clusterWeight));
}
// TODO(chengyuanzhang): validate if the sum of weights equals to total weight.
break;
case CLUSTERSPECIFIER_NOT_SET:
default:
return StructOrError.fromError(
"Unknown cluster specifier: " + proto.getClusterSpecifierCase());
}
return StructOrError.fromStruct(new RouteAction(cluster, weightedClusters));
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight}.
*/
static final class ClusterWeight {
private final String name;
private final int weight;
@VisibleForTesting
ClusterWeight(String name, int weight) {
this.name = name;
this.weight = weight;
}
String getName() {
return name;
}
int getWeight() {
return weight;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClusterWeight that = (ClusterWeight) o;
return weight == that.weight && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, weight);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.add("weight", weight)
.toString();
}
@VisibleForTesting
static ClusterWeight fromEnvoyProtoClusterWeight(
io.envoyproxy.envoy.config.route.v3.WeightedCluster.ClusterWeight proto) {
return new ClusterWeight(proto.getName(), proto.getWeight().getValue());
}
}
}