From 3ef634e904d0a5ae8cdb72e051c604eff2e68017 Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Tue, 14 Apr 2020 17:45:16 +0200 Subject: [PATCH 1/2] yaml: add support for CircuitBreaker EIP --- .../yaml/parser/CircuitBreakerStepParser.java | 115 ++++++++++++++++++ .../yaml/parser/CircuitBreakerTest.groovy | 92 ++++++++++++++ .../loader/yaml/parser/ClaimCheckTest.groovy | 3 +- 3 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/CircuitBreakerStepParser.java create mode 100644 camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/CircuitBreakerTest.groovy diff --git a/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/CircuitBreakerStepParser.java b/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/CircuitBreakerStepParser.java new file mode 100644 index 000000000..cbf6b3d45 --- /dev/null +++ b/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/CircuitBreakerStepParser.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.k.loader.yaml.parser; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonAlias; +import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition; +import org.apache.camel.k.annotation.yaml.YAMLStepParser; +import org.apache.camel.k.loader.yaml.model.Step; +import org.apache.camel.model.CircuitBreakerDefinition; +import org.apache.camel.model.HystrixConfigurationDefinition; +import org.apache.camel.model.OnFallbackDefinition; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.Resilience4jConfigurationCommon; +import org.apache.camel.model.Resilience4jConfigurationDefinition; +import org.apache.camel.reifier.CircuitBreakerReifier; +import org.apache.camel.reifier.OnFallbackReifier; + +@YAMLStepParser("circuit-breaker") +public class CircuitBreakerStepParser implements ProcessorStepParser { + @Override + public ProcessorDefinition toProcessor(Context context) { + CBDefinition definition = context.node(CBDefinition.class); + + ProcessorDefinition processor = StepParserSupport.convertSteps( + context, + definition.delegate, + definition.steps + ); + + if (definition.onFallback != null) { + StepParserSupport.convertSteps( + context, + definition.onFallback, + definition.onFallback.steps + ); + + definition.delegate.setOnFallback(definition.onFallback); + } + + return processor; + } + + @YAMLNodeDefinition(reifiers = CircuitBreakerReifier.class) + public static final class CBDefinition { + public CircuitBreakerDefinition delegate = new CircuitBreakerDefinition(); + public FBDefinition onFallback; + + public List steps; + + public HystrixConfigurationDefinition getHystrixConfiguration() { + return delegate.getHystrixConfiguration(); + } + + public void setHystrixConfiguration(HystrixConfigurationDefinition hystrixConfiguration) { + delegate.setHystrixConfiguration(hystrixConfiguration); + } + + public Resilience4jConfigurationCommon getResilience4jConfiguration() { + return delegate.getResilience4jConfiguration(); + } + + public void setResilience4jConfiguration(Resilience4jConfigurationDefinition resilience4jConfiguration) { + delegate.setResilience4jConfiguration(resilience4jConfiguration); + } + + public String getConfigurationRef() { + return delegate.getConfigurationRef(); + } + + public void setConfigurationRef(String configurationRef) { + delegate.setConfigurationRef(configurationRef); + } + + public FBDefinition getOnFallback() { + return onFallback; + } + + public void setOnFallback(FBDefinition onFallback) { + this.onFallback = onFallback; + } + } + + @YAMLNodeDefinition(reifiers = OnFallbackReifier.class) + public static final class FBDefinition extends OnFallbackDefinition { + public List steps; + + @Override + public String getFallbackViaNetwork() { + return super.getFallbackViaNetwork(); + } + + @JsonAlias({"fallback-via-network", "via-network"}) + @Override + public void setFallbackViaNetwork(String fallbackViaNetwork) { + super.setFallbackViaNetwork(fallbackViaNetwork); + } + } +} + diff --git a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/CircuitBreakerTest.groovy b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/CircuitBreakerTest.groovy new file mode 100644 index 000000000..a4068db3e --- /dev/null +++ b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/CircuitBreakerTest.groovy @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.k.loader.yaml.parser + +import org.apache.camel.k.loader.yaml.TestSupport +import org.apache.camel.model.CircuitBreakerDefinition +import org.apache.camel.model.ToDefinition + +class CircuitBreakerTest extends TestSupport { + def "definition"() { + given: + def stepContext = stepContext(''' + configuration-ref: "my-config" + resilience4j-configuration: + failure-rate-threshold: "10" + hystrix-configuration: + group-key: "my-group" + on-fallback: + fallback-via-network: "true" + steps: + - log: + message: "test" + ''') + when: + def processor = new CircuitBreakerStepParser().toProcessor(stepContext) + then: + with(processor, CircuitBreakerDefinition) { + configurationRef == 'my-config' + + resilience4jConfiguration != null + resilience4jConfiguration.failureRateThreshold == '10' + + hystrixConfiguration != null + hystrixConfiguration.groupKey == 'my-group' + + onFallback != null + onFallback.fallbackViaNetwork == "true" + } + } + + def "definition with alias"() { + given: + def stepContext = stepContext(''' + on-fallback: + via-network: "true" + ''') + when: + def processor = new CircuitBreakerStepParser().toProcessor(stepContext) + then: + with(processor, CircuitBreakerDefinition) { + onFallback != null + onFallback.fallbackViaNetwork == "true" + } + } + + def "definition with steps"() { + given: + def stepContext = stepContext(''' + steps: + - to: "log:cb" + on-fallback: + steps: + - to: "log:fb" + ''') + when: + def processor = new CircuitBreakerStepParser().toProcessor(stepContext) + then: + with(processor, CircuitBreakerDefinition) { + with (outputs[0], ToDefinition) { + endpointUri == "log:cb" + } + with (onFallback.outputs[0], ToDefinition) { + endpointUri == "log:fb" + } + } + } +} + diff --git a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/ClaimCheckTest.groovy b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/ClaimCheckTest.groovy index 4e861844b..a3005673b 100644 --- a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/ClaimCheckTest.groovy +++ b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/ClaimCheckTest.groovy @@ -16,9 +16,10 @@ */ package org.apache.camel.k.loader.yaml.parser +import org.apache.camel.k.loader.yaml.TestSupport import org.apache.camel.model.ClaimCheckDefinition -class ClaimCheckTest extends org.apache.camel.k.loader.yaml.TestSupport { +class ClaimCheckTest extends TestSupport { def "definition"() { given: def stepContext = stepContext(''' From 584792a0b15bac609dc313a3ca4327145b3554b0 Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Tue, 14 Apr 2020 18:25:09 +0200 Subject: [PATCH 2/2] yaml: add support for LoadBalance EIP --- .../yaml/parser/LoadBalanceStepParser.java | 158 +++++++++++++++ .../camel/k/loader/yaml/TestSupport.groovy | 6 + .../loader/yaml/parser/LoadBalanceTest.groovy | 186 ++++++++++++++++++ .../GenerateYamlLoaderSupportClasses.java | 3 + .../GenerateYamlParserSupportClasses.java | 53 +++++ .../k/tooling/maven/GenerateYamlSupport.java | 3 +- 6 files changed, 407 insertions(+), 2 deletions(-) create mode 100644 camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/LoadBalanceStepParser.java create mode 100644 camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/LoadBalanceTest.groovy diff --git a/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/LoadBalanceStepParser.java b/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/LoadBalanceStepParser.java new file mode 100644 index 000000000..811e03560 --- /dev/null +++ b/camel-k-loader-yaml/camel-k-loader-yaml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/LoadBalanceStepParser.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.k.loader.yaml.parser; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import org.apache.camel.Expression; +import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition; +import org.apache.camel.k.annotation.yaml.YAMLStepParser; +import org.apache.camel.k.loader.yaml.model.Step; +import org.apache.camel.model.ExpressionSubElementDefinition; +import org.apache.camel.model.LoadBalanceDefinition; +import org.apache.camel.model.LoadBalancerDefinition; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.language.ExpressionDefinition; +import org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.RandomLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.RoundRobinLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.StickyLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.TopicLoadBalancerDefinition; +import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition; +import org.apache.camel.reifier.LoadBalanceReifier; + +@YAMLStepParser("load-balance") +public class LoadBalanceStepParser implements ProcessorStepParser { + @Override + public ProcessorDefinition toProcessor(Context context) { + Definition definition = context.node(Definition.class); + + return StepParserSupport.convertSteps( + context, + definition, + definition.steps + ); + } + + @YAMLNodeDefinition(reifiers = LoadBalanceReifier.class) + public static final class Definition extends LoadBalanceDefinition { + public List steps; + + @JsonAlias({"load-balancer-type", "type"}) + @JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.WRAPPER_OBJECT + ) + @Override + public void setLoadBalancerType(LoadBalancerDefinition loadbalancer) { + super.setLoadBalancerType(loadbalancer); + } + + @Override + public LoadBalancerDefinition getLoadBalancerType() { + return super.getLoadBalancerType(); + } + + @JsonAlias("random") + public void setRandom(RandomLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias({"customLoadBalancer", "custom"}) + public void setCustomLoadBalancer(CustomLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("failover") + public void setFailover(FailoverLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("sticky") + public void setSticky(Sticky definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("topic") + public void setTopic(TopicLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("weighted") + public void setWeighted(WeightedLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("roundRobin") + public void setRoundRobin(RoundRobinLoadBalancerDefinition definition) { + if (getLoadBalancerType() != null) { + throw new IllegalArgumentException("A load-balancer has already been set"); + } + setLoadBalancerType(definition); + } + + @JsonAlias("custom") + public void setCustom(CustomLoadBalancerDefinition definition) { + setCustomLoadBalancer(definition); + } + + public static final class Sticky extends StickyLoadBalancerDefinition implements HasExpression { + @JsonIgnore + @Override + public void setCorrelationExpression(Expression expression) { + super.setCorrelationExpression(expression); + } + + @Override + public void setExpression(ExpressionDefinition expressionDefinition) { + super.setCorrelationExpression(expressionDefinition); + } + + @Override + public ExpressionDefinition getExpression() { + final ExpressionSubElementDefinition expression = super.getCorrelationExpression(); + + return expression != null + ? super.getCorrelationExpression().getExpressionType() + : null; + } + } + } +} + diff --git a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/TestSupport.groovy b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/TestSupport.groovy index 1b1c4d175..97d5b4208 100644 --- a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/TestSupport.groovy +++ b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/TestSupport.groovy @@ -21,7 +21,9 @@ import groovy.util.logging.Slf4j import org.apache.camel.CamelContext import org.apache.camel.component.mock.MockEndpoint import org.apache.camel.impl.DefaultCamelContext +import org.apache.camel.k.loader.yaml.parser.ProcessorStepParser import org.apache.camel.k.loader.yaml.parser.StepParser +import org.apache.camel.model.ProcessorDefinition import spock.lang.Specification import java.nio.charset.StandardCharsets @@ -99,4 +101,8 @@ class TestSupport extends Specification { return closure.delegate } + + static ProcessorDefinition toProcessor(Class type, String content) { + return type.getConstructor().newInstance().toProcessor(stepContext(content)) + } } diff --git a/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/LoadBalanceTest.groovy b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/LoadBalanceTest.groovy new file mode 100644 index 000000000..b5843d3de --- /dev/null +++ b/camel-k-loader-yaml/camel-k-loader-yaml/src/test/groovy/org/apache/camel/k/loader/yaml/parser/LoadBalanceTest.groovy @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.k.loader.yaml.parser + +import org.apache.camel.k.loader.yaml.TestSupport +import org.apache.camel.model.LoadBalanceDefinition +import org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition +import org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition +import org.apache.camel.model.loadbalancer.RandomLoadBalancerDefinition +import org.apache.camel.model.loadbalancer.RoundRobinLoadBalancerDefinition +import org.apache.camel.model.loadbalancer.TopicLoadBalancerDefinition +import org.apache.camel.model.loadbalancer.WeightedLoadBalancerDefinition + +class LoadBalanceTest extends TestSupport { + + def "random load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + random: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof RandomLoadBalancerDefinition + } + } + + def "custom load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + custom-load-balancer: + ref: my-lb + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, CustomLoadBalancerDefinition) { + ref == 'my-lb' + } + } + } + + def "custom load balancer (alias)"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + custom: + ref: my-lb + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, CustomLoadBalancerDefinition) { + ref == 'my-lb' + } + } + } + + def "failover load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + failover: + exceptions: + - java.lang.Exception + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, FailoverLoadBalancerDefinition) { + exceptions.size() == 1 + } + } + } + + def "sticky load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + sticky: + simple: '${header.id}' + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, LoadBalanceStepParser.Definition.Sticky) { + correlationExpression.expressionType.expression == '${header.id}' + } + } + } + + def "sticky load balancer (expression)"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + sticky: + expression: + simple: '${header.id}' + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, LoadBalanceStepParser.Definition.Sticky) { + correlationExpression.expressionType.expression == '${header.id}' + } + } + } + + def "topic load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + topic: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof TopicLoadBalancerDefinition + } + } + + def "weighted load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + weighted: + distribution-ratio: "1;2;3" + distribution-ratio-delimiter: ";" + ''') + then: + with(processor, LoadBalanceDefinition) { + with(loadBalancerType, WeightedLoadBalancerDefinition) { + distributionRatio == '1;2;3' + distributionRatioDelimiter == ';' + } + } + } + + def "round-robin load balancer"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + roundRobin: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof RoundRobinLoadBalancerDefinition + } + } + + def "round-robin load balancer (alias)"() { + when: + def processor = toProcessor(LoadBalanceStepParser, ''' + round-robin: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof RoundRobinLoadBalancerDefinition + } + } + + def "load balancer (type)"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + load-balancer-type: + random: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof RandomLoadBalancerDefinition + } + } + + def "load balancer (type alias)"() { + when: + def processor = toProcessor(LoadBalanceStepParser,''' + type: + random: {} + ''') + then: + with(processor, LoadBalanceDefinition) { + loadBalancerType instanceof RandomLoadBalancerDefinition + } + } + +} diff --git a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlLoaderSupportClasses.java b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlLoaderSupportClasses.java index 6ba82379c..8a4c36e8a 100644 --- a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlLoaderSupportClasses.java +++ b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlLoaderSupportClasses.java @@ -91,6 +91,9 @@ public final TypeSpec generateJacksonModule() { definitions(DATAFORMAT_DEFINITION_CLASS).forEach( (k, v) -> mb.addStatement("context.registerSubtypes(new com.fasterxml.jackson.databind.jsontype.NamedType($T.class, $S))", v, k) ); + definitions(LOAD_BALANCE_DEFINITION_CLASS).forEach( + (k, v) -> mb.addStatement("context.registerSubtypes(new com.fasterxml.jackson.databind.jsontype.NamedType($T.class, $S))", v, k) + ); type.addMethod(mb.build()); diff --git a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlParserSupportClasses.java b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlParserSupportClasses.java index 04a0c590b..ad151a058 100644 --- a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlParserSupportClasses.java +++ b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlParserSupportClasses.java @@ -30,6 +30,7 @@ import com.squareup.javapoet.MethodSpec; import com.squareup.javapoet.TypeSpec; import org.apache.camel.model.DataFormatDefinition; +import org.apache.camel.model.LoadBalancerDefinition; import org.apache.camel.model.language.ExpressionDefinition; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.WordUtils; @@ -56,6 +57,10 @@ public void execute() throws MojoFailureException { .indent(" ") .build() .writeTo(Paths.get(output)); + JavaFile.builder("org.apache.camel.k.loader.yaml.parser", generateHasLoadBalancerType()) + .indent(" ") + .build() + .writeTo(Paths.get(output)); } catch (IOException e) { throw new MojoFailureException(e.getMessage()); } @@ -161,4 +166,52 @@ public final TypeSpec generateHasDataFormat() { return type.build(); } + + public final TypeSpec generateHasLoadBalancerType() { + TypeSpec.Builder type = TypeSpec.interfaceBuilder("HasLoadBalancerType"); + type.addModifiers(Modifier.PUBLIC); + type.addMethod( + MethodSpec.methodBuilder("setLoadBalancerType") + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) + .addParameter(LoadBalancerDefinition.class, "loadbalancer") + .addAnnotation( + AnnotationSpec.builder(JsonTypeInfo.class) + .addMember("use", "$L", "JsonTypeInfo.Id.NAME") + .addMember("include", "$L", "JsonTypeInfo.As.WRAPPER_OBJECT") + .build()) + .build() + ); + + type.addMethod( + MethodSpec.methodBuilder("getLoadBalancerType") + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) + .returns(LoadBalancerDefinition.class) + .build() + ); + + definitions(LOAD_BALANCE_DEFINITION_CLASS).forEach( + (k, v) -> { + String name = k; + name = WordUtils.capitalize(name, '_', '-'); + name = StringUtils.remove(name, "_"); + name = StringUtils.remove(name, "-"); + + type.addMethod(MethodSpec.methodBuilder("set" + name) + .addAnnotation( + AnnotationSpec.builder(JsonAlias.class).addMember("value", "$S", k).build()) + .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) + .addParameter(v, "definition") + .addCode( + CodeBlock.builder() + .beginControlFlow("if (getLoadBalancerType() != null)") + .addStatement("throw new IllegalArgumentException(\"A load-balancer has already been set\")") + .endControlFlow() + .addStatement("setLoadBalancerType(definition);").build()) + .build() + ); + } + ); + + return type.build(); + } } diff --git a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlSupport.java b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlSupport.java index 055c4dd60..22bb25b36 100644 --- a/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlSupport.java +++ b/tooling/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateYamlSupport.java @@ -47,9 +47,8 @@ public abstract class GenerateYamlSupport extends AbstractMojo { public static final DotName EXPRESSION_DEFINITION_CLASS = DotName.createSimple("org.apache.camel.model.language.ExpressionDefinition"); public static final DotName DATAFORMAT_DEFINITION_CLASS = DotName.createSimple("org.apache.camel.model.DataFormatDefinition"); public static final DotName XMLROOTELEMENT_ANNOTATION_CLASS = DotName.createSimple("javax.xml.bind.annotation.XmlRootElement"); - public static final DotName YAML_STEP_DEFINITION_CLASS = DotName.createSimple("org.apache.camel.k.loader.yaml.model.Step$Definition"); - public static final DotName YAML_STEP_PARSER_ANNOTATION = DotName.createSimple("org.apache.camel.k.annotation.yaml.YAMLStepParser"); public static final DotName YAML_STEP_DEFINITION_ANNOTATION = DotName.createSimple("org.apache.camel.k.annotation.yaml.YAMLNodeDefinition"); + public static final DotName LOAD_BALANCE_DEFINITION_CLASS = DotName.createSimple("org.apache.camel.model.LoadBalancerDefinition"); @Parameter(defaultValue = "${project}", readonly = true, required = true) protected MavenProject project;