summaryrefslogtreecommitdiff
path: root/module/ri-engine
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-12 22:08:14 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-12 22:08:14 +0100
commit0e8048146ddf85adf28c1da09e45b98760f23210 (patch)
tree76c2755ec7f1da4af2361e85f1cd84c44b66c00a /module/ri-engine
parent34bb772073b1954ac95b4f1491236eb8b063ef83 (diff)
downloadrules-sandbox-0e8048146ddf85adf28c1da09e45b98760f23210.tar.gz
rules-sandbox-0e8048146ddf85adf28c1da09e45b98760f23210.tar.bz2
rules-sandbox-0e8048146ddf85adf28c1da09e45b98760f23210.tar.xz
rules-sandbox-0e8048146ddf85adf28c1da09e45b98760f23210.zip
Better output YAML.
Enabling object references internally in the document. Needed to write all objects in one go for Jackson to resolve all internal references. Applied some sorting magic to write out as many as possible objects on the root level. Will need some more magic later for customers to customize the output ordering.
Diffstat (limited to 'module/ri-engine')
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java4
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java6
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java77
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java4
4 files changed, 82 insertions, 9 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java
index 949d9ae..0b65aaa 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Cluster.java
@@ -1,5 +1,9 @@
package io.trygvis.rules.dba;
+import com.fasterxml.jackson.annotation.JsonIdentityInfo;
+import com.fasterxml.jackson.annotation.ObjectIdGenerators;
+
+@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
public class Cluster {
public String name;
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
index d852115..f6d2ba4 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/dba/Container.java
@@ -1,8 +1,13 @@
package io.trygvis.rules.dba;
+import com.fasterxml.jackson.annotation.JsonIdentityReference;
import io.trygvis.rules.machine.Machine;
+//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Container {
+ public final String id;
+
+// @JsonIdentityReference(alwaysAsId = true)
public final Cluster cluster;
public final String name;
public final String machineRole;
@@ -12,6 +17,7 @@ public class Container {
private Machine machine;
public Container(Cluster cluster, String name, String machineRole, String image, String tag) {
+ this.id = cluster.name + "-" + name;
this.cluster = cluster;
this.name = name;
this.machineRole = machineRole;
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
index 3173109..b402173 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
@@ -189,6 +189,9 @@ public class DbIo {
return comparator;
}
+ static record DbObject2(String type, Object data) {
+ }
+
public void dump(String s, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException {
var yamlFile = new File("out", s + ".yaml");
@@ -214,18 +217,35 @@ public class DbIo {
}
}
+ var objects = new ArrayList<DbObject2>(facts.size());
+ for (var e : facts.entrySet()) {
+ var name = e.getKey().getName();
+
+ var collection = e.getValue();
+ collection.sort();
+ for (var fact : collection.values) {
+ objects.add(new DbObject2(name, fact));
+ }
+ }
+
+ /*
+ var x = new ArrayList<DbObject2>();
+ x.add(new DbObject2("io.trygvis.rules.dba.Container", null));
+ x.add(new DbObject2("io.trygvis.rules.machine.Machine", null));
+ x.add(new DbObject2("io.trygvis.acme.apps.AcmeMyApp", null));
+
+ System.out.println("xxxxxx");
+ x.sort(new DbObjectComparator());
+ x.forEach(System.out::println);
+ System.out.println("xxxxxx");
+ */
+
+ objects.sort(new DbObjectComparator());
+
var factory = mapper.getFactory();
try (var writer = new FileWriter(yamlFile);
var g = factory.createGenerator(writer)) {
- for (var e : facts.entrySet()) {
- var name = e.getKey().getName();
-
- var collection = e.getValue();
- collection.sort();
- for (var fact : collection.values) {
- g.writeObject(new DbObject(name, mapper.valueToTree(fact)));
- }
- }
+ g.writeObject(objects);
}
}
@@ -259,4 +279,43 @@ public class DbIo {
}
}
}
+
+ private static class DbObjectComparator implements Comparator<DbObject2> {
+ private final List<String> prioritizedPackages = List.of(
+ "io.trygvis.rules.core",
+ "io.trygvis.rules.machine",
+ "io.trygvis.rules.network",
+ "io.trygvis.rules.dns",
+ "io.trygvis.rules.dba",
+ "io.trygvis.rules");
+
+ @Override
+ public int compare(DbObject2 a, DbObject2 b) {
+ var indexA = a.type.lastIndexOf(".");
+ String packageA = indexA == -1 ? null : a.type.substring(0, indexA);
+ String classA = indexA == -1 ? a.type : a.type.substring(indexA + 1);
+
+ var indexB = b.type.lastIndexOf(".");
+ String packageB = indexB == -1 ? null : b.type.substring(0, indexB);
+ String classB = indexB == -1 ? b.type : b.type.substring(indexB + 1);
+
+ var priIdxA = prioritizedPackages.indexOf(packageA);
+ var priIdxB = prioritizedPackages.indexOf(packageB);
+
+ if (priIdxA == -1 && priIdxB == -1) {
+ return classB.compareTo(classA);
+ } else if (priIdxA == -1) {
+ return 1;
+ } else if (priIdxB == -1) {
+ return -1;
+ }
+ return priIdxA - priIdxB;
+// var diff = priIdxB - priIdxA;
+// if (diff != 0) {
+// return diff;
+// }
+//
+// return classB.compareTo(classA);
+ }
+ }
}
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java b/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java
index 52721e1..8e54d60 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/machine/Machine.java
@@ -1,5 +1,9 @@
package io.trygvis.rules.machine;
+import com.fasterxml.jackson.annotation.JsonIdentityInfo;
+import com.fasterxml.jackson.annotation.ObjectIdGenerators;
+
+@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
public class Machine {
public String name;
public String fqdn;