/* * Copyright (C) 2014 EBay Software Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Authors : Ashwin Raveendran */ package org.opendaylight.ovsdb.lib.meta; import com.fasterxml.jackson.databind.JsonNode; import org.opendaylight.ovsdb.Insert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TableSchema> { protected static final Logger logger = LoggerFactory.getLogger(TableSchema.class); private String name; private Map columns; public TableSchema() { } public TableSchema(String name, Map columns) { this.name = name; this.columns = columns; } public static TableSchema fromJson(String tableName, JsonNode json) { if (!json.isObject() || !json.has("columns")) { //todo specific types of exception throw new RuntimeException("bad tableschema root, expected \"columns\" as child"); } Map columns = new HashMap<>(); for (Iterator> iter = json.get("columns").fields(); iter.hasNext(); ) { Map.Entry column = iter.next(); logger.debug("%s:%s", tableName, column.getKey()); columns.put(column.getKey(), ColumnSchema.fromJson(column.getKey(), column.getValue())); } TableSchema tableSchema = new TableSchema(tableName, columns); return tableSchema; } public > E as(Class clazz) { try { Constructor e = clazz.getConstructor(TableSchema.class); return e.newInstance(this); } catch (Exception e) { throw new RuntimeException("exception constructing instance of clazz " + clazz, e); } } public Insert insert() { return new Insert<>(this); } public ColumnSchema column(String column) { //todo exception handling return columns.get(column); } public String getName() { return name; } public void setName(String name) { this.name = name; } public static class AnyTableSchema extends TableSchema{} }