2 * Copyright (c) 2014, 2015 EBay Software Foundation and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.lib.schema;
11 import com.fasterxml.jackson.databind.JsonNode;
12 import com.fasterxml.jackson.databind.node.ObjectNode;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
19 import java.util.Map.Entry;
21 import org.opendaylight.ovsdb.lib.message.TableUpdate;
22 import org.opendaylight.ovsdb.lib.notation.Column;
23 import org.opendaylight.ovsdb.lib.notation.Row;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.operations.Insert;
26 import org.opendaylight.ovsdb.lib.schema.BaseType.UuidBaseType;
27 import org.opendaylight.ovsdb.lib.schema.ColumnType.AtomicColumnType;
30 public abstract class TableSchema<E extends TableSchema<E>> {
33 private Map<String, ColumnSchema> columns;
35 public TableSchema() {
38 protected TableSchema(String name) {
42 public TableSchema(String name, Map<String, ColumnSchema> columns) {
44 this.columns = columns;
47 public Set<String> getColumns() {
48 return this.columns.keySet();
51 public Map<String, ColumnSchema> getColumnSchemas() {
55 public boolean hasColumn(String column) {
56 return this.getColumns().contains(column);
60 public ColumnType getColumnType(String column) {
61 return this.columns.get(column).getType();
64 public <E extends TableSchema<E>> E as(Class<E> clazz) {
66 Constructor<E> instance = clazz.getConstructor(TableSchema.class);
67 return instance.newInstance(this);
68 } catch (InstantiationException | IllegalAccessException
69 | InvocationTargetException | NoSuchMethodException e) {
70 throw new RuntimeException("exception constructing instance of clazz " + clazz, e);
74 public Insert<E> insert() {
75 return new Insert<>(this);
78 public <D> ColumnSchema<E, Set<D>> multiValuedColumn(String column, Class<D> type) {
79 //todo exception handling
81 ColumnSchema<E, Set<D>> columnSchema = columns.get(column);
82 columnSchema.validateType(type);
86 public <K,V> ColumnSchema<E, Map<K,V>> multiValuedColumn(String column, Class<K> keyType, Class<V> valueType) {
87 //todo exception handling
89 ColumnSchema<E, Map<K, V>> columnSchema = columns.get(column);
90 columnSchema.validateType(valueType);
94 public <D> ColumnSchema<E, D> column(String column, Class<D> type) {
95 //todo exception handling
97 ColumnSchema<E, D> columnSchema = columns.get(column);
98 if (columnSchema != null) {
99 columnSchema.validateType(type);
104 public ColumnSchema column(String column) {
105 return this.columns.get(column);
109 public String getName() {
113 protected void setName(String name) {
117 protected void setColumns(Map<String, ColumnSchema> columns) {
118 this.columns = columns;
121 public TableUpdate<E> updatesFromJson(JsonNode value) {
122 TableUpdate<E> tableUpdate = new TableUpdate<>();
123 Iterator<Entry<String, JsonNode>> fields = value.fields();
124 while (fields.hasNext()) {
125 Map.Entry<String, JsonNode> idOldNew = fields.next();
126 String uuid = idOldNew.getKey();
128 ObjectNode newObjectNode = (ObjectNode) idOldNew.getValue().get("new");
129 ObjectNode oldObjectNode = (ObjectNode) idOldNew.getValue().get("old");
131 Row<E> newRow = newObjectNode != null ? createRow(newObjectNode) : null;
132 Row<E> oldRow = oldObjectNode != null ? createRow(oldObjectNode) : null;
134 tableUpdate.addRow(new UUID(uuid), oldRow, newRow);
139 public Row<E> createRow(ObjectNode rowNode) {
140 List<Column<E, ?>> newColumns = new ArrayList<>();
141 for (Iterator<Map.Entry<String, JsonNode>> iter = rowNode.fields(); iter.hasNext();) {
142 Map.Entry<String, JsonNode> next = iter.next();
143 ColumnSchema<E, Object> schema = column(next.getKey(), Object.class);
145 * Ideally the ColumnSchema shouldn't be null at this stage. But there can be cases in which
146 * the OVSDB manager Schema implementation might decide to include some "hidden" columns that
147 * are NOT reported in getSchema, but decide to report it in unfiltered monitor.
148 * Hence adding some safety checks around that.
150 if (schema != null) {
151 Object value = schema.valueFromJson(next.getValue());
152 newColumns.add(new Column<>(schema, value));
155 return new Row<>(this, newColumns);
158 public List<Row<E>> createRows(JsonNode rowsNode) {
159 List<Row<E>> rows = new ArrayList<>();
160 for (JsonNode rowNode : rowsNode.get("rows")) {
161 rows.add(createRow((ObjectNode)rowNode));
168 * RFC 7047 Section 3.2 specifies 2 internally generated columns in each table
169 * namely _uuid and _version which are not exposed in get_schema call.
170 * Since these 2 columns are extremely useful for Mutate, update and select operations,
171 * the ColumnSchema for these 2 columns are manually populated.
173 * It is to be noted that these 2 columns are specified as part of the RFC7047 and not
174 * a specific Schema implementation detail & hence adding it by default in the Library
175 * for better application experience using the library.
177 public void populateInternallyGeneratedColumns() {
178 columns.put("_uuid", new ColumnSchema("_uuid", new AtomicColumnType(new UuidBaseType())));
179 columns.put("_version", new ColumnSchema("_version", new AtomicColumnType(new UuidBaseType())));