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
75 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
76 return getTypedRowWrapper(dbSchema, klazz, new Row<>());
80 * Returns a Typed Proxy implementation for the klazz passed as a parameter.
81 * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
82 * Row which is untyped.
84 * <p>Being just a wrapper, it is state-less and more of a convenience functionality
85 * to provide a type-safe infrastructure for the applications to built on top of.
86 * And this Typed infra is completely optional.
88 * <p>It is the applications responsibility to pass on the raw Row parameter and this method
89 * will return the appropriate Proxy wrapper for the passed klazz Type.
90 * The raw Row parameter may be null if the caller is interested in just the
91 * ColumnSchema. But that is not a very common use-case.
93 * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
94 * @param klazz Typed Class that represents a Table
95 * @param row The actual Row that the wrapper is operating on. It can be null if the caller
96 * is just interested in getting ColumnSchema.
98 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz,
99 final Row<GenericTableSchema> row) {
100 return dbSchema == null ? null : getTyped(dbSchema).getTypedRowWrapper(klazz, row);
104 * This method extracts all row updates of Class<T> klazz from a TableUpdates
105 * that correspond to insertion or updates of rows of type klazz.
108 * Map<UUID,Bridge> updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
111 * @param klazz Class for row type to be extracted
112 * @param updates TableUpdates from which to extract rowUpdates
113 * @param dbSchema Dbschema for the TableUpdates
114 * @return Map<UUID,T> for the type of things being sought
116 public static <T> Map<UUID,T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates,
117 final DatabaseSchema dbSchema) {
118 return getTyped(dbSchema).extractRowsUpdated(klazz, updates);
122 * This method extracts all row updates of Class<T> klazz from a TableUpdates
123 * that correspond to old version of rows of type klazz that have been updated.
126 * Map<UUID,Bridge> oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
129 * @param klazz Class for row type to be extracted
130 * @param updates TableUpdates from which to extract rowUpdates
131 * @param dbSchema Dbschema for the TableUpdates
132 * @return Map<UUID,T> for the type of things being sought
134 public static <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates,
135 final DatabaseSchema dbSchema) {
136 return getTyped(dbSchema).extractRowsOld(klazz, updates);
140 * This method extracts all row updates of Class<T> klazz from a TableUpdates
141 * that correspond to removal of rows of type klazz.
144 * Map<UUID,Bridge> updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
147 * @param klazz Class for row type to be extracted
148 * @param updates TableUpdates from which to extract rowUpdates
149 * @param dbSchema Dbschema for the TableUpdates
150 * @return Map<UUID,T> for the type of things being sought
152 public static <T> Map<UUID,T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates,
153 final DatabaseSchema dbSchema) {
154 return getTyped(dbSchema).extractRowsRemoved(klazz, updates);
157 private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
158 return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
159 : TYPED_CACHE.getUnchecked(dbSchema);