Rework TypedRowInvocationHandler invocation path
[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.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
22
23 /**
24  * Utility methods for typed OVSDB schema data.
25  */
26 public final class TyperUtils {
27     private static final LoadingCache<DatabaseSchema, TypedDatabaseSchema> TYPED_CACHE = CacheBuilder.newBuilder()
28             .weakKeys().weakValues().build(new CacheLoader<DatabaseSchema, TypedDatabaseSchema>() {
29                 @Override
30                 public TypedDatabaseSchema load(final DatabaseSchema key) {
31                     return new TypedDatabaseSchemaImpl(key);
32                 }
33             });
34
35     private TyperUtils() {
36         // Prevent instantiating a utility class
37     }
38
39     /**
40      * Retrieve the table schema for the given table in the given database schema.
41      *
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.
46      */
47     @Deprecated
48     public static GenericTableSchema getTableSchema(final DatabaseSchema dbSchema, final Class<?> klazz) {
49         return getTyped(dbSchema).getTableSchema(klazz);
50     }
51
52     static void checkVersion(final Version schemaVersion, final Range<Version> range) {
53         if (!range.contains(schemaVersion)) {
54             throw new SchemaVersionMismatchException(schemaVersion, range);
55         }
56     }
57
58     /**
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.
62      *
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.
66      *
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.
71      *
72      * @param dbSchema DatabaseSchema as learnt from a OVSDB connection
73      * @param klazz Typed Class that represents a Table
74      */
75     public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
76         return getTypedRowWrapper(dbSchema, klazz, new Row<>());
77     }
78
79     /**
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.
83      *
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.
87      *
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.
92      *
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.
97      */
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);
101     }
102
103     /**
104      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
105      * that correspond to insertion or updates of rows of type klazz.
106      * Example:
107      * <code>
108      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
109      * </code>
110      *
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&lt;UUID,T&gt; for the type of things being sought
115      */
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);
119     }
120
121     /**
122      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
123      * that correspond to old version of rows of type klazz that have been updated.
124      * Example:
125      * <code>
126      * Map&lt;UUID,Bridge&gt; oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
127      * </code>
128      *
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&lt;UUID,T&gt; for the type of things being sought
133      */
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);
137     }
138
139     /**
140      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
141      * that correspond to removal of rows of type klazz.
142      * Example:
143      * <code>
144      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
145      * </code>
146      *
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&lt;UUID,T&gt; for the type of things being sought
151      */
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);
155     }
156
157     private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
158         return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
159                 : TYPED_CACHE.getUnchecked(dbSchema);
160     }
161 }