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 java.lang.reflect.Constructor;
13 import java.util.Iterator;
14 import java.util.List;
18 import org.opendaylight.ovsdb.lib.message.TableUpdate;
19 import org.opendaylight.ovsdb.lib.notation.Column;
20 import org.opendaylight.ovsdb.lib.notation.Row;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.Insert;
23 import org.opendaylight.ovsdb.lib.schema.BaseType.UuidBaseType;
24 import org.opendaylight.ovsdb.lib.schema.ColumnType.AtomicColumnType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30 import com.google.common.collect.Lists;
33 public abstract class TableSchema<E extends TableSchema<E>> {
36 protected static final Logger logger = LoggerFactory.getLogger(TableSchema.class);
38 private Map<String, ColumnSchema> columns;
40 public TableSchema() {
43 protected TableSchema(String name) {
47 public TableSchema(String name, Map<String, ColumnSchema> columns) {
49 this.columns = columns;
52 public Set<String> getColumns() {
53 return this.columns.keySet();
56 public Map<String, ColumnSchema> getColumnSchemas() {
60 public boolean hasColumn(String column) {
61 return this.getColumns().contains(column);
65 public ColumnType getColumnType(String column) {
66 return this.columns.get(column).getType();
69 public <E extends TableSchema<E>> E as(Class<E> clazz) {
71 Constructor<E> e = clazz.getConstructor(TableSchema.class);
72 return e.newInstance(this);
73 } catch (Exception e) {
74 throw new RuntimeException("exception constructing instance of clazz " + clazz, e);
78 public Insert<E> insert() {
79 return new Insert<>(this);
82 public <D> ColumnSchema<E, Set<D>> multiValuedColumn(String column, Class<D> type) {
83 //todo exception handling
85 ColumnSchema columnSchema = columns.get(column);
86 columnSchema.validateType(type);
90 public <K,V> ColumnSchema<E, Map<K,V>> multiValuedColumn(String column, Class<K> keyType, Class<V> valueType) {
91 //todo exception handling
93 ColumnSchema columnSchema = columns.get(column);
94 columnSchema.validateType(valueType);
98 public <D> ColumnSchema<E, D> column(String column, Class<D> type) {
99 //todo exception handling
101 ColumnSchema columnSchema = columns.get(column);
102 columnSchema.validateType(type);
106 public ColumnSchema column(String column) {
107 return this.columns.get(column);
111 public String getName() {
115 protected void setName(String name) {
119 protected void setColumns(Map<String, ColumnSchema> columns) {
120 this.columns = columns;
123 public TableUpdate<E> updatesFromJson(JsonNode value) {
125 Map.Entry<String, JsonNode> idOldNew = value.fields().next();
126 String uuid = idOldNew.getKey();
128 ObjectNode new_ = (ObjectNode) idOldNew.getValue().get("new");
129 ObjectNode old = (ObjectNode) idOldNew.getValue().get("old");
131 Row<E> newRow = new_ != null ? createRow(new_) : null;
132 Row<E> oldRow = old != null ? createRow(old) : null;
134 TableUpdate<E> tableUpdate = new TableUpdate<>(new UUID(uuid));
135 tableUpdate.setNew(newRow);
136 tableUpdate.setOld(oldRow);
141 protected Row<E> createRow(ObjectNode rowNode) {
142 List<Column<E, ?>> columns = Lists.newArrayList();
143 for (Iterator<Map.Entry<String, JsonNode>> iter = rowNode.fields(); iter.hasNext();) {
144 Map.Entry<String, JsonNode> next = iter.next();
145 ColumnSchema<E, Object> schema = column(next.getKey(), Object.class);
146 Object o = schema.valueFromJson(next.getValue());
147 columns.add(new Column<>(schema, o));
149 return new Row<>(columns);
153 * RFC 7047 Section 3.2 specifies 2 internally generated columns in each table
154 * namely _uuid and version which are not exposed in get_schema call.
155 * Since these 2 columns are extremely useful for Mutate, update and select operations,
156 * the ColumnSchema for these 2 columns are manually populated.
158 * It is to be noted that these 2 columns are specified as part of the RFC7047 and not
159 * a specific Schema implementation detail & hence adding it by default in the Library
160 * for better application experience using the library.
162 public void populateInternallyGeneratedColumns() {
163 columns.put("_uuid", new ColumnSchema("_uuid", new AtomicColumnType(new UuidBaseType())));
164 columns.put("version", new ColumnSchema("version", new AtomicColumnType(new UuidBaseType())));