47a0d9a12c833a52cde700ac71053055629b4cf2
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / GenericTableSchema.java
1 /*
2  * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
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 package org.opendaylight.ovsdb.lib.schema;
9
10 import com.fasterxml.jackson.databind.JsonNode;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.Map;
14 import org.opendaylight.ovsdb.lib.error.BadSchemaException;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class GenericTableSchema extends TableSchema<GenericTableSchema> {
19     private static final Logger LOG = LoggerFactory.getLogger(GenericTableSchema.class);
20
21     public GenericTableSchema(final String name) {
22         super(name);
23     }
24
25     public GenericTableSchema(final String name, final Map<String, ColumnSchema> columns) {
26         super(name, columns);
27     }
28
29     public GenericTableSchema(final TableSchema tableSchema) {
30         super(tableSchema.getName(), tableSchema.getColumnSchemas());
31     }
32
33     public static GenericTableSchema fromJson(final String tableName, final JsonNode json) {
34         if (!json.isObject() || !json.has("columns")) {
35             throw new BadSchemaException("bad tableschema root, expected \"columns\" as child");
36         }
37
38         Map<String, ColumnSchema> columns = new HashMap<>();
39         for (Iterator<Map.Entry<String, JsonNode>> iter = json.get("columns").fields(); iter.hasNext(); ) {
40             Map.Entry<String, JsonNode> column = iter.next();
41             LOG.trace("fromJson() table/column = {}:{}", tableName, column.getKey());
42             columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue()));
43         }
44
45         return new GenericTableSchema(tableName, columns);
46     }
47 }