Code ReOrganization and Re-Architecture changes
[ovsdb.git] / library / src / main / java / org / opendaylight / ovsdb / lib / schema / TableSchema.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
3  *
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
7  *
8  * Authors : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.schema;
11
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;
16
17 import java.lang.reflect.Constructor;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.Set;
22
23
24 public class TableSchema<E extends TableSchema<E>> {
25
26     protected static final Logger logger = LoggerFactory.getLogger(TableSchema.class);
27     private String name;
28     private Map<String, ColumnSchema> columns;
29
30     public TableSchema() {
31     }
32
33     public TableSchema(String name, Map<String, ColumnSchema> columns) {
34         this.name = name;
35         this.columns = columns;
36     }
37
38     public Set<String> getColumns() {
39         return this.columns.keySet();
40     }
41
42     public Map<String, ColumnSchema> getColumnSchemas() {
43         return columns;
44     }
45
46     public boolean hasColumn(String column) {
47         return this.getColumns().contains(column);
48     }
49
50     public ColumnSchema getColumn(String column) {
51         return this.columns.get(column);
52     }
53
54     public ColumnType getColumnType(String column) {
55         return this.columns.get(column).getType();
56     }
57
58     public static TableSchema fromJson(String tableName, JsonNode json) {
59
60         if (!json.isObject() || !json.has("columns")) {
61             //todo specific types of exception
62             throw new RuntimeException("bad tableschema root, expected \"columns\" as child");
63         }
64
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()));
70         }
71
72         TableSchema tableSchema = new TableSchema(tableName, columns);
73         return tableSchema;
74     }
75
76     public <E extends TableSchema<E>> E as(Class<E> clazz) {
77         try {
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);
82         }
83     }
84
85     public Insert<E> insert() {
86         return new Insert<>(this);
87     }
88
89     public <D> ColumnSchema<E, D> column(String column, Class<D> type) {
90         //todo exception handling
91         return columns.get(column);
92     }
93
94     public String getName() {
95         return name;
96     }
97
98     public void setName(String name) {
99         this.name = name;
100     }
101
102 }