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