Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.internal; |
| 2 | |
| 3 | |
| 4 | import java.util.Iterator; |
| 5 | |
| 6 | import junit.framework.TestCase; |
| 7 | import net.floodlightcontroller.core.ISwitchStorage; |
| 8 | import net.floodlightcontroller.core.ISwitchStorage.SwitchState; |
| 9 | |
| 10 | import org.junit.After; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.Test; |
| 13 | import org.openflow.protocol.OFPhysicalPort; |
| 14 | |
| 15 | import com.thinkaurelius.titan.core.TitanGraph; |
| 16 | import com.tinkerpop.blueprints.Direction; |
| 17 | import com.tinkerpop.blueprints.Vertex; |
| 18 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 19 | |
| 20 | public class SwitchStorageImplTest extends TestCase { |
| 21 | |
| 22 | private static ISwitchStorage switchStorage; |
| 23 | private static TitanGraph titanGraph; |
| 24 | |
| 25 | @Before |
| 26 | protected void setUp() throws Exception { |
| 27 | super.setUp(); |
| 28 | |
| 29 | TestDatabaseManager.deleteTestDatabase(); |
| 30 | |
| 31 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 32 | TestDatabaseManager.populateTestData(titanGraph); |
| 33 | |
| 34 | switchStorage = new TestableSwitchStorageImpl(titanGraph); |
| 35 | } |
| 36 | |
| 37 | @After |
| 38 | protected void tearDown() throws Exception { |
| 39 | super.tearDown(); |
| 40 | //TODO reenable once test debugging is finished |
| 41 | TestDatabaseManager.deleteTestDatabase(); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testUpdate() { |
| 46 | fail("Not yet implemented"); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testAddPort() { |
| 51 | |
| 52 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 53 | short portNumber = 5; |
| 54 | |
| 55 | OFPhysicalPort portToAdd = new OFPhysicalPort(); |
| 56 | portToAdd.setName("port 5 at SEA switch"); |
| 57 | portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue()); |
| 58 | portToAdd.setPortNumber(portNumber); |
| 59 | |
| 60 | switchStorage.addPort(dpid, portToAdd); |
| 61 | |
| 62 | Vertex sw = titanGraph.getVertices("dpid", dpid).iterator().next(); |
| 63 | |
| 64 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 65 | pipe.start(sw).out("on").has("number", portNumber); |
| 66 | |
| 67 | assertTrue(pipe.hasNext()); |
| 68 | Vertex addedPort = pipe.next(); |
| 69 | assertFalse(pipe.hasNext()); |
| 70 | |
| 71 | assertEquals(addedPort.getProperty("number"), portNumber); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testGetPorts() { |
| 76 | fail("Not yet implemented"); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testGetPortStringShort() { |
| 81 | fail("Not yet implemented"); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testGetPortStringString() { |
| 86 | fail("Not yet implemented"); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testAddSwitch() { |
| 91 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 92 | |
| 93 | switchStorage.addSwitch(dpid); |
| 94 | |
| 95 | Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator(); |
| 96 | assertTrue(it.hasNext()); |
| 97 | Vertex addedSwitch = it.next(); |
| 98 | assertFalse(it.hasNext()); |
| 99 | |
| 100 | assertEquals(addedSwitch.getProperty("type"), "switch"); |
| 101 | assertEquals(addedSwitch.getProperty("dpid"), dpid); |
| 102 | assertEquals(addedSwitch.getProperty("state"), SwitchState.ACTIVE.toString()); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | //FIXME something causes this test to fail when run in the suite. Probably something |
| 107 | //to do with not properly refreshing our DB connection for each test |
| 108 | @Test |
| 109 | public void testDeleteSwitch() { |
| 110 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 111 | |
| 112 | switchStorage.deleteSwitch(dpid); |
| 113 | |
| 114 | Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator(); |
| 115 | assertFalse(it.hasNext()); |
| 116 | } |
| 117 | |
| 118 | //TODO there's an issue with the datatypes for things link port numbers. |
| 119 | //There should be a standard type in the DB for everything, however there |
| 120 | //are discrepancies for example when I read data in from a file port numbers |
| 121 | //are ints but if they're put in by the API they're shorts. |
| 122 | |
| 123 | @Test |
| 124 | public void testDeletePortByPortNum() { |
| 125 | //FIXME fails because query for the port is wrong in SwitchStorageImpl |
| 126 | |
| 127 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 128 | short portNum = 3; |
| 129 | |
| 130 | switchStorage.deletePort(dpid, portNum); |
| 131 | |
| 132 | Vertex sw = titanGraph.getVertices("dpid", dpid).iterator().next(); |
| 133 | |
| 134 | Iterator<Vertex> it = sw.getVertices(Direction.OUT, "on").iterator(); |
| 135 | |
| 136 | while (it.hasNext()){ |
| 137 | System.out.println(it.next()); |
| 138 | } |
| 139 | |
| 140 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 141 | pipe.start(sw).out("on").has("number", (int)portNum); |
| 142 | assertFalse(pipe.hasNext()); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testDeletePortStringString() { |
| 147 | fail("Not yet implemented"); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testGetActiveSwitches() { |
| 152 | fail("Not yet implemented"); |
| 153 | } |
| 154 | |
| 155 | } |