use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.
the class ClientXdsClient method parseHttpFilter.
@VisibleForTesting
// Returns null if the filter is optional but not supported.
@Nullable
static StructOrError<FilterConfig> parseHttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter httpFilter, FilterRegistry filterRegistry, boolean isForClient) {
String filterName = httpFilter.getName();
boolean isOptional = httpFilter.getIsOptional();
if (!httpFilter.hasTypedConfig()) {
if (isOptional) {
return null;
} else {
return StructOrError.fromError("HttpFilter [" + filterName + "] is not optional and has no typed config");
}
}
Message rawConfig = httpFilter.getTypedConfig();
String typeUrl = httpFilter.getTypedConfig().getTypeUrl();
try {
if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
TypedStruct typedStruct = httpFilter.getTypedConfig().unpack(TypedStruct.class);
typeUrl = typedStruct.getTypeUrl();
rawConfig = typedStruct.getValue();
} else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
com.github.xds.type.v3.TypedStruct newTypedStruct = httpFilter.getTypedConfig().unpack(com.github.xds.type.v3.TypedStruct.class);
typeUrl = newTypedStruct.getTypeUrl();
rawConfig = newTypedStruct.getValue();
}
} catch (InvalidProtocolBufferException e) {
return StructOrError.fromError("HttpFilter [" + filterName + "] contains invalid proto: " + e);
}
Filter filter = filterRegistry.get(typeUrl);
if ((isForClient && !(filter instanceof ClientInterceptorBuilder)) || (!isForClient && !(filter instanceof ServerInterceptorBuilder))) {
if (isOptional) {
return null;
} else {
return StructOrError.fromError("HttpFilter [" + filterName + "](" + typeUrl + ") is required but unsupported for " + (isForClient ? "client" : "server"));
}
}
ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfig(rawConfig);
if (filterConfig.errorDetail != null) {
return StructOrError.fromError("Invalid filter config for HttpFilter [" + filterName + "]: " + filterConfig.errorDetail);
}
return StructOrError.fromStruct(filterConfig.config);
}
use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.
the class ClientXdsClient method parseOverrideFilterConfigs.
@VisibleForTesting
static StructOrError<Map<String, FilterConfig>> parseOverrideFilterConfigs(Map<String, Any> rawFilterConfigMap, FilterRegistry filterRegistry) {
Map<String, FilterConfig> overrideConfigs = new HashMap<>();
for (String name : rawFilterConfigMap.keySet()) {
Any anyConfig = rawFilterConfigMap.get(name);
String typeUrl = anyConfig.getTypeUrl();
boolean isOptional = false;
if (typeUrl.equals(TYPE_URL_FILTER_CONFIG)) {
io.envoyproxy.envoy.config.route.v3.FilterConfig filterConfig;
try {
filterConfig = anyConfig.unpack(io.envoyproxy.envoy.config.route.v3.FilterConfig.class);
} catch (InvalidProtocolBufferException e) {
return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
}
isOptional = filterConfig.getIsOptional();
anyConfig = filterConfig.getConfig();
typeUrl = anyConfig.getTypeUrl();
}
Message rawConfig = anyConfig;
try {
if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
TypedStruct typedStruct = anyConfig.unpack(TypedStruct.class);
typeUrl = typedStruct.getTypeUrl();
rawConfig = typedStruct.getValue();
} else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
com.github.xds.type.v3.TypedStruct newTypedStruct = anyConfig.unpack(com.github.xds.type.v3.TypedStruct.class);
typeUrl = newTypedStruct.getTypeUrl();
rawConfig = newTypedStruct.getValue();
}
} catch (InvalidProtocolBufferException e) {
return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
}
Filter filter = filterRegistry.get(typeUrl);
if (filter == null) {
if (isOptional) {
continue;
}
return StructOrError.fromError("HttpFilter [" + name + "](" + typeUrl + ") is required but unsupported");
}
ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfigOverride(rawConfig);
if (filterConfig.errorDetail != null) {
return StructOrError.fromError("Invalid filter config for HttpFilter [" + name + "]: " + filterConfig.errorDetail);
}
overrideConfigs.put(name, filterConfig.config);
}
return StructOrError.fromStruct(overrideConfigs);
}
use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.
the class ClientXdsClientDataTest method parseClusterSpecifierPlugin_typedStructInTypedExtension.
@Test
public void parseClusterSpecifierPlugin_typedStructInTypedExtension() throws Exception {
class TestPluginConfig implements PluginConfig {
@Override
public String typeUrl() {
return "type.googleapis.com/google.protobuf.Empty";
}
}
ClusterSpecifierPluginRegistry registry = ClusterSpecifierPluginRegistry.newRegistry();
registry.register(new ClusterSpecifierPlugin() {
@Override
public String[] typeUrls() {
return new String[] { "type.googleapis.com/google.protobuf.Empty" };
}
@Override
public ConfigOrError<? extends PluginConfig> parsePlugin(Message rawProtoMessage) {
return ConfigOrError.fromConfig(new TestPluginConfig());
}
});
TypedStruct typedStruct = TypedStruct.newBuilder().setTypeUrl("type.googleapis.com/google.protobuf.Empty").setValue(Struct.newBuilder()).build();
io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin pluginProto = io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin.newBuilder().setExtension(TypedExtensionConfig.newBuilder().setTypedConfig(Any.pack(typedStruct))).build();
PluginConfig pluginConfig = ClientXdsClient.parseClusterSpecifierPlugin(pluginProto, registry);
assertThat(pluginConfig).isInstanceOf(TestPluginConfig.class);
}
use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.
the class ClientXdsClient method parseClusterSpecifierPlugin.
@VisibleForTesting
static PluginConfig parseClusterSpecifierPlugin(ClusterSpecifierPlugin pluginProto, ClusterSpecifierPluginRegistry registry) throws ResourceInvalidException {
TypedExtensionConfig extension = pluginProto.getExtension();
String pluginName = extension.getName();
Any anyConfig = extension.getTypedConfig();
String typeUrl = anyConfig.getTypeUrl();
Message rawConfig = anyConfig;
if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA) || typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
try {
TypedStruct typedStruct = unpackCompatibleType(anyConfig, TypedStruct.class, TYPE_URL_TYPED_STRUCT_UDPA, TYPE_URL_TYPED_STRUCT);
typeUrl = typedStruct.getTypeUrl();
rawConfig = typedStruct.getValue();
} catch (InvalidProtocolBufferException e) {
throw new ResourceInvalidException("ClusterSpecifierPlugin [" + pluginName + "] contains invalid proto", e);
}
}
io.grpc.xds.ClusterSpecifierPlugin plugin = registry.get(typeUrl);
if (plugin == null) {
throw new ResourceInvalidException("Unsupported ClusterSpecifierPlugin type: " + typeUrl);
}
ConfigOrError<? extends PluginConfig> pluginConfigOrError = plugin.parsePlugin(rawConfig);
if (pluginConfigOrError.errorDetail != null) {
throw new ResourceInvalidException(pluginConfigOrError.errorDetail);
}
return pluginConfigOrError.config;
}
Aggregations