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