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.Insert;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.lang.reflect.Constructor;
18 import java.util.HashMap;
19 import java.util.Iterator;
24 public class TableSchema<E extends TableSchema<E>> {
26 protected static final Logger logger = LoggerFactory.getLogger(TableSchema.class);
28 private Map<String, ColumnSchema> columns;
30 public TableSchema() {
33 public TableSchema(String name, Map<String, ColumnSchema> columns) {
35 this.columns = columns;
38 public Set<String> getColumns() {
39 return this.columns.keySet();
42 public Map<String, ColumnSchema> getColumnSchemas() {
46 public boolean hasColumn(String column) {
47 return this.getColumns().contains(column);
50 public ColumnSchema getColumn(String column) {
51 return this.columns.get(column);
54 public ColumnType getColumnType(String column) {
55 return this.columns.get(column).getType();
58 public static TableSchema fromJson(String tableName, JsonNode json) {
60 if (!json.isObject() || !json.has("columns")) {
61 //todo specific types of exception
62 throw new RuntimeException("bad tableschema root, expected \"columns\" as child");
65 Map<String, ColumnSchema> columns = new HashMap<>();
66 for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("columns").fields(); iter.hasNext(); ) {
67 Map.Entry<String, JsonNode> column = iter.next();
68 logger.debug("%s:%s", tableName, column.getKey());
69 columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue()));
72 TableSchema tableSchema = new TableSchema(tableName, columns);
76 public <E extends TableSchema<E>> E as(Class<E> clazz) {
78 Constructor<E> e = clazz.getConstructor(TableSchema.class);
79 return e.newInstance(this);
80 } catch (Exception e) {
81 throw new RuntimeException("exception constructing instance of clazz " + clazz, e);
85 public Insert<E> insert() {
86 return new Insert<>(this);
89 public <D> ColumnSchema<E, D> column(String column, Class<D> type) {
90 //todo exception handling
91 return columns.get(column);
94 public String getName() {
98 public void setName(String name) {