blob: c9da6f0bf1ff17dc73db1dd25109c0e26b2d60e5 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.schema.type;
17
Sho SHIMIZU8700d422015-08-26 18:13:44 -070018import org.onosproject.ovsdb.rfc.exception.AbnormalJsonNodeException;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070019import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
20
21import com.fasterxml.jackson.databind.JsonNode;
22
23/**
24 * ColumnType Factory class.
25 */
26public final class ColumnTypeFactory {
27
28 /**
29 * Constructs a ColumnTypeFactory object. This class should not be
30 * instantiated.
31 */
32 private ColumnTypeFactory() {
33 }
34
35 /**
36 * Those Json's key/value pairs.
37 */
38 public enum Type {
39 KEY("key"), VALUE("value");
40
41 private final String type;
42
43 private Type(String type) {
44 this.type = type;
45 }
46
47 /**
48 * Returns the type for Type.
49 * @return the type
50 */
51 public String type() {
52 return type;
53 }
54 }
55
56 /**
57 * JsonNode like
58 * "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
59 * "integer"},"min":0,"value":{"type":"uuid","refTable":"Flow_Table"},"max":
60 * "unlimited"}}.
61 * @param columnTypeJson the ColumnType JsonNode
62 * @return ColumnType
63 */
64 public static ColumnType getColumnTypeFromJson(JsonNode columnTypeJson) {
65 if (!columnTypeJson.isObject() || !columnTypeJson.has(Type.VALUE.type())) {
66 return createAtomicColumnType(columnTypeJson);
67 } else if (!columnTypeJson.isValueNode() && columnTypeJson.has(Type.VALUE.type())) {
68 return createKeyValuedColumnType(columnTypeJson);
69 }
70 String message = "Abnormal ColumnType JsonNode, it should be AtomicColumnType or KeyValuedColumnType"
71 + ObjectMapperUtil.convertToString(columnTypeJson);
72 throw new AbnormalJsonNodeException(message);
73 }
74
75 /**
76 * Create AtomicColumnType entity.
77 * @param json JsonNode
78 * @return AtomicColumnType entity
79 */
80 private static AtomicColumnType createAtomicColumnType(JsonNode json) {
81 BaseType baseType = BaseTypeFactory.getBaseTypeFromJson(json, Type.KEY.type());
82 int min = 1;
83 int max = 1;
84 JsonNode node = json.get("min");
85 if (node != null && node.isNumber()) {
86 min = node.asInt();
87 }
88 node = json.get("max");
89 if (node != null) {
90 if (node.isNumber()) {
91 max = node.asInt();
92 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
93 max = Integer.MAX_VALUE;
94 }
95 }
96 return new AtomicColumnType(baseType, min, max);
97 }
98
99 /**
100 * Create KeyValuedColumnType entity.
101 * @param json JsonNode
102 * @return KeyValuedColumnType entity
103 */
104 private static KeyValuedColumnType createKeyValuedColumnType(JsonNode json) {
105 BaseType keyType = BaseTypeFactory.getBaseTypeFromJson(json, Type.KEY.type());
106 BaseType valueType = BaseTypeFactory.getBaseTypeFromJson(json, Type.VALUE.type());
107 int min = 1;
108 int max = 1;
109 JsonNode node = json.get("min");
110 if (node != null && node.isNumber()) {
111 min = node.asInt();
112 }
113 node = json.get("max");
114 if (node != null) {
115 if (node.isNumber()) {
116 max = node.asInt();
117 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
118 max = Integer.MAX_VALUE;
119 }
120 }
121 return new KeyValuedColumnType(keyType, valueType, min, max);
122 }
123}