2 * Copyright © 2019 PANTHEON.tech, s.r.o. 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 static java.util.Objects.requireNonNull;
12 import com.google.common.reflect.Reflection;
13 import java.util.HashMap;
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;
23 final class TypedDatabaseSchemaImpl extends ForwardingDatabaseSchema implements TypedDatabaseSchema {
24 private final DatabaseSchema delegate;
26 TypedDatabaseSchemaImpl(final DatabaseSchema delegate) {
27 this.delegate = requireNonNull(delegate);
31 protected DatabaseSchema delegate() {
36 public TypedDatabaseSchema withInternallyGeneratedColumns() {
37 final DatabaseSchema newDelegate = delegate.withInternallyGeneratedColumns();
38 return newDelegate == delegate ? this : new TypedDatabaseSchemaImpl(newDelegate);
42 public GenericTableSchema getTableSchema(final Class<?> klazz) {
43 return TyperUtils.getTableSchema(this, klazz);
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())) {
55 TyperUtils.checkVersion(getVersion(), TypedReflections.getTableVersionRange(klazz));
58 row.setTableSchema(getTableSchema(klazz));
60 return Reflection.newProxy(klazz, new TypedRowInvocationHandler(klazz, this, row));
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));
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));
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));
106 * This method extracts all RowUpdates of Class<T> klazz from a TableUpdates that correspond to rows of type
109 * Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>> updatedBridges =
110 * extractRowsUpdates(Bridge.class,updates,dbSchema)
113 * @param klazz Class for row type to be extracted
114 * @param updates TableUpdates from which to extract rowUpdates
115 * @return Map<UUID,TableUpdate<GenericTableSchema>.RowUpdate<GenericTableSchema>>
116 * for the type of things being sought
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();