Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feature(eip): Add Try-Catch EIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Delawen committed Dec 19, 2022
1 parent e4562af commit 550e2fc
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,26 @@ spec:
simple: ola ke ase
- sort:
comparator: myComparator
- do-try:
steps:
- set-body:
simple: abc
- set-exchange-pattern: InOut
do-catch:
- exception:
- java.io.FileNotFoundException
- java.io.IOException
on-when:
simple: ${body.size()} == 1
steps:
- log:
message: test
logging-level: INFO
log-name: yaml
do-finally:
steps:
- enrich:
expression:
simple: ${body}
- to:
uri: kamelet:sink
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import io.kaoto.backend.model.deployment.kamelet.step.ToFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TransactedFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TransformFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TryCatchFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.UnmarshalFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.UriFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.ValidateFlowStep;
Expand Down Expand Up @@ -397,6 +398,9 @@ private FlowStep processStep(final Step step, final boolean to) {
case "choice":
flowStep = new ChoiceFlowStep(step, this);
break;
case "do-try":
flowStep = new TryCatchFlowStep(step, this);
break;
case "filter":
flowStep = new FilterFlowStep(step, this);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import io.kaoto.backend.model.deployment.kamelet.step.ToFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TransactedFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TransformFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.TryCatchFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.UnmarshalFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.UriFlowStep;
import io.kaoto.backend.model.deployment.kamelet.step.ValidateFlowStep;
Expand Down Expand Up @@ -303,6 +304,7 @@ private void addEIP() {
ToFlowStep.class,
TransactedFlowStep.class,
TransformFlowStep.class,
TryCatchFlowStep.class,
UnmarshalFlowStep.class,
UriFlowStep.class,
ValidateFlowStep.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CircuitBreaker extends EIPStep {
private List<FlowStep> steps;

@JsonProperty(ON_FALLBACK_LABEL)
private CircuitBreakerOnFallback onFallback;
private GenericFlowWithSteps onFallback;

@JsonProperty(RESILIENCE_4_J_CONFIGURATION_LABEL)
private Map<String, String> resilience4jConfiguration;
Expand All @@ -50,7 +50,7 @@ public CircuitBreaker() {
@JsonCreator
public CircuitBreaker(
final @JsonProperty(STEPS_LABEL) List<FlowStep> steps,
final @JsonProperty(ON_FALLBACK_LABEL) CircuitBreakerOnFallback onFallback,
final @JsonProperty(ON_FALLBACK_LABEL) GenericFlowWithSteps onFallback,
final @JsonProperty(RESILIENCE_4_J_CONFIGURATION_LABEL) Map<String, String> resilience4jConfiguration,
final @JsonProperty(FAULT_TOLERANCE_CONFIGURATION_LABEL) Map<String, String> faultToleranceConfiguration,
final @JsonProperty(CONFIGURATION_LABEL) String configuration,
Expand All @@ -72,7 +72,7 @@ public CircuitBreaker(final Step step, final KamelPopulator kameletPopulator) {
setSteps(kameletPopulator.processSteps(step.getBranches().get(0)));
}
if (step.getBranches().size() > 1) {
setOnFallback(new CircuitBreakerOnFallback());
setOnFallback(new GenericFlowWithSteps());
getOnFallback().setSteps(kameletPopulator.processSteps(step.getBranches().get(1)));
}
}
Expand Down Expand Up @@ -169,11 +169,11 @@ public void setSteps(final List<FlowStep> steps) {
this.steps = steps;
}

public CircuitBreakerOnFallback getOnFallback() {
public GenericFlowWithSteps getOnFallback() {
return onFallback;
}

public void setOnFallback(final CircuitBreakerOnFallback onFallback) {
public void setOnFallback(final GenericFlowWithSteps onFallback) {
this.onFallback = onFallback;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.kaoto.backend.model.deployment.kamelet.step;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.kaoto.backend.KamelPopulator;
import io.kaoto.backend.model.deployment.kamelet.FlowStep;
import io.kaoto.backend.model.deployment.kamelet.expression.Expression;
import io.kaoto.backend.model.step.Branch;

import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


@JsonIgnoreProperties(ignoreUnknown = true)
public class DoCatch implements Serializable {

private List<FlowStep> steps;
private List<String> exceptions;
private Expression onWhen;

public DoCatch(Branch branch, KamelPopulator kameletPopulator) {
setSteps(kameletPopulator.processSteps(branch));
var exception = branch.getParameters().stream().filter(p -> p.getId().equalsIgnoreCase("exceptions")).findAny();
if (exception.isPresent()) {
setExceptions(new LinkedList<>());
Arrays.stream((Object[]) exception.get().getValue()).forEach(v -> getExceptions().add(String.valueOf(v)));
}
var onW = branch.getParameters().stream().filter(p -> p.getId().equalsIgnoreCase("on-when")).findAny();
if (onW.isPresent()) {
setOnWhen(new Expression(onW.get().getValue()));
}
}

@JsonCreator
public DoCatch(
final @JsonProperty("steps") List<FlowStep> steps,
final @JsonProperty("exception") List<String> exceptions,
final @JsonProperty("on-when") Expression onWhen,
final @JsonProperty("onWhen") Expression onWhen2) {
super();
setSteps(steps);
setExceptions(exceptions);
setOnWhen(onWhen != null ? onWhen : onWhen2);
}

public List<String> getExceptions() {
return exceptions;
}

public void setExceptions(final List<String> exceptions) {
this.exceptions = exceptions;
}

public List<FlowStep> getSteps() {
return steps;
}

public void setSteps(final List<FlowStep> steps) {
this.steps = steps;
}

public Expression getOnWhen() {
return onWhen;
}

public void setOnWhen(final Expression onWhen) {
this.onWhen = onWhen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ private Map<String, Class> getFlowSteps() {
steps.put("to", ToFlowStep.class);
steps.put("transacted", TransactedFlowStep.class);
steps.put("transform", TransformFlowStep.class);
steps.put("do-try", TryCatchFlowStep.class);
steps.put("doTry", TryCatchFlowStep.class);
steps.put("unmarshal", UnmarshalFlowStep.class);
steps.put("uri", UriFlowStep.class);
steps.put("validate", ValidateFlowStep.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.kaoto.backend.model.deployment.kamelet.FlowStep;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;


@JsonPropertyOrder({"steps"})
@JsonIgnoreProperties(ignoreUnknown = true)
public class CircuitBreakerOnFallback implements Serializable {
@Serial
private static final long serialVersionUID = 3541785323L;
public class GenericFlowWithSteps implements Serializable {

@JsonProperty("steps")
private List<FlowStep> steps;
Expand All @@ -23,8 +20,7 @@ public List<FlowStep> getSteps() {
return steps;
}

public void setSteps(
final List<FlowStep> steps) {
public void setSteps(final List<FlowStep> steps) {
this.steps = steps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package io.kaoto.backend.model.deployment.kamelet.step;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.kaoto.backend.KamelPopulator;
import io.kaoto.backend.api.metadata.catalog.StepCatalog;
import io.kaoto.backend.api.service.step.parser.kamelet.KameletStepParserService;
import io.kaoto.backend.model.deployment.kamelet.FlowStep;
import io.kaoto.backend.model.parameter.ArrayParameter;
import io.kaoto.backend.model.parameter.ObjectParameter;
import io.kaoto.backend.model.parameter.Parameter;
import io.kaoto.backend.model.step.Branch;
import io.kaoto.backend.model.step.Step;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class TryCatch extends EIPStep {

public static final String STEPS_LABEL = "steps";

public static final String DO_CATCH_LABEL = "do-catch";
public static final String DO_CATCH_LABEL2 = "doCatch";

public static final String DO_FINALLY_LABEL = "do-finally";
public static final String DO_FINALLY_LABEL2 = "doFinally";

private List<FlowStep> steps;
private List<DoCatch> doCatch;
private GenericFlowWithSteps doFinally;

public TryCatch() {
super();
}

@JsonCreator
public TryCatch(
final @JsonProperty(STEPS_LABEL) List<FlowStep> steps,
final @JsonProperty(DO_CATCH_LABEL) List<DoCatch> doCatch,
final @JsonProperty(DO_CATCH_LABEL2) List<DoCatch> doCatch2,
final @JsonProperty(DO_FINALLY_LABEL) GenericFlowWithSteps doFinally,
final @JsonProperty(DO_FINALLY_LABEL2) GenericFlowWithSteps doFinally2) {
super();
setSteps(steps);
setDoCatch(doCatch != null ? doCatch : doCatch2);
setDoFinally(doFinally != null ? doFinally : doFinally2);
}

public TryCatch(final Step step, final KamelPopulator kameletPopulator) {
super(step);

if (step.getBranches() != null) {
if (!step.getBranches().isEmpty()) {
var stepsBranch = step.getBranches().stream()
.filter(b -> b.getIdentifier().equalsIgnoreCase(STEPS_LABEL))
.findAny();
if (stepsBranch.isPresent()) {
setSteps(kameletPopulator.processSteps(stepsBranch.get()));
}
}
this.setDoCatch(new LinkedList<>());
step.getBranches().stream()
.filter(b -> !b.getIdentifier().equalsIgnoreCase(DO_FINALLY_LABEL)
&& !b.getIdentifier().equalsIgnoreCase(STEPS_LABEL))
.forEach(branch -> this.getDoCatch().add(new DoCatch(branch, kameletPopulator)));
var doFinally =
step.getBranches().stream()
.filter(b -> b.getIdentifier().equalsIgnoreCase(DO_FINALLY_LABEL))
.findAny();
if (doFinally.isPresent()) {
this.setDoFinally(new GenericFlowWithSteps());
this.getDoFinally().setSteps(kameletPopulator.processSteps(doFinally.get()));
}
}
}

@Override
public Map<String, Object> getRepresenterProperties() {
Map<String, Object> properties = new LinkedHashMap<>();
properties.put(STEPS_LABEL, this.getSteps());
List<Map<String, Object>> doC = new LinkedList<>();
for (var doCatch : this.getDoCatch()) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("exception", doCatch.getExceptions());
map.put("on-when", doCatch.getOnWhen());
map.put(STEPS_LABEL, doCatch.getSteps());
doC.add(map);
}
properties.put(DO_CATCH_LABEL, doC);
Map<String, Object> doF = new LinkedHashMap<>();
doF.put(STEPS_LABEL, doFinally.getSteps());
properties.put(DO_FINALLY_LABEL, doF);
return properties;
}

@Override
public void processBranches(final Step step, final StepCatalog catalog,
final KameletStepParserService kameletStepParserService) {
step.setBranches(new LinkedList<>());
var identifier = STEPS_LABEL;
step.getBranches().add(createBranch(identifier, this.getSteps(), kameletStepParserService));
if (this.getDoCatch() != null) {
for (DoCatch doC : this.getDoCatch()) {
final var branch = createBranch(DO_CATCH_LABEL, doC.getSteps(), kameletStepParserService);
branch.setParameters(new LinkedList<>());
setExceptions(doC, branch);
setOnWhen(doC, branch);
step.getBranches().add(branch);
}
}
if (this.getDoFinally() != null) {
step.getBranches()
.add(createBranch(DO_FINALLY_LABEL, this.getDoFinally().getSteps(), kameletStepParserService));
}
}

private void setOnWhen(final DoCatch doC, final Branch branch) {
final var onWhen = new ObjectParameter();
onWhen.setValue(doC.getOnWhen());
onWhen.setTitle("On When");
onWhen.setId("on-when");
onWhen.setNullable(true);
branch.getParameters().add(onWhen);
}

private void setExceptions(final DoCatch doC, final Branch branch) {
final var exceptions = new ArrayParameter();
exceptions.setValue(doC.getExceptions().toArray());
exceptions.setTitle("Exceptions");
exceptions.setId("exceptions");
exceptions.setNullable(false);
branch.getParameters().add(exceptions);
}


@Override
protected void assignAttribute(final Parameter parameter) {
}

@Override
protected void assignProperty(final Parameter parameter) {
}

public List<FlowStep> getSteps() {
return steps;
}

public void setSteps(final List<FlowStep> steps) {
this.steps = steps;
}

public List<DoCatch> getDoCatch() {
return doCatch;
}

public void setDoCatch(final List<DoCatch> doCatch) {
this.doCatch = doCatch;
}

public GenericFlowWithSteps getDoFinally() {
return doFinally;
}

public void setDoFinally(final GenericFlowWithSteps doFinally) {
this.doFinally = doFinally;
}
}
Loading

0 comments on commit 550e2fc

Please sign in to comment.