diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-06 12:05:23 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-06 12:05:23 +0100 |
commit | b8699860653e593271e50c8ba1b73f66fcb6287c (patch) | |
tree | a7029ee71f1539d269fda241a3f345255783dcf1 /src/main/java/io/trygvis/rules/acme | |
parent | 9eac79348242b5dc33f4cccdd86beda2a4ed4746 (diff) | |
download | rules-sandbox-b8699860653e593271e50c8ba1b73f66fcb6287c.tar.gz rules-sandbox-b8699860653e593271e50c8ba1b73f66fcb6287c.tar.bz2 rules-sandbox-b8699860653e593271e50c8ba1b73f66fcb6287c.tar.xz rules-sandbox-b8699860653e593271e50c8ba1b73f66fcb6287c.zip |
Starting on splitting up into different modules.
Diffstat (limited to 'src/main/java/io/trygvis/rules/acme')
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeIo.java | 260 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeMyApp.java | 6 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeObject.java | 16 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeOps.java | 4 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/IpCalc.java | 67 |
5 files changed, 0 insertions, 353 deletions
diff --git a/src/main/java/io/trygvis/rules/acme/AcmeIo.java b/src/main/java/io/trygvis/rules/acme/AcmeIo.java deleted file mode 100644 index 456195d..0000000 --- a/src/main/java/io/trygvis/rules/acme/AcmeIo.java +++ /dev/null @@ -1,260 +0,0 @@ -package io.trygvis.rules.acme; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.type.TypeFactory; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; -import org.apache.commons.collections4.OrderedMap; -import org.drools.core.common.DefaultFactHandle; -import org.kie.api.KieBase; -import org.kie.api.runtime.rule.FactHandle; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.*; -import java.util.function.Function; - -@SuppressWarnings("unchecked") -public class AcmeIo { - private final ObjectMapper mapper; - - public AcmeIo(KieBase kieBase) { - var factory = new YAMLFactory(); - factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID); - factory.enable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID); - mapper = new ObjectMapper(factory); - var typeFactory = TypeFactory.defaultInstance() - .withClassLoader(new AcmeClassLoader(kieBase)); - mapper.setTypeFactory(typeFactory); - mapper.findAndRegisterModules(); - } - - public List<Object> load(String file) throws IOException { - var parser = mapper.getFactory().createParser(new File(file)); - - var objects = mapper.readValues(parser, AcmeObject.class).readAll(new ArrayList<>()); - - List<Object> items = new ArrayList<>(objects.size()); - for (AcmeObject object : objects) { - try { - var type = mapper.getTypeFactory().findClass(object.type); - var x = mapper.treeToValue(object.data, type); - items.add(x); - } catch (ClassNotFoundException e) { - throw new IOException(e); - } - } - - return items; - } - - public void dump(String s, Collection<FactHandle> factHandles) throws IOException { - dump(s, factHandles, (o) -> true); - } - - // This should just sort by all getters instead. - static class FactCollection<T> { - public final Class<T> type; - public final List<T> values; - - public FactCollection(Class<T> type) { - this.type = type; - this.values = new ArrayList<>(); - } - - public void sort() { - var comparator = comparable(type); - - this.values.sort(comparator); - } - } - - private static final Map<Class<?>, Comparator> comparators = new HashMap<>(); - - private static <A, T extends Comparable<T>> Comparator comparable(Class<A> klass) { - var comparator = comparators.get(klass); - if (comparator != null) { - return comparator; - } - - // TODO: check if klass is a Comparable directly. - - var prioritizedKeys = List.of("key", "name", "fqdn"); - - var discoveredFieldsP1 = new LinkedHashMap<String, Function<Object, Object>>(); - var discoveredFieldsP2 = new LinkedHashMap<String, Function<Object, Object>>(); - - var prioritizedTypes = List.of(String.class, int.class, Number.class); - - for (var f : klass.getDeclaredFields()) { - if (f.getDeclaringClass() == Object.class) { - continue; - } - - if (!f.trySetAccessible()) { - continue; - } - - var collection = discoveredFieldsP2; - - if (prioritizedTypes.contains(f.getType())) { - collection = discoveredFieldsP1; - } - - collection.put(f.getName(), (Object o) -> { - try { - return f.get(o); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - }); - } - -// for (var m : klass.getFields()) { -// if (m.getParameterCount() != 0) { -// continue; -// } -// -// var name = m.getName(); -// -// if (name.startsWith("get") && name.length() > 3 && Character.isUpperCase(name.charAt(4))) { -// name = name.substring(3, 3).toLowerCase() + name.substring(4); -// } else { -// continue; -// } -// -// if (!m.isAccessible()) { -// if (!m.trySetAccessible()) -// return null; -// } -// -// discoveredFields.put(name, m); -// } - -// System.out.printf("Sorting %s by:%n", klass.getName()); - - var discoveredFields = new LinkedHashMap<>(discoveredFieldsP1); - discoveredFields.putAll(discoveredFieldsP2); - - List<Function<Object, Object>> accessors = new ArrayList<>(); - for (String prioritizedKey : prioritizedKeys) { - var m = discoveredFields.remove(prioritizedKey); - if (m == null) { - continue; - } - - accessors.add(m); -// System.out.println(" + " + prioritizedKey); - } - accessors.addAll(discoveredFields.values()); -// discoveredFields.keySet().forEach((s)-> System.out.println(" - " + s)); - - comparator = (a, b) -> { -// if (klass.getName().contains("AcmeServer")) { -// System.out.println("AcmeIo.comparable"); -// } - - for (var method : accessors) { - var x = method.apply(a); - var y = method.apply(b); - - if (x == null && y == null) { - continue; - } - - if (x == null) { - return -1; - } else if (y == null) { - return 1; - } else { - var res = x.toString().compareTo(y.toString()); - if (res != 0) { - return res; - } - } - } - - return 0; - }; - - comparators.put(klass, comparator); - - return comparator; - } - - public void dump(String s, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException { - var out = new File("out"); - - if (!out.isDirectory()) { - if (!out.mkdirs()) { - throw new IOException("Could not create directory: " + out); - } - } - - var facts = new TreeMap<Class<?>, FactCollection<Object>>(Comparator.comparing(Class::getName)); - for (var handle : factHandles) { - if (handle instanceof DefaultFactHandle h) { - var obj = h.getObject(); - if (!filter.apply(obj)) { - continue; - } - - Class<?> type = obj.getClass(); - var collection = facts.get(type); - - if (collection == null) { - collection = new FactCollection(type); - facts.put(type, collection); - } - - collection.values.add(obj); - } - } - - var factory = mapper.getFactory(); - try (var writer = new FileWriter(new File(out, s + ".yaml")); - 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 AcmeObject(name, mapper.valueToTree(fact))); - } - } - } - } - - private static class AcmeClassLoader extends ClassLoader { - private final KieBase kieBase; - - public AcmeClassLoader(KieBase kieBase) { - this.kieBase = kieBase; - } - - @Override - public Class<?> loadClass(String name) throws ClassNotFoundException { - try { - return super.loadClass(name); - } catch (ClassNotFoundException e) { - var i = name.lastIndexOf('.'); - String pkg, klass; - if (i == -1) { - pkg = null; - klass = name; - } else { - pkg = name.substring(0, i); - klass = name.substring(i + 1); - } - var clazz = kieBase.getFactType(pkg, klass); - if (clazz == null) { - throw e; - } - - return clazz.getFactClass(); - } - } - } -} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java b/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java deleted file mode 100644 index a1340e7..0000000 --- a/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.trygvis.rules.acme; - -public class AcmeMyApp { - public String environment; - public String dockerTag; -} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeObject.java b/src/main/java/io/trygvis/rules/acme/AcmeObject.java deleted file mode 100644 index a75c4ba..0000000 --- a/src/main/java/io/trygvis/rules/acme/AcmeObject.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.trygvis.rules.acme; - -import com.fasterxml.jackson.databind.node.ObjectNode; - -public final class AcmeObject { - public String type; - public ObjectNode data; - - public AcmeObject() { - } - - public AcmeObject(String type, ObjectNode data) { - this.type = type; - this.data = data; - } -} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeOps.java b/src/main/java/io/trygvis/rules/acme/AcmeOps.java deleted file mode 100644 index 147fcfa..0000000 --- a/src/main/java/io/trygvis/rules/acme/AcmeOps.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.trygvis.rules.acme; - -public class AcmeOps { -} diff --git a/src/main/java/io/trygvis/rules/acme/IpCalc.java b/src/main/java/io/trygvis/rules/acme/IpCalc.java deleted file mode 100644 index 5369d62..0000000 --- a/src/main/java/io/trygvis/rules/acme/IpCalc.java +++ /dev/null @@ -1,67 +0,0 @@ -package io.trygvis.rules.acme; - -import io.trygvis.rules.network.Ipv4Cidr; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -public class IpCalc { - private static final Pattern pattern = Pattern.compile("([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})/([0-9]{1,3})"); - - public static class FirstLast { - public final int first; - public final int last; - - public FirstLast(int first, int last) { - this.first = first; - this.last = last; - } - } - - public static Ipv4Cidr cidr(String cidr) { - var matcher = pattern.matcher(cidr); - if (!matcher.matches()) { - throw new IllegalArgumentException("Not a CIDR: " + cidr); - } - - var b1 = matcher.group(1); - var b2 = matcher.group(2); - var b3 = matcher.group(3); - var b4 = matcher.group(4); - - int network = parse(b1) << 24 | - parse(b2) << 16 | - parse(b3) << 8 | - parse(b4); - -// System.out.printf("network = %x%n", network); - - var l = matcher.group(5); - var bits = Integer.parseInt(l); - var hostBits = 32 - bits; - int size = 1 << hostBits; - - int netmask = (-1 >> hostBits) << hostBits; -// System.out.printf("netmask = %08x%n", netmask); - - int x = network & ~netmask; - - if (x != 0) { - throw new IllegalArgumentException("Not a CIDR: " + cidr); - } - - return new Ipv4Cidr(network, netmask, size, bits); - } - - private static int parse(String s) { - var i = Integer.parseInt(s); - if (i > 255) { - throw new IllegalArgumentException("Not a CIDR"); - } - - return i; - } -} |