Initial Netty + JSON Deserializer + all the enhancements happening in many private...
[ovsdb.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / message / MonitorResponseTest.java
1 package org.opendaylight.ovsdb.lib.message;
2
3 import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 import com.fasterxml.jackson.annotation.PropertyAccessor;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.google.common.base.Function;
7 import com.google.common.collect.Ordering;
8 import com.google.common.io.InputSupplier;
9 import com.google.common.io.Resources;
10
11 import junit.framework.TestCase;
12
13 import org.opendaylight.ovsdb.lib.datatype.OvsDBMap;
14 import org.opendaylight.ovsdb.lib.datatype.OvsDBSet;
15 import org.opendaylight.ovsdb.lib.datatype.UUID;
16 import org.opendaylight.ovsdb.lib.message.TableUpdate;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.table.Bridge;
19 import org.opendaylight.ovsdb.lib.table.Interface;
20 import org.opendaylight.ovsdb.lib.table.Port;
21 import org.opendaylight.ovsdb.lib.table.internal.Table;
22 import org.sonatype.inject.Nullable;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 public class MonitorResponseTest extends TestCase {
33
34     public void testDeser() throws IOException {
35         URL resource = Resources.getResource(MonitorResponseTest.class, "monitor_response1.json");
36         InputSupplier<InputStream> inputStreamInputSupplier = Resources.newInputStreamSupplier(resource);
37         InputStream input = inputStreamInputSupplier.getInput();
38         ObjectMapper mapper = new ObjectMapper();
39         mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
40         TableUpdates updates = mapper.readValue(input, TableUpdates.class);
41
42         Set<Table.Name> available = updates.availableUpdates();
43         for (Table.Name name : available) {
44             if (Bridge.NAME.equals(name)) {
45                 verifyBridge(updates);
46             } else if (Port.NAME.equals(name)) {
47                 veriftyPort(updates);
48             } else if (Interface.NAME.equals(name)) {
49                 verifyInterface(updates);
50             }
51         }
52     }
53
54     private void verifyInterface(TableUpdates updates) {
55         TableUpdate<Interface> update = updates.getUpdate(Interface.NAME);
56         for (TableUpdate.Row<Interface> interfaceRow : update.getRows()) {
57             System.out.println("interfaceRow = " + interfaceRow);
58             Interface aNew = interfaceRow.getNew();
59             if (null != aNew) {
60                 OvsDBMap<String, String> options = aNew.getOptions();
61                 if (options != null) {
62                     for (Map.Entry<String, String> optE : options.entrySet()) {
63                         System.out.println("optE.getKey() = " + optE.getKey());
64                         System.out.println("optE.getValue() = " + optE.getValue());
65                     }
66                 }
67             }
68         }
69     }
70
71     private void verifyBridge(TableUpdates updates) {
72         TableUpdate<Bridge> update = updates.getUpdate(Bridge.NAME);
73         for (TableUpdate.Row<Bridge> row : update.getRows()) {
74             assertEquals("788de61c-0e4f-43d8-a068-259e75aabbba", row.getId());
75             Bridge bridge = row.getNew();
76             assertNotNull(bridge);
77             OvsDBSet<UUID> ports = bridge.getPorts();
78             assertEquals(2, ports.size());
79             List<UUID> uuids =  Ordering.usingToString().sortedCopy(ports);
80             assertEquals("f6018e7a-7ca5-4e72-a744-a9b434f47011", uuids.get(0).toString());
81             assertEquals("fe3c89fd-2ff3-44d8-9f27-f9c7ac2a693d", uuids.get(1).toString());
82             bridge = row.getOld();
83             assertNull(bridge);
84         }
85     }
86
87     private void veriftyPort(TableUpdates updates) {
88         TableUpdate<Port> update = updates.getUpdate(Port.NAME);
89         Collection<TableUpdate.Row<Port>> rows = update.getRows();
90         assertEquals(2, rows.size());
91         List<TableUpdate.Row<Port>> sorted = Ordering.natural().onResultOf(new Function<TableUpdate.Row<Port>, String>() {
92             @Override
93             public String apply(@Nullable org.opendaylight.ovsdb.lib.message.TableUpdate.Row<Port> input) {
94                 return input.getId();
95             }
96         }).sortedCopy(rows);
97
98         TableUpdate.Row<Port> portRow = sorted.get(0);
99         assertEquals("f6018e7a-7ca5-4e72-a744-a9b434f47011", portRow.getId());
100         Port port = portRow.getNew();
101         assertNotNull(port);
102         List<UUID> interfaces = Ordering.usingToString().sortedCopy(port.getInterfaces());
103         assertEquals("13548b08-dca3-4d4b-9e9b-f50c237dcb9e", interfaces.get(0).toString());
104         port = portRow.getOld();
105         assertNull(port);
106
107         portRow = sorted.get(1);
108         assertEquals("fe3c89fd-2ff3-44d8-9f27-f9c7ac2a693d", portRow.getId());
109         port = portRow.getNew();
110         assertNotNull(port);
111         interfaces = Ordering.usingToString().sortedCopy(port.getInterfaces());
112         assertEquals("88ae29fb-8c91-41a9-a14f-a74126e790c0", interfaces.get(0).toString());
113         port = portRow.getOld();
114         assertNull(port);
115     }
116
117 }