2 * Copyright (c) 2014, 2015 EBay Software Foundation 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
9 package org.opendaylight.ovsdb.lib.schema;
11 import com.fasterxml.jackson.databind.JsonNode;
12 import com.google.common.reflect.Invokable;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.HashMap;
16 import java.util.Iterator;
19 import org.opendaylight.ovsdb.lib.error.ParsingException;
20 import org.opendaylight.ovsdb.lib.notation.Version;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Represents an ovsdb database schema, which is comprised of a set of tables.
27 public class DatabaseSchema {
29 private static final Logger LOG = LoggerFactory.getLogger(DatabaseSchema.class);
33 private Version version;
34 private final Map<String, TableSchema> tables;
36 public DatabaseSchema(final Map<String, TableSchema> tables) {
40 public DatabaseSchema(final String name, final Version version, final Map<String, TableSchema> tables) {
42 this.version = version;
46 public Set<String> getTables() {
47 return this.tables.keySet();
50 public boolean hasTable(final String table) {
51 return this.getTables().contains(table);
54 public <E extends TableSchema<E>> E table(final String tableName, final Class<E> clazz) {
55 TableSchema<E> table = tables.get(tableName);
57 if (clazz.isInstance(table)) {
58 return clazz.cast(table);
61 return createTableSchema(clazz, table);
64 protected <E extends TableSchema<E>> E createTableSchema(final Class<E> clazz, final TableSchema<E> table) {
65 Constructor<E> declaredConstructor;
67 declaredConstructor = clazz.getDeclaredConstructor(TableSchema.class);
68 } catch (NoSuchMethodException e) {
69 String message = String.format("Class %s does not have public constructor that accepts TableSchema object",
71 throw new IllegalArgumentException(message, e);
73 Invokable<E, E> invokable = Invokable.from(declaredConstructor);
75 return invokable.invoke(null, table);
76 } catch (InvocationTargetException | IllegalAccessException e) {
77 String message = String.format("Not able to create instance of class %s using public constructor "
78 + "that accepts TableSchema object", clazz);
79 throw new IllegalArgumentException(message, e);
83 //todo : this needs to move to a custom factory
84 public static DatabaseSchema fromJson(final String dbName, final JsonNode json) {
85 if (!json.isObject() || !json.has("tables")) {
86 throw new ParsingException("bad DatabaseSchema root, expected \"tables\" as child but was not found");
88 if (!json.isObject() || !json.has("version")) {
89 throw new ParsingException("bad DatabaseSchema root, expected \"version\" as child but was not found");
92 Version dbVersion = Version.fromString(json.get("version").asText());
94 Map<String, TableSchema> tables = new HashMap<>();
95 for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("tables").fields(); iter.hasNext(); ) {
96 Map.Entry<String, JsonNode> table = iter.next();
97 LOG.trace("Read schema for table[{}]:{}", table.getKey(), table.getValue());
99 //todo : this needs to done by a factory
100 tables.put(table.getKey(), GenericTableSchema.fromJson(table.getKey(), table.getValue()));
103 return new DatabaseSchema(dbName, dbVersion, tables);
106 public String getName() {
110 public void setName(final String name) {
114 public Version getVersion() {
118 public void setVersion(final Version version) {
119 this.version = version;
122 public void populateInternallyGeneratedColumns() {
123 for (TableSchema tableSchema : tables.values()) {
124 tableSchema.populateInternallyGeneratedColumns();