Search in sources :

Example 6 with RouteConfig

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;
}
Also used : JsonSchemaReader(org.leadpony.justify.api.JsonSchemaReader) ErrorHandler(org.agrona.ErrorHandler) BodyHandlers(java.net.http.HttpResponse.BodyHandlers) EngineExtContext(io.aklivity.zilla.runtime.engine.ext.EngineExtContext) URL(java.net.URL) Engine(io.aklivity.zilla.runtime.engine.Engine) NamespaceConfig(io.aklivity.zilla.runtime.engine.config.NamespaceConfig) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) HttpRequest(java.net.http.HttpRequest) Jsonb(jakarta.json.bind.Jsonb) UniquePropertyKeysSchema(io.aklivity.zilla.runtime.engine.internal.registry.json.UniquePropertyKeysSchema) URLConnection(java.net.URLConnection) RouteConfig(io.aklivity.zilla.runtime.engine.config.RouteConfig) JsonObject(jakarta.json.JsonObject) Mustache(io.aklivity.zilla.runtime.engine.internal.util.Mustache) HttpClient(java.net.http.HttpClient) JsonException(jakarta.json.JsonException) JsonReader(jakarta.json.JsonReader) LinkedList(java.util.LinkedList) JsonSchema(org.leadpony.justify.api.JsonSchema) ProblemHandler(org.leadpony.justify.api.ProblemHandler) HttpResponse(java.net.http.HttpResponse) JsonbBuilder(jakarta.json.bind.JsonbBuilder) JsonbConfig(jakarta.json.bind.JsonbConfig) HTTP_2(java.net.http.HttpClient.Version.HTTP_2) JsonProvider(jakarta.json.spi.JsonProvider) JsonParser(jakarta.json.stream.JsonParser) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Optional.ofNullable(java.util.Optional.ofNullable) Tuning(io.aklivity.zilla.runtime.engine.internal.Tuning) Collection(java.util.Collection) ToIntFunction(java.util.function.ToIntFunction) NORMAL(java.net.http.HttpClient.Redirect.NORMAL) EngineExtSpi(io.aklivity.zilla.runtime.engine.ext.EngineExtSpi) BindingConfig(io.aklivity.zilla.runtime.engine.config.BindingConfig) Consumer(java.util.function.Consumer) List(java.util.List) JsonPatch(jakarta.json.JsonPatch) VaultConfig(io.aklivity.zilla.runtime.engine.config.VaultConfig) StringReader(java.io.StringReader) JsonValidationService(org.leadpony.justify.api.JsonValidationService) NamespaceAdapter(io.aklivity.zilla.runtime.engine.internal.config.NamespaceAdapter) NamespacedId(io.aklivity.zilla.runtime.engine.internal.stream.NamespacedId) JsonArray(jakarta.json.JsonArray) InputStream(java.io.InputStream) NamespaceConfig(io.aklivity.zilla.runtime.engine.config.NamespaceConfig) JsonException(jakarta.json.JsonException) NamespaceAdapter(io.aklivity.zilla.runtime.engine.internal.config.NamespaceAdapter) JsonSchema(org.leadpony.justify.api.JsonSchema) JsonObject(jakarta.json.JsonObject) UniquePropertyKeysSchema(io.aklivity.zilla.runtime.engine.internal.registry.json.UniquePropertyKeysSchema) JsonProvider(jakarta.json.spi.JsonProvider) URL(java.net.URL) VaultConfig(io.aklivity.zilla.runtime.engine.config.VaultConfig) ProblemHandler(org.leadpony.justify.api.ProblemHandler) Jsonb(jakarta.json.bind.Jsonb) CompletableFuture(java.util.concurrent.CompletableFuture) JsonValidationService(org.leadpony.justify.api.JsonValidationService) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Engine(io.aklivity.zilla.runtime.engine.Engine) JsonParser(jakarta.json.stream.JsonParser) HttpRequest(java.net.http.HttpRequest) InputStream(java.io.InputStream) HttpResponse(java.net.http.HttpResponse) RouteConfig(io.aklivity.zilla.runtime.engine.config.RouteConfig) JsonPatch(jakarta.json.JsonPatch) URLConnection(java.net.URLConnection) LinkedList(java.util.LinkedList) JsonArray(jakarta.json.JsonArray) JsonbConfig(jakarta.json.bind.JsonbConfig) JsonSchemaReader(org.leadpony.justify.api.JsonSchemaReader) BindingConfig(io.aklivity.zilla.runtime.engine.config.BindingConfig) HttpClient(java.net.http.HttpClient)

Example 7 with RouteConfig

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);
}
Also used : WithConfig(io.aklivity.zilla.runtime.engine.config.WithConfig) RouteConfig(io.aklivity.zilla.runtime.engine.config.RouteConfig) ConditionConfig(io.aklivity.zilla.runtime.engine.config.ConditionConfig)

Example 8 with RouteConfig

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\"}]}}"));
}
Also used : BindingConfig(io.aklivity.zilla.runtime.engine.config.BindingConfig) RouteConfig(io.aklivity.zilla.runtime.engine.config.RouteConfig) Test(org.junit.Test)

Aggregations

RouteConfig (io.aklivity.zilla.runtime.engine.config.RouteConfig)8 BindingConfig (io.aklivity.zilla.runtime.engine.config.BindingConfig)4 Test (org.junit.Test)4 JsonObject (jakarta.json.JsonObject)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Engine (io.aklivity.zilla.runtime.engine.Engine)1 EngineConfiguration (io.aklivity.zilla.runtime.engine.EngineConfiguration)1 ConditionConfig (io.aklivity.zilla.runtime.engine.config.ConditionConfig)1 KindConfig (io.aklivity.zilla.runtime.engine.config.KindConfig)1 NamespaceConfig (io.aklivity.zilla.runtime.engine.config.NamespaceConfig)1 NamespacedRef (io.aklivity.zilla.runtime.engine.config.NamespacedRef)1 OptionsConfig (io.aklivity.zilla.runtime.engine.config.OptionsConfig)1 OptionsConfigAdapterSpi (io.aklivity.zilla.runtime.engine.config.OptionsConfigAdapterSpi)1 VaultConfig (io.aklivity.zilla.runtime.engine.config.VaultConfig)1 WithConfig (io.aklivity.zilla.runtime.engine.config.WithConfig)1 EngineExtContext (io.aklivity.zilla.runtime.engine.ext.EngineExtContext)1 EngineExtSpi (io.aklivity.zilla.runtime.engine.ext.EngineExtSpi)1 Tuning (io.aklivity.zilla.runtime.engine.internal.Tuning)1 TestConditionConfig (io.aklivity.zilla.runtime.engine.internal.config.ConditionConfigAdapterTest.TestConditionConfig)1