blob: 694fa8be68711da21e5c38423d6d43532fd78594 [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 */
lishuai91d986c2015-07-28 09:45:20 +080090 private static Object getValueFromAtoType(JsonNode json,
91 AtomicColumnType atoType) {
92 BaseType baseType = atoType.baseType();
93 // If "min" or "max" is not specified, If "min" is not 1 or "max" is not
94 // 1,
95 // or both, and "value" is not specified, the type is a set of scalar
96 // type "key".
97 // Refer to RFC 7047, Section 3.2 <type>.
98 if (atoType.min() != atoType.max()) {
99 Set set = Sets.newHashSet();
100 if (json.isArray()) {
101 if (json.size() == 2) {
102 if (json.get(0).isTextual()
103 && "set".equals(json.get(0).asText())) {
104 for (JsonNode node : json.get(1)) {
105 set.add(TransValueUtil.transToValue(node, baseType));
106 }
107 } else {
108 set.add(TransValueUtil.transToValue(json, baseType));
109 }
110 }
111 } else {
112 set.add(TransValueUtil.transToValue(json, baseType));
113 }
114 return OvsdbSet.ovsdbSet(set);
115 } else {
116 return TransValueUtil.transToValue(json, baseType);
117 }
118 }
119
lishuai2f197432015-07-31 16:27:58 +0800120 /**
121 * Convert KeyValuedColumnType JsonNode into OvsdbMap value.
122 * @param json KeyValuedColumnType JsonNode
123 * @param kvType KeyValuedColumnType entity
124 * @return Object OvsdbMap
125 */
lishuai91d986c2015-07-28 09:45:20 +0800126 private static Object getValueFromKvType(JsonNode json,
127 KeyValuedColumnType kvType) {
128 if (json.isArray()) {
129 if (json.size() == 2) {
130 if (json.get(0).isTextual()
131 && "map".equals(json.get(0).asText())) {
132 Map map = Maps.newHashMap();
133 for (JsonNode pairNode : json.get(1)) {
134 if (pairNode.isArray() && json.size() == 2) {
135 Object key = TransValueUtil.transToValue(pairNode
136 .get(0), kvType.keyType());
137 Object value = TransValueUtil.transToValue(pairNode
138 .get(1), kvType.valueType());
139 map.put(key, value);
140 }
141 }
142 return OvsdbMap.ovsdbMap(map);
143 }
144 }
145 }
146 return null;
147 }
148
149 /**
150 * convert into value.
151 * @param valueNode the BaseType JsonNode
152 * @param baseType BooleanBaseType or IntegerBaseType or RealBaseType or
153 * StringBaseType or UuidBaseType
154 * @return Object the value of JsonNode
155 */
156 public static Object transToValue(JsonNode valueNode, BaseType baseType) {
157 if (baseType instanceof BooleanBaseType) {
158 return valueNode.asBoolean();
159 } else if (baseType instanceof IntegerBaseType) {
160 return valueNode.asInt();
161 } else if (baseType instanceof RealBaseType) {
162 return valueNode.asDouble();
163 } else if (baseType instanceof StringBaseType) {
164 return valueNode.asText();
165 } else if (baseType instanceof UuidBaseType) {
166 if (valueNode.isArray()) {
167 if (valueNode.size() == 2) {
168 if (valueNode.get(0).isTextual()
169 && "uuid".equals(valueNode.get(0).asText())
170 || "named-uuid".equals(valueNode.get(0).asText())) {
171 return UUID.uuid(valueNode.get(1).asText());
172 }
173 }
174 } else {
175 return new RefTableRow(((UuidBaseType) baseType).getRefTable(),
176 valueNode);
177 }
178 }
179 return null;
180 }
181}