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 com.fasterxml.jackson.databind.JsonNode;
13 import org.opendaylight.ovsdb.lib.jsonrpc.JsonUtils;
16 public abstract class ColumnType {
21 private static ColumnType columns[] = new ColumnType[]{
22 new AtomicColumnType(),
23 new KeyValuedColumnType()
31 public ColumnType(BaseType baseType) {
32 this.baseType = baseType;
35 public BaseType getBaseType() {
42 "maxInteger": 4294967295,
56 public static ColumnType fromJson(JsonNode json) {
57 for (ColumnType colType : columns) {
58 ColumnType columnType = colType.fromJsonNode(json);
59 if (null != columnType) {
63 //todo mode to speicfic typed exception
64 throw new RuntimeException(String.format("could not find the right column type %s",
65 JsonUtils.prettyString(json)));
70 * Creates a ColumnType from the JsonNode if the type knows how to,
71 * returns null otherwise
73 * @param json the JSONNode object that needs to converted
74 * @return a valid SubType or Null (if the JsonNode does not represent
77 protected abstract ColumnType fromJsonNode(JsonNode json);
79 public static class AtomicColumnType extends ColumnType {
81 public AtomicColumnType() {
84 public AtomicColumnType(BaseType baseType1) {
88 public AtomicColumnType fromJsonNode(JsonNode json) {
89 if (json.isObject() && json.has("value")) {
92 BaseType baseType = BaseType.fromJson(json, "key");
94 return baseType != null ? new AtomicColumnType(baseType) : null;
99 public static class KeyValuedColumnType extends ColumnType {
103 public KeyValuedColumnType() {
106 public KeyValuedColumnType(BaseType baseType, BaseType valueType) {
110 public KeyValuedColumnType fromJsonNode(JsonNode json) {
111 if (json.isValueNode() || !json.has("value")) {
114 BaseType keyType = BaseType.fromJson(json, "key");
115 BaseType valueType = BaseType.fromJson(json, "value");
117 return new KeyValuedColumnType(keyType, valueType);