diff --git a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/AdapterMigrator.java b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/AdapterMigrator.java index 33c3d19209..96f32d1e62 100644 --- a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/AdapterMigrator.java +++ b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/AdapterMigrator.java @@ -21,5 +21,13 @@ import org.apache.streampipes.extensions.api.extractor.IStaticPropertyExtractor; import org.apache.streampipes.model.connect.adapter.AdapterDescription; -public interface AdapterMigrator extends ModelMigrator { +public abstract class AdapterMigrator implements ModelMigrator { + + @Override + public boolean equals(Object obj) { + if (obj instanceof ModelMigrator) { + return this.config().equals(((ModelMigrator) obj).config()); + } + return false; + } } diff --git a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/DataSinkMigrator.java b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/DataSinkMigrator.java index dafee59c41..f88dbbf5ce 100644 --- a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/DataSinkMigrator.java +++ b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/DataSinkMigrator.java @@ -21,5 +21,13 @@ import org.apache.streampipes.extensions.api.extractor.IDataSinkParameterExtractor; import org.apache.streampipes.model.graph.DataSinkInvocation; -public interface DataSinkMigrator extends ModelMigrator { +public abstract class DataSinkMigrator implements ModelMigrator { + + @Override + public boolean equals(Object obj) { + if (obj instanceof ModelMigrator) { + return this.config().equals(((ModelMigrator) obj).config()); + } + return false; + } } diff --git a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/ModelMigrator.java b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/ModelMigrator.java index d734cdfd82..d95ff49c13 100644 --- a/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/ModelMigrator.java +++ b/streampipes-extensions-api/src/main/java/org/apache/streampipes/extensions/api/migration/ModelMigrator.java @@ -22,17 +22,30 @@ import org.apache.streampipes.model.base.NamedStreamPipesEntity; import org.apache.streampipes.model.migration.ModelMigratorConfig; -public interface ModelMigrator> { +public interface ModelMigrator< + T extends NamedStreamPipesEntity, + ExT extends IParameterExtractor + > extends Comparable { ModelMigratorConfig config(); /** * Defines the migration to be performed. - * @param element Entity to be transformed. + * + * @param element Entity to be transformed. * @param extractor Extractor that allows to handle static properties. * @return Result of the migration that describes both outcomes: successful and failed migrations * @throws RuntimeException in case any unexpected error occurs */ MigrationResult migrate(T element, ExT extractor) throws RuntimeException; + @Override + default int compareTo(Object o) { + if (!(o instanceof ModelMigrator)) { + throw new ClassCastException("Given object is not an instance of `ModelMigrator` - " + + "only instances of `ModelMigrator` can be compared."); + } else { + return config().compareTo(((ModelMigrator) o).config()); + } + } } diff --git a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/model/SpServiceDefinition.java b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/model/SpServiceDefinition.java index 49cefee013..dfa1489dda 100644 --- a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/model/SpServiceDefinition.java +++ b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/model/SpServiceDefinition.java @@ -27,6 +27,7 @@ import org.apache.streampipes.model.extensions.configuration.ConfigItem; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.UUID; @@ -190,7 +191,19 @@ public void addRuntimeProvider(IStreamPipesRuntimeProvider runtimeProvider) { return this.migrators; } + /** + * Add a list of migrations to the service definition. + * This inherently checks for duplicates and sorts the migrations as such that + * migrations affecting lower versions always come first. + * + * @param migrators migrators to add + */ public void addMigrators(List> migrators) { - this.migrators.addAll(migrators); + for (var migratorToAdd : migrators) { + if (!this.migrators.contains(migratorToAdd)) { + this.migrators.add(migratorToAdd); + } + } + Collections.sort(this.migrators); } } diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java b/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java index d667b1a995..65713d905a 100644 --- a/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java +++ b/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java @@ -29,5 +29,29 @@ * @param toVersion Target version that the migration aims to achieve */ public record ModelMigratorConfig(String targetAppId, SpServiceTagPrefix modelType, - Integer fromVersion, Integer toVersion) { + int fromVersion, int toVersion) implements Comparable{ + + + @Override + public int compareTo(Object o) { + + if (o == null) { + throw new NullPointerException(); + } + + if (!(o instanceof ModelMigratorConfig)){ + throw new ClassCastException("Given object is not an instance of `ModelMigratorConfig` - " + + "only instances of `ModelMigratorConfig` can be compared."); + } + + if (targetAppId.equals(((ModelMigratorConfig) o).targetAppId())) { + + if (fromVersion != ((ModelMigratorConfig) o).fromVersion()) { + return this.toVersion() - ((ModelMigratorConfig) o).toVersion(); + } + return this.fromVersion() - ((ModelMigratorConfig) o).fromVersion(); + } + + return 0; + } }