--- /dev/null
+/*
+ * 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;
+ }
+}
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;
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;
}
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(); ) {
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() {
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();
--- /dev/null
+/*
+ * 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);
+
+ }
+}
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;
DatabaseSchema schema = DatabaseSchema.fromJson("some", jsonNode.get("result"));
assertNotNull(schema);
-
+ assertEquals(Version.fromString("6.12.0"), schema.getVersion());
}
}