Skip to content

Commit

Permalink
Support endpoint DSL in YAML/JSON #366
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Sep 21, 2020
1 parent afd3847 commit 957cbc2
Show file tree
Hide file tree
Showing 26 changed files with 40,851 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser;
import org.apache.camel.k.loader.yaml.support.StepParserSupport;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.ToDefinition;
import org.apache.camel.util.StringHelper;

public class EndpointStepParser implements ProcessorStepParser {
private final String scheme;

public EndpointStepParser(String scheme) {
this.scheme = scheme;
}

@Override
public ProcessorDefinition<?> toProcessor(Context context) {
final ObjectNode node = context.node(ObjectNode.class);
final Map<String, Object> parameters = new HashMap<>();

node.fields().forEachRemaining(entry -> {
parameters.put(
StringHelper.dashToCamelCase(entry.getKey()),
entry.getValue().asText()
);
});

return new ToDefinition(
StepParserSupport.createEndpointUri(context.getCamelContext(), this.scheme, parameters)
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,81 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
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.k.loader.yaml.spi.StartStepParser;
import org.apache.camel.k.loader.yaml.support.StepParserSupport;
import org.apache.camel.model.RouteDefinition;

@YAMLStepParser(id = "from", definition = FromStepParser.Definition.class)
public class FromStepParser implements StartStepParser {
@Override
public Object process(Context context) {
final Definition definition = context.node(Definition.class);
final String uri = StepParserSupport.createEndpointUri(definition.uri, definition.parameters);
final RouteDefinition route = context.builder().from(uri);
if (definition.uri == null && definition.scheme == null) {
throw new IllegalArgumentException("Either uri or scheme must be set");
}

String uri = definition.uri != null
? StepParserSupport.createEndpointUri(definition.uri, definition.parameters)
: StepParserSupport.createEndpointUri(context.getCamelContext(), definition.scheme, definition.parameters);

// as this is a start converter, steps are mandatory
StepParserSupport.notNull(definition.steps, "steps");

return StepParserSupport.convertSteps(
context,
route,
context.builder().from(uri),
definition.steps
);
}

@YAMLNodeDefinition
public static final class Definition {
@JsonProperty(required = true)
public static final class Definition implements HasEndpointConsumer {
public String scheme;
public String uri;
@JsonProperty
public Map<String, Object> parameters;

@JsonProperty(required = true)
public List<Step> steps;

@JsonIgnore
@Override
public void setEndpointScheme(String scheme) {
this.scheme = scheme;
}

@JsonIgnore
@Override
public String getEndpointScheme() {
return this.scheme ;
}

@JsonProperty(required = true)
@Override
public void setUri(String uri) {
this.uri = uri;
}

@JsonProperty
@Override
public String getUri() {
return this.uri;
}

@JsonProperty
@Override
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}

@JsonProperty
@Override
public Map<String, Object> getParameters() {
return this.parameters;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition;
import org.apache.camel.k.annotation.yaml.YAMLStepParser;
Expand All @@ -33,8 +34,15 @@ public class RouteStepParser implements StartStepParser {
@Override
public Object process(Context context) {
final Definition definition = context.node(Definition.class);
final String uri = StepParserSupport.createEndpointUri(definition.from.uri, definition.from.parameters);
final RouteDefinition route = context.builder().from(uri);
if (definition.from.uri == null && definition.from.scheme == null) {
throw new IllegalArgumentException("Either uri or scheme must be set");
}

String uri = definition.from.uri != null
? StepParserSupport.createEndpointUri(definition.from.uri, definition.from.parameters)
: StepParserSupport.createEndpointUri(context.getCamelContext(), definition.from.scheme, definition.from.parameters);

RouteDefinition route = context.builder().from(uri);

ObjectHelper.ifNotEmpty(definition.id, route::routeId);
ObjectHelper.ifNotEmpty(definition.group, route::routeGroup);
Expand Down Expand Up @@ -62,18 +70,54 @@ public static final class Definition {
}

@YAMLNodeDefinition
public static final class From {
@JsonProperty
public static final class From implements HasEndpointConsumer {
public String uri;
@JsonProperty
public Map<String, Object> parameters;
public String scheme;

public From() {
}

public From(String uri) {
this.uri = uri;
}

@JsonIgnore
@Override
public void setEndpointScheme(String scheme) {
this.scheme = scheme;
}

@JsonIgnore
@Override
public String getEndpointScheme() {
return null;
}

@JsonProperty(required = true)
@Override
public void setUri(String uri) {
this.uri = uri;
}

@JsonProperty
@Override
public String getUri() {
return this.uri;
}

@JsonProperty
@Override
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;

}

@JsonProperty
@Override
public Map<String, Object> getParameters() {
return this.parameters;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.spi;

import com.fasterxml.jackson.annotation.JsonIgnore;

public interface HasEndpoint extends HasUri {
@JsonIgnore
void setEndpointScheme(String scheme);

@JsonIgnore
String getEndpointScheme();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.spi;

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public interface HasUri {
@JsonProperty
void setUri(String uri);

@JsonProperty
String getUri();

@JsonIgnore
void setParameters(Map<String, Object> parameters);

@JsonIgnore
Map<String, Object> getParameters();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public CamelContext getCamelContext() {
return builder.getContext();
}

public <T extends CamelContext> T getCamelContext(Class<T> type) {
return builder.getContext().adapt(type);
}

public ProcessorDefinition<?> processor() {
return this.processor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
*/
package org.apache.camel.k.loader.yaml.support;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.k.loader.yaml.model.Step;
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser;
import org.apache.camel.k.loader.yaml.spi.StepParser;
Expand Down Expand Up @@ -82,4 +87,15 @@ public static String createEndpointUri(String uri, Map<String, Object> parameter

return answer;
}

public static String createEndpointUri(CamelContext context, String scheme, Map<String, Object> parameters) {
try {
Map<String, String> params = new HashMap<>();
parameters.forEach((k, v) -> params.put(k, Objects.toString(v)));

return context.adapt(ExtendedCamelContext.class).getRuntimeCamelCatalog().asEndpointUri(scheme, params, false);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
}
21 changes: 21 additions & 0 deletions camel-k-loader-yaml/camel-k-loader-yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<!-- -->
<!-- ****************************** -->

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core-catalog</artifactId>
</dependency>

<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-core</artifactId>
Expand Down Expand Up @@ -100,6 +105,11 @@
<artifactId>camel-bean</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-telegram</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
Expand Down Expand Up @@ -173,6 +183,7 @@
<version>${project.version}</version>
<executions>
<execution>
<id>generate-yaml-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate-yaml-schema</goal>
Expand All @@ -196,6 +207,16 @@
</bannedDefinitions>
</configuration>
</execution>
<execution>
<id>generate-yaml-endpoint-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate-yaml-endpoints-schema</goal>
</goals>
<configuration>
<outputFile>${project.basedir}/src/generated/resources/camel-yaml-endpoint.json</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Loading

0 comments on commit 957cbc2

Please sign in to comment.