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 { |
| 24 | IKVTable switchTable; |
| 25 | static final Long dpid1 = 0x1L; |
| 26 | KVSwitch sw1; |
| 27 | |
| 28 | @Before |
| 29 | public void setUp() throws Exception { |
| 30 | switchTable = DataStoreClient.getClient().getTable(KVSwitch.GLOBAL_SWITCH_TABLE_NAME); |
| 31 | sw1 = new KVSwitch(dpid1); |
| 32 | } |
| 33 | |
| 34 | @After |
| 35 | public void tearDown() throws Exception { |
| 36 | DataStoreClient.getClient().dropTable(switchTable); |
| 37 | } |
| 38 | |
| 39 | public KVSwitch assertSwitchInDataStore(final Long dpid, final STATUS status) { |
| 40 | 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; |
| 51 | } |
| 52 | public void assertSwitchNotInDataStore(final Long dpid) { |
| 53 | final KVSwitch sw = new KVSwitch(dpid); |
| 54 | try { |
| 55 | sw.read(); |
| 56 | fail("Switch was not supposed to be there in datastore"); |
| 57 | } catch (ObjectDoesntExistException e) { |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testGetAllSwitches() throws ObjectExistsException { |
| 63 | final int NUM_SWITCHES = 100; |
| 64 | Map<Long,KVSwitch> expected = new HashMap<>(); |
| 65 | for (long dpid = 1 ; dpid <= NUM_SWITCHES ; ++dpid) { |
| 66 | KVSwitch sw = new KVSwitch(dpid); |
| 67 | sw.setStatus(STATUS.ACTIVE); |
| 68 | sw.create(); |
| 69 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 70 | expected.put(sw.getDpid(), sw); |
| 71 | } |
| 72 | |
| 73 | Iterable<KVSwitch> switches = KVSwitch.getAllSwitches(); |
| 74 | |
| 75 | for (KVSwitch sw : switches) { |
| 76 | KVSwitch expectedSw = expected.get(sw.getDpid()); |
| 77 | assertNotNull(expectedSw); |
| 78 | assertEquals(expectedSw.getDpid(), sw.getDpid()); |
| 79 | assertEquals(expectedSw.getStatus(), sw.getStatus()); |
| 80 | assertEquals(expectedSw.getVersion(), sw.getVersion()); |
| 81 | |
| 82 | assertArrayEquals(expectedSw.getKey(), sw.getKey()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testCreate() throws ObjectExistsException { |
| 88 | sw1.setStatus(STATUS.ACTIVE); |
| 89 | sw1.create(); |
| 90 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 91 | |
| 92 | assertEquals(dpid1, sw1.getDpid()); |
| 93 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
| 94 | |
| 95 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 96 | } |
| 97 | |
| 98 | @Test(expected = ObjectExistsException.class) |
| 99 | public void testCreateFailAlreadyExist() throws ObjectExistsException { |
| 100 | // setup pre-existing Switch |
| 101 | KVSwitch sw = new KVSwitch(dpid1); |
| 102 | sw.forceCreate(); |
| 103 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 104 | assertSwitchInDataStore(dpid1, STATUS.INACTIVE); |
| 105 | |
| 106 | sw1.setStatus(STATUS.ACTIVE); |
| 107 | sw1.create(); |
| 108 | fail("Should have thrown an exception"); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testForceCreate() { |
| 113 | // setup pre-existing Switch |
| 114 | KVSwitch sw = new KVSwitch(dpid1); |
| 115 | sw.forceCreate(); |
| 116 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 117 | assertSwitchInDataStore(dpid1, STATUS.INACTIVE); |
| 118 | |
| 119 | |
| 120 | sw1.setStatus(STATUS.ACTIVE); |
| 121 | sw1.forceCreate(); |
| 122 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 123 | |
| 124 | assertEquals(dpid1, sw1.getDpid()); |
| 125 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
| 126 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testRead() throws ObjectDoesntExistException { |
| 131 | // setup pre-existing Switch |
| 132 | KVSwitch sw = new KVSwitch(dpid1); |
| 133 | sw.setStatus(STATUS.ACTIVE); |
| 134 | sw.forceCreate(); |
| 135 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 136 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 137 | |
| 138 | sw1.read(); |
| 139 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 140 | assertEquals(sw.getVersion(), sw1.getVersion()); |
| 141 | assertEquals(dpid1, sw1.getDpid()); |
| 142 | assertEquals(STATUS.ACTIVE, sw1.getStatus()); |
| 143 | } |
| 144 | |
| 145 | @Test(expected = ObjectDoesntExistException.class) |
| 146 | public void testReadFailNoExist() throws ObjectDoesntExistException { |
| 147 | |
| 148 | sw1.read(); |
| 149 | fail("Should have thrown an exception"); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testUpdate() throws ObjectDoesntExistException, WrongVersionException { |
| 154 | // setup pre-existing Switch |
| 155 | KVSwitch sw = new KVSwitch(dpid1); |
| 156 | sw.setStatus(STATUS.ACTIVE); |
| 157 | sw.forceCreate(); |
| 158 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 159 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 160 | |
| 161 | |
| 162 | sw1.read(); |
| 163 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 164 | |
| 165 | sw1.setStatus(STATUS.INACTIVE); |
| 166 | sw1.update(); |
| 167 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 168 | assertNotEquals(sw.getVersion(), sw1.getVersion()); |
| 169 | assertEquals(dpid1, sw1.getDpid()); |
| 170 | assertEquals(STATUS.INACTIVE, sw1.getStatus()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testDelete() throws ObjectDoesntExistException, WrongVersionException { |
| 175 | // setup pre-existing Switch |
| 176 | KVSwitch sw = new KVSwitch(dpid1); |
| 177 | sw.setStatus(STATUS.ACTIVE); |
| 178 | sw.forceCreate(); |
| 179 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 180 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 181 | |
| 182 | |
| 183 | try { |
| 184 | sw1.read(); |
| 185 | } catch (ObjectDoesntExistException e) { |
| 186 | fail("Failed reading switch to delete"); |
| 187 | } |
| 188 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 189 | sw1.delete(); |
| 190 | assertSwitchNotInDataStore(dpid1); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testForceDelete() { |
| 195 | // setup pre-existing Switch |
| 196 | KVSwitch sw = new KVSwitch(dpid1); |
| 197 | sw.setStatus(STATUS.ACTIVE); |
| 198 | sw.forceCreate(); |
| 199 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw.getVersion()); |
| 200 | assertSwitchInDataStore(dpid1, STATUS.ACTIVE); |
| 201 | |
| 202 | |
| 203 | sw1.forceDelete(); |
| 204 | assertNotEquals(DataStoreClient.getClient().VERSION_NONEXISTENT(), sw1.getVersion()); |
| 205 | } |
| 206 | |
| 207 | } |