2 * Copyright (C) 2014 EBay Software Foundation
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 * Authors : Ashwin Raveendran
10 package org.opendaylight.ovsdb.lib.schema;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.util.HashMap;
18 import java.util.Iterator;
23 public class DatabaseSchema {
25 public static Logger logger = LoggerFactory.getLogger(DatabaseSchema.class);
27 public Map<String, TableSchema> tables;
29 public DatabaseSchema(Map<String, TableSchema> tables) {
33 public Set<String> getTables() {
34 return this.tables.keySet();
37 public boolean hasTable(String table) {
38 return this.getTables().contains(table);
41 public TableSchema getTable(String table) {
42 return this.tables.get(table);
45 public static DatabaseSchema fromJson(JsonNode json) {
46 if (!json.isObject() || !json.has("tables")) {
47 //todo specific types of exception
48 throw new RuntimeException("bad databaseschema root, expected \"tables\" as child");
51 Map<String, TableSchema> tables = new HashMap<>();
52 //Iterator<Map.Entry<String,JsonNode>> fields = json.fields();
53 for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("tables").fields(); iter.hasNext(); ) {
54 Map.Entry<String, JsonNode> table = iter.next();
55 logger.debug("Read schema for table[{}]:{}", table.getKey(), table.getValue());
57 tables.put(table.getKey(), TableSchema.fromJson(table.getKey(), table.getValue()));
60 return new DatabaseSchema(tables);
63 public TransactionBuilder beginTransaction() {
64 return new TransactionBuilder(this);
67 public <E extends TableSchema<E>> TableSchema<E> table(String tableName) {
68 //todo : error handling
69 return tables.get(tableName);
72 public <E extends TableSchema<E>> E table(String tableName, Class<E> clazz) {
73 TableSchema<E> table = table(tableName);
74 return table.as(clazz);