blob: e8927ae8f1ac53886396f589ddc32b0fdefd1ad5 [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 {
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070024
25 static {
26 // configuration to quickly fall back to instance mode for faster test run
27 System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0");
28 }
29
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070030 IKVTable switchTable;
31 static final Long dpid1 = 0x1L;
32 KVSwitch sw1;
33
34 @Before
35 public void setUp() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME);
37 sw1 = new KVSwitch(dpid1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070038 }
39
40 @After
41 public void tearDown() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070042 DataStoreClient.getClient().dropTable(switchTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070043 }
44
45 public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) {
Ray Milkey269ffb92014-04-03 14:43:30 -070046 try {
47 final KVSwitch sw = new KVSwitch(dpid);
48 sw.read();
Ray Milkey7531a342014-04-11 15:08:12 -070049 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070050 assertEquals(dpid, sw.getDpid());
51 assertEquals(status, sw.getStatus());
52 return sw;
53 } catch (ObjectDoesntExistException e) {
54 fail("Switch was not written to datastore");
55 }
56 return null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070057 }
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070059 public void assertSwitchNotInDataStore(final Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 final KVSwitch sw = new KVSwitch(dpid);
61 try {
62 sw.read();
63 fail("Switch was not supposed to be there in datastore");
64 } catch (ObjectDoesntExistException e) {
65 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070066 }
67
68 @Test
69 public void testGetAllSwitches() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070070 final int NUM_SWITCHES = 100;
71 Map<Long, KVSwitch> expected = new HashMap<>();
72 for (long dpid = 1; dpid <= NUM_SWITCHES; ++dpid) {
73 KVSwitch sw = new KVSwitch(dpid);
74 sw.setStatus(STATUS.ACTIVE);
75 sw.create();
Ray Milkey7531a342014-04-11 15:08:12 -070076 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070077 expected.put(sw.getDpid(), sw);
78 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 Iterable<KVSwitch> switches = KVSwitch.getAllSwitches();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 for (KVSwitch sw : switches) {
83 KVSwitch expectedSw = expected.get(sw.getDpid());
84 assertNotNull(expectedSw);
85 assertEquals(expectedSw.getDpid(), sw.getDpid());
86 assertEquals(expectedSw.getStatus(), sw.getStatus());
87 assertEquals(expectedSw.getVersion(), sw.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 assertArrayEquals(expectedSw.getKey(), sw.getKey());
90 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070091 }
92
93 @Test
94 public void testCreate() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070095 sw1.setStatus(STATUS.ACTIVE);
96 sw1.create();
Ray Milkey7531a342014-04-11 15:08:12 -070097 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 assertEquals(dpid1, sw1.getDpid());
100 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700103 }
104
105 @Test(expected = ObjectExistsException.class)
106 public void testCreateFailAlreadyExist() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 // setup pre-existing Switch
108 KVSwitch sw = new KVSwitch(dpid1);
109 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700110 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 sw1.setStatus(STATUS.ACTIVE);
114 sw1.create();
115 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700116 }
117
118 @Test
119 public void testForceCreate() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 // setup pre-existing Switch
121 KVSwitch sw = new KVSwitch(dpid1);
122 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700123 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 assertSwitchInDataStore(dpid1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700125
126
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 sw1.setStatus(STATUS.ACTIVE);
128 sw1.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700129 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 assertEquals(dpid1, sw1.getDpid());
132 assertEquals(STATUS.ACTIVE, sw1.getStatus());
133 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700134 }
135
136 @Test
137 public void testRead() throws ObjectDoesntExistException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 // setup pre-existing Switch
139 KVSwitch sw = new KVSwitch(dpid1);
140 sw.setStatus(STATUS.ACTIVE);
141 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700142 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700144
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700146 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 assertEquals(sw.getVersion(), sw1.getVersion());
148 assertEquals(dpid1, sw1.getDpid());
149 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700150 }
151
152 @Test(expected = ObjectDoesntExistException.class)
153 public void testReadFailNoExist() throws ObjectDoesntExistException {
154
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 sw1.read();
156 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700157 }
158
159 @Test
160 public void testUpdate() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700161 // setup pre-existing Switch
162 KVSwitch sw = new KVSwitch(dpid1);
163 sw.setStatus(STATUS.ACTIVE);
164 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700165 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700167
168
Ray Milkey269ffb92014-04-03 14:43:30 -0700169 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700170 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700171
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 sw1.setStatus(STATUS.INACTIVE);
173 sw1.update();
Ray Milkey7531a342014-04-11 15:08:12 -0700174 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700175 assertNotEquals(sw.getVersion(), sw1.getVersion());
176 assertEquals(dpid1, sw1.getDpid());
177 assertEquals(STATUS.INACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700178 }
179
180 @Test
181 public void testDelete() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 // setup pre-existing Switch
183 KVSwitch sw = new KVSwitch(dpid1);
184 sw.setStatus(STATUS.ACTIVE);
185 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700186 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700188
189
Ray Milkey269ffb92014-04-03 14:43:30 -0700190 try {
191 sw1.read();
192 } catch (ObjectDoesntExistException e) {
193 fail("Failed reading switch to delete");
194 }
Ray Milkey7531a342014-04-11 15:08:12 -0700195 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 sw1.delete();
197 assertSwitchNotInDataStore(dpid1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700198 }
199
200 @Test
201 public void testForceDelete() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700202 // setup pre-existing Switch
203 KVSwitch sw = new KVSwitch(dpid1);
204 sw.setStatus(STATUS.ACTIVE);
205 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700206 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 assertSwitchInDataStore(dpid1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700208
209
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 sw1.forceDelete();
Ray Milkey7531a342014-04-11 15:08:12 -0700211 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700212 }
213
214}