blob: a911339d512cb855b7f9eeeac162670cc7f32b6f [file] [log] [blame]
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08001package net.floodlightcontroller.linkdiscovery.internal;
2
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -08003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08007import java.util.Iterator;
Jonathan Hartc86a2ea2013-01-15 22:39:42 -08008import java.util.List;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08009
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080010import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
Jonathan Hart627f10c2013-01-16 14:20:03 -080011import net.floodlightcontroller.core.internal.TestDatabaseManager;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080012import net.floodlightcontroller.linkdiscovery.ILinkStorage;
13import net.floodlightcontroller.routing.Link;
14
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080015import org.junit.After;
16import org.junit.Before;
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080017import org.junit.Ignore;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080018import org.junit.Test;
19
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080020import com.thinkaurelius.titan.core.TitanGraph;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080021import com.tinkerpop.blueprints.Vertex;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080022import com.tinkerpop.gremlin.java.GremlinPipeline;
23
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080024public class LinkStorageImplTest {
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080025 private static ILinkStorage linkStorage;
26 private static TitanGraph titanGraph;
27
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080028 //TODO Future ideas:
29 //Test add links with CREATE and UPDATE
30 //Test adding existing link again
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080031
32 @Before
Jonathan Hart627f10c2013-01-16 14:20:03 -080033 public void setUp() throws Exception{
Jonathan Hart627f10c2013-01-16 14:20:03 -080034 titanGraph = TestDatabaseManager.getTestDatabase();
35 TestDatabaseManager.populateTestData(titanGraph);
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080036
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080037 linkStorage = new TestableLinkStorageImpl(titanGraph);
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080038 }
39
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080040 @After
41 public void tearDown() throws Exception {
42 titanGraph.shutdown();
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080043 }
44
45 /*
46 * Add a link between port 1.102 and 2.104
47 * i.e SEA switch port 3 to LAX switch port 1
48 */
49 @Test
50 public void testAddSingleLink(){
51 Link linkToAdd = new Link(Long.decode("0x0000000000000a01"), 3, Long.decode("0x0000000000000a02"), 1);
52
53 //Use the link storage API to add the link
54 linkStorage.update(linkToAdd, ILinkStorage.DM_OPERATION.INSERT);
55
56 //Test if it was added correctly with the Gremlin API
57 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
58 Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
59
60 assertTrue(it.hasNext());
61 Vertex sw1 = it.next();
62 assertFalse(it.hasNext());
63
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080064 pipe.start(sw1).out("on").has("number", (short)3).out("link").in("on");
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080065
66 assertTrue(pipe.hasNext());
67 Vertex sw2 = pipe.next();
68 assertFalse(pipe.hasNext());
69
70 //Check we ended up at the right vertex
71 assertEquals((String)sw2.getProperty("dpid"), "00:00:00:00:00:00:0a:02");
72 }
73
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080074 //TODO enable once method is written
75 @Ignore @Test
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080076 public void testGetLinks(){
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080077 List<Link> list = linkStorage.getLinks(Long.decode("0x0000000000000a01"), 2);
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080078
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080079 assertEquals(list.size(), 1);
80
81 Link l = list.get(0);
82 assertEquals(l.getSrc(), 2561L);
83 assertEquals(l.getSrcPort(), (short)2);
84 assertEquals(l.getDst(), 2563L);
85 assertEquals(l.getDstPort(), (short)1);
86 }
87
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -080088 //TODO enable once method is written
89 @Ignore @Test
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080090 public void testUpdateDelete(){
Jonathan Hartc86a2ea2013-01-15 22:39:42 -080091 Link linkToDelete = new Link(Long.decode("0x0000000000000a01"), 2, Long.decode("0x0000000000000a03"), 1);
92
93 linkStorage.update(linkToDelete, DM_OPERATION.DELETE);
94
95 //Test if it was deleted correctly with the Gremlin API
96 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
97 Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
98
99 assertTrue(it.hasNext());
100 Vertex sw1 = it.next();
101 assertFalse(it.hasNext());
102
103 pipe.start(sw1).out("on").has("number", 2).out("link");
104
105 assertFalse(pipe.hasNext());
106 }
107
Jonathan Hartd1c5a1c2013-01-23 00:56:14 -0800108 //TODO enable once method is written
109 @Ignore @Test
Jonathan Hartc86a2ea2013-01-15 22:39:42 -0800110 public void testDeleteLinks(){
Jonathan Hartc86a2ea2013-01-15 22:39:42 -0800111 linkStorage.deleteLinks(Long.decode("0x0000000000000a01"), 2);
112
113 //Test if it was deleted correctly with the Gremlin API
114 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
115 Iterator<Vertex> it = titanGraph.getVertices("dpid", "00:00:00:00:00:00:0a:01").iterator();
116
117 assertTrue(it.hasNext());
118 Vertex sw1 = it.next();
119 assertFalse(it.hasNext());
120
121 pipe.start(sw1).out("on").has("number", 2).out("link");
122
123 assertFalse(pipe.hasNext());
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -0800124 }
125
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -0800126}