blob: 607e0fbbb17ac57e391bf36799477e8bba178d42 [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;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070024
25public class KVSwitchTest {
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070026
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070027 private static final Logger log = LoggerFactory.getLogger(KVSwitchTest.class);
28
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070029 static {
30 // configuration to quickly fall back to instance mode for faster test run
31 System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0");
32 }
33
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070034 IKVTable switchTable;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070035 static final Long DPID1 = 0x1L;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070036 KVSwitch sw1;
37
38 @Before
39 public void setUp() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070040 switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME);
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070041 sw1 = new KVSwitch(DPID1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070042 }
43
44 @After
45 public void tearDown() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070046 DataStoreClient.getClient().dropTable(switchTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070047 }
48
49 public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) {
Ray Milkey269ffb92014-04-03 14:43:30 -070050 try {
51 final KVSwitch sw = new KVSwitch(dpid);
52 sw.read();
Ray Milkey7531a342014-04-11 15:08:12 -070053 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070054 assertEquals(dpid, sw.getDpid());
55 assertEquals(status, sw.getStatus());
56 return sw;
57 } catch (ObjectDoesntExistException e) {
58 fail("Switch was not written to datastore");
59 }
60 return null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070061 }
Ray Milkey269ffb92014-04-03 14:43:30 -070062
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070063 public void assertSwitchNotInDataStore(final Long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070064 final KVSwitch sw = new KVSwitch(dpid);
65 try {
66 sw.read();
67 fail("Switch was not supposed to be there in datastore");
68 } catch (ObjectDoesntExistException e) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070069 log.debug("Exception thrown as expected", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070070 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070071 }
72
73 @Test
74 public void testGetAllSwitches() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 final int NUM_SWITCHES = 100;
76 Map<Long, KVSwitch> expected = new HashMap<>();
77 for (long dpid = 1; dpid <= NUM_SWITCHES; ++dpid) {
78 KVSwitch sw = new KVSwitch(dpid);
79 sw.setStatus(STATUS.ACTIVE);
80 sw.create();
Ray Milkey7531a342014-04-11 15:08:12 -070081 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070082 expected.put(sw.getDpid(), sw);
83 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 Iterable<KVSwitch> switches = KVSwitch.getAllSwitches();
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 for (KVSwitch sw : switches) {
88 KVSwitch expectedSw = expected.get(sw.getDpid());
89 assertNotNull(expectedSw);
90 assertEquals(expectedSw.getDpid(), sw.getDpid());
91 assertEquals(expectedSw.getStatus(), sw.getStatus());
92 assertEquals(expectedSw.getVersion(), sw.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 assertArrayEquals(expectedSw.getKey(), sw.getKey());
95 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070096 }
97
98 @Test
99 public void testCreate() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 sw1.setStatus(STATUS.ACTIVE);
101 sw1.create();
Ray Milkey7531a342014-04-11 15:08:12 -0700102 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700103
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700104 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700106
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700107 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700108 }
109
110 @Test(expected = ObjectExistsException.class)
111 public void testCreateFailAlreadyExist() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700113 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700115 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700116 assertSwitchInDataStore(DPID1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 sw1.setStatus(STATUS.ACTIVE);
119 sw1.create();
120 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700121 }
122
123 @Test
124 public void testForceCreate() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700126 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700128 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700129 assertSwitchInDataStore(DPID1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700130
131
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 sw1.setStatus(STATUS.ACTIVE);
133 sw1.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700134 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700135
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700136 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700138 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700139 }
140
141 @Test
142 public void testRead() throws ObjectDoesntExistException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700144 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 sw.setStatus(STATUS.ACTIVE);
146 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700147 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700148 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700149
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700151 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 assertEquals(sw.getVersion(), sw1.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700153 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700155 }
156
157 @Test(expected = ObjectDoesntExistException.class)
158 public void testReadFailNoExist() throws ObjectDoesntExistException {
159
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 sw1.read();
161 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700162 }
163
164 @Test
165 public void testUpdate() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700167 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 sw.setStatus(STATUS.ACTIVE);
169 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700170 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700171 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700172
173
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700175 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 sw1.setStatus(STATUS.INACTIVE);
178 sw1.update();
Ray Milkey7531a342014-04-11 15:08:12 -0700179 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 assertNotEquals(sw.getVersion(), sw1.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700181 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 assertEquals(STATUS.INACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700183 }
184
185 @Test
186 public void testDelete() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700188 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 sw.setStatus(STATUS.ACTIVE);
190 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700191 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700192 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700193
194
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 try {
196 sw1.read();
197 } catch (ObjectDoesntExistException e) {
198 fail("Failed reading switch to delete");
199 }
Ray Milkey7531a342014-04-11 15:08:12 -0700200 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700201 sw1.delete();
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700202 assertSwitchNotInDataStore(DPID1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700203 }
204
205 @Test
206 public void testForceDelete() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 // setup pre-existing Switch
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700208 KVSwitch sw = new KVSwitch(DPID1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 sw.setStatus(STATUS.ACTIVE);
210 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700211 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700212 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700213
214
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 sw1.forceDelete();
Ray Milkey7531a342014-04-11 15:08:12 -0700216 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700217 }
218
219}