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