2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.ovsdb.rfc.schema.type;
18 import org.onosproject.ovsdb.rfc.exception.AbnormalJsonNodeException;
19 import org.onosproject.ovsdb.rfc.utils.ObjectMapperUtil;
21 import com.fasterxml.jackson.databind.JsonNode;
24 * ColumnType Factory class.
26 public final class ColumnTypeFactory {
29 * Constructs a ColumnTypeFactory object. This class should not be
32 private ColumnTypeFactory() {
36 * Those Json's key/value pairs.
39 KEY("key"), VALUE("value");
41 private final String type;
43 private Type(String type) {
48 * Returns the type for Type.
51 public String type() {
58 * "flow_tables":{"type":{"key":{"maxInteger":254,"minInteger":0,"type":
59 * "integer"},"min":0,"value":{"type":"uuid","refTable":"Flow_Table"},"max":
61 * @param columnTypeJson the ColumnType JsonNode
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);
70 String message = "Abnormal ColumnType JsonNode, it should be AtomicColumnType or KeyValuedColumnType"
71 + ObjectMapperUtil.convertToString(columnTypeJson);
72 throw new AbnormalJsonNodeException(message);
76 * Create AtomicColumnType entity.
77 * @param json JsonNode
78 * @return AtomicColumnType entity
80 private static AtomicColumnType createAtomicColumnType(JsonNode json) {
81 BaseType baseType = BaseTypeFactory.getBaseTypeFromJson(json, Type.KEY.type());
84 JsonNode node = json.get("min");
85 if (node != null && node.isNumber()) {
88 node = json.get("max");
90 if (node.isNumber()) {
92 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
93 max = Integer.MAX_VALUE;
96 return new AtomicColumnType(baseType, min, max);
100 * Create KeyValuedColumnType entity.
101 * @param json JsonNode
102 * @return KeyValuedColumnType entity
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());
109 JsonNode node = json.get("min");
110 if (node != null && node.isNumber()) {
113 node = json.get("max");
115 if (node.isNumber()) {
117 } else if (node.isTextual() && "unlimited".equals(node.asText())) {
118 max = Integer.MAX_VALUE;
121 return new KeyValuedColumnType(keyType, valueType, min, max);