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.ColumnSchema;
21 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
22 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
25 * Utility methods for typed OVSDB schema data.
27 public final class TyperUtils {
28 private static final LoadingCache<DatabaseSchema, TypedDatabaseSchema> TYPED_CACHE = CacheBuilder.newBuilder()
29 .weakKeys().weakValues().build(new CacheLoader<DatabaseSchema, TypedDatabaseSchema>() {
31 public TypedDatabaseSchema load(final DatabaseSchema key) {
32 return new TypedDatabaseSchemaImpl(key);
36 private TyperUtils() {
37 // Prevent instantiating a utility class
41 * Retrieve the table schema for the given table in the given database schema.
43 * @param dbSchema The database schema.
44 * @param klazz The class whose table schema should be retrieved. Classes are matched in the database schema either
45 * using their {@link TypedTable} annotation, if they have one, or by name.
46 * @return the table schema.
49 public static GenericTableSchema getTableSchema(final DatabaseSchema dbSchema, final Class<?> klazz) {
50 return getTyped(dbSchema).getTableSchema(klazz);
53 static ColumnSchema<GenericTableSchema, Object> getColumnSchema(final GenericTableSchema tableSchema,
54 final String columnName, final Class<Object> metaClass) {
55 return tableSchema.column(columnName, metaClass);
58 static void checkVersion(final Version schemaVersion, final Range<Version> range) {
59 if (!range.contains(schemaVersion)) {
60 throw new SchemaVersionMismatchException(schemaVersion,
61 range.hasLowerBound() ? range.lowerEndpoint() : Version.NULL,
62 range.hasUpperBound() ? range.upperEndpoint() : Version.NULL);
67 * Returns a Typed Proxy implementation for the klazz passed as a parameter.
68 * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
69 * Row which is untyped.
71 * <p>Being just a wrapper, it is state-less and more of a convenience functionality to
72 * provide a type-safe infrastructure for the applications to built on top of.
73 * And this Typed infra is completely optional.
75 * <p>It is the applications responsibility to pass on the raw Row parameter and this method will
76 * return the appropriate Proxy wrapper for the passed klazz Type.
77 * The raw Row parameter may be null if the caller is interested in just the ColumnSchema.
78 * But that is not a very common use-case.
80 * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
81 * @param klazz Typed Class that represents a Table
83 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
84 return getTypedRowWrapper(dbSchema, klazz, new Row<>());
88 * Returns a Typed Proxy implementation for the klazz passed as a parameter.
89 * Per design choice, the Typed Proxy implementation is just a Wrapper on top of the actual
90 * Row which is untyped.
92 * <p>Being just a wrapper, it is state-less and more of a convenience functionality
93 * to provide a type-safe infrastructure for the applications to built on top of.
94 * And this Typed infra is completely optional.
96 * <p>It is the applications responsibility to pass on the raw Row parameter and this method
97 * will return the appropriate Proxy wrapper for the passed klazz Type.
98 * The raw Row parameter may be null if the caller is interested in just the
99 * ColumnSchema. But that is not a very common use-case.
101 * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
102 * @param klazz Typed Class that represents a Table
103 * @param row The actual Row that the wrapper is operating on. It can be null if the caller
104 * is just interested in getting ColumnSchema.
106 public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz,
107 final Row<GenericTableSchema> row) {
108 return dbSchema == null ? null : getTyped(dbSchema).getTypedRowWrapper(klazz, row);
112 * This method extracts all row updates of Class<T> klazz from a TableUpdates
113 * that correspond to insertion or updates of rows of type klazz.
116 * Map<UUID,Bridge> updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
119 * @param klazz Class for row type to be extracted
120 * @param updates TableUpdates from which to extract rowUpdates
121 * @param dbSchema Dbschema for the TableUpdates
122 * @return Map<UUID,T> for the type of things being sought
124 public static <T> Map<UUID,T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates,
125 final DatabaseSchema dbSchema) {
126 return getTyped(dbSchema).extractRowsUpdated(klazz, updates);
130 * This method extracts all row updates of Class<T> klazz from a TableUpdates
131 * that correspond to old version of rows of type klazz that have been updated.
134 * Map<UUID,Bridge> oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
137 * @param klazz Class for row type to be extracted
138 * @param updates TableUpdates from which to extract rowUpdates
139 * @param dbSchema Dbschema for the TableUpdates
140 * @return Map<UUID,T> for the type of things being sought
142 public static <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates,
143 final DatabaseSchema dbSchema) {
144 return getTyped(dbSchema).extractRowsOld(klazz, updates);
148 * This method extracts all row updates of Class<T> klazz from a TableUpdates
149 * that correspond to removal of rows of type klazz.
152 * Map<UUID,Bridge> updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
155 * @param klazz Class for row type to be extracted
156 * @param updates TableUpdates from which to extract rowUpdates
157 * @param dbSchema Dbschema for the TableUpdates
158 * @return Map<UUID,T> for the type of things being sought
160 public static <T> Map<UUID,T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates,
161 final DatabaseSchema dbSchema) {
162 return getTyped(dbSchema).extractRowsRemoved(klazz, updates);
165 private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
166 return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
167 : TYPED_CACHE.getUnchecked(dbSchema);