Add schema version to DatabaseSchema 27/8127/6
authorDave Tucker <djt@redhat.com>
Thu, 19 Jun 2014 01:37:42 +0000 (02:37 +0100)
committerDave Tucker <djt@redhat.com>
Thu, 19 Jun 2014 18:29:03 +0000 (19:29 +0100)
This commit also adds a Version class to lib.notation that allows
versions to be compared. This is necessary for adding fromVersion
and toVersion to the TypedTable and TypedClass annotations

Change-Id: I30c821cd570a6b78e6eee874dafff269399a7120
Signed-off-by: Dave Tucker <djt@redhat.com>
library/src/main/java/org/opendaylight/ovsdb/lib/notation/Version.java [new file with mode: 0644]
library/src/main/java/org/opendaylight/ovsdb/lib/schema/DatabaseSchema.java
library/src/test/java/org/opendaylight/ovsdb/lib/notation/VersionTest.java [new file with mode: 0644]
library/src/test/java/org/opendaylight/ovsdb/lib/schema/OvsDBSchemaTest.java

diff --git a/library/src/main/java/org/opendaylight/ovsdb/lib/notation/Version.java b/library/src/main/java/org/opendaylight/ovsdb/lib/notation/Version.java
new file mode 100644 (file)
index 0000000..b06a883
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Authors : Dave Tucker
+ */
+
+package org.opendaylight.ovsdb.lib.notation;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * This class represents a version according to RFC 7047
+ * The default implementation assumes the left-most digit is most significant when performing comparisons
+ * @see <a href-="http://tools.ietf.org/html/rfc7047#section-3.1">RFC7047 Section 3.1</a>
+ */
+public class Version implements Comparable<Version> {
+
+    int major;
+    int minor;
+    int patch;
+
+    private static final String FORMAT = "(\\d+)\\.(\\d+)\\.(\\d+)";
+    private static final Pattern PATTERN = Pattern.compile(Version.FORMAT);
+
+    public Version(int major, int minor, int patch) {
+        this.major = major;
+        this.minor = minor;
+        this.patch = patch;
+    }
+
+    public static Version fromString(String version){
+        final Matcher matcher = Version.PATTERN.matcher(version);
+        if (!matcher.matches()) {
+            throw new IllegalArgumentException("<"+version+"> does not match format "+Version.FORMAT);
+        }
+        int major = Integer.valueOf(matcher.group(1));
+        int minor = Integer.valueOf(matcher.group(2));
+        int patch = Integer.valueOf(matcher.group(3));
+        return new Version(major, minor, patch);
+    }
+
+    public String toString(){
+        return "" + major + "." + minor + "." + patch;
+    }
+
+    public int getMajor() {
+        return major;
+    }
+
+    public void setMajor(int major) {
+        this.major = major;
+    }
+
+    public int getMinor() {
+        return minor;
+    }
+
+    public void setMinor(int minor) {
+        this.minor = minor;
+    }
+
+    public int getPatch() {
+        return patch;
+    }
+
+    public void setPatch(int patch) {
+        this.patch = patch;
+    }
+
+
+    // ToDo: While format is X.X.X semantics are schema dependent.
+    // Therefore we should allow equals to be overridden by the schema
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Version version = (Version) o;
+
+        if (major != version.major) return false;
+        if (minor != version.minor) return false;
+        if (patch != version.patch) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = major;
+        result = 31 * result + minor;
+        result = 31 * result + patch;
+        return result;
+    }
+
+    // ToDo: While format is X.X.X semantics are schema dependent
+    // Therefore we should allow compareTo to be overridden by the schema
+    @Override
+    public int compareTo(Version o) {
+        if (this.equals(o)) return 0;
+        if (this.major > o.major) return 1;
+        if (this.major < o.major) return -1;
+        // major is equal
+        if (this.minor > o.minor) return 1;
+        if (this.minor < o.minor) return -1;
+        // minor is equal
+        if (this.patch > o.patch) return 1;
+        // must be less than
+        return -1;
+    }
+}
index 417dd9ac9e193e6d2c1dcb33268a6aaf7e2b74a4..9ad7cbaaf04608d664226553cba82ac0ff00b8ff 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.opendaylight.ovsdb.lib.ParsingException;
+import org.opendaylight.ovsdb.lib.notation.Version;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -31,14 +32,17 @@ public class DatabaseSchema {
     public static Logger logger = LoggerFactory.getLogger(DatabaseSchema.class);
 
     private String name;
+
+    private Version version;
     private Map<String, TableSchema> tables;
 
     public DatabaseSchema(Map<String, TableSchema> tables) {
         this.tables = tables;
     }
 
-    public DatabaseSchema(String name, Map<String, TableSchema> tables) {
+    public DatabaseSchema(String name, Version version, Map<String, TableSchema> tables) {
         this.name = name;
+        this.version = version;
         this.tables = tables;
     }
 
@@ -88,6 +92,11 @@ public class DatabaseSchema {
         if (!json.isObject() || !json.has("tables")) {
             throw new ParsingException("bad DatabaseSchema root, expected \"tables\" as child but was not found");
         }
+        if (!json.isObject() || !json.has("version")) {
+            throw new ParsingException("bad DatabaseSchema root, expected \"version\" as child but was not found");
+        }
+
+        Version dbVersion = Version.fromString(json.get("version").asText());
 
         Map<String, TableSchema> tables = new HashMap<>();
         for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("tables").fields(); iter.hasNext(); ) {
@@ -98,7 +107,7 @@ public class DatabaseSchema {
             tables.put(table.getKey(), new GenericTableSchema().fromJson(table.getKey(), table.getValue()));
         }
 
-        return new DatabaseSchema(dbName, tables);
+        return new DatabaseSchema(dbName, dbVersion, tables);
     }
 
     public String getName() {
@@ -109,6 +118,10 @@ public class DatabaseSchema {
         this.name = name;
     }
 
+    public Version getVersion() { return version; }
+
+    public void setVersion(Version version) { this.version = version; }
+
     public void populateInternallyGeneratedColumns() {
         for (TableSchema tableSchema : tables.values()) {
             tableSchema.populateInternallyGeneratedColumns();
diff --git a/library/src/test/java/org/opendaylight/ovsdb/lib/notation/VersionTest.java b/library/src/test/java/org/opendaylight/ovsdb/lib/notation/VersionTest.java
new file mode 100644 (file)
index 0000000..d875254
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Authors : Dave Tucker
+ */
+
+package org.opendaylight.ovsdb.lib.notation;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class VersionTest {
+
+    @Test
+    public void testToString() throws Exception {
+        Version a = Version.fromString("1.2.3");
+        assertEquals("1.2.3", a.toString());
+    }
+
+    @Test
+    public void testEquals() throws Exception {
+        Version a = Version.fromString("1.2.3");
+        Version b = Version.fromString("0.0.0");
+        assertEquals(a, Version.fromString("1.2.3"));
+        assertTrue(a.hashCode() == Version.fromString("1.2.3").hashCode());
+        assertEquals(b, Version.fromString("0.0.0"));
+        assertTrue(b.hashCode() == Version.fromString("0.0.0").hashCode());
+    }
+
+    @Test
+    public void testCompareTo() throws Exception {
+        Version a = Version.fromString("1.2.27");
+        Version b = Version.fromString("1.2.0");
+        Version c = Version.fromString("1.99.0");
+        Version d = Version.fromString("99.1.0");
+
+        assertTrue(a.compareTo(b) > 0);
+        assertTrue(b.compareTo(a) < 0);
+        assertTrue(a.compareTo(c) < 0);
+        assertTrue(c.compareTo(b) > 0);
+        assertTrue(d.compareTo(a) > 0);
+        assertTrue(b.compareTo(d) < 0);
+
+    }
+}
index f277542c5ec7aa4fe79ab999e5cecef78c07b5e5..a262f68d2a7696f4755918b26362e058ee513871 100644 (file)
@@ -12,6 +12,9 @@ package org.opendaylight.ovsdb.lib.schema;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.junit.Test;
+import org.opendaylight.ovsdb.lib.notation.Version;
+
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import java.io.IOException;
 import java.io.InputStream;
@@ -28,6 +31,6 @@ public class OvsDBSchemaTest {
 
         DatabaseSchema schema = DatabaseSchema.fromJson("some", jsonNode.get("result"));
         assertNotNull(schema);
-
+        assertEquals(Version.fromString("6.12.0"), schema.getVersion());
     }
 }