Skip to content

Commit

Permalink
kamelets: load templates at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed May 3, 2021
1 parent 4b8aef4 commit ca8083a
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/kamelet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,21 @@ Or add the coordinates to your existing project:
----

Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.

== Additional Camel Quarkus configuration

[width="100%",cols="80,5,15",options="header"]
|===
| Configuration property | Type | Default


|icon:lock[title=Fixed at build time] [[quarkus.camel.kamelet.kamelets]]`link:#quarkus.camel.kamelet.kamelets[quarkus.camel.kamelet.kamelets]`

List of kamelets.
| `string`
|
|===

[.configuration-legend]
icon:lock[title=Fixed at build time] Configuration property fixed at build time. All other configuration properties are overridable at runtime.

8 changes: 8 additions & 0 deletions extensions/kamelet/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-kamelet</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-yaml-dsl</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,91 @@
*/
package org.apache.camel.quarkus.component.kamelet.deployment;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.quarkus.component.kamelet.KameletConfiguration;
import org.apache.camel.quarkus.component.kamelet.KameletRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.ResourceHelper;

class KameletProcessor {

private static final String KAMELET_LOCATION = "/camel/kamelets";
private static final String FEATURE = "camel-kamelet";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
CamelContextCustomizerBuildItem configureTemplates(
KameletConfiguration configuration,
KameletRecorder recorder) throws Exception {

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
List<RouteTemplateDefinition> definitions = new ArrayList<>();

try (YamlRoutesBuilderLoader ybl = new YamlRoutesBuilderLoader()) {
ybl.setCamelContext(new DefaultCamelContext());
ybl.start();

for (String kamelet : configuration.kamelets.orElse(Collections.emptyList())) {
final String path = KAMELET_LOCATION + "/" + kamelet + ".yaml";

try (InputStream is = KameletProcessor.class.getResourceAsStream(path)) {
final ObjectNode definition = (ObjectNode) mapper.readTree(is);
final JsonNode properties = definition.requiredAt("/spec/definition/properties");
final JsonNode flow = mapper.createArrayNode().add(definition.requiredAt("/spec/flow"));
final Resource res = ResourceHelper.fromBytes(path, mapper.writeValueAsBytes(flow));

RouteTemplateDefinition rt = new RouteTemplateDefinition();
rt.setId(kamelet);

Iterator<Map.Entry<String, JsonNode>> it = properties.fields();
while (it.hasNext()) {
final Map.Entry<String, JsonNode> property = it.next();
final String key = property.getKey();
final JsonNode def = property.getValue().at("/default");

if (def.isMissingNode()) {
rt.templateParameter(key);
} else {
rt.templateParameter(key, def.asText());
}
}

RouteBuilder rb = (RouteBuilder) ybl.loadRoutesBuilder(res);
rb.configure();
if (rb.getRouteCollection().getRoutes().size() != 1) {
throw new IllegalArgumentException("TODO");
}

rt.setRoute(rb.getRouteCollection().getRoutes().get(0));

definitions.add(rt);
}
}
}

return new CamelContextCustomizerBuildItem(recorder.createTemplateLoaderCustomizer(definitions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.quarkus.component.kamelet;

import java.util.List;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.kamelet", phase = ConfigPhase.BUILD_TIME)
public class KameletConfiguration {
/**
* List of kamelets.
*/
@ConfigItem
public Optional<List<String>> kamelets;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.quarkus.component.kamelet;

import java.util.List;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.CamelContext;
import org.apache.camel.model.Model;
import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.quarkus.core.CamelContextCustomizer;

@Recorder
public class KameletRecorder {
public RuntimeValue<CamelContextCustomizer> createTemplateLoaderCustomizer(List<RouteTemplateDefinition> definitions) {
return new RuntimeValue<>(new CamelContextCustomizer() {
@Override
public void customize(CamelContext context) {
try {
context.getExtension(Model.class).addRouteTemplateDefinitions(definitions);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}
4 changes: 4 additions & 0 deletions integration-tests/kamelet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@
package org.apache.camel.quarkus.component.kamelet.it;

import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.model.Model;
import org.apache.camel.model.OptionalIdentifiedDefinition;

@Path("/kamelet")
public class KameletResource {

@Inject
CamelContext camelContext;

@Inject
FluentProducerTemplate fluentProducerTemplate;

Expand Down Expand Up @@ -64,4 +73,19 @@ public String bodyFromProperty() throws Exception {
public String kameletChain(String message) throws Exception {
return fluentProducerTemplate.to("direct:chain").withBody(message).request(String.class);
}

@Path("/list")
@GET
@Produces(MediaType.TEXT_PLAIN)
public JsonArray list() {
JsonArrayBuilder builder = Json.createArrayBuilder();

camelContext.getExtension(Model.class)
.getRouteTemplateDefinitions()
.stream()
.map(OptionalIdentifiedDefinition::getId)
.forEach(builder::add);

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
## limitations under the License.
## ---------------------------------------------------------------------------
camel.kamelet.setBodyFromProperties.bodyValueFromProperty=Camel Quarkus Kamelet Property

quarkus.camel.kamelet.kamelets = injector,logger
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# 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.
#

apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
name: injector-source-v1alpha1
labels:
camel.apache.org/kamelet.type: "source"
camel.apache.org/kamelet.name: "injector"
camel.apache.org/kamelet.version: "v1alpha1"
camel.apache.org/kamelet.revision: "1"
spec:
definition:
title: "Injector"
description: "Inject Data"
properties:
delay:
title: Delay
description: Delay
type: string
default: "1s"
types:
out:
mediaType: application/json
dependencies:
- "camel:timer"
flow:
from:
uri: timer
properties:
timerName: "{{routeId}}"
period: "{{delay}}"
steps:
- set-body:
exchange-property: "CamelTimerCounter"
- to: "kamelet:sink"
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# 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.
#

apiVersion: camel.apache.org/v1alpha1
kind: Kamelet
metadata:
labels:
camel.apache.org/kamelet.type: "sink"
camel.apache.org/kamelet.name: "log"
camel.apache.org/kamelet.version: "v1alpha1"
spec:
definition:
title: "Logger"
description: "Logger"
properties:
loggerName:
title: Name of the logging category
description: Name of the logging category
type: string
default: "logger"
showAll:
title: Show All
description: Show All
type: boolean
default: false
multiLine:
title: Multi Line
description: Multi Line
type: boolean
default: false
dependencies:
- "camel:log"
flow:
from:
uri: "kamelet:source"
steps:
- to:
uri: "log"
properties:
loggerName: "{{loggerName}}"
showAll: "{{showAll}}"
multiline: "{{multiLine}}"
Loading

0 comments on commit ca8083a

Please sign in to comment.