Skip to content

Commit

Permalink
Create a Maven Kamelet Plugin - First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
oscerd committed May 5, 2022
1 parent 8795f9c commit ad752a8
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 70 deletions.
22 changes: 22 additions & 0 deletions library/camel-kamelets-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,26 @@
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>

<!-- Validate Kamelets -->
<plugin>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>kamelets-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>

</build>
</project>
6 changes: 0 additions & 6 deletions library/camel-kamelets-catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@
<version>${classgraph.version}</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-catalog</artifactId>
<version>${camel.version}</version>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
import io.fabric8.camelk.v1alpha1.Kamelet;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.github.classgraph.ClassGraph;

import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.kamelets.catalog.model.KameletTypeEnum;
import org.apache.camel.tooling.model.ApiMethodModel;
import org.apache.camel.tooling.model.ApiModel;
import org.apache.camel.tooling.model.ComponentModel;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -33,12 +28,8 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class KameletsCatalogTest {
static KameletsCatalog catalog;
Expand Down Expand Up @@ -133,59 +124,4 @@ void testAllKameletFilesLoaded() throws Exception {
void testAllKameletDependencies() throws Exception {
catalog.getAllKameletDependencies();
}

@Test
void testOptionsParams() throws Exception {
String[] bannedDeps = {"mvn:", "camel:gson", "camel:core", "camel:kamelet", "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"};
List<String> bannedDepsList = Arrays.asList(bannedDeps);
DefaultCamelCatalog cc = new DefaultCamelCatalog();
List<String> names = catalog.getKameletsName();
for (String name:
names) {
Map<String, Object> kd = catalog.getKameletTemplate(name);
Map<String,Object> f = (Map) kd.get("from");
Map<String,Object> p = (Map) f.get("parameters");
List<String> deps = catalog.getKameletDependencies(name).stream()
.filter(Predicate.not(bannedDepsList::contains)).collect(Collectors.toList());
String cleanName;
if (!deps.isEmpty()) {
if (deps.get(0).equals("camel:jackson") && deps.size() > 1) {
cleanName = deps.get(1).replace("camel:", "");
} else {
cleanName = deps.get(0).replace("camel:", "");
}
if (cleanName.equalsIgnoreCase("cassandraql")) {
cleanName = "cql";
}
if (cleanName.equalsIgnoreCase("aws2-ddb") && name.equals("aws-ddb-streams-source")) {
cleanName = "aws2-ddb-streams";
}
if (cleanName.equalsIgnoreCase("google-sheets") && name.equals("google-sheets-source")) {
cleanName = "google-sheets-stream";
}
if (cleanName.equalsIgnoreCase("google-mail") && name.equals("google-mail-source")) {
cleanName = "google-mail-stream";
}
if (cleanName.equalsIgnoreCase("google-calendar") && name.equals("google-calendar-source")) {
cleanName = "google-calendar-stream";
}
if (p != null && !p.isEmpty()) {
ComponentModel componentModel = cc.componentModel(cleanName);
if (componentModel != null) {
List<ComponentModel.EndpointOptionModel> ce = componentModel.getEndpointOptions();
List<String> ceInternal =
ce.stream()
.map(ComponentModel.EndpointOptionModel::getName)
.collect(Collectors.toList());

for (Map.Entry<String, Object> entry : p.entrySet()) {
if (!entry.getKey().equals("period") && (!name.equals("kafka-ssl-source") && !name.equals("timer-source") && !name.equals("cron-source") && !name.equals("fhir-source"))) {
assertTrue(ceInternal.contains(entry.getKey()));
}
}
}
}
}
}
}
}
132 changes: 132 additions & 0 deletions library/kamelets-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets-parent</artifactId>
<version>main-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>kamelets-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>Camel Kamelets Maven Plugin</name>
<description>Camel Kamelets Maven Plugin</description>

<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-util</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-tooling-util</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.8.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-catalog</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets-catalog</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.4</version>
<configuration>
<mojoDependencies>
<dep>org.apache.maven:maven-plugin-api</dep>
</mojoDependencies>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.kamelets.maven.plugin;

import java.io.File;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.kamelets.catalog.KameletsCatalog;
import org.apache.camel.tooling.model.ComponentModel;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
* Copy the properties from a source POM to a different destination POM for syncing purposes.
*/
@Mojo(name = "validate", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
public class ValidateKameletsMojo extends AbstractMojo {

/**
* The base directory
*/
@Parameter(defaultValue = "${project.basedir}")
protected File baseDir;

/**
* The Maven project.
*/
@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;

/**
* Whether to fail if validation failed or not. By default true.
*/
@Parameter(property = "kamelets.failOnError", defaultValue = "true")
private boolean failOnError = true;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
String[] bannedDeps = {"mvn:", "camel:gson", "camel:core", "camel:kamelet", "github:apache.camel-kamelets:camel-kamelets-utils:main-SNAPSHOT"};
List<String> bannedDepsList = Arrays.asList(bannedDeps);
KameletsCatalog catalog = new KameletsCatalog();
DefaultCamelCatalog cc = new DefaultCamelCatalog();
List<String> names = catalog.getKameletsName();
for (String name:
names) {
Map<String, Object> kd = catalog.getKameletTemplate(name);
Map<String,Object> f = (Map) kd.get("from");
Map<String,Object> p = (Map) f.get("parameters");
List<String> deps = catalog.getKameletDependencies(name).stream()
.filter(Predicate.not(bannedDepsList::contains)).collect(Collectors.toList());
String cleanName;
if (!deps.isEmpty()) {
if (deps.get(0).equals("camel:jackson") && deps.size() > 1) {
cleanName = deps.get(1).replace("camel:", "");
} else {
cleanName = deps.get(0).replace("camel:", "");
}
if (cleanName.equalsIgnoreCase("cassandraql")) {
cleanName = "cql";
}
if (cleanName.equalsIgnoreCase("aws2-ddb") && name.equals("aws-ddb-streams-source")) {
cleanName = "aws2-ddb-streams";
}
if (cleanName.equalsIgnoreCase("google-sheets") && name.equals("google-sheets-source")) {
cleanName = "google-sheets-stream";
}
if (cleanName.equalsIgnoreCase("google-mail") && name.equals("google-mail-source")) {
cleanName = "google-mail-stream";
}
if (cleanName.equalsIgnoreCase("google-calendar") && name.equals("google-calendar-source")) {
cleanName = "google-calendar-stream";
}
if (p != null && !p.isEmpty()) {
ComponentModel componentModel = cc.componentModel(cleanName);
if (componentModel != null) {
List<ComponentModel.EndpointOptionModel> ce = componentModel.getEndpointOptions();
List<String> ceInternal =
ce.stream()
.map(ComponentModel.EndpointOptionModel::getName)
.collect(Collectors.toList());
for (Map.Entry<String, Object> entry : p.entrySet()) {
if (!entry.getKey().equals("period") && (!name.equals("kafka-ssl-source") && !name.equals("timer-source") && !name.equals("cron-source") && !name.equals("fhir-source"))) {
if (!ceInternal.contains(entry.getKey())) {
getLog().error("Kamelet Name: " + name);
getLog().error("Scheme Name: " + cleanName);
getLog().error("Parameter: " + entry.getKey());
getLog().error("The parameter " + entry.getKey() + " doesn't exist in the endpoint options of " + cleanName + " component");
if (failOnError) {
throw new MojoExecutionException("The Kamelets Validation failed. See logs for more information." + "\n");
}
break;
}
}
}
}
}
}
}
getLog().info("Validation passed");
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<modules>
<module>library/camel-kamelets</module>
<module>library/camel-kamelets-catalog</module>
<module>library/kamelets-maven-plugin</module>
<module>library/camel-kamelets-utils</module>
<module>library/camel-kamelets-bom</module>
</modules>
Expand Down

0 comments on commit ad752a8

Please sign in to comment.