2 * Copyright © 2014, 2017 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.ovsdb.lib.schema.typed;
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.Range;
15 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
16 import org.opendaylight.ovsdb.lib.message.TableUpdates;
17 import org.opendaylight.ovsdb.lib.notation.Row;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.notation.Version;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
24 * Utility methods for typed OVSDB schema data.
26 public final class TyperUtils {
27 private static final LoadingCache<DatabaseSchema, TypedDatabaseSchema> TYPED_CACHE = CacheBuilder.newBuilder()
28 .weakKeys().weakValues().build(new CacheLoader<DatabaseSchema, TypedDatabaseSchema>() {
30 public TypedDatabaseSchema load(final DatabaseSchema key) {
31 return new TypedDatabaseSchemaImpl(key);
35 private TyperUtils() {
36 // Prevent instantiating a utility class
40 * Retrieve the table schema for the given table in the given database schema.
42 * @param dbSchema The database schema.
43 * @param klazz The class whose table schema should be retrieved. Classes are matched in the database schema either
44 * using their {@link TypedTable} annotation, if they have one, or by name.
45 * @return the table schema.
48 public static GenericTableSchema getTableSchema(final DatabaseSchema dbSchema, final Class<?> klazz) {
49 return getTyped(dbSchema).getTableSchema(klazz);
52 static void checkVersion(final Version schemaVersion, final Range<Version> range) {
53 if (!range.contains(schemaVersion)) {
54 throw new SchemaVersionMismatchException(schemaVersion, range);
59 * Returns a Typed Proxy implementation for the klazz passed as a parameter.
60 * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
61 * Row which is untyped.
63 * <p>Being just a wrapper, it is state-less and more of a convenience functionality to
64 * provide a type-safe infrastructure for the applications to built on top of.
65 * And this Typed infra is completely optional.
67 * <p>It is the applications responsibility to pass on the raw Row parameter and this method will
68 * return the appropriate Proxy wrapper for the passed klazz Type.
69 * The raw Row parameter may be null if the caller is interested in just the ColumnSchema.
70 * But that is not a very common use-case.
72 * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
73 * @param klazz Typed Class that represents a Table
76 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
77 return getTypedRowWrapper(dbSchema, klazz, new Row<>());
81 * Returns a Typed Proxy implementation for the klazz passed as a parameter.
82 * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
83 * Row which is untyped.
85 * <p>Being just a wrapper, it is state-less and more of a convenience functionality
86 * to provide a type-safe infrastructure for the applications to built on top of.
87 * And this Typed infra is completely optional.
89 * <p>It is the applications responsibility to pass on the raw Row parameter and this method
90 * will return the appropriate Proxy wrapper for the passed klazz Type.
91 * The raw Row parameter may be null if the caller is interested in just the
92 * ColumnSchema. But that is not a very common use-case.
94 * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
95 * @param klazz Typed Class that represents a Table
96 * @param row The actual Row that the wrapper is operating on. It can be null if the caller
97 * is just interested in getting ColumnSchema.
100 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz,
101 final Row<GenericTableSchema> row) {
102 return dbSchema == null ? null : getTyped(dbSchema).getTypedRowWrapper(klazz, row);
106 * This method extracts all row updates of Class<T> klazz from a TableUpdates
107 * that correspond to insertion or updates of rows of type klazz.
110 * Map<UUID,Bridge> updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
113 * @param klazz Class for row type to be extracted
114 * @param updates TableUpdates from which to extract rowUpdates
115 * @param dbSchema Dbschema for the TableUpdates
116 * @return Map<UUID,T> for the type of things being sought
118 public static <T> Map<UUID,T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates,
119 final DatabaseSchema dbSchema) {
120 return getTyped(dbSchema).extractRowsUpdated(klazz, updates);
124 * This method extracts all row updates of Class<T> klazz from a TableUpdates
125 * that correspond to old version of rows of type klazz that have been updated.
128 * Map<UUID,Bridge> oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
131 * @param klazz Class for row type to be extracted
132 * @param updates TableUpdates from which to extract rowUpdates
133 * @param dbSchema Dbschema for the TableUpdates
134 * @return Map<UUID,T> for the type of things being sought
136 public static <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates,
137 final DatabaseSchema dbSchema) {
138 return getTyped(dbSchema).extractRowsOld(klazz, updates);
142 * This method extracts all row updates of Class<T> klazz from a TableUpdates
143 * that correspond to removal of rows of type klazz.
146 * Map<UUID,Bridge> updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
149 * @param klazz Class for row type to be extracted
150 * @param updates TableUpdates from which to extract rowUpdates
151 * @param dbSchema Dbschema for the TableUpdates
152 * @return Map<UUID,T> for the type of things being sought
154 public static <T> Map<UUID,T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates,
155 final DatabaseSchema dbSchema) {
156 return getTyped(dbSchema).extractRowsRemoved(klazz, updates);
159 private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
160 return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
161 : TYPED_CACHE.getUnchecked(dbSchema);