blob: ce730344eefbfd6a2c624cb7437849c2fcf90015 [file] [log] [blame]
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07001package net.onrc.onos.datastore.topology;
2
3import static org.junit.Assert.*;
4
5import java.util.HashMap;
6import java.util.Map;
7
8import net.onrc.onos.datastore.DataStoreClient;
9import net.onrc.onos.datastore.IKVTable;
10import net.onrc.onos.datastore.ObjectDoesntExistException;
11import net.onrc.onos.datastore.ObjectExistsException;
12import net.onrc.onos.datastore.WrongVersionException;
13import net.onrc.onos.datastore.topology.KVSwitch.STATUS;
14
15import org.junit.After;
16import org.junit.Before;
17import org.junit.Test;
18
19public class KVSwitchTest {
20 IKVTable switchTable;
21 static final Long dpid1 = 0x1L;
22 KVSwitch sw1;
23
24 @Before
25 public void setUp() throws Exception {
26 switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME);
27 sw1 = new KVSwitch(dpid1);
28 }
29
30 @After
31 public void tearDown() throws Exception {
32 DataStoreClient.getClient().dropTable(switchTable);
33 }
34
35 public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) {
36 try {
37 final KVSwitch sw = new KVSwitch(dpid);
38 sw.read();
39 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
40 assertEquals(dpid, sw.getDpid());
41 assertEquals(status, sw.getStatus());
42 return sw;
43 } catch (ObjectDoesntExistException e) {
44 fail("Switch was not written to datastore");
45 }
46 return null;
47 }
48 public void assertSwitchNotInDataStore(final Long dpid) {
49 final KVSwitch sw = new KVSwitch(dpid);
50 try {
51 sw.read();
52 fail("Switch was not supposed to be there in datastore");
53 } catch (ObjectDoesntExistException e) {
54 }
55 }
56
57 @Test
58 public void testGetAllSwitches() throws ObjectExistsException {
59 final int NUM_SWITCHES = 100;
60 Map<Long,KVSwitch> expected = new HashMap<>();
61 for (long dpid = 1 ; dpid <= NUM_SWITCHES ; ++dpid) {
62 KVSwitch sw = new KVSwitch(dpid);
63 sw.setStatus(STATUS.ACTIVE);
64 sw.create();
65 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
66 expected.put(sw.getDpid(), sw);
67 }
68
69 Iterable<KVSwitch> switches = KVSwitch.getAllSwitches();
70
71 for (KVSwitch sw : switches) {
72 KVSwitch expectedSw = expected.get(sw.getDpid());
73 assertNotNull(expectedSw);
74 assertEquals(expectedSw.getDpid(), sw.getDpid());
75 assertEquals(expectedSw.getStatus(), sw.getStatus());
76 assertEquals(expectedSw.getVersion(), sw.getVersion());
77
78 assertArrayEquals(expectedSw.getKey(), sw.getKey());
79 }
80 }
81
82 @Test
83 public void testCreate() throws ObjectExistsException {
84 sw1.setStatus(STATUS.ACTIVE);
85 sw1.create();
86 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
87
88 assertEquals(dpid1, sw1.getDpid());
89 assertEquals(STATUS.ACTIVE, sw1.getStatus());
90
91 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
92 }
93
94 @Test(expected = ObjectExistsException.class)
95 public void testCreateFailAlreadyExist() throws ObjectExistsException {
96 // setup pre-existing Switch
97 KVSwitch sw = new KVSwitch(dpid1);
98 sw.forceCreate();
99 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
100 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
101
102 sw1.setStatus(STATUS.ACTIVE);
103 sw1.create();
104 fail("Should have thrown an exception");
105 }
106
107 @Test
108 public void testForceCreate() {
109 // setup pre-existing Switch
110 KVSwitch sw = new KVSwitch(dpid1);
111 sw.forceCreate();
112 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
113 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
114
115
116 sw1.setStatus(STATUS.ACTIVE);
117 sw1.forceCreate();
118 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
119
120 assertEquals(dpid1, sw1.getDpid());
121 assertEquals(STATUS.ACTIVE, sw1.getStatus());
122 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
123 }
124
125 @Test
126 public void testRead() throws ObjectDoesntExistException {
127 // setup pre-existing Switch
128 KVSwitch sw = new KVSwitch(dpid1);
129 sw.setStatus(STATUS.ACTIVE);
130 sw.forceCreate();
131 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
132 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
133
134 sw1.read();
135 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
136 assertEquals(sw.getVersion(), sw1.getVersion());
137 assertEquals(dpid1, sw1.getDpid());
138 assertEquals(STATUS.ACTIVE, sw1.getStatus());
139 }
140
141 @Test(expected = ObjectDoesntExistException.class)
142 public void testReadFailNoExist() throws ObjectDoesntExistException {
143
144 sw1.read();
145 fail("Should have thrown an exception");
146 }
147
148 @Test
149 public void testUpdate() throws ObjectDoesntExistException, WrongVersionException {
150 // setup pre-existing Switch
151 KVSwitch sw = new KVSwitch(dpid1);
152 sw.setStatus(STATUS.ACTIVE);
153 sw.forceCreate();
154 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
155 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
156
157
158 sw1.read();
159 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
160
161 sw1.setStatus(STATUS.INACTIVE);
162 sw1.update();
163 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
164 assertNotEquals(sw.getVersion(), sw1.getVersion());
165 assertEquals(dpid1, sw1.getDpid());
166 assertEquals(STATUS.INACTIVE, sw1.getStatus());
167 }
168
169 @Test
170 public void testDelete() throws ObjectDoesntExistException, WrongVersionException {
171 // setup pre-existing Switch
172 KVSwitch sw = new KVSwitch(dpid1);
173 sw.setStatus(STATUS.ACTIVE);
174 sw.forceCreate();
175 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
176 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
177
178
179 try {
180 sw1.read();
181 } catch (ObjectDoesntExistException e) {
182 fail("Failed reading switch to delete");
183 }
184 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
185 sw1.delete();
186 assertSwitchNotInDataStore(dpid1);
187 }
188
189 @Test
190 public void testForceDelete() {
191 // setup pre-existing Switch
192 KVSwitch sw = new KVSwitch(dpid1);
193 sw.setStatus(STATUS.ACTIVE);
194 sw.forceCreate();
195 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
196 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
197
198
199 sw1.forceDelete();
200 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
201 }
202
203}