Add fromVersion and untilVersion to Typed Annotations 29/8129/6
authorDave Tucker <djt@redhat.com>
Thu, 19 Jun 2014 03:03:30 +0000 (04:03 +0100)
committerDave Tucker <djt@redhat.com>
Thu, 19 Jun 2014 21:23:52 +0000 (22:23 +0100)
This allows a column or row to be validated against a specific version
of the schema. This gives better error messages to the user than a
simply asserting that a Table or Column does not exist

Change-Id: I3d5a76c7c33d49311cecd92c0e5f9714f7522b83
Signed-off-by: Dave Tucker <djt@redhat.com>
library/src/main/java/org/opendaylight/ovsdb/lib/notation/Version.java
library/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TypedColumn.java
library/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TypedTable.java
library/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java

index b06a8833f814a418407c7b2d83a6fc56737a187c..8298ebca2b0bdc1032185f0fc67142e6b9538d24 100644 (file)
@@ -33,6 +33,9 @@ public class Version implements Comparable<Version> {
         this.patch = patch;
     }
 
+    public static final Version NULL = new Version(0,0,0);
+    public static final String NULL_VERSION_STRING = "0.0.0";
+
     public static Version fromString(String version){
         final Matcher matcher = Version.PATTERN.matcher(version);
         if (!matcher.matches()) {
index 5e0b73dbd42843977a22b6992cb10285adaaa992..71cce2f4bed70fed43d94db30052dfbc0feb92fe 100644 (file)
@@ -10,6 +10,8 @@
 
 package org.opendaylight.ovsdb.lib.schema.typed;
 
+import org.opendaylight.ovsdb.lib.notation.Version;
+
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -20,4 +22,6 @@ import java.lang.annotation.ElementType;;
 public @interface TypedColumn {
     public String name();
     public MethodType method();
+    public String fromVersion() default Version.NULL_VERSION_STRING;
+    public String untilVersion() default Version.NULL_VERSION_STRING;
 }
index c7a91a84ade421483e5009a565fa76d2be6f8549..e8a7d9a482a9ac79595e2a31ba0c5e3f3e95316d 100644 (file)
@@ -10,6 +10,8 @@
 
 package org.opendaylight.ovsdb.lib.schema.typed;
 
+import org.opendaylight.ovsdb.lib.notation.Version;
+
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -20,4 +22,6 @@ import java.lang.annotation.ElementType;;
 public @interface TypedTable {
     public String name();
     public String database();
+    public String fromVersion() default Version.NULL_VERSION_STRING;
+    public String untilVersion() default Version.NULL_VERSION_STRING;
 }
index 12161c461ad174f087391e7661bb40aad5598412..6a03ab13c23f3ca4f61e66357f2360f80849e52b 100644 (file)
@@ -15,6 +15,7 @@ import java.lang.reflect.Method;
 
 import org.opendaylight.ovsdb.lib.notation.Column;
 import org.opendaylight.ovsdb.lib.notation.Row;
+import org.opendaylight.ovsdb.lib.notation.Version;
 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
@@ -102,6 +103,30 @@ public class TyperUtils {
         return false;
     }
 
+    public static Version getFromVersion (Method method) {
+        TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
+        if (typedColumn != null) {
+            return Version.fromString(typedColumn.fromVersion());
+        }
+        TypedTable typedTable = method.getAnnotation(TypedTable.class);
+        if (typedTable != null) {
+            return Version.fromString(typedTable.fromVersion());
+        }
+        return Version.NULL;
+    }
+
+    public static Version getUntilVersion(Method method) {
+        TypedColumn typedColumn = method.getAnnotation(TypedColumn.class);
+        if (typedColumn != null) {
+            return Version.fromString(typedColumn.untilVersion());
+        }
+        TypedTable typedTable = method.getAnnotation(TypedTable.class);
+        if (typedTable != null) {
+            return Version.fromString(typedTable.untilVersion());
+        }
+        return Version.NULL;
+    }
+
     /**
      * Method that checks validity of the parameter passed to getTypedRowWrapper.
      * This method checks for a valid Database Schema matching the expected Database for a given table
@@ -125,6 +150,23 @@ public class TyperUtils {
         return true;
     }
 
+    private static void checkSchemaVersion(DatabaseSchema dbSchema, Method method) {
+        Version fromVersion = getFromVersion(method);
+        Version untilVersion = getUntilVersion(method);
+        if (!fromVersion.equals(Version.NULL)) {
+            if (dbSchema.getVersion().compareTo(fromVersion) < 0) {
+                throw new RuntimeException("This row is not supported until version "
+                        + fromVersion + "of the Schema");
+            }
+        }
+        if (!untilVersion.equals(Version.NULL)) {
+            if (dbSchema.getVersion().compareTo(untilVersion) > 0) {
+                throw new RuntimeException("This row was deprecated in "
+                        + untilVersion + "of the Schema");
+            }
+        }
+    }
+
     /**
      * This method returns a Typed Proxy implementation for the klazz passed as a parameter.
      * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual Row which is untyped.
@@ -147,6 +189,7 @@ public class TyperUtils {
         return Reflection.newProxy(klazz, new InvocationHandler() {
             private Object processGetData(Method method) throws Throwable {
                 String columnName = getColumnName(method);
+                checkSchemaVersion(dbSchema, method);
                 if (columnName == null) {
                     throw new RuntimeException("Error processing Getter : "+ method.getName());
                 }
@@ -166,6 +209,7 @@ public class TyperUtils {
 
             private Object processGetColumn(Method method) throws Throwable {
                 String columnName = getColumnName(method);
+                checkSchemaVersion(dbSchema, method);
                 if (columnName == null) {
                     throw new RuntimeException("Error processing GetColumn : "+ method.getName());
                 }
@@ -188,6 +232,7 @@ public class TyperUtils {
                 if (args == null || args.length != 1) {
                     throw new RuntimeException("Setter method : "+method.getName() + " requires 1 argument");
                 }
+                checkSchemaVersion(dbSchema, method);
                 String columnName = getColumnName(method);
                 if (columnName == null) {
                     throw new RuntimeException("Unable to locate Column Name for "+method.getName());