2 * Copyright (C) 2013 EBay Software Foundation
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Ashwin Raveendran
10 package org.opendaylight.ovsdb.lib.notation.json;
12 import org.opendaylight.ovsdb.lib.message.TableUpdates;
13 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
14 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
15 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import com.fasterxml.jackson.databind.DeserializationFeature;
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.util.StdConverter;
23 public class Converter {
25 static AtomDeser atomDeser = new AtomDeser();
26 static MapDeser mapDeser = new MapDeser();
27 static SetDeser setDeser = new SetDeser();
28 static UpdateNotificationDeser unDeser = new UpdateNotificationDeser();
30 public static class MapConverter extends StdConverter<JsonNode, OvsDBMap<Object, Object>> {
32 public OvsDBMap<Object, Object> convert(JsonNode value) {
33 return mapDeser.deserialize(value);
37 public static class SetConverter extends StdConverter<JsonNode, OvsDBSet<Object>> {
39 public OvsDBSet<Object> convert(JsonNode value) {
40 return setDeser.deserialize(value);
44 public static class UpdateNotificationConverter extends StdConverter<JsonNode, UpdateNotification> {
46 public UpdateNotification convert(JsonNode value) {
47 return unDeser.deserialize(value);
51 static class MapDeser {
52 public OvsDBMap<Object, Object> deserialize(JsonNode node) {
54 if (node.size() == 2) {
55 if (node.get(0).isTextual() && "map".equals(node.get(0).asText())) {
56 OvsDBMap<Object, Object> map = new OvsDBMap<Object, Object>();
57 for (JsonNode pairNode : node.get(1)) {
58 if (pairNode.isArray() && node.size() == 2) {
59 Object key = atomDeser.deserialize(pairNode.get(0));
60 Object value = atomDeser.deserialize(pairNode.get(1));
65 } else if (node.size() == 0) {
70 throw new RuntimeException("not a map type");
74 static class SetDeser {
75 public OvsDBSet<Object> deserialize(JsonNode node) {
76 OvsDBSet<Object> set = new OvsDBSet<Object>();
78 if (node.size() == 2) {
79 if (node.get(0).isTextual() && "set".equals(node.get(0).asText())) {
80 for (JsonNode atomNode : node.get(1)) {
81 set.add(atomDeser.deserialize(atomNode));
85 } else if (node.size() == 0) {
89 //treat the whole thing as a single Atom
90 Object atom = atomDeser.deserialize(node);
98 static class UpdateNotificationDeser {
99 public UpdateNotification deserialize(JsonNode node) {
100 UpdateNotification un = new UpdateNotification();
101 if (node.isArray()) {
102 if (node.size() == 2) {
103 un.setContext(node.get(0).asText());
104 ObjectMapper objectMapper = new ObjectMapper();
105 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106 TableUpdates updates = objectMapper.convertValue(node.get(1), TableUpdates.class);
107 un.setUpdate(updates);
115 static class AtomDeser {
117 public Object deserialize(JsonNode node) {
118 if (!node.isArray()) {
119 switch (node.getNodeType()) {
121 return node.asBoolean();
123 if (node.isFloatingPointNumber()) {
124 return node.decimalValue();
126 return node.bigIntegerValue();
129 return node.asText();
133 if (node.isArray() && node.get(0).isTextual()) {
134 if ("uuid".equals(node.get(0).asText()) || "named-uuid".equals(node.get(0).asText())) {
135 return new UUID(node.get(1).asText());
139 throw new RuntimeException("not an atom node");