Migrate TyperUtils methods to TypedDatabaseSchemaImpl 86/86086/11
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 1 Dec 2019 18:09:17 +0000 (19:09 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 2 Dec 2019 11:20:08 +0000 (12:20 +0100)
TyperUtils are utterly stateless while TypedDatabaseSchema can
provide schema binding. Migrate method implementatios there, leaving
a simple lookup compability layer in TyperUtils.

Change-Id: If533c8fd8fe0027e8ca67dcd474b8b92fbadd767
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TypedDatabaseSchema.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TypedDatabaseSchemaImpl.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java

index d3da2d183562aa7ec2b128df0fb55ec6e023972c..14f52fa9c2e65bb981338d3b628b800ec7c8d8a8 100644 (file)
@@ -49,7 +49,9 @@ public interface TypedDatabaseSchema extends DatabaseSchema {
      *
      * @param klazz Typed Class that represents a Table
      */
-    <T> T getTypedRowWrapper(Class<T> klazz);
+    default <T> T getTypedRowWrapper(final Class<T> klazz) {
+        return getTypedRowWrapper(klazz, new Row<>());
+    }
 
     /**
      * Returns a Typed Proxy implementation for the klazz passed as a parameter.
index 0cfee20faa9e757fcbd5ea77b4259114d42ae065..ced7787e248a4488ecfef8b0ade884343e9eebb6 100644 (file)
@@ -9,7 +9,10 @@ package org.opendaylight.ovsdb.lib.schema.typed;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.reflect.Reflection;
+import java.util.HashMap;
 import java.util.Map;
+import org.opendaylight.ovsdb.lib.message.TableUpdate;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.notation.UUID;
@@ -40,28 +43,89 @@ final class TypedDatabaseSchemaImpl extends ForwardingDatabaseSchema implements
         return TyperUtils.getTableSchema(this, klazz);
     }
 
-    @Override
-    public <T> T getTypedRowWrapper(final Class<T> klazz) {
-        return TyperUtils.getTypedRowWrapper(this, klazz);
-    }
-
     @Override
     public <T> T getTypedRowWrapper(final Class<T> klazz, final Row<GenericTableSchema> row) {
-        return TyperUtils.getTypedRowWrapper(this, klazz, row);
+        // Check validity of  of the parameter passed to getTypedRowWrapper:
+        // -  checks for a valid Database Schema matching the expected Database for a given table
+        // - checks for the presence of the Table in Database Schema.
+        final String dbName = TypedReflections.getTableDatabase(klazz);
+        if (dbName != null && !dbName.equalsIgnoreCase(getName())) {
+            return null;
+        }
+        TyperUtils.checkVersion(getVersion(), TypedReflections.getTableVersionRange(klazz));
+
+        if (row != null) {
+            row.setTableSchema(getTableSchema(klazz));
+        }
+        return Reflection.newProxy(klazz, new TypedRowInvocationHandler(klazz, this, row));
     }
 
     @Override
     public <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates) {
-        return TyperUtils.extractRowsOld(klazz, updates, this);
+        Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates = extractRowUpdates(
+            requireNonNull(klazz), requireNonNull(updates));
+        Map<UUID,T> result = new HashMap<>();
+        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
+            if (rowUpdate != null && rowUpdate.getOld() != null) {
+                Row<GenericTableSchema> row = rowUpdate.getOld();
+                result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
+            }
+        }
+        return result;
     }
 
     @Override
     public <T> Map<UUID, T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates) {
-        return TyperUtils.extractRowsUpdated(klazz, updates, this);
+        final Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
+                extractRowUpdates(klazz, updates);
+        final Map<UUID,T> result = new HashMap<>();
+        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
+            if (rowUpdate != null && rowUpdate.getNew() != null) {
+                Row<GenericTableSchema> row = rowUpdate.getNew();
+                result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
+            }
+        }
+        return result;
     }
 
     @Override
     public <T> Map<UUID, T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates) {
-        return TyperUtils.extractRowsRemoved(klazz, updates, this);
+        final Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
+                extractRowUpdates(klazz, updates);
+        final Map<UUID, T> result = new HashMap<>();
+        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
+            if (rowUpdate != null && rowUpdate.getNew() == null && rowUpdate.getOld() != null) {
+                Row<GenericTableSchema> row = rowUpdate.getOld();
+                result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
+            }
+        }
+        return result;
+    }
+
+    /**
+     * This method extracts all RowUpdates of Class&lt;T&gt; klazz from a TableUpdates that correspond to rows of type
+     * klazz. Example:
+     * <code>
+     * Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt; updatedBridges =
+     *     extractRowsUpdates(Bridge.class,updates,dbSchema)
+     * </code>
+     *
+     * @param klazz Class for row type to be extracted
+     * @param updates TableUpdates from which to extract rowUpdates
+     * @return Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt;
+     *     for the type of things being sought
+     */
+    private Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> extractRowUpdates(
+            final Class<?> klazz, final TableUpdates updates) {
+        TableUpdate<GenericTableSchema> update = updates.getUpdate(table(TypedReflections.getTableName(klazz),
+            GenericTableSchema.class));
+        Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> result = new HashMap<>();
+        if (update != null) {
+            Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rows = update.getRows();
+            if (rows != null) {
+                result = rows;
+            }
+        }
+        return result;
     }
 }
index 0e2029c3a2c51ad75c15f0948318a5a265f4e46b..3162130ac69e442512e73a6538b3fc8f22fa543b 100644 (file)
@@ -7,13 +7,12 @@
  */
 package org.opendaylight.ovsdb.lib.schema.typed;
 
-import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.common.collect.Range;
-import com.google.common.reflect.Reflection;
-import java.util.HashMap;
 import java.util.Map;
 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
-import org.opendaylight.ovsdb.lib.message.TableUpdate;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.notation.UUID;
@@ -26,6 +25,14 @@ import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
  * Utility methods for typed OVSDB schema data.
  */
 public final class TyperUtils {
+    private static final LoadingCache<DatabaseSchema, TypedDatabaseSchema> TYPED_CACHE = CacheBuilder.newBuilder()
+            .weakKeys().weakValues().build(new CacheLoader<DatabaseSchema, TypedDatabaseSchema>() {
+                @Override
+                public TypedDatabaseSchema load(final DatabaseSchema key) {
+                    return new TypedDatabaseSchemaImpl(key);
+                }
+            });
+
     private TyperUtils() {
         // Prevent instantiating a utility class
     }
@@ -42,30 +49,11 @@ public final class TyperUtils {
         return dbSchema.table(TypedReflections.getTableName(klazz), GenericTableSchema.class);
     }
 
-    public static ColumnSchema<GenericTableSchema, Object> getColumnSchema(final GenericTableSchema tableSchema,
+    static ColumnSchema<GenericTableSchema, Object> getColumnSchema(final GenericTableSchema tableSchema,
             final String columnName, final Class<Object> metaClass) {
         return tableSchema.column(columnName, metaClass);
     }
 
-    /**
-     * 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
-     * and checks for the presence of the Table in Database Schema.
-     *
-     * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
-     * @param klazz Typed Class that represents a Table
-     * @return true if valid, false otherwise
-     */
-    private static <T> boolean isValid(final DatabaseSchema dbSchema, final Class<T> klazz) {
-        final String dbName = TypedReflections.getTableDatabase(klazz);
-        if (dbName != null && !dbSchema.getName().equalsIgnoreCase(dbName)) {
-            return false;
-        }
-
-        checkVersion(dbSchema.getVersion(), TypedReflections.getTableVersionRange(klazz));
-        return true;
-    }
-
     static void checkVersion(final Version schemaVersion, final Range<Version> range) {
         if (!range.contains(schemaVersion)) {
             throw new SchemaVersionMismatchException(schemaVersion,
@@ -116,13 +104,7 @@ public final class TyperUtils {
      */
     public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz,
                                            final Row<GenericTableSchema> row) {
-        if (dbSchema == null || !isValid(dbSchema, klazz)) {
-            return null;
-        }
-        if (row != null) {
-            row.setTableSchema(getTableSchema(dbSchema, klazz));
-        }
-        return Reflection.newProxy(klazz, new TypedRowInvocationHandler(klazz, dbSchema, row));
+        return dbSchema == null ? null : getTyped(dbSchema).getTypedRowWrapper(klazz, row);
     }
 
     /**
@@ -140,19 +122,7 @@ public final class TyperUtils {
      */
     public static <T> Map<UUID,T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates,
             final DatabaseSchema dbSchema) {
-        Preconditions.checkNotNull(klazz);
-        Preconditions.checkNotNull(updates);
-        Preconditions.checkNotNull(dbSchema);
-        Map<UUID,T> result = new HashMap<>();
-        Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
-                extractRowUpdates(klazz,updates,dbSchema);
-        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
-            if (rowUpdate != null && rowUpdate.getNew() != null) {
-                Row<GenericTableSchema> row = rowUpdate.getNew();
-                result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
-            }
-        }
-        return result;
+        return getTyped(dbSchema).extractRowsUpdated(klazz, updates);
     }
 
     /**
@@ -170,19 +140,7 @@ public final class TyperUtils {
      */
     public static <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates,
             final DatabaseSchema dbSchema) {
-        Preconditions.checkNotNull(klazz);
-        Preconditions.checkNotNull(updates);
-        Preconditions.checkNotNull(dbSchema);
-        Map<UUID,T> result = new HashMap<>();
-        Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
-                extractRowUpdates(klazz,updates,dbSchema);
-        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
-            if (rowUpdate != null && rowUpdate.getOld() != null) {
-                Row<GenericTableSchema> row = rowUpdate.getOld();
-                result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
-            }
-        }
-        return result;
+        return getTyped(dbSchema).extractRowsOld(klazz, updates);
     }
 
     /**
@@ -200,50 +158,11 @@ public final class TyperUtils {
      */
     public static <T> Map<UUID,T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates,
             final DatabaseSchema dbSchema) {
-        Preconditions.checkNotNull(klazz);
-        Preconditions.checkNotNull(updates);
-        Preconditions.checkNotNull(dbSchema);
-        Map<UUID,T> result = new HashMap<>();
-        Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
-                extractRowUpdates(klazz,updates,dbSchema);
-        for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
-            if (rowUpdate != null && rowUpdate.getNew() == null && rowUpdate.getOld() != null) {
-                Row<GenericTableSchema> row = rowUpdate.getOld();
-                result.put(rowUpdate.getUuid(),TyperUtils.getTypedRowWrapper(dbSchema,klazz,row));
-            }
-        }
-        return result;
+        return getTyped(dbSchema).extractRowsRemoved(klazz, updates);
     }
 
-    /**
-     * This method extracts all RowUpdates of Class&lt;T&gt; klazz from a TableUpdates
-     * that correspond to rows of type klazz.
-     * Example:
-     * <code>
-     * Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt; updatedBridges =
-     *     extractRowsUpdates(Bridge.class,updates,dbSchema)
-     * </code>
-     *
-     * @param klazz Class for row type to be extracted
-     * @param updates TableUpdates from which to extract rowUpdates
-     * @param dbSchema Dbschema for the TableUpdates
-     * @return Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt;
-     *     for the type of things being sought
-     */
-    static Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> extractRowUpdates(
-            final Class<?> klazz,final TableUpdates updates,final DatabaseSchema dbSchema) {
-        Preconditions.checkNotNull(klazz);
-        Preconditions.checkNotNull(updates);
-        Preconditions.checkNotNull(dbSchema);
-        Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> result =
-                new HashMap<>();
-        TableUpdate<GenericTableSchema> update = updates.getUpdate(TyperUtils.getTableSchema(dbSchema, klazz));
-        if (update != null) {
-            Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rows = update.getRows();
-            if (rows != null) {
-                result = rows;
-            }
-        }
-        return result;
+    private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
+        return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
+                : TYPED_CACHE.getUnchecked(dbSchema);
     }
 }