2 * Copyright (c) 2013, 2015 EBay Software Foundation and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.lib.notation.json;
11 import org.opendaylight.ovsdb.lib.message.TableUpdates;
12 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
13 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
14 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import com.fasterxml.jackson.databind.DeserializationFeature;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.util.StdConverter;
22 public class Converter {
24 static AtomDeser atomDeser = new AtomDeser();
25 static MapDeser mapDeser = new MapDeser();
26 static SetDeser setDeser = new SetDeser();
27 static UpdateNotificationDeser unDeser = new UpdateNotificationDeser();
29 public static class MapConverter extends StdConverter<JsonNode, OvsdbMap<Object, Object>> {
31 public OvsdbMap<Object, Object> convert(JsonNode value) {
32 return mapDeser.deserialize(value);
36 public static class SetConverter extends StdConverter<JsonNode, OvsdbSet<Object>> {
38 public OvsdbSet<Object> convert(JsonNode value) {
39 return setDeser.deserialize(value);
43 public static class UpdateNotificationConverter extends StdConverter<JsonNode, UpdateNotification> {
45 public UpdateNotification convert(JsonNode value) {
46 return unDeser.deserialize(value);
50 static class MapDeser {
51 public OvsdbMap<Object, Object> deserialize(JsonNode node) {
53 if (node.size() == 2) {
54 if (node.get(0).isTextual() && "map".equals(node.get(0).asText())) {
55 OvsdbMap<Object, Object> map = new OvsdbMap<Object, Object>();
56 for (JsonNode pairNode : node.get(1)) {
57 if (pairNode.isArray() && node.size() == 2) {
58 Object key = atomDeser.deserialize(pairNode.get(0));
59 Object value = atomDeser.deserialize(pairNode.get(1));
64 } else if (node.size() == 0) {
69 throw new RuntimeException("not a map type");
73 static class SetDeser {
74 public OvsdbSet<Object> deserialize(JsonNode node) {
75 OvsdbSet<Object> set = new OvsdbSet<Object>();
77 if (node.size() == 2) {
78 if (node.get(0).isTextual() && "set".equals(node.get(0).asText())) {
79 for (JsonNode atomNode : node.get(1)) {
80 set.add(atomDeser.deserialize(atomNode));
84 } else if (node.size() == 0) {
88 //treat the whole thing as a single Atom
89 Object atom = atomDeser.deserialize(node);
97 static class UpdateNotificationDeser {
98 public UpdateNotification deserialize(JsonNode node) {
99 UpdateNotification un = new UpdateNotification();
100 if (node.isArray()) {
101 if (node.size() == 2) {
102 un.setContext(node.get(0).asText());
103 un.setUpdates(node.get(1));
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();
135 if (node.isArray() && node.get(0).isTextual()) {
136 if ("uuid".equals(node.get(0).asText()) || "named-uuid".equals(node.get(0).asText())) {
137 return new UUID(node.get(1).asText());
141 throw new RuntimeException("not an atom node");