Skip to content

Commit

Permalink
CAMEL-20687: camel-kamelet - Make kamelets that have ids assigned - t… (
Browse files Browse the repository at this point in the history
apache#13849)

* CAMEL-20687: camel-kamelet - Make kamelets that have ids assigned - to use nodePrefixId for avoid duplicate id clashes

* CAMEL-20687: camel-kamelet - Make kamelets that have ids assigned - to use nodePrefixId for avoid duplicate id clashes
  • Loading branch information
davsclaus authored Apr 18, 2024
1 parent d849631 commit 2de3e3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public final class Kamelet {
public static final String PARAM_ROUTE_ID = "routeId";
public static final String PARAM_TEMPLATE_ID = "templateId";
public static final String PARAM_LOCATION = "location";
public static final String PARAM_UUID = "uuid";
public static final String DEFAULT_LOCATION = "classpath:/kamelets";
public static final String NO_ERROR_HANDLER = "noErrorHandler";

Expand Down Expand Up @@ -77,7 +78,11 @@ public static String extractTemplateId(CamelContext context, String remaining, M
return answer;
}

public static String extractRouteId(CamelContext context, String remaining, Map<String, Object> parameters) {
public static String extractUuid() {
return UUID.generateUuid();
}

public static String extractRouteId(CamelContext context, String remaining, Map<String, Object> parameters, String uuid) {
Object param = parameters.get(PARAM_ROUTE_ID);
if (param != null) {
return CamelContextHelper.mandatoryConvertTo(context, String.class, param);
Expand All @@ -92,7 +97,7 @@ public static String extractRouteId(CamelContext context, String remaining, Map<
answer = StringHelper.after(remaining, "/");
}
if (answer == null) {
answer = extractTemplateId(context, remaining, parameters) + "-" + UUID.generateUuid();
answer = extractTemplateId(context, remaining, parameters) + "-" + uuid;
}

return answer;
Expand Down Expand Up @@ -126,15 +131,18 @@ public static void extractKameletProperties(CamelContext context, Map<String, Ob
public static RouteDefinition templateToRoute(RouteTemplateDefinition in, Map<String, Object> parameters) {
final String rid = (String) parameters.get(PARAM_ROUTE_ID);
final boolean noErrorHandler = (boolean) parameters.get(NO_ERROR_HANDLER);
final String uuid = (String) parameters.get(PARAM_UUID);

ObjectHelper.notNull(rid, PARAM_ROUTE_ID);
ObjectHelper.notNull(uuid, PARAM_UUID);

RouteDefinition def = in.asRouteDefinition();
// mark this as created from a kamelet
def.setKamelet(true);
def.setLocation(in.getLocation());
def.setLineNumber(in.getLineNumber());
def.setId(rid);
def.setNodePrefixId(uuid);
if (noErrorHandler) {
def.setErrorHandlerFactory(new NoErrorHandlerBuilder());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.apache.camel.component.kamelet.Kamelet.PARAM_LOCATION;
import static org.apache.camel.component.kamelet.Kamelet.PARAM_ROUTE_ID;
import static org.apache.camel.component.kamelet.Kamelet.PARAM_TEMPLATE_ID;
import static org.apache.camel.component.kamelet.Kamelet.PARAM_UUID;

/**
* Materialize route templates
Expand Down Expand Up @@ -105,12 +106,14 @@ public Processor getKameletEip(String key) {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
final String templateId = Kamelet.extractTemplateId(getCamelContext(), remaining, parameters);
final String routeId = Kamelet.extractRouteId(getCamelContext(), remaining, parameters);
final String uuid = Kamelet.extractUuid();
final String routeId = Kamelet.extractRouteId(getCamelContext(), remaining, parameters, uuid);
final String loc = Kamelet.extractLocation(getCamelContext(), parameters);

parameters.remove(PARAM_TEMPLATE_ID);
parameters.remove(PARAM_ROUTE_ID);
parameters.remove(PARAM_LOCATION);
parameters.remove(PARAM_UUID);

// manually need to resolve raw parameters as input to the kamelet because
// resolveRawParameterValues is false
Expand Down Expand Up @@ -224,6 +227,7 @@ protected void doInit() throws Exception {
//
kameletProperties.put(PARAM_TEMPLATE_ID, templateId);
kameletProperties.put(PARAM_ROUTE_ID, routeId);
kameletProperties.put(PARAM_UUID, uuid);
kameletProperties.put(NO_ERROR_HANDLER, endpoint.isNoErrorHandler());

// set kamelet specific properties
Expand Down Expand Up @@ -415,6 +419,7 @@ public void createRouteForEndpoint(KameletEndpoint endpoint) throws Exception {
final String templateId = endpoint.getTemplateId();
final String routeId = endpoint.getRouteId();
final String loc = endpoint.getLocation() != null ? endpoint.getLocation() : getLocation();
final String uuid = (String) endpoint.getKameletProperties().get(PARAM_UUID);

if (context.getRouteTemplateDefinition(templateId) == null && loc != null) {
LOGGER.debug("Loading route template={} from {}", templateId, loc);
Expand All @@ -424,7 +429,7 @@ public void createRouteForEndpoint(KameletEndpoint endpoint) throws Exception {

LOGGER.debug("Creating route from template={} and id={}", templateId, routeId);
try {
String id = context.addRouteFromTemplate(routeId, templateId, endpoint.getKameletProperties());
String id = context.addRouteFromTemplate(routeId, templateId, uuid, endpoint.getKameletProperties());
RouteDefinition def = context.getRouteDefinition(id);

// start the route if not already started
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@

import java.util.UUID;

import org.apache.camel.FailedToCreateRouteException;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class KameletRouteTest extends CamelTestSupport {
@Test
Expand All @@ -44,19 +42,6 @@ public void testChain() {
fluentTemplate.toF("direct:chain").withBody(body).request(String.class)).isEqualTo("b-a-" + body);
}

@Test
public void duplicateRouteId() {
RouteBuilder rb = new RouteBuilder(context) {
@Override
public void configure() {
from("direct:start")
.to("kamelet:echo/test?prefix=test");
}
};

assertThrows(FailedToCreateRouteException.class, () -> rb.addRoutesToCamelContext(context));
}

// **********************************************
//
// test set-up
Expand Down

0 comments on commit 2de3e3a

Please sign in to comment.