Fix ALOTOF Checkstyle violation, and switch over to enforcement.
[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 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;
20
21 public class Converter {
22
23     static AtomDeser atomDeser = new AtomDeser();
24     static MapDeser mapDeser = new MapDeser();
25     static SetDeser setDeser = new SetDeser();
26     static UpdateNotificationDeser unDeser = new UpdateNotificationDeser();
27
28     private Converter() {
29         // Prevent instantiating a utility class
30     }
31
32     public static class MapConverter extends StdConverter<JsonNode, OvsdbMap<Object, Object>> {
33         @Override
34         public OvsdbMap<Object, Object> convert(JsonNode value) {
35             return mapDeser.deserialize(value);
36         }
37     }
38
39     public static class SetConverter extends StdConverter<JsonNode, OvsdbSet<Object>> {
40         @Override
41         public OvsdbSet<Object> convert(JsonNode value) {
42             return setDeser.deserialize(value);
43         }
44     }
45
46     public static class UpdateNotificationConverter extends StdConverter<JsonNode, UpdateNotification> {
47         @Override
48         public UpdateNotification convert(JsonNode value) {
49             return unDeser.deserialize(value);
50         }
51     }
52
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));
62                             map.put(key, value);
63                         }
64                     }
65                     return map;
66                 } else if (node.size() == 0) {
67                     return null;
68                 }
69             }
70             throw new IllegalArgumentException("not a map type");
71         }
72     }
73
74     static class SetDeser {
75         public OvsdbSet<Object> deserialize(JsonNode node) {
76             OvsdbSet<Object> set = new OvsdbSet<>();
77             if (node.isArray()) {
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));
82                         }
83                         return set;
84                     }
85                 } else if (node.size() == 0) {
86                     return null;
87                 }
88             }
89             //treat the whole thing as a single Atom
90             Object atom = atomDeser.deserialize(node);
91             if (null != atom) {
92                 set.add(atom);
93             }
94             return set;
95         }
96     }
97
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);
108                 return un;
109             }
110             return null;
111         }
112     }
113
114     static class AtomDeser {
115
116         public Object deserialize(JsonNode node) {
117             if (!node.isArray()) {
118                 switch (node.getNodeType()) {
119                     case BOOLEAN:
120                         return node.asBoolean();
121                     case NUMBER:
122                         if (node.isFloatingPointNumber()) {
123                             return node.decimalValue();
124                         } else {
125                             return node.bigIntegerValue();
126                         }
127                     case STRING:
128                         return node.asText();
129                     default:
130                         break;
131                 }
132             }
133
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());
137             }
138
139             throw new IllegalArgumentException("not an atom node");
140         }
141     }
142 }