use of io.aklivity.zilla.runtime.engine.config.RouteConfig in project zilla by aklivity.
the class ConfigureTask method call.
@Override
public Void call() throws Exception {
String configText;
if (configURL == null) {
configText = "{\"name\":\"default\"}";
} else if ("https".equals(configURL.getProtocol()) || "https".equals(configURL.getProtocol())) {
HttpClient client = HttpClient.newBuilder().version(HTTP_2).followRedirects(NORMAL).build();
HttpRequest request = HttpRequest.newBuilder().GET().uri(configURL.toURI()).build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();
configText = body;
} else {
URLConnection connection = configURL.openConnection();
try (InputStream input = connection.getInputStream()) {
configText = new String(input.readAllBytes(), UTF_8);
}
}
if (config.configSyntaxMustache()) {
configText = Mustache.resolve(configText, System::getenv);
}
logger.accept(configText);
List<String> errors = new LinkedList<>();
parse: try {
InputStream schemaInput = Engine.class.getResourceAsStream("internal/schema/engine.schema.json");
JsonProvider schemaProvider = JsonProvider.provider();
JsonReader schemaReader = schemaProvider.createReader(schemaInput);
JsonObject schemaObject = schemaReader.readObject();
for (URL schemaType : schemaTypes) {
InputStream schemaPatchInput = schemaType.openStream();
JsonReader schemaPatchReader = schemaProvider.createReader(schemaPatchInput);
JsonArray schemaPatchArray = schemaPatchReader.readArray();
JsonPatch schemaPatch = schemaProvider.createPatch(schemaPatchArray);
schemaObject = schemaPatch.apply(schemaObject);
}
JsonParser schemaParser = schemaProvider.createParserFactory(null).createParser(new StringReader(schemaObject.toString()));
JsonValidationService service = JsonValidationService.newInstance();
ProblemHandler handler = service.createProblemPrinter(errors::add);
JsonSchemaReader reader = service.createSchemaReader(schemaParser);
JsonSchema schema = new UniquePropertyKeysSchema(reader.read());
JsonProvider provider = service.createJsonProvider(schema, parser -> handler);
provider.createReader(new StringReader(configText)).read();
if (!errors.isEmpty()) {
break parse;
}
JsonbConfig config = new JsonbConfig().withAdapters(new NamespaceAdapter());
Jsonb jsonb = JsonbBuilder.newBuilder().withProvider(provider).withConfig(config).build();
NamespaceConfig namespace = jsonb.fromJson(configText, NamespaceConfig.class);
if (!errors.isEmpty()) {
break parse;
}
namespace.id = supplyId.applyAsInt(namespace.name);
for (BindingConfig binding : namespace.bindings) {
binding.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.entry));
if (binding.vault != null) {
binding.vault.id = NamespacedId.id(supplyId.applyAsInt(ofNullable(binding.vault.namespace).orElse(namespace.name)), supplyId.applyAsInt(binding.vault.name));
}
// TODO: consider route exit namespace
for (RouteConfig route : binding.routes) {
route.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(route.exit));
}
// TODO: consider binding exit namespace
if (binding.exit != null) {
binding.exit.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.exit.exit));
}
tuning.affinity(binding.id, tuning.affinity(binding.id));
}
for (VaultConfig vault : namespace.vaults) {
vault.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(vault.name));
}
CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
for (DispatchAgent dispatcher : dispatchers) {
future = CompletableFuture.allOf(future, dispatcher.attach(namespace));
}
future.join();
extensions.forEach(e -> e.onConfigured(context));
} catch (Throwable ex) {
errorHandler.onError(ex);
}
if (!errors.isEmpty()) {
errors.forEach(msg -> errorHandler.onError(new JsonException(msg)));
}
return null;
}
use of io.aklivity.zilla.runtime.engine.config.RouteConfig in project zilla by aklivity.
the class RouteAdapter method adaptFromJson.
@Override
public RouteConfig adaptFromJson(JsonObject object) {
String exit = object.getString(EXIT_NAME);
List<ConditionConfig> when = object.containsKey(WHEN_NAME) ? object.getJsonArray(WHEN_NAME).stream().map(JsonValue::asJsonObject).map(condition::adaptFromJson).collect(Collectors.toList()) : WHEN_DEFAULT;
WithConfig wth = object.containsKey(WITH_NAME) ? with.adaptFromJson(object.getJsonObject(WITH_NAME)) : null;
return new RouteConfig(index, exit, when, wth);
}
use of io.aklivity.zilla.runtime.engine.config.RouteConfig in project zilla by aklivity.
the class BindingConfigsAdapterTest method shouldWriteBindingWithRoute.
@Test
public void shouldWriteBindingWithRoute() {
BindingConfig[] bindings = { new BindingConfig(null, "test", "test", SERVER, null, singletonList(new RouteConfig("test")), null) };
String text = jsonb.toJson(bindings);
assertThat(text, not(nullValue()));
assertThat(text, equalTo("{\"test\":{\"type\":\"test\",\"kind\":\"server\",\"routes\":[{\"exit\":\"test\"}]}}"));
}
Aggregations