Added Security Rule for Custom ICMP
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / notation / json / Converter.java
1 /*
2  * Copyright (c) 2013, 2015 EBay Software Foundation and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.lib.notation.json;
10
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;
16
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;
21
22 public class Converter {
23
24     static AtomDeser atomDeser = new AtomDeser();
25     static MapDeser mapDeser = new MapDeser();
26     static SetDeser setDeser = new SetDeser();
27     static UpdateNotificationDeser unDeser = new UpdateNotificationDeser();
28
29     public static class MapConverter extends StdConverter<JsonNode, OvsdbMap<Object, Object>> {
30         @Override
31         public OvsdbMap<Object, Object> convert(JsonNode value) {
32             return mapDeser.deserialize(value);
33         }
34     }
35
36     public static class SetConverter extends StdConverter<JsonNode, OvsdbSet<Object>> {
37         @Override
38         public OvsdbSet<Object> convert(JsonNode value) {
39             return setDeser.deserialize(value);
40         }
41     }
42
43     public static class UpdateNotificationConverter extends StdConverter<JsonNode, UpdateNotification> {
44         @Override
45         public UpdateNotification convert(JsonNode value) {
46             return unDeser.deserialize(value);
47         }
48     }
49
50     static class MapDeser {
51         public OvsdbMap<Object, Object> deserialize(JsonNode node) {
52             if (node.isArray()) {
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));
60                                 map.put(key, value);
61                             }
62                         }
63                         return map;
64                     } else if (node.size() == 0) {
65                         return null;
66                     }
67                 }
68             }
69             throw new RuntimeException("not a map type");
70         }
71     }
72
73     static class SetDeser {
74         public OvsdbSet<Object> deserialize(JsonNode node) {
75             OvsdbSet<Object> set = new OvsdbSet<Object>();
76             if (node.isArray()) {
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));
81                         }
82                         return set;
83                     }
84                 } else if (node.size() == 0) {
85                     return null;
86                 }
87             }
88             //treat the whole thing as a single Atom
89             Object atom = atomDeser.deserialize(node);
90             if (null != atom) {
91                 set.add(atom);
92             }
93             return set;
94         }
95     }
96
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);
108                     return un;
109                 }
110             }
111             return null;
112         }
113     }
114
115     static class AtomDeser {
116
117         public Object deserialize(JsonNode node) {
118             if (!node.isArray()) {
119                 switch (node.getNodeType()) {
120                     case BOOLEAN:
121                         return node.asBoolean();
122                     case NUMBER:
123                         if (node.isFloatingPointNumber()) {
124                             return node.decimalValue();
125                         } else {
126                             return node.bigIntegerValue();
127                         }
128                     case STRING:
129                         return node.asText();
130                     default:
131                         break;
132                 }
133             }
134
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());
138                 }
139             }
140
141             throw new RuntimeException("not an atom node");
142         }
143     }
144 }