lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ovsdb.rfc.utils; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.onosproject.ovsdb.rfc.notation.OvsdbMap; |
| 22 | import org.onosproject.ovsdb.rfc.notation.OvsdbSet; |
| 23 | import org.onosproject.ovsdb.rfc.notation.RefTableRow; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 24 | import org.onosproject.ovsdb.rfc.notation.Uuid; |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 25 | import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType; |
| 26 | import org.onosproject.ovsdb.rfc.schema.type.BaseType; |
| 27 | import org.onosproject.ovsdb.rfc.schema.type.BooleanBaseType; |
| 28 | import org.onosproject.ovsdb.rfc.schema.type.ColumnType; |
| 29 | import org.onosproject.ovsdb.rfc.schema.type.IntegerBaseType; |
| 30 | import org.onosproject.ovsdb.rfc.schema.type.KeyValuedColumnType; |
| 31 | import org.onosproject.ovsdb.rfc.schema.type.RealBaseType; |
| 32 | import org.onosproject.ovsdb.rfc.schema.type.StringBaseType; |
| 33 | import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType; |
| 34 | |
| 35 | import com.fasterxml.jackson.databind.JsonNode; |
| 36 | import com.google.common.collect.Maps; |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
| 39 | /** |
| 40 | * Object value utility class. |
| 41 | */ |
| 42 | public final class TransValueUtil { |
| 43 | |
| 44 | /** |
| 45 | * Constructs a TransValueUtil object. Utility classes should not have a |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 46 | * public or default constructor, otherwise IDE will compile unsuccessfully. |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 47 | * This class should not be instantiated. |
| 48 | */ |
| 49 | private TransValueUtil() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * if the type is Set, convert into OvsdbSet, if Map, convert into OvsdbMap. |
| 54 | * @param value Object |
| 55 | * @return Object |
| 56 | */ |
| 57 | public static Object getFormatData(Object value) { |
| 58 | if (value instanceof Map) { |
| 59 | return OvsdbMap.ovsdbMap((Map) value); |
| 60 | } else if (value instanceof Set) { |
| 61 | return OvsdbSet.ovsdbSet((Set) value); |
| 62 | } else { |
| 63 | return value; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Transform JsonNode to corresponding value. |
| 69 | * @param json the ColumnType JsonNode |
| 70 | * @param columnType AtomicColumnType or KeyValuedColumnType |
| 71 | * @return Object OvsdbMap or OvsdbSet |
| 72 | */ |
| 73 | public static Object getValueFromJson(JsonNode json, ColumnType columnType) { |
| 74 | if (columnType instanceof AtomicColumnType) { |
| 75 | AtomicColumnType atoType = (AtomicColumnType) columnType; |
| 76 | return getValueFromAtoType(json, atoType); |
| 77 | } else if (columnType instanceof KeyValuedColumnType) { |
| 78 | KeyValuedColumnType kvType = (KeyValuedColumnType) columnType; |
| 79 | return getValueFromKvType(json, kvType); |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 84 | /** |
| 85 | * Convert AtomicColumnType JsonNode into OvsdbSet value. |
| 86 | * @param json AtomicColumnType JsonNode |
| 87 | * @param atoType AtomicColumnType entity |
| 88 | * @return Object OvsdbSet or the value of JsonNode |
| 89 | */ |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 90 | private static Object getValueFromAtoType(JsonNode json, AtomicColumnType atoType) { |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 91 | BaseType baseType = atoType.baseType(); |
| 92 | // If "min" or "max" is not specified, If "min" is not 1 or "max" is not |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 93 | // 1, or both, and "value" is not specified, the type is a set of scalar |
| 94 | // type "key". Refer to RFC 7047, Section 3.2 <type>. |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 95 | if (atoType.min() != atoType.max()) { |
| 96 | Set set = Sets.newHashSet(); |
| 97 | if (json.isArray()) { |
| 98 | if (json.size() == 2) { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 99 | if (json.get(0).isTextual() && "set".equals(json.get(0).asText())) { |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 100 | for (JsonNode node : json.get(1)) { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 101 | set.add(transToValue(node, baseType)); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 102 | } |
| 103 | } else { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 104 | set.add(transToValue(json, baseType)); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } else { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 108 | set.add(transToValue(json, baseType)); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 109 | } |
| 110 | return OvsdbSet.ovsdbSet(set); |
| 111 | } else { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 112 | return transToValue(json, baseType); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 116 | /** |
| 117 | * Convert KeyValuedColumnType JsonNode into OvsdbMap value. |
| 118 | * @param json KeyValuedColumnType JsonNode |
| 119 | * @param kvType KeyValuedColumnType entity |
| 120 | * @return Object OvsdbMap |
| 121 | */ |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 122 | private static Object getValueFromKvType(JsonNode json, KeyValuedColumnType kvType) { |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 123 | if (json.isArray()) { |
| 124 | if (json.size() == 2) { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 125 | if (json.get(0).isTextual() && "map".equals(json.get(0).asText())) { |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 126 | Map map = Maps.newHashMap(); |
| 127 | for (JsonNode pairNode : json.get(1)) { |
| 128 | if (pairNode.isArray() && json.size() == 2) { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 129 | Object key = transToValue(pairNode.get(0), kvType.keyType()); |
| 130 | Object value = transToValue(pairNode.get(1), kvType.valueType()); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 131 | map.put(key, value); |
| 132 | } |
| 133 | } |
| 134 | return OvsdbMap.ovsdbMap(map); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * convert into value. |
| 143 | * @param valueNode the BaseType JsonNode |
| 144 | * @param baseType BooleanBaseType or IntegerBaseType or RealBaseType or |
| 145 | * StringBaseType or UuidBaseType |
| 146 | * @return Object the value of JsonNode |
| 147 | */ |
| 148 | public static Object transToValue(JsonNode valueNode, BaseType baseType) { |
| 149 | if (baseType instanceof BooleanBaseType) { |
| 150 | return valueNode.asBoolean(); |
| 151 | } else if (baseType instanceof IntegerBaseType) { |
| 152 | return valueNode.asInt(); |
| 153 | } else if (baseType instanceof RealBaseType) { |
| 154 | return valueNode.asDouble(); |
| 155 | } else if (baseType instanceof StringBaseType) { |
| 156 | return valueNode.asText(); |
| 157 | } else if (baseType instanceof UuidBaseType) { |
| 158 | if (valueNode.isArray()) { |
| 159 | if (valueNode.size() == 2) { |
| 160 | if (valueNode.get(0).isTextual() |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 161 | && ("uuid".equals(valueNode.get(0).asText()) || "named-uuid" |
| 162 | .equals(valueNode.get(0).asText()))) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 163 | return Uuid.uuid(valueNode.get(1).asText()); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } else { |
lishuai | fa7013d | 2015-08-10 19:35:10 +0800 | [diff] [blame] | 167 | return new RefTableRow(((UuidBaseType) baseType).getRefTable(), valueNode); |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | } |