Migrate TyperUtils.getTableSchema() users
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / typed / TyperUtils.java
1 /*
2  * Copyright © 2014, 2017 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.lib.schema.typed;
9
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;
14 import java.util.Map;
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;
23
24 /**
25  * Utility methods for typed OVSDB schema data.
26  */
27 public final class TyperUtils {
28     private static final LoadingCache<DatabaseSchema, TypedDatabaseSchema> TYPED_CACHE = CacheBuilder.newBuilder()
29             .weakKeys().weakValues().build(new CacheLoader<DatabaseSchema, TypedDatabaseSchema>() {
30                 @Override
31                 public TypedDatabaseSchema load(final DatabaseSchema key) {
32                     return new TypedDatabaseSchemaImpl(key);
33                 }
34             });
35
36     private TyperUtils() {
37         // Prevent instantiating a utility class
38     }
39
40     /**
41      * Retrieve the table schema for the given table in the given database schema.
42      *
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.
47      */
48     @Deprecated
49     public static GenericTableSchema getTableSchema(final DatabaseSchema dbSchema, final Class<?> klazz) {
50         return getTyped(dbSchema).getTableSchema(klazz);
51     }
52
53     static ColumnSchema<GenericTableSchema, Object> getColumnSchema(final GenericTableSchema tableSchema,
54             final String columnName, final Class<Object> metaClass) {
55         return tableSchema.column(columnName, metaClass);
56     }
57
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);
63         }
64     }
65
66     /**
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.
70      *
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.
74      *
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.
79      *
80      * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
81      * @param klazz Typed Class that represents a Table
82      */
83     public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
84         return getTypedRowWrapper(dbSchema, klazz, new Row<>());
85     }
86
87     /**
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.
91      *
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.
95      *
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.
100      *
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.
105      */
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);
109     }
110
111     /**
112      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
113      * that correspond to insertion or updates of rows of type klazz.
114      * Example:
115      * <code>
116      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
117      * </code>
118      *
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&lt;UUID,T&gt; for the type of things being sought
123      */
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);
127     }
128
129     /**
130      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
131      * that correspond to old version of rows of type klazz that have been updated.
132      * Example:
133      * <code>
134      * Map&lt;UUID,Bridge&gt; oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
135      * </code>
136      *
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&lt;UUID,T&gt; for the type of things being sought
141      */
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);
145     }
146
147     /**
148      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
149      * that correspond to removal of rows of type klazz.
150      * Example:
151      * <code>
152      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
153      * </code>
154      *
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&lt;UUID,T&gt; for the type of things being sought
159      */
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);
163     }
164
165     private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
166         return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
167                 : TYPED_CACHE.getUnchecked(dbSchema);
168     }
169 }