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.meta;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import org.opendaylight.ovsdb.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;
23 public class TableSchema<E extends TableSchema<E>> {
24 protected static final Logger logger = LoggerFactory.getLogger(TableSchema.class);
26 private Map<String, ColumnSchema> columns;
28 public TableSchema() {
31 public TableSchema(String name, Map<String, ColumnSchema> columns) {
33 this.columns = columns;
36 public static TableSchema fromJson(String tableName, JsonNode json) {
38 if (!json.isObject() || !json.has("columns")) {
39 //todo specific types of exception
40 throw new RuntimeException("bad tableschema root, expected \"columns\" as child");
43 Map<String, ColumnSchema> columns = new HashMap<>();
44 for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("columns").fields(); iter.hasNext(); ) {
45 Map.Entry<String, JsonNode> column = iter.next();
46 logger.debug("%s:%s", tableName, column.getKey());
47 columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue()));
50 TableSchema tableSchema = new TableSchema(tableName, columns);
54 public <E extends TableSchema<E>> E as(Class<E> clazz) {
56 Constructor<E> e = clazz.getConstructor(TableSchema.class);
57 return e.newInstance(this);
58 } catch (Exception e) {
59 throw new RuntimeException("exception constructing instance of clazz " + clazz, e);
63 public Insert<E> insert() {
64 return new Insert<>(this);
69 public <D> ColumnSchema<E, D> column(String column) {
70 //todo exception handling
71 return columns.get(column);
74 public String getName() {
78 public void setName(String name) {
82 public static class AnyTableSchema extends TableSchema<AnyTableSchema>{}