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
8 package org.opendaylight.ovsdb.lib.schema;
10 import com.fasterxml.jackson.databind.JsonNode;
12 import java.util.Objects;
14 import org.opendaylight.ovsdb.lib.error.BadSchemaException;
15 import org.opendaylight.ovsdb.lib.notation.Condition;
16 import org.opendaylight.ovsdb.lib.notation.Function;
17 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
18 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
20 public class ColumnSchema<E extends TableSchema<E>, D> {
21 private final String name;
22 private final ColumnType type;
24 public ColumnSchema(final String name, final ColumnType columnType) {
26 this.type = columnType;
29 public static <E extends TableSchema<E>, D> ColumnSchema<E, D> fromJson(final String name, final JsonNode json) {
30 if (!json.isObject()) {
31 throw new BadSchemaException("bad column schema root, expected an object");
33 final JsonNode type = json.get("type");
35 throw new BadSchemaException("bad column schema root, expected \"type\" as child");
37 return new ColumnSchema<>(name, ColumnType.fromJson(type));
40 public String getName() {
44 public ColumnType getType() {
48 // --- Operations on the column ----------//
50 public Condition opEqual(final D data) {
51 return new Condition(this.getName(), Function.EQUALS, data);
54 public Condition opGreaterThan(final D data) {
55 return new Condition(this.getName(), Function.GREATER_THAN, data);
58 public Condition opLesserThan(final D data) {
59 return new Condition(this.getName(), Function.GREATER_THAN, data);
62 public Condition opLesserThanOrEquals(final D data) {
63 return new Condition(this.getName(), Function.LESS_THAN_OR_EQUALS, data);
66 public Condition opIncludes(final D data) {
67 return new Condition(this.getName(), Function.INCLUDES, data);
70 public Condition opExcludes(final D data) {
71 return new Condition(this.getName(), Function.EXCLUDES, data);
74 // --- Operations on the column ----------//:w
78 public String toString() {
79 return "ColumnSchema{"
80 + "name='" + name + '\''
86 public int hashCode() {
89 result = prime * result + (name == null ? 0 : name.hashCode());
90 result = prime * result + (type == null ? 0 : type.hashCode());
95 public boolean equals(final Object obj) {
99 if (obj == null || getClass() != obj.getClass()) {
102 final ColumnSchema<?, ?> other = (ColumnSchema<?, ?>) obj;
103 return Objects.equals(name, other.name) && Objects.equals(type, other.type);
107 * Validates the passed in value against the constraints set for this ColumnSchema.
109 public D validate(final Object value) {
110 //todo(type check and validate based on constraints set)
111 this.type.validate(value);
116 * Verifies if this Column if of the specified type.
117 * @param typeClass the type to check for
119 public void validateType(final Class<?> typeClass) {
123 public D valueFromJson(final JsonNode value) {
124 return (D) this.getType().valueFromJson(value);
127 public Object getNormalizeData(final D value) {
129 if (value instanceof Set) {
130 untypedValue = OvsdbSet.fromSet((Set) value);
131 } else if (value instanceof Map) {
132 untypedValue = OvsdbMap.fromMap((Map) value);
134 untypedValue = value;