Code ReOrganization and Re-Architecture changes
[ovsdb.git] / library / src / main / java / org / opendaylight / ovsdb / lib / schema / ColumnType.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
3  *
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
7  *
8  * Authors : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.schema;
11
12 import com.fasterxml.jackson.databind.JsonNode;
13 import org.opendaylight.ovsdb.lib.jsonrpc.JsonUtils;
14
15
16 public abstract class ColumnType {
17     BaseType baseType;
18     int min = 0;
19     int max = 0;
20
21     private static ColumnType columns[] = new ColumnType[]{
22             new AtomicColumnType(),
23             new KeyValuedColumnType()
24     };
25
26
27     public ColumnType() {
28
29     }
30
31     public ColumnType(BaseType baseType) {
32         this.baseType = baseType;
33     }
34
35     public BaseType getBaseType() {
36         return baseType;
37     }
38
39     /**
40             "type": {
41                 "key": {
42                      "maxInteger": 4294967295,
43                      "minInteger": 0,
44                      "type": "integer"
45                 },
46                 "min": 0,
47                 "value": {
48                     "type": "uuid",
49                     "refTable": "Queue"
50                  },
51                  "max": "unlimited"
52             }
53      * @param json
54      * @return
55      */
56     public static ColumnType fromJson(JsonNode json) {
57         for (ColumnType colType : columns) {
58             ColumnType columnType = colType.fromJsonNode(json);
59             if (null != columnType) {
60                 return columnType;
61             }
62         }
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)));
66     }
67
68
69     /**
70      * Creates a ColumnType from the JsonNode if the type knows how to,
71      * returns null otherwise
72      *
73      * @param json the JSONNode object that needs to converted
74      * @return a valid SubType or Null (if the JsonNode does not represent
75      * the subtype)
76      */
77     protected abstract ColumnType fromJsonNode(JsonNode json);
78
79     public static class AtomicColumnType extends ColumnType {
80
81         public AtomicColumnType() {
82         }
83
84         public AtomicColumnType(BaseType baseType1) {
85             super(baseType1);
86         }
87
88         public AtomicColumnType fromJsonNode(JsonNode json) {
89             if (json.isObject() && json.has("value")) {
90                 return null;
91             }
92             BaseType baseType = BaseType.fromJson(json, "key");
93
94             return baseType != null ? new AtomicColumnType(baseType) : null;
95         }
96
97     }
98
99     public static class KeyValuedColumnType extends ColumnType {
100
101         BaseType valueType;
102
103         public KeyValuedColumnType() {
104         }
105
106         public KeyValuedColumnType(BaseType baseType, BaseType valueType) {
107             super(baseType);
108         }
109
110         public KeyValuedColumnType fromJsonNode(JsonNode json) {
111             if (json.isValueNode() || !json.has("value")) {
112                 return null;
113             }
114             BaseType keyType = BaseType.fromJson(json, "key");
115             BaseType valueType = BaseType.fromJson(json, "value");
116
117             return new KeyValuedColumnType(keyType, valueType);
118         }
119     }
120 }