blob: fac33f5b1d3620245db72fa7bd64a9d5b8293f09 [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;
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070011import java.util.UUID;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070012
Jonathan Hart6df90172014-04-03 10:13:11 -070013import net.onrc.onos.core.datastore.DataStoreClient;
14import net.onrc.onos.core.datastore.IKVTable;
15import net.onrc.onos.core.datastore.ObjectDoesntExistException;
16import net.onrc.onos.core.datastore.ObjectExistsException;
17import net.onrc.onos.core.datastore.WrongVersionException;
Jonathan Hart6df90172014-04-03 10:13:11 -070018import net.onrc.onos.core.datastore.topology.KVSwitch.STATUS;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070019
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070025
26public class KVSwitchTest {
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070027
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070028 private static final Logger log = LoggerFactory.getLogger(KVSwitchTest.class);
29
Yuta HIGUCHIceb21b62014-04-17 15:46:05 -070030 static {
31 // configuration to quickly fall back to instance mode for faster test run
32 System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0");
33 }
34
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070035 private static final String namespace = UUID.randomUUID().toString();
36
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070037 IKVTable switchTable;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070038 static final Long DPID1 = 0x1L;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070039 KVSwitch sw1;
40
41 @Before
42 public void setUp() throws Exception {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070043 switchTable = DataStoreClient.getClient().getTable(namespace + KVSwitch.SWITCH_TABLE_SUFFIX);
44 sw1 = new KVSwitch(DPID1, namespace);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070045 }
46
47 @After
48 public void tearDown() throws Exception {
Ray Milkey269ffb92014-04-03 14:43:30 -070049 DataStoreClient.getClient().dropTable(switchTable);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070050 }
51
52 public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 try {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070054 final KVSwitch sw = new KVSwitch(dpid, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -070055 sw.read();
Ray Milkey7531a342014-04-11 15:08:12 -070056 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070057 assertEquals(dpid, sw.getDpid());
58 assertEquals(status, sw.getStatus());
59 return sw;
60 } catch (ObjectDoesntExistException e) {
61 fail("Switch was not written to datastore");
62 }
63 return null;
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070064 }
Ray Milkey269ffb92014-04-03 14:43:30 -070065
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070066 public void assertSwitchNotInDataStore(final Long dpid) {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070067 final KVSwitch sw = new KVSwitch(dpid, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -070068 try {
69 sw.read();
70 fail("Switch was not supposed to be there in datastore");
71 } catch (ObjectDoesntExistException e) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070072 log.debug("Exception thrown as expected", e);
Ray Milkey269ffb92014-04-03 14:43:30 -070073 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070074 }
75
76 @Test
77 public void testGetAllSwitches() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 final int NUM_SWITCHES = 100;
79 Map<Long, KVSwitch> expected = new HashMap<>();
80 for (long dpid = 1; dpid <= NUM_SWITCHES; ++dpid) {
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070081 KVSwitch sw = new KVSwitch(dpid, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -070082 sw.setStatus(STATUS.ACTIVE);
83 sw.create();
Ray Milkey7531a342014-04-11 15:08:12 -070084 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -070085 expected.put(sw.getDpid(), sw);
86 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070087
Yuta HIGUCHIcc561882014-05-22 10:31:32 -070088 Iterable<KVSwitch> switches = KVSwitch.getAllSwitches(namespace);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 for (KVSwitch sw : switches) {
91 KVSwitch expectedSw = expected.get(sw.getDpid());
92 assertNotNull(expectedSw);
93 assertEquals(expectedSw.getDpid(), sw.getDpid());
94 assertEquals(expectedSw.getStatus(), sw.getStatus());
95 assertEquals(expectedSw.getVersion(), sw.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070096
Ray Milkey269ffb92014-04-03 14:43:30 -070097 assertArrayEquals(expectedSw.getKey(), sw.getKey());
98 }
Yuta HIGUCHI6a643132014-03-18 22:39:27 -070099 }
100
101 @Test
102 public void testCreate() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 sw1.setStatus(STATUS.ACTIVE);
104 sw1.create();
Ray Milkey7531a342014-04-11 15:08:12 -0700105 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700106
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700107 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700109
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700110 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700111 }
112
113 @Test(expected = ObjectExistsException.class)
114 public void testCreateFailAlreadyExist() throws ObjectExistsException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700116 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700118 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700119 assertSwitchInDataStore(DPID1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 sw1.setStatus(STATUS.ACTIVE);
122 sw1.create();
123 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700124 }
125
126 @Test
127 public void testForceCreate() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700129 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700131 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700132 assertSwitchInDataStore(DPID1, STATUS.INACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700133
134
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 sw1.setStatus(STATUS.ACTIVE);
136 sw1.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700137 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700138
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700139 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700141 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700142 }
143
144 @Test
145 public void testRead() throws ObjectDoesntExistException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700147 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 sw.setStatus(STATUS.ACTIVE);
149 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700150 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700151 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700152
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700154 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 assertEquals(sw.getVersion(), sw1.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700156 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 assertEquals(STATUS.ACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700158 }
159
160 @Test(expected = ObjectDoesntExistException.class)
161 public void testReadFailNoExist() throws ObjectDoesntExistException {
162
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 sw1.read();
164 fail("Should have thrown an exception");
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700165 }
166
167 @Test
168 public void testUpdate() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700169 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700170 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700171 sw.setStatus(STATUS.ACTIVE);
172 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700173 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700174 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700175
176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 sw1.read();
Ray Milkey7531a342014-04-11 15:08:12 -0700178 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700179
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 sw1.setStatus(STATUS.INACTIVE);
181 sw1.update();
Ray Milkey7531a342014-04-11 15:08:12 -0700182 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 assertNotEquals(sw.getVersion(), sw1.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700184 assertEquals(DPID1, sw1.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700185 assertEquals(STATUS.INACTIVE, sw1.getStatus());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700186 }
187
188 @Test
189 public void testDelete() throws ObjectDoesntExistException, WrongVersionException {
Ray Milkey269ffb92014-04-03 14:43:30 -0700190 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700191 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 sw.setStatus(STATUS.ACTIVE);
193 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700194 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700195 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700196
197
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 try {
199 sw1.read();
200 } catch (ObjectDoesntExistException e) {
201 fail("Failed reading switch to delete");
202 }
Ray Milkey7531a342014-04-11 15:08:12 -0700203 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 sw1.delete();
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700205 assertSwitchNotInDataStore(DPID1);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700206 }
207
208 @Test
209 public void testForceDelete() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 // setup pre-existing Switch
Yuta HIGUCHIcc561882014-05-22 10:31:32 -0700211 KVSwitch sw = new KVSwitch(DPID1, namespace);
Ray Milkey269ffb92014-04-03 14:43:30 -0700212 sw.setStatus(STATUS.ACTIVE);
213 sw.forceCreate();
Ray Milkey7531a342014-04-11 15:08:12 -0700214 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion());
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700215 assertSwitchInDataStore(DPID1, STATUS.ACTIVE);
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700216
217
Ray Milkey269ffb92014-04-03 14:43:30 -0700218 sw1.forceDelete();
Ray Milkey7531a342014-04-11 15:08:12 -0700219 assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion());
Yuta HIGUCHI6a643132014-03-18 22:39:27 -0700220 }
221
222}