Make TransactionBuilder type-aware
[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     @Deprecated
76     public static <T> T getTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
77         return getTypedRowWrapper(dbSchema, klazz, new Row<>());
78     }
79
80     /**
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.
84      *
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.
88      *
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.
93      *
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.
98      */
99     @Deprecated
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);
103     }
104
105     /**
106      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
107      * that correspond to insertion or updates of rows of type klazz.
108      * Example:
109      * <code>
110      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsUpdated(Bridge.class,updates,dbSchema)
111      * </code>
112      *
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&lt;UUID,T&gt; for the type of things being sought
117      */
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);
121     }
122
123     /**
124      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
125      * that correspond to old version of rows of type klazz that have been updated.
126      * Example:
127      * <code>
128      * Map&lt;UUID,Bridge&gt; oldBridges = extractRowsOld(Bridge.class,updates,dbSchema)
129      * </code>
130      *
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&lt;UUID,T&gt; for the type of things being sought
135      */
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);
139     }
140
141     /**
142      * This method extracts all row updates of Class&lt;T&gt; klazz from a TableUpdates
143      * that correspond to removal of rows of type klazz.
144      * Example:
145      * <code>
146      * Map&lt;UUID,Bridge&gt; updatedBridges = extractRowsRemoved(Bridge.class,updates,dbSchema)
147      * </code>
148      *
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&lt;UUID,T&gt; for the type of things being sought
153      */
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);
157     }
158
159     private static TypedDatabaseSchema getTyped(final DatabaseSchema dbSchema) {
160         return dbSchema instanceof TypedDatabaseSchema ? (TypedDatabaseSchema) dbSchema
161                 : TYPED_CACHE.getUnchecked(dbSchema);
162     }
163 }