blob: 49fbd3757c8659f8d2063e66f0e5174bbd35c301 [file] [log] [blame]
lishuai91d986c2015-07-28 09:45:20 +08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.ovsdb.rfc.utils;
17
18import java.util.Map;
19import java.util.Set;
20
21import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
22import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
23import org.onosproject.ovsdb.rfc.notation.RefTableRow;
24import org.onosproject.ovsdb.rfc.notation.UUID;
25import org.onosproject.ovsdb.rfc.schema.type.AtomicColumnType;
26import org.onosproject.ovsdb.rfc.schema.type.BaseType;
27import org.onosproject.ovsdb.rfc.schema.type.BooleanBaseType;
28import org.onosproject.ovsdb.rfc.schema.type.ColumnType;
29import org.onosproject.ovsdb.rfc.schema.type.IntegerBaseType;
30import org.onosproject.ovsdb.rfc.schema.type.KeyValuedColumnType;
31import org.onosproject.ovsdb.rfc.schema.type.RealBaseType;
32import org.onosproject.ovsdb.rfc.schema.type.StringBaseType;
33import org.onosproject.ovsdb.rfc.schema.type.UuidBaseType;
34
35import com.fasterxml.jackson.databind.JsonNode;
36import com.google.common.collect.Maps;
37import com.google.common.collect.Sets;
38
39/**
40 * Object value utility class.
41 */
42public final class TransValueUtil {
43
44 /**
45 * Constructs a TransValueUtil object. Utility classes should not have a
lishuai2f197432015-07-31 16:27:58 +080046 * public or default constructor, otherwise IDE will compile unsuccessfully.
lishuai91d986c2015-07-28 09:45:20 +080047 * 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
lishuai2f197432015-07-31 16:27:58 +080084 /**
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 */
lishuaifa7013d2015-08-10 19:35:10 +080090 private static Object getValueFromAtoType(JsonNode json, AtomicColumnType atoType) {
lishuai91d986c2015-07-28 09:45:20 +080091 BaseType baseType = atoType.baseType();
92 // If "min" or "max" is not specified, If "min" is not 1 or "max" is not
lishuaifa7013d2015-08-10 19:35:10 +080093 // 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>.
lishuai91d986c2015-07-28 09:45:20 +080095 if (atoType.min() != atoType.max()) {
96 Set set = Sets.newHashSet();
97 if (json.isArray()) {
98 if (json.size() == 2) {
lishuaifa7013d2015-08-10 19:35:10 +080099 if (json.get(0).isTextual() && "set".equals(json.get(0).asText())) {
lishuai91d986c2015-07-28 09:45:20 +0800100 for (JsonNode node : json.get(1)) {
lishuaifa7013d2015-08-10 19:35:10 +0800101 set.add(transToValue(node, baseType));
lishuai91d986c2015-07-28 09:45:20 +0800102 }
103 } else {
lishuaifa7013d2015-08-10 19:35:10 +0800104 set.add(transToValue(json, baseType));
lishuai91d986c2015-07-28 09:45:20 +0800105 }
106 }
107 } else {
lishuaifa7013d2015-08-10 19:35:10 +0800108 set.add(transToValue(json, baseType));
lishuai91d986c2015-07-28 09:45:20 +0800109 }
110 return OvsdbSet.ovsdbSet(set);
111 } else {
lishuaifa7013d2015-08-10 19:35:10 +0800112 return transToValue(json, baseType);
lishuai91d986c2015-07-28 09:45:20 +0800113 }
114 }
115
lishuai2f197432015-07-31 16:27:58 +0800116 /**
117 * Convert KeyValuedColumnType JsonNode into OvsdbMap value.
118 * @param json KeyValuedColumnType JsonNode
119 * @param kvType KeyValuedColumnType entity
120 * @return Object OvsdbMap
121 */
lishuaifa7013d2015-08-10 19:35:10 +0800122 private static Object getValueFromKvType(JsonNode json, KeyValuedColumnType kvType) {
lishuai91d986c2015-07-28 09:45:20 +0800123 if (json.isArray()) {
124 if (json.size() == 2) {
lishuaifa7013d2015-08-10 19:35:10 +0800125 if (json.get(0).isTextual() && "map".equals(json.get(0).asText())) {
lishuai91d986c2015-07-28 09:45:20 +0800126 Map map = Maps.newHashMap();
127 for (JsonNode pairNode : json.get(1)) {
128 if (pairNode.isArray() && json.size() == 2) {
lishuaifa7013d2015-08-10 19:35:10 +0800129 Object key = transToValue(pairNode.get(0), kvType.keyType());
130 Object value = transToValue(pairNode.get(1), kvType.valueType());
lishuai91d986c2015-07-28 09:45:20 +0800131 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()
lishuaifa7013d2015-08-10 19:35:10 +0800161 && ("uuid".equals(valueNode.get(0).asText()) || "named-uuid"
162 .equals(valueNode.get(0).asText()))) {
lishuai91d986c2015-07-28 09:45:20 +0800163 return UUID.uuid(valueNode.get(1).asText());
164 }
165 }
166 } else {
lishuaifa7013d2015-08-10 19:35:10 +0800167 return new RefTableRow(((UuidBaseType) baseType).getRefTable(), valueNode);
lishuai91d986c2015-07-28 09:45:20 +0800168 }
169 }
170 return null;
171 }
172}