Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.core.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 | import static org.junit.Assert.fail; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 8 | import java.util.ArrayList; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 9 | import java.util.Iterator; |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 10 | import java.util.List; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 11 | |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.ISwitchStorage; |
| 13 | import net.floodlightcontroller.core.ISwitchStorage.SwitchState; |
| 14 | |
| 15 | import org.junit.After; |
| 16 | import org.junit.Before; |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 17 | import org.junit.Ignore; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 18 | import org.junit.Test; |
| 19 | import org.openflow.protocol.OFPhysicalPort; |
| 20 | |
| 21 | import com.thinkaurelius.titan.core.TitanGraph; |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 22 | import com.tinkerpop.blueprints.Direction; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 23 | import com.tinkerpop.blueprints.Vertex; |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 24 | import com.tinkerpop.gremlin.groovy.Gremlin; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 25 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 26 | import com.tinkerpop.pipes.Pipe; |
| 27 | import com.tinkerpop.pipes.PipeFunction; |
| 28 | import com.tinkerpop.pipes.branch.LoopPipe; |
| 29 | import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle; |
| 30 | import com.tinkerpop.pipes.filter.FilterPipe.Filter; |
| 31 | import com.tinkerpop.pipes.util.PipesFluentPipeline; |
| 32 | |
| 33 | import javax.script.ScriptContext; |
| 34 | import javax.script.ScriptEngine; |
| 35 | import javax.script.ScriptException; |
| 36 | import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine; |
| 37 | |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 38 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 39 | public class SwitchStorageImplTest { |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 40 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 41 | private ISwitchStorage switchStorage; |
| 42 | private TitanGraph titanGraph; |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 43 | |
| 44 | @Before |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 45 | public void setUp() throws Exception { |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 46 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 47 | TestDatabaseManager.populateTestData(titanGraph); |
| 48 | |
| 49 | switchStorage = new TestableSwitchStorageImpl(titanGraph); |
| 50 | } |
| 51 | |
| 52 | @After |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 53 | public void tearDown() throws Exception { |
| 54 | titanGraph.shutdown(); |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 57 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 58 | public void testUpdate() { |
| 59 | fail("Not yet implemented"); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testAddPort() { |
| 64 | |
| 65 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 66 | short portNumber = 5; |
| 67 | |
| 68 | OFPhysicalPort portToAdd = new OFPhysicalPort(); |
| 69 | portToAdd.setName("port 5 at SEA switch"); |
| 70 | portToAdd.setCurrentFeatures(OFPhysicalPort.OFPortFeatures.OFPPF_100MB_FD.getValue()); |
| 71 | portToAdd.setPortNumber(portNumber); |
| 72 | |
| 73 | switchStorage.addPort(dpid, portToAdd); |
| 74 | |
| 75 | Vertex sw = titanGraph.getVertices("dpid", dpid).iterator().next(); |
| 76 | |
| 77 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 78 | pipe.start(sw).out("on").has("number", portNumber); |
| 79 | |
| 80 | assertTrue(pipe.hasNext()); |
| 81 | Vertex addedPort = pipe.next(); |
| 82 | assertFalse(pipe.hasNext()); |
| 83 | |
| 84 | assertEquals(addedPort.getProperty("number"), portNumber); |
| 85 | } |
| 86 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 87 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 88 | public void testGetPorts() { |
| 89 | fail("Not yet implemented"); |
| 90 | } |
| 91 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 92 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 93 | public void testGetPortStringShort() { |
| 94 | fail("Not yet implemented"); |
| 95 | } |
| 96 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 97 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 98 | public void testGetPortStringString() { |
| 99 | fail("Not yet implemented"); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testAddSwitch() { |
| 104 | String dpid = "00:00:00:00:00:00:0a:07"; |
| 105 | |
| 106 | switchStorage.addSwitch(dpid); |
| 107 | |
| 108 | Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator(); |
| 109 | assertTrue(it.hasNext()); |
| 110 | Vertex addedSwitch = it.next(); |
| 111 | assertFalse(it.hasNext()); |
| 112 | |
| 113 | assertEquals(addedSwitch.getProperty("type"), "switch"); |
| 114 | assertEquals(addedSwitch.getProperty("dpid"), dpid); |
| 115 | assertEquals(addedSwitch.getProperty("state"), SwitchState.ACTIVE.toString()); |
| 116 | } |
| 117 | |
| 118 | |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 119 | @Test |
| 120 | public void testDeleteSwitch() { |
| 121 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 122 | |
| 123 | switchStorage.deleteSwitch(dpid); |
| 124 | |
| 125 | Iterator<Vertex> it = titanGraph.getVertices("dpid", dpid).iterator(); |
| 126 | assertFalse(it.hasNext()); |
| 127 | } |
| 128 | |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 129 | @Test |
| 130 | public void testDeletePortByPortNum() { |
| 131 | //FIXME fails because query for the port is wrong in SwitchStorageImpl |
| 132 | |
| 133 | String dpid = "00:00:00:00:00:00:0a:01"; |
| 134 | short portNum = 3; |
| 135 | |
| 136 | switchStorage.deletePort(dpid, portNum); |
| 137 | |
| 138 | Vertex sw = titanGraph.getVertices("dpid", dpid).iterator().next(); |
| 139 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 140 | /* |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 141 | Iterator<Vertex> it = sw.getVertices(Direction.OUT, "on").iterator(); |
| 142 | |
| 143 | while (it.hasNext()){ |
| 144 | System.out.println(it.next()); |
| 145 | } |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 146 | */ |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 147 | |
| 148 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 149 | pipe.start(sw).out("on").has("number", portNum); |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 150 | assertFalse(pipe.hasNext()); |
| 151 | } |
| 152 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 153 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 154 | public void testDeletePortStringString() { |
| 155 | fail("Not yet implemented"); |
| 156 | } |
| 157 | |
Jonathan Hart | d1c5a1c | 2013-01-23 00:56:14 -0800 | [diff] [blame] | 158 | @Ignore @Test |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 159 | public void testGetActiveSwitches() { |
| 160 | fail("Not yet implemented"); |
| 161 | } |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame^] | 162 | |
| 163 | static class MyLoopFunction implements PipeFunction<LoopBundle<Vertex>, Boolean> { |
| 164 | String dpid; |
| 165 | public MyLoopFunction(String dpid) { |
| 166 | super(); |
| 167 | this.dpid = dpid; |
| 168 | } |
| 169 | public Boolean compute(LoopBundle<Vertex> bundle) { |
| 170 | Boolean output = false; |
| 171 | if ((bundle.getObject().getProperty("dpid") != dpid) && |
| 172 | (bundle.getLoops() < 10)) { |
| 173 | output = true; |
| 174 | } |
| 175 | return output; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testShortestPath() { |
| 181 | String dpid_src = "00:00:00:00:00:00:0a:01"; |
| 182 | String dpid_dest = "00:00:00:00:00:00:0a:06"; |
| 183 | |
| 184 | // |
| 185 | // Implement the Shortest Path between two vertices by using |
| 186 | // the following Gremlin code: |
| 187 | // results = []; v_src.as('x').out.out.in.has("type", "switch").dedup().loop('x'){it.object.dpid != v_dest.dpid & it.loops < 10}.path().fill(results) |
| 188 | // |
| 189 | |
| 190 | String gremlin = "v_src.as(\"x\").out.out.in.has(\"type\", \"switch\").dedup().loop(\"x\"){it.object.dpid != v_dest.dpid & it.loops < 10}.path().fill(results)"; |
| 191 | |
| 192 | // Get the source vertex |
| 193 | Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator(); |
| 194 | if (! iter.hasNext()) |
| 195 | return; // Source vertex not found |
| 196 | Vertex v_src = iter.next(); |
| 197 | |
| 198 | // Get the destination vertex |
| 199 | iter = titanGraph.getVertices("dpid", dpid_dest).iterator(); |
| 200 | if (! iter.hasNext()) |
| 201 | return; // Destination vertex not found |
| 202 | Vertex v_dest = iter.next(); |
| 203 | |
| 204 | // |
| 205 | // Implement the Gremlin script and run it |
| 206 | // |
| 207 | ScriptEngine engine = new GremlinGroovyScriptEngine(); |
| 208 | |
| 209 | ArrayList<ArrayList<Vertex>> results = new ArrayList<ArrayList<Vertex>>(); |
| 210 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", titanGraph); |
| 211 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_src", v_src); |
| 212 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_dest", v_dest); |
| 213 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("results", results); |
| 214 | |
| 215 | try { |
| 216 | engine.eval(gremlin); |
| 217 | } catch (ScriptException e) { |
| 218 | System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage()); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // |
| 223 | // Extract the result and compose it into a string |
| 224 | // |
| 225 | String results_str = ""; |
| 226 | // System.out.println("BEGIN " + results.size()); |
| 227 | for (ArrayList<Vertex> lv : results) { |
| 228 | // System.out.println(lv); |
| 229 | for (Vertex v: lv) { |
| 230 | // System.out.println(v); |
| 231 | String type = v.getProperty("type").toString(); |
| 232 | results_str += "[type: " + type; |
| 233 | // System.out.println("type: " + type); |
| 234 | if (type.equals("port")) { |
| 235 | String number = v.getProperty("number").toString(); |
| 236 | // System.out.println("number: " + number); |
| 237 | results_str += " number: " + number + "]"; |
| 238 | } |
| 239 | if (type.equals("switch")) { |
| 240 | String dpid = v.getProperty("dpid").toString(); |
| 241 | // System.out.println("dpid: " + dpid); |
| 242 | results_str += " dpid: " + dpid + "]"; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | // System.out.println("END\n"); |
| 247 | System.out.println(results_str); |
| 248 | |
| 249 | String expected_result = "[type: switch dpid: 00:00:00:00:00:00:0a:01][type: port number: 2][type: port number: 1][type: switch dpid: 00:00:00:00:00:00:0a:03][type: port number: 2][type: port number: 2][type: switch dpid: 00:00:00:00:00:00:0a:04][type: port number: 3][type: port number: 1][type: switch dpid: 00:00:00:00:00:00:0a:06]"; |
| 250 | |
| 251 | |
| 252 | // Pipe<Vertex, Vertex> pipe = Gremlin.compile(gremlin); |
| 253 | // pipe.setStarts(new SingleIterator<Vertex>(v1)); |
| 254 | |
| 255 | // |
| 256 | // XXX: An alternative (faster?) solution that fails to compile |
| 257 | // |
| 258 | MyLoopFunction whileFunction = new MyLoopFunction(dpid_dest); |
| 259 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 260 | // pipe.start(v_src).as("x").out().out().in().has("type", "switch").dedup().loop("x", whileFunction); |
| 261 | |
| 262 | // Check the result |
| 263 | assertEquals(results_str, expected_result); |
| 264 | } |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 265 | } |