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;
14 import org.opendaylight.ovsdb.lib.jsonrpc.JsonUtils;
15 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
17 import com.fasterxml.jackson.databind.JsonNode;
18 import com.google.common.collect.Sets;
21 public abstract class ColumnType {
26 public long getMin() {
30 void setMin(long min) {
34 public long getMax() {
38 void setMax(long max) {
42 private static ColumnType columns[] = new ColumnType[]{
43 new AtomicColumnType(),
44 new KeyValuedColumnType()
52 public ColumnType(BaseType baseType) {
53 this.baseType = baseType;
56 public BaseType getBaseType() {
63 "maxInteger": 4294967295,
77 public static ColumnType fromJson(JsonNode json) {
78 for (ColumnType colType : columns) {
79 ColumnType columnType = colType.fromJsonNode(json);
80 if (null != columnType) {
84 //todo move to speicfic typed exception
85 throw new RuntimeException(String.format("could not find the right column type %s",
86 JsonUtils.prettyString(json)));
91 * Creates a ColumnType from the JsonNode if the implementation knows how to, returns null otherwise
93 * @param json the JSONNode object that needs to converted
94 * @return a valid SubType or Null (if the JsonNode does not represent
97 protected abstract ColumnType fromJsonNode(JsonNode json);
99 public boolean isMultiValued() {
100 //todo check if this is the right logic
101 return this.min != this.max && this.min != 1;
104 public abstract Object valueFromJson(JsonNode value);
106 public abstract void validate(Object value);
109 public String toString() {
110 return "ColumnType{" +
111 "baseType=" + baseType +
117 public static class AtomicColumnType extends ColumnType {
119 public AtomicColumnType() {
122 public AtomicColumnType(BaseType baseType1) {
127 public AtomicColumnType fromJsonNode(JsonNode json) {
128 if (json.isObject() && json.has("value")) {
131 BaseType baseType = BaseType.fromJson(json, "key");
133 if (baseType != null) {
135 AtomicColumnType atomicColumnType = new AtomicColumnType(baseType);
137 JsonNode node = null;
138 if ((node = json.get("min")) != null) {
139 atomicColumnType.setMin(node.asLong());
142 if ((node = json.get("max")) != null) {
144 atomicColumnType.setMax(node.asLong());
145 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
146 max = Long.MAX_VALUE;
149 return atomicColumnType;
156 public Object valueFromJson(JsonNode value) {
157 if (isMultiValued()) {
158 Set<Object> result = Sets.newHashSet();
159 if(value.isArray()) {
160 if (value.size() == 2) {
161 if (value.get(0).isTextual() && "set".equals(value.get(0).asText())) {
162 for(JsonNode node: value.get(1)) {
163 result.add(getBaseType().toValue(node));
168 result.add(getBaseType().toValue(value));
172 return getBaseType().toValue(value);
177 public void validate(Object value) {
178 this.baseType.validate(value);
183 public static class KeyValuedColumnType extends ColumnType {
186 public BaseType getKeyType() {
190 public KeyValuedColumnType() {
193 public KeyValuedColumnType(BaseType keyType, BaseType valueType) {
195 this.keyType = keyType;
199 public KeyValuedColumnType fromJsonNode(JsonNode json) {
200 if (json.isValueNode() || !json.has("value")) {
203 BaseType keyType = BaseType.fromJson(json, "key");
204 BaseType valueType = BaseType.fromJson(json, "value");
206 KeyValuedColumnType keyValueColumnType = new KeyValuedColumnType(keyType, valueType);
207 JsonNode node = null;
208 if ((node = json.get("min")) != null) {
209 keyValueColumnType.setMin(node.asLong());
212 if ((node = json.get("max")) != null) {
214 keyValueColumnType.setMax(node.asLong());
215 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
216 max = Long.MAX_VALUE;
220 return keyValueColumnType;
224 public Object valueFromJson(JsonNode node) {
225 if (node.isArray()) {
226 if (node.size() == 2) {
227 if (node.get(0).isTextual() && "map".equals(node.get(0).asText())) {
228 OvsDBMap<Object, Object> map = new OvsDBMap<Object, Object>();
229 for (JsonNode pairNode : node.get(1)) {
230 if (pairNode.isArray() && node.size() == 2) {
231 Object key = getKeyType().toValue(pairNode.get(0));
232 Object value = getBaseType().toValue(pairNode.get(1));
237 } else if (node.size() == 0) {
246 public void validate(Object value) {
247 this.baseType.validate(value);