Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.topology; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertArrayEquals; |
| 4 | import static org.junit.Assert.assertEquals; |
| 5 | import static org.junit.Assert.assertNotEquals; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | import static org.junit.Assert.fail; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 8 | |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Map; |
| 11 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 13 | import net.onrc.onos.core.datastore.IKVTable; |
| 14 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 15 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 16 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datastore.topology.KVSwitch.STATUS; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | public class KVSwitchTest { |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame^] | 24 | |
| 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 HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 30 | IKVTable switchTable; |
| 31 | static final Long dpid1 = 0x1L; |
| 32 | KVSwitch sw1; |
| 33 | |
| 34 | @Before |
| 35 | public void setUp() throws Exception { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME); |
| 37 | sw1 = new KVSwitch(dpid1); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @After |
| 41 | public void tearDown() throws Exception { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | DataStoreClient.getClient().dropTable(switchTable); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | try { |
| 47 | final KVSwitch sw = new KVSwitch(dpid); |
| 48 | sw.read(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 49 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | 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 HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 57 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 59 | public void assertSwitchNotInDataStore(final Long dpid) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | 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 HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testGetAllSwitches() throws ObjectExistsException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | 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 Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 76 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | expected.put(sw.getDpid(), sw); |
| 78 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | Iterable<KVSwitch> switches = KVSwitch.getAllSwitches(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | 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 HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 88 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | assertArrayEquals(expectedSw.getKey(), sw.getKey()); |
| 90 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testCreate() throws ObjectExistsException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | sw1.setStatus(STATUS.ACTIVE); |
| 96 | sw1.create(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 97 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 98 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | assertEquals(dpid1, sw1.getDpid()); |
| 100 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 101 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test(expected = ObjectExistsException.class) |
| 106 | public void testCreateFailAlreadyExist() throws ObjectExistsException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | // setup pre-existing Switch |
| 108 | KVSwitch sw = new KVSwitch(dpid1); |
| 109 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 110 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | assertSwitchInDataStore(dpid1, STATUS.INACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 112 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | sw1.setStatus(STATUS.ACTIVE); |
| 114 | sw1.create(); |
| 115 | fail("Should have thrown an exception"); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testForceCreate() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | // setup pre-existing Switch |
| 121 | KVSwitch sw = new KVSwitch(dpid1); |
| 122 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 123 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | assertSwitchInDataStore(dpid1, STATUS.INACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 125 | |
| 126 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | sw1.setStatus(STATUS.ACTIVE); |
| 128 | sw1.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 129 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | assertEquals(dpid1, sw1.getDpid()); |
| 132 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
| 133 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testRead() throws ObjectDoesntExistException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | // setup pre-existing Switch |
| 139 | KVSwitch sw = new KVSwitch(dpid1); |
| 140 | sw.setStatus(STATUS.ACTIVE); |
| 141 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 142 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 144 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | sw1.read(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 146 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | assertEquals(sw.getVersion(), sw1.getVersion()); |
| 148 | assertEquals(dpid1, sw1.getDpid()); |
| 149 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Test(expected = ObjectDoesntExistException.class) |
| 153 | public void testReadFailNoExist() throws ObjectDoesntExistException { |
| 154 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | sw1.read(); |
| 156 | fail("Should have thrown an exception"); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testUpdate() throws ObjectDoesntExistException, WrongVersionException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | // setup pre-existing Switch |
| 162 | KVSwitch sw = new KVSwitch(dpid1); |
| 163 | sw.setStatus(STATUS.ACTIVE); |
| 164 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 165 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 166 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 167 | |
| 168 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | sw1.read(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 170 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 171 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | sw1.setStatus(STATUS.INACTIVE); |
| 173 | sw1.update(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 174 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | assertNotEquals(sw.getVersion(), sw1.getVersion()); |
| 176 | assertEquals(dpid1, sw1.getDpid()); |
| 177 | assertEquals(STATUS.INACTIVE, sw1.getStatus()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testDelete() throws ObjectDoesntExistException, WrongVersionException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | // setup pre-existing Switch |
| 183 | KVSwitch sw = new KVSwitch(dpid1); |
| 184 | sw.setStatus(STATUS.ACTIVE); |
| 185 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 186 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 188 | |
| 189 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 190 | try { |
| 191 | sw1.read(); |
| 192 | } catch (ObjectDoesntExistException e) { |
| 193 | fail("Failed reading switch to delete"); |
| 194 | } |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 195 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | sw1.delete(); |
| 197 | assertSwitchNotInDataStore(dpid1); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testForceDelete() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | // setup pre-existing Switch |
| 203 | KVSwitch sw = new KVSwitch(dpid1); |
| 204 | sw.setStatus(STATUS.ACTIVE); |
| 205 | sw.forceCreate(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 206 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw.getVersion()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 207 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 208 | |
| 209 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | sw1.forceDelete(); |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 211 | assertNotEquals(DataStoreClient.getClient().getVersionNonexistant(), sw1.getVersion()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } |