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 com.fasterxml.jackson.databind.DeserializationFeature;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.util.StdConverter;
15 import org.opendaylight.ovsdb.lib.message.TableUpdates;
16 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
17 import org.opendaylight.ovsdb.lib.notation.OvsdbMap;
18 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
21 public class Converter {
23 static AtomDeser atomDeser = new AtomDeser();
24 static MapDeser mapDeser = new MapDeser();
25 static SetDeser setDeser = new SetDeser();
26 static UpdateNotificationDeser unDeser = new UpdateNotificationDeser();
29 // Prevent instantiating a utility class
32 public static class MapConverter extends StdConverter<JsonNode, OvsdbMap<Object, Object>> {
34 public OvsdbMap<Object, Object> convert(JsonNode value) {
35 return mapDeser.deserialize(value);
39 public static class SetConverter extends StdConverter<JsonNode, OvsdbSet<Object>> {
41 public OvsdbSet<Object> convert(JsonNode value) {
42 return setDeser.deserialize(value);
46 public static class UpdateNotificationConverter extends StdConverter<JsonNode, UpdateNotification> {
48 public UpdateNotification convert(JsonNode value) {
49 return unDeser.deserialize(value);
53 static class MapDeser {
54 public OvsdbMap<Object, Object> deserialize(JsonNode node) {
55 if (node.isArray() && node.size() == 2) {
56 if (node.get(0).isTextual() && "map".equals(node.get(0).asText())) {
57 OvsdbMap<Object, Object> map = new OvsdbMap<>();
58 for (JsonNode pairNode : node.get(1)) {
59 if (pairNode.isArray() && node.size() == 2) {
60 Object key = atomDeser.deserialize(pairNode.get(0));
61 Object value = atomDeser.deserialize(pairNode.get(1));
66 } else if (node.size() == 0) {
70 throw new IllegalArgumentException("not a map type");
74 static class SetDeser {
75 public OvsdbSet<Object> deserialize(JsonNode node) {
76 OvsdbSet<Object> set = new OvsdbSet<>();
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() && 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);
114 static class AtomDeser {
116 public Object deserialize(JsonNode node) {
117 if (!node.isArray()) {
118 switch (node.getNodeType()) {
120 return node.asBoolean();
122 if (node.isFloatingPointNumber()) {
123 return node.decimalValue();
125 return node.bigIntegerValue();
128 return node.asText();
134 if (node.isArray() && node.get(0).isTextual()
135 && ("uuid".equals(node.get(0).asText()) || "named-uuid".equals(node.get(0).asText()))) {
136 return new UUID(node.get(1).asText());
139 throw new IllegalArgumentException("not an atom node");