blob: ba25903435bfd39a54a7a663799dcfa2fd295d1b [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.topology;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotEquals;
6import static org.junit.Assert.assertNotNull;
7import static org.junit.Assert.fail;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -07008
9import java.util.HashMap;
10import java.util.Map;
11
Jonathan Hart6df90172014-04-03 10:13:11 -070012import net.onrc.onos.core.datastore.DataStoreClient;
13import net.onrc.onos.core.datastore.IKVTable;
14import net.onrc.onos.core.datastore.ObjectDoesntExistException;
15import net.onrc.onos.core.datastore.ObjectExistsException;
16import net.onrc.onos.core.datastore.WrongVersionException;
Jonathan Hart6df90172014-04-03 10:13:11 -070017import net.onrc.onos.core.datastore.topology.KVSwitch.STATUS;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070018
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22
23public class KVSwitchTest {
24 IKVTable switchTable;
25 static final Long dpid1 = 0x1L;
26 KVSwitch sw1;
27
28 @Before
29 public void setUp() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070030 switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME);
31 sw1 = new KVSwitch(dpid1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070032 }
33
34 @After
35 public void tearDown() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 DataStoreClient.getClient().dropTable(switchTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070037 }
38
39 public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) {
Ray Milkey269ffb92014-04-03 14:43:30 -070040 try {
41 final KVSwitch sw = new KVSwitch(dpid);
42 sw.read();
43 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
44 assertEquals(dpid, sw.getDpid());
45 assertEquals(status, sw.getStatus());
46 return sw;
47 } catch (ObjectDoesntExistException e) {
48 fail("Switch was not written to datastore");
49 }
50 return null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070051 }
Ray Milkey269ffb92014-04-03 14:43:30 -070052
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070053 public void assertSwitchNotInDataStore(final Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070054 final KVSwitch sw = new KVSwitch(dpid);
55 try {
56 sw.read();
57 fail("Switch was not supposed to be there in datastore");
58 } catch (ObjectDoesntExistException e) {
59 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070060 }
61
62 @Test
63 public void testGetAllSwitches() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070064 final int NUM_SWITCHES = 100;
65 Map<Long, KVSwitch> expected = new HashMap<>();
66 for (long dpid = 1; dpid <= NUM_SWITCHES; ++dpid) {
67 KVSwitch sw = new KVSwitch(dpid);
68 sw.setStatus(STATUS.ACTIVE);
69 sw.create();
70 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
71 expected.put(sw.getDpid(), sw);
72 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 Iterable<KVSwitch> switches = KVSwitch.getAllSwitches();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 for (KVSwitch sw : switches) {
77 KVSwitch expectedSw = expected.get(sw.getDpid());
78 assertNotNull(expectedSw);
79 assertEquals(expectedSw.getDpid(), sw.getDpid());
80 assertEquals(expectedSw.getStatus(), sw.getStatus());
81 assertEquals(expectedSw.getVersion(), sw.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070082
Ray Milkey269ffb92014-04-03 14:43:30 -070083 assertArrayEquals(expectedSw.getKey(), sw.getKey());
84 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070085 }
86
87 @Test
88 public void testCreate() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 sw1.setStatus(STATUS.ACTIVE);
90 sw1.create();
91 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 assertEquals(dpid1, sw1.getDpid());
94 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070097 }
98
99 @Test(expected = ObjectExistsException.class)
100 public void testCreateFailAlreadyExist() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 // setup pre-existing Switch
102 KVSwitch sw = new KVSwitch(dpid1);
103 sw.forceCreate();
104 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
105 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700106
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 sw1.setStatus(STATUS.ACTIVE);
108 sw1.create();
109 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700110 }
111
112 @Test
113 public void testForceCreate() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 // setup pre-existing Switch
115 KVSwitch sw = new KVSwitch(dpid1);
116 sw.forceCreate();
117 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
118 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700119
120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 sw1.setStatus(STATUS.ACTIVE);
122 sw1.forceCreate();
123 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700124
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 assertEquals(dpid1, sw1.getDpid());
126 assertEquals(STATUS.ACTIVE, sw1.getStatus());
127 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700128 }
129
130 @Test
131 public void testRead() throws ObjectDoesntExistException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 // setup pre-existing Switch
133 KVSwitch sw = new KVSwitch(dpid1);
134 sw.setStatus(STATUS.ACTIVE);
135 sw.forceCreate();
136 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
137 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 sw1.read();
140 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
141 assertEquals(sw.getVersion(), sw1.getVersion());
142 assertEquals(dpid1, sw1.getDpid());
143 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700144 }
145
146 @Test(expected = ObjectDoesntExistException.class)
147 public void testReadFailNoExist() throws ObjectDoesntExistException {
148
Ray Milkey269ffb92014-04-03 14:43:30 -0700149 sw1.read();
150 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700151 }
152
153 @Test
154 public void testUpdate() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 // setup pre-existing Switch
156 KVSwitch sw = new KVSwitch(dpid1);
157 sw.setStatus(STATUS.ACTIVE);
158 sw.forceCreate();
159 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
160 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700161
162
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 sw1.read();
164 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700165
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 sw1.setStatus(STATUS.INACTIVE);
167 sw1.update();
168 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
169 assertNotEquals(sw.getVersion(), sw1.getVersion());
170 assertEquals(dpid1, sw1.getDpid());
171 assertEquals(STATUS.INACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700172 }
173
174 @Test
175 public void testDelete() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 // setup pre-existing Switch
177 KVSwitch sw = new KVSwitch(dpid1);
178 sw.setStatus(STATUS.ACTIVE);
179 sw.forceCreate();
180 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
181 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700182
183
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 try {
185 sw1.read();
186 } catch (ObjectDoesntExistException e) {
187 fail("Failed reading switch to delete");
188 }
189 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
190 sw1.delete();
191 assertSwitchNotInDataStore(dpid1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700192 }
193
194 @Test
195 public void testForceDelete() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 // setup pre-existing Switch
197 KVSwitch sw = new KVSwitch(dpid1);
198 sw.setStatus(STATUS.ACTIVE);
199 sw.forceCreate();
200 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion());
201 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700202
203
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 sw1.forceDelete();
205 assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700206 }
207
208}