Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.linkdiscovery.internal; |
| 2 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 7 | import java.util.ArrayList; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 8 | import java.util.Iterator; |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 9 | import java.util.List; |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 10 | import java.util.Set; |
| 11 | |
| 12 | import junit.framework.Assert; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 13 | |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.internal.TestDatabaseManager; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.linkdiscovery.ILinkStorage; |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 17 | import net.floodlightcontroller.linkdiscovery.LinkInfo; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.routing.Link; |
| 19 | |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 20 | import org.easymock.EasyMock; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 23 | import org.junit.Ignore; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 25 | import org.junit.runner.RunWith; |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 26 | import org.openflow.protocol.OFPhysicalPort; |
| 27 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 28 | import org.openflow.util.HexString; |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 29 | import org.powermock.api.easymock.PowerMock; |
| 30 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 31 | import org.powermock.modules.junit4.PowerMockRunner; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 32 | |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 33 | import com.thinkaurelius.titan.core.TitanFactory; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 34 | import com.thinkaurelius.titan.core.TitanGraph; |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 35 | import com.tinkerpop.blueprints.Direction; |
| 36 | import com.tinkerpop.blueprints.Edge; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 37 | import com.tinkerpop.blueprints.Vertex; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 38 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 39 | import com.tinkerpop.pipes.PipeFunction; |
| 40 | import com.tinkerpop.pipes.transform.PathPipe; |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 41 | |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 42 | @RunWith(PowerMockRunner.class) |
| 43 | @PrepareForTest({TitanFactory.class}) |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 44 | public class LinkStorageImplTest { |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 45 | private static ILinkStorage linkStorage; |
| 46 | private static TitanGraph titanGraph; |
| 47 | |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 48 | //private static IController |
| 49 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Setup code called before each tests. |
| 52 | * Read test graph data and replace DB access by test graph data. |
| 53 | * @throws Exception |
| 54 | */ |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 55 | @Before |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 56 | public void setUp() throws Exception{ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 57 | TestDatabaseManager.deleteTestDatabase(); |
| 58 | |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 59 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 60 | TestDatabaseManager.populateTestData(titanGraph); |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 61 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 62 | // replace return value of TitanFactory.open() to dummy DB created above |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 63 | PowerMock.mockStatic(TitanFactory.class); |
| 64 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 65 | PowerMock.replay(TitanFactory.class); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 66 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 67 | linkStorage = new LinkStorageImpl(); |
| 68 | |
| 69 | // initialize with dummy string |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 70 | linkStorage.init("/dummy/path/to/db"); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 71 | } |
| 72 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Closing code called after each tests. |
| 75 | * Discard test graph data. |
| 76 | * @throws Exception |
| 77 | */ |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 78 | @After |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 79 | public void tearDown() throws Exception { |
| 80 | // finish code |
| 81 | linkStorage.close(); |
| 82 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 83 | titanGraph.shutdown(); |
mininet | 37dc589 | 2013-06-04 10:58:44 -0700 | [diff] [blame] | 84 | TestDatabaseManager.deleteTestDatabase(); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 87 | // TODO: remove @Ignore after UPDATE method is implemented |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 88 | @Ignore @Test |
| 89 | public void testUpdate_UpdateSingleLink() { |
| 90 | Link linkToUpdate= createExistingLink(); |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 91 | long currentTime = System.currentTimeMillis(); |
| 92 | LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime); |
| 93 | LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 94 | |
| 95 | linkStorage.update(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE); |
| 96 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 97 | doTestLinkHasStateOf(linkToUpdate, infoToVerify); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testUpdate_CreateSingleLink() { |
| 102 | Link linkToCreate = createFeasibleLink(); |
| 103 | Link linkToVerify = createFeasibleLink(); |
| 104 | |
| 105 | //Use the link storage API to add the link |
| 106 | linkStorage.update(linkToCreate, ILinkStorage.DM_OPERATION.CREATE); |
| 107 | doTestLinkIsInGraph(linkToVerify); |
| 108 | |
| 109 | // Add same link |
| 110 | Link linkToCreateTwice = createFeasibleLink(); |
| 111 | linkStorage.update(linkToCreateTwice, ILinkStorage.DM_OPERATION.CREATE); |
| 112 | |
| 113 | // this occurs assertion failure if there are two links in titanGraph |
| 114 | doTestLinkIsInGraph(linkToVerify); |
| 115 | } |
| 116 | |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 117 | @Test |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 118 | public void testUpdate_InsertSingleLink(){ |
| 119 | Link linkToInsert = createFeasibleLink(); |
| 120 | Link linkToVerify = createFeasibleLink(); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 121 | |
| 122 | //Use the link storage API to add the link |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 123 | linkStorage.update(linkToInsert, ILinkStorage.DM_OPERATION.INSERT); |
| 124 | |
| 125 | doTestLinkIsInGraph(linkToVerify); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 126 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 127 | // Add same link |
| 128 | Link linkToInsertTwice = createFeasibleLink(); |
| 129 | linkStorage.update(linkToInsertTwice, ILinkStorage.DM_OPERATION.INSERT); |
| 130 | |
| 131 | // this occurs assertion failure if there are two links in titanGraph |
| 132 | doTestLinkIsInGraph(linkToVerify); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 133 | } |
| 134 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 135 | @Test |
| 136 | public void testUpdate_DeleteSingleLink(){ |
| 137 | Link linkToDelete = createExistingLink(); |
| 138 | Link linkToVerify = createExistingLink(); |
| 139 | |
| 140 | // Test deletion of existing link |
| 141 | linkStorage.update(linkToDelete, DM_OPERATION.DELETE); |
| 142 | doTestLinkIsNotInGraph(linkToVerify); |
| 143 | |
| 144 | linkToDelete = createFeasibleLink(); |
| 145 | linkToVerify = createFeasibleLink(); |
| 146 | |
| 147 | // Test deletion of not-existing link |
| 148 | linkStorage.update(linkToDelete, DM_OPERATION.DELETE); |
| 149 | doTestLinkIsNotInGraph(linkToVerify); |
| 150 | } |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 151 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 152 | @Test |
| 153 | public void testUpdate_CreateLinks(){ |
| 154 | List<Link> linksToCreate = createFeasibleLinks(); |
| 155 | List<Link> linksToVerify = createFeasibleLinks(); |
| 156 | |
| 157 | // Test creation of new links |
| 158 | linkStorage.update(linksToCreate, ILinkStorage.DM_OPERATION.CREATE); |
| 159 | for(Link l : linksToVerify) { |
| 160 | doTestLinkIsInGraph(l); |
| 161 | } |
| 162 | |
| 163 | // Test creation of existing links |
| 164 | linksToCreate = createFeasibleLinks(); |
| 165 | linkStorage.update(linksToCreate, ILinkStorage.DM_OPERATION.CREATE); |
| 166 | for(Link l : linksToVerify) { |
| 167 | doTestLinkIsInGraph(l); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 172 | public void testUpdate_CreateLinks_Mixuture(){ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 173 | List<Link> linksToCreate = new ArrayList<Link>(); |
| 174 | linksToCreate.add(createFeasibleLink()); |
| 175 | linksToCreate.add(createExistingLink()); |
| 176 | |
| 177 | // Test creation of mixture of new/existing links |
| 178 | linkStorage.update(linksToCreate, ILinkStorage.DM_OPERATION.CREATE); |
| 179 | doTestLinkIsInGraph(createFeasibleLink()); |
| 180 | doTestLinkIsInGraph(createExistingLink()); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testUpdate_InsertLinks(){ |
| 185 | List<Link> linksToInsert = createFeasibleLinks(); |
| 186 | List<Link> linksToVerify = createFeasibleLinks(); |
| 187 | |
| 188 | // Test insertion of new links |
| 189 | linkStorage.update(linksToInsert, ILinkStorage.DM_OPERATION.INSERT); |
| 190 | for(Link l : linksToVerify) { |
| 191 | doTestLinkIsInGraph(l); |
| 192 | } |
| 193 | |
| 194 | // Test insertion of existing links |
| 195 | linksToInsert = createFeasibleLinks(); |
| 196 | linkStorage.update(linksToInsert, ILinkStorage.DM_OPERATION.INSERT); |
| 197 | for(Link l : linksToVerify) { |
| 198 | doTestLinkIsInGraph(l); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 203 | public void testUpdate_InsertLinks_Mixuture(){ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 204 | List<Link> linksToInsert = new ArrayList<Link>(); |
| 205 | linksToInsert.add(createFeasibleLink()); |
| 206 | linksToInsert.add(createExistingLink()); |
| 207 | |
| 208 | // Test insertion of mixture of new/existing links |
| 209 | linkStorage.update(linksToInsert, ILinkStorage.DM_OPERATION.INSERT); |
| 210 | doTestLinkIsInGraph(createFeasibleLink()); |
| 211 | doTestLinkIsInGraph(createExistingLink()); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | @Test |
| 216 | public void testUpdate_DeleteLinks(){ |
| 217 | List<Link> linksToDelete = createExistingLinks(); |
| 218 | List<Link> linksToVerify = createExistingLinks(); |
| 219 | |
| 220 | // Test deletion of existing links |
| 221 | linkStorage.update(linksToDelete, ILinkStorage.DM_OPERATION.DELETE); |
| 222 | for(Link l : linksToVerify) { |
| 223 | doTestLinkIsNotInGraph(l); |
| 224 | } |
| 225 | |
| 226 | // Test deletion of not-existing links |
| 227 | linksToDelete = createExistingLinks(); |
| 228 | linkStorage.update(linksToDelete, ILinkStorage.DM_OPERATION.DELETE); |
| 229 | for(Link l : linksToVerify) { |
| 230 | doTestLinkIsNotInGraph(l); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 235 | public void testUpdate_DeleteLinks_Mixuture(){ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 236 | List<Link> linksToDelete = new ArrayList<Link>(); |
| 237 | linksToDelete.add(createFeasibleLink()); |
| 238 | linksToDelete.add(createExistingLink()); |
| 239 | |
| 240 | // Test deletion of mixture of new/existing links |
| 241 | linkStorage.update(linksToDelete, ILinkStorage.DM_OPERATION.DELETE); |
| 242 | doTestLinkIsNotInGraph(createFeasibleLink()); |
| 243 | doTestLinkIsNotInGraph(createExistingLink()); |
| 244 | } |
| 245 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 246 | // TODO: remove @Ignore after UPDATE method is implemented |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 247 | @Ignore @Test |
| 248 | public void testAddOrUpdateLink_Update() { |
| 249 | Link linkToUpdate= createExistingLink(); |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 250 | long currentTime = System.currentTimeMillis(); |
| 251 | LinkInfo infoToUpdate = createFeasibleLinkInfo(currentTime); |
| 252 | LinkInfo infoToVerify = createFeasibleLinkInfo(currentTime); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 253 | |
| 254 | linkStorage.addOrUpdateLink(linkToUpdate, infoToUpdate, ILinkStorage.DM_OPERATION.UPDATE); |
| 255 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 256 | doTestLinkHasStateOf(linkToUpdate, infoToVerify); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 260 | public void testAddOrUpdateLink_Create() { |
| 261 | Link linkToCreate = createFeasibleLink(); |
| 262 | Link linkToVerify = createFeasibleLink(); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 263 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 264 | //Use the link storage API to add the link |
| 265 | linkStorage.addOrUpdateLink(linkToCreate, null, ILinkStorage.DM_OPERATION.CREATE); |
| 266 | doTestLinkIsInGraph(linkToVerify); |
| 267 | |
| 268 | // Add same link |
| 269 | Link linkToCreateTwice = createFeasibleLink(); |
| 270 | linkStorage.addOrUpdateLink(linkToCreateTwice, null, ILinkStorage.DM_OPERATION.CREATE); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 271 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 272 | // this occurs assertion failure if there are two links in titanGraph |
| 273 | doTestLinkIsInGraph(linkToVerify); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 277 | public void testAddOrUpdateLink_Insert() { |
| 278 | Link linkToInsert = createFeasibleLink(); |
| 279 | Link linkToVerify = createFeasibleLink(); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 280 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 281 | //Use the link storage API to add the link |
| 282 | linkStorage.addOrUpdateLink(linkToInsert, null, ILinkStorage.DM_OPERATION.INSERT); |
| 283 | |
| 284 | doTestLinkIsInGraph(linkToVerify); |
| 285 | |
| 286 | // Add same link |
| 287 | Link linkToInsertTwice = createFeasibleLink(); |
| 288 | linkStorage.addOrUpdateLink(linkToInsertTwice, null, ILinkStorage.DM_OPERATION.INSERT); |
| 289 | |
| 290 | // this occurs assertion failure if there are two links in titanGraph |
| 291 | doTestLinkIsInGraph(linkToVerify); |
| 292 | } |
| 293 | |
| 294 | // TODO: Check if addOrUpdateLink() should accept DELETE operation. If not, remove this test. |
| 295 | @Ignore @Test |
| 296 | public void testAddOrUpdateLink_Delete() { |
| 297 | Link linkToDelete = createExistingLink(); |
| 298 | Link linkToVerify = createExistingLink(); |
| 299 | |
| 300 | // Test deletion of existing link |
| 301 | linkStorage.addOrUpdateLink(linkToDelete, null, DM_OPERATION.DELETE); |
| 302 | doTestLinkIsNotInGraph(linkToVerify); |
| 303 | |
| 304 | linkToDelete = createFeasibleLink(); |
| 305 | linkToVerify = createFeasibleLink(); |
| 306 | |
| 307 | // Test deletion of not-existing link |
| 308 | linkStorage.addOrUpdateLink(linkToDelete, null, DM_OPERATION.DELETE); |
| 309 | doTestLinkIsNotInGraph(linkToVerify); |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | public void testGetLinks_ByDpidPort(){ |
| 314 | Link linkToVerify = createExistingLink(); |
| 315 | Long dpid = linkToVerify.getSrc(); |
| 316 | short port = (short)linkToVerify.getSrcPort(); |
| 317 | |
| 318 | List<Link> list = linkStorage.getLinks(dpid, port); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 319 | |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 320 | assertEquals(list.size(), 1); |
| 321 | |
| 322 | Link l = list.get(0); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 323 | assertEquals(l.getSrc(), linkToVerify.getSrc()); |
| 324 | assertEquals(l.getSrcPort(), linkToVerify.getSrcPort()); |
| 325 | assertEquals(l.getDst(), linkToVerify.getDst()); |
| 326 | assertEquals(l.getDstPort(), linkToVerify.getDstPort()); |
| 327 | |
| 328 | Link linkToVerifyNot = createFeasibleLink(); |
| 329 | |
| 330 | List<Link> list2 = linkStorage.getLinks(linkToVerifyNot.getSrc(), (short)linkToVerifyNot.getSrcPort()); |
| 331 | |
| 332 | assertEquals(list2.size(), 0); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 333 | } |
| 334 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 335 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 336 | public void testGetLinks_ByString() { |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 337 | Link linkToVeryfy = createExistingLink(); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 338 | String dpid = HexString.toHexString(linkToVeryfy.getSrc()); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 339 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 340 | List<Link> links = linkStorage.getLinks(dpid); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 341 | assertTrue(links.contains(linkToVeryfy)); |
| 342 | |
| 343 | Link linkToVerifyNot = createFeasibleLink(); |
| 344 | assertFalse(links.contains(linkToVerifyNot)); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 345 | } |
| 346 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 347 | @Test |
| 348 | public void testDeleteLink() { |
| 349 | // Deletion of existing link |
| 350 | Link linkToDelete = createExistingLink(); |
| 351 | Link linkToVerify = createExistingLink(); |
| 352 | |
| 353 | linkStorage.deleteLink(linkToDelete); |
| 354 | doTestLinkIsNotInGraph(linkToVerify); |
| 355 | |
| 356 | // Deletion of not existing link |
| 357 | linkToDelete = createFeasibleLink(); |
| 358 | linkToVerify = createFeasibleLink(); |
| 359 | |
| 360 | linkStorage.deleteLink(linkToDelete); |
| 361 | doTestLinkIsNotInGraph(linkToVerify); |
| 362 | } |
| 363 | |
| 364 | @Test |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 365 | public void testDeleteLinks(){ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 366 | List<Link> linksToDelete = createExistingLinks(); |
| 367 | List<Link> linksToVerify = createExistingLinks(); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 368 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 369 | linkStorage.deleteLinks(linksToDelete); |
| 370 | for(Link l : linksToVerify) { |
| 371 | doTestLinkIsNotInGraph(l); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | @Test |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 376 | public void testDeleteLinks_Mixture(){ |
| 377 | List<Link> linksToDelete = new ArrayList<Link>(); |
| 378 | linksToDelete.add(createFeasibleLink()); |
| 379 | linksToDelete.add(createExistingLink()); |
| 380 | |
| 381 | // Test deletion of mixture of new/existing links |
| 382 | linkStorage.deleteLinks(linksToDelete); |
| 383 | doTestLinkIsNotInGraph(createFeasibleLink()); |
| 384 | doTestLinkIsNotInGraph(createExistingLink()); |
| 385 | } |
| 386 | |
| 387 | @Test |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 388 | public void testGetActiveLinks() { |
| 389 | Link existingLink = createExistingLink(); |
| 390 | Link notExistingLink = createFeasibleLink(); |
| 391 | |
| 392 | List<Link> links = linkStorage.getActiveLinks(); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 393 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 394 | assertTrue(links.contains(existingLink)); |
| 395 | assertFalse(links.contains(notExistingLink)); |
| 396 | } |
| 397 | |
| 398 | @Test |
| 399 | public void testDeleteLinksOnPort() { |
| 400 | Link linkToDelete = createExistingLink(); |
| 401 | Link linkToVerify = createExistingLink(); |
| 402 | |
| 403 | linkStorage.deleteLinksOnPort(linkToDelete.getSrc(), linkToDelete.getSrcPort()); |
| 404 | |
| 405 | doTestLinkIsNotInGraph(linkToVerify); |
| 406 | } |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 407 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 408 | /** |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 409 | * Test if titanGraph has specific link |
| 410 | * @param link |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 411 | */ |
| 412 | private void doTestLinkIsInGraph(Link link) { |
| 413 | String src_dpid = HexString.toHexString(link.getSrc()); |
| 414 | String dst_dpid = HexString.toHexString(link.getDst()); |
| 415 | short src_port = link.getSrcPort(); |
| 416 | short dst_port = link.getDstPort(); |
| 417 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 418 | Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator(); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 419 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 420 | // Test if just one switch is found in the graph |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 421 | assertTrue(it.hasNext()); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 422 | Vertex sw = it.next(); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 423 | assertFalse(it.hasNext()); |
| 424 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 425 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 426 | pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 427 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 428 | // Test if just one link is found in the graph |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 429 | assertTrue(pipe.hasNext()); |
| 430 | pipe.next(); |
Jonathan Hart | c86a2ea | 2013-01-15 22:39:42 -0800 | [diff] [blame] | 431 | assertFalse(pipe.hasNext()); |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 432 | } |
| 433 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 434 | /** |
| 435 | * Test if titanGraph doesn't have specific link |
| 436 | * @param link |
| 437 | */ |
| 438 | private void doTestLinkIsNotInGraph(Link link) { |
| 439 | String src_dpid = HexString.toHexString(link.getSrc()); |
| 440 | String dst_dpid = HexString.toHexString(link.getDst()); |
| 441 | short src_port = link.getSrcPort(); |
| 442 | short dst_port = link.getDstPort(); |
| 443 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 444 | Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator(); |
| 445 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 446 | // Test if just one switch is found in the graph |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 447 | assertTrue(it.hasNext()); |
| 448 | Vertex sw = it.next(); |
| 449 | assertFalse(it.hasNext()); |
| 450 | |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 451 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 452 | pipe.start(sw).out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid); |
| 453 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 454 | // Test if no link is found in the graph |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 455 | assertFalse(pipe.hasNext()); |
| 456 | } |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 457 | |
| 458 | /** |
| 459 | * Test if titanGraph has specific Link with specific LinkInfo |
| 460 | * @param link |
| 461 | */ |
| 462 | private void doTestLinkHasStateOf(Link link, LinkInfo info) { |
| 463 | String src_dpid = HexString.toHexString(link.getSrc()); |
| 464 | String dst_dpid = HexString.toHexString(link.getDst()); |
| 465 | short src_port = link.getSrcPort(); |
| 466 | short dst_port = link.getDstPort(); |
| 467 | |
| 468 | Iterator<Vertex> it = titanGraph.getVertices("dpid", src_dpid).iterator(); |
| 469 | |
| 470 | // Test if just one switch is found in the graph |
| 471 | assertTrue(it.hasNext()); |
| 472 | Vertex sw = it.next(); |
| 473 | assertFalse(it.hasNext()); |
| 474 | |
| 475 | GremlinPipeline<Vertex, Edge> pipe = new GremlinPipeline<Vertex, Edge>(); |
| 476 | pipe.start(sw); |
| 477 | pipe.enablePath(); |
| 478 | pipe.out("on").has("number", src_port).out("link").has("number", dst_port).in("on").has("dpid", dst_dpid) |
| 479 | .path().step(new PipeFunction<PathPipe<Vertex>, Edge>() { |
| 480 | @Override |
| 481 | public Edge compute(PathPipe<Vertex> pipepath) { |
| 482 | List<Vertex> V = pipepath.next(); |
| 483 | |
| 484 | Vertex port_src = V.get(1); |
| 485 | Vertex port_dst = V.get(2); |
| 486 | |
| 487 | for(Edge e : port_src.getEdges(Direction.OUT)) { |
| 488 | if(e.getVertex(Direction.IN).equals(port_dst)) { |
| 489 | return e; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return null; |
| 494 | } |
| 495 | }); |
| 496 | |
| 497 | // Test if just one link is found in the graph |
| 498 | assertTrue(pipe.hasNext()); |
| 499 | Edge edge = pipe.next(); |
| 500 | assertTrue(edge != null); |
| 501 | assertFalse(pipe.hasNext()); |
| 502 | |
| 503 | // TODO: implement test code to check if update is correctly done. |
| 504 | int portStateSrc = edge.getVertex(Direction.OUT).getProperty("port_state"); |
| 505 | int portStateDst = edge.getVertex(Direction.IN).getProperty("port_state"); |
| 506 | |
| 507 | assertTrue(portStateSrc == info.getSrcPortState()); |
| 508 | assertTrue(portStateDst == info.getDstPortState()); |
| 509 | |
| 510 | // long firstSeenTime = edge.getProperty("first_seen_time"); |
| 511 | // long lastLldpReceivedTime = edge.getProperty("last_lldp_received_time"); |
| 512 | // long lastBddpReceivedTime = edge.getProperty("last_bddp_received_time"); |
| 513 | long firstSeenTime = edge.getVertex(Direction.OUT).getProperty("first_seen_time"); |
| 514 | long lastLldpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_lldp_received_time"); |
| 515 | long lastBddpReceivedTime = edge.getVertex(Direction.OUT).getProperty("last_bddp_received_time"); |
| 516 | assertTrue(firstSeenTime == info.getFirstSeenTime()); |
| 517 | assertTrue(lastLldpReceivedTime == info.getUnicastValidTime()); |
| 518 | assertTrue(lastBddpReceivedTime == info.getMulticastValidTime()); |
| 519 | } |
| 520 | |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 521 | |
| 522 | //----------------- Creation of test data ----------------------- |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 523 | /** |
| 524 | * Returns new Link object which has information of existing link in titanGraph |
| 525 | * @return new Link object |
| 526 | */ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 527 | private Link createExistingLink() { |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 528 | // Link from SEA switch port 2 to CHI switch port 1 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 529 | return new Link(Long.decode("0x0000000000000a01"), 2, Long.decode("0x0000000000000a03"), 1); |
| 530 | } |
| 531 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 532 | /** |
| 533 | * Returns new Link object which has information of not-existing but feasible link in titanGraph |
| 534 | * @return new Link object |
| 535 | */ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 536 | private Link createFeasibleLink() { |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 537 | // Link from SEA switch port 1 to LAX switch port 1 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 538 | return new Link(Long.decode("0x0000000000000a01"), 4, Long.decode("0x0000000000000a02"), 1); |
| 539 | } |
| 540 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 541 | // make NO sense while test-network data doesn't define physical network (i.e. any link is feasible) |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 542 | @SuppressWarnings("unused") |
| 543 | private Link createInfeasibleLink() { |
| 544 | return new Link(Long.decode("0x0000000000000a01"), 1, Long.decode("0x0000000000000a02"), 1); |
| 545 | } |
| 546 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 547 | /** |
| 548 | * Returns list of Link objects which all has information of existing link in titanGraph |
| 549 | * @return ArrayList of new Link objects |
| 550 | */ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 551 | private List<Link> createExistingLinks() { |
| 552 | List<Link> links = new ArrayList<Link>(); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 553 | // Link from SEA switch port 2 to CHI switch port 1 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 554 | links.add(new Link(Long.decode("0x0000000000000a01"), 2, Long.decode("0x0000000000000a03"), 1)); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 555 | // Link from LAX switch port 1 to SEA switch port 3 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 556 | links.add(new Link(Long.decode("0x0000000000000a02"), 1, Long.decode("0x0000000000000a01"), 3)); |
| 557 | return links; |
| 558 | } |
| 559 | |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 560 | /** |
| 561 | * Returns list of Link objects which all has information of not-existing but feasible link |
| 562 | * @return ArrayList of new Link objects |
| 563 | */ |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 564 | private List<Link> createFeasibleLinks() { |
| 565 | List<Link> links = new ArrayList<Link>(); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 566 | // Link from CHI switch port 4 to NYC switch port 3 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 567 | links.add(new Link(Long.decode("0x0000000000000a03"), 4, Long.decode("0x0000000000000a05"), 3)); |
mininet | 9d203de | 2013-06-05 08:40:45 -0700 | [diff] [blame] | 568 | // Link from SEA switch port 4 to LAX switch port 1 |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 569 | links.add(new Link(Long.decode("0x0000000000000a01"), 4, Long.decode("0x0000000000000a02"), 1)); |
| 570 | return links; |
| 571 | } |
Naoki Shiota | cae568a | 2013-06-05 17:53:41 -0700 | [diff] [blame^] | 572 | |
| 573 | /** |
| 574 | * Returns new LinkInfo object with convenient values. |
| 575 | * @return LinkInfo object |
| 576 | */ |
| 577 | private LinkInfo createFeasibleLinkInfo(long time) { |
| 578 | long time_first = time; |
| 579 | long time_last_lldp = time + 50; |
| 580 | long time_last_bddp = time + 100; |
| 581 | int state_src = OFPhysicalPort.OFPortState.OFPPS_STP_FORWARD.getValue(); |
| 582 | int state_dst = OFPhysicalPort.OFPortState.OFPPS_STP_LISTEN.getValue(); |
| 583 | |
| 584 | return new LinkInfo(time_first, |
| 585 | time_last_lldp, |
| 586 | time_last_bddp, |
| 587 | state_src, |
| 588 | state_dst); |
| 589 | } |
mininet | 403d589 | 2013-06-05 03:48:17 -0700 | [diff] [blame] | 590 | //--------------------------------------------------------------- |
Jonathan Hart | b7e3d2c | 2013-01-15 18:45:19 -0800 | [diff] [blame] | 591 | } |