Migrate TyperUtils.getTableSchema() users
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / typed / TypedDatabaseSchemaImpl.java
1 /*
2  * Copyright © 2019 PANTHEON.tech, s.r.o. 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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.reflect.Reflection;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.ovsdb.lib.message.TableUpdate;
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.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.ForwardingDatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
22
23 final class TypedDatabaseSchemaImpl extends ForwardingDatabaseSchema implements TypedDatabaseSchema {
24     private final DatabaseSchema delegate;
25
26     TypedDatabaseSchemaImpl(final DatabaseSchema delegate) {
27         this.delegate = requireNonNull(delegate);
28     }
29
30     @Override
31     protected DatabaseSchema delegate() {
32         return delegate;
33     }
34
35     @Override
36     public TypedDatabaseSchema withInternallyGeneratedColumns() {
37         final DatabaseSchema newDelegate = delegate.withInternallyGeneratedColumns();
38         return newDelegate == delegate ? this : new TypedDatabaseSchemaImpl(newDelegate);
39     }
40
41     @Override
42     public GenericTableSchema getTableSchema(final Class<?> klazz) {
43         return table(TypedReflections.getTableName(klazz), GenericTableSchema.class);
44     }
45
46     @Override
47     public <T> T getTypedRowWrapper(final Class<T> klazz, final Row<GenericTableSchema> row) {
48         // Check validity of  of the parameter passed to getTypedRowWrapper:
49         // -  checks for a valid Database Schema matching the expected Database for a given table
50         // - checks for the presence of the Table in Database Schema.
51         final String dbName = TypedReflections.getTableDatabase(klazz);
52         if (dbName != null && !dbName.equalsIgnoreCase(getName())) {
53             return null;
54         }
55         TyperUtils.checkVersion(getVersion(), TypedReflections.getTableVersionRange(klazz));
56
57         if (row != null) {
58             row.setTableSchema(getTableSchema(klazz));
59         }
60         return Reflection.newProxy(klazz, new TypedRowInvocationHandler(klazz, this, row));
61     }
62
63     @Override
64     public <T> Map<UUID, T> extractRowsOld(final Class<T> klazz, final TableUpdates updates) {
65         Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates = extractRowUpdates(
66             requireNonNull(klazz), requireNonNull(updates));
67         Map<UUID,T> result = new HashMap<>();
68         for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
69             if (rowUpdate != null && rowUpdate.getOld() != null) {
70                 Row<GenericTableSchema> row = rowUpdate.getOld();
71                 result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
72             }
73         }
74         return result;
75     }
76
77     @Override
78     public <T> Map<UUID, T> extractRowsUpdated(final Class<T> klazz, final TableUpdates updates) {
79         final Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
80                 extractRowUpdates(klazz, updates);
81         final Map<UUID,T> result = new HashMap<>();
82         for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
83             if (rowUpdate != null && rowUpdate.getNew() != null) {
84                 Row<GenericTableSchema> row = rowUpdate.getNew();
85                 result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
86             }
87         }
88         return result;
89     }
90
91     @Override
92     public <T> Map<UUID, T> extractRowsRemoved(final Class<T> klazz, final TableUpdates updates) {
93         final Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rowUpdates =
94                 extractRowUpdates(klazz, updates);
95         final Map<UUID, T> result = new HashMap<>();
96         for (TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema> rowUpdate : rowUpdates.values()) {
97             if (rowUpdate != null && rowUpdate.getNew() == null && rowUpdate.getOld() != null) {
98                 Row<GenericTableSchema> row = rowUpdate.getOld();
99                 result.put(rowUpdate.getUuid(), getTypedRowWrapper(klazz, row));
100             }
101         }
102         return result;
103     }
104
105     /**
106      * This method extracts all RowUpdates of Class&lt;T&gt; klazz from a TableUpdates that correspond to rows of type
107      * klazz. Example:
108      * <code>
109      * Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt; updatedBridges =
110      *     extractRowsUpdates(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      * @return Map&lt;UUID,TableUpdate&lt;GenericTableSchema&gt;.RowUpdate&lt;GenericTableSchema&gt;&gt;
116      *     for the type of things being sought
117      */
118     private Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> extractRowUpdates(
119             final Class<?> klazz, final TableUpdates updates) {
120         TableUpdate<GenericTableSchema> update = updates.getUpdate(table(TypedReflections.getTableName(klazz),
121             GenericTableSchema.class));
122         Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> result = new HashMap<>();
123         if (update != null) {
124             Map<UUID, TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> rows = update.getRows();
125             if (rows != null) {
126                 result = rows;
127             }
128         }
129         return result;
130     }
131 }