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; |
Pavlin Radoslavov | 544d313 | 2013-02-13 10:16:52 -0800 | [diff] [blame^] | 171 | if (bundle.getObject().getProperty("dpid") != dpid) { |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame] | 172 | output = true; |
| 173 | } |
| 174 | return output; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void testShortestPath() { |
| 180 | String dpid_src = "00:00:00:00:00:00:0a:01"; |
| 181 | String dpid_dest = "00:00:00:00:00:00:0a:06"; |
| 182 | |
| 183 | // |
| 184 | // Implement the Shortest Path between two vertices by using |
Pavlin Radoslavov | 544d313 | 2013-02-13 10:16:52 -0800 | [diff] [blame^] | 185 | // the following Gremlin CLI code: |
| 186 | // v_src.as("x").out("on").out("link").in("on").dedup().loop("x"){it.object.dpid != v_dest.dpid}.path(){it.dpid}{it.number}{it.number} |
| 187 | // The equivalent code used here is: |
| 188 | // results = []; v_src.as("x").out("on").out("link").in("on").dedup().loop("x"){it.object.dpid != v_dest.dpid}.path().fill(results) |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame] | 189 | // |
| 190 | |
Pavlin Radoslavov | 544d313 | 2013-02-13 10:16:52 -0800 | [diff] [blame^] | 191 | String gremlin = "v_src.as(\"x\").out(\"on\").out(\"link\").in(\"on\").dedup().loop(\"x\"){it.object.dpid != v_dest.dpid}.path().fill(results)"; |
| 192 | |
| 193 | String gremlin_nopath = "v_src.as(\"x\").out(\"on\").out(\"link\").in(\"on\").dedup().loop(\"x\"){it.object.dpid != \"NO-SUCH-DPID\"}.path().fill(results)"; |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame] | 194 | |
| 195 | // Get the source vertex |
| 196 | Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator(); |
| 197 | if (! iter.hasNext()) |
| 198 | return; // Source vertex not found |
| 199 | Vertex v_src = iter.next(); |
| 200 | |
| 201 | // Get the destination vertex |
| 202 | iter = titanGraph.getVertices("dpid", dpid_dest).iterator(); |
| 203 | if (! iter.hasNext()) |
| 204 | return; // Destination vertex not found |
| 205 | Vertex v_dest = iter.next(); |
| 206 | |
| 207 | // |
| 208 | // Implement the Gremlin script and run it |
| 209 | // |
| 210 | ScriptEngine engine = new GremlinGroovyScriptEngine(); |
| 211 | |
| 212 | ArrayList<ArrayList<Vertex>> results = new ArrayList<ArrayList<Vertex>>(); |
| 213 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", titanGraph); |
| 214 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_src", v_src); |
| 215 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_dest", v_dest); |
| 216 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("results", results); |
| 217 | |
| 218 | try { |
| 219 | engine.eval(gremlin); |
| 220 | } catch (ScriptException e) { |
| 221 | System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage()); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // |
| 226 | // Extract the result and compose it into a string |
| 227 | // |
| 228 | String results_str = ""; |
| 229 | // System.out.println("BEGIN " + results.size()); |
| 230 | for (ArrayList<Vertex> lv : results) { |
| 231 | // System.out.println(lv); |
| 232 | for (Vertex v: lv) { |
| 233 | // System.out.println(v); |
| 234 | String type = v.getProperty("type").toString(); |
| 235 | results_str += "[type: " + type; |
| 236 | // System.out.println("type: " + type); |
| 237 | if (type.equals("port")) { |
| 238 | String number = v.getProperty("number").toString(); |
| 239 | // System.out.println("number: " + number); |
| 240 | results_str += " number: " + number + "]"; |
| 241 | } |
| 242 | if (type.equals("switch")) { |
| 243 | String dpid = v.getProperty("dpid").toString(); |
| 244 | // System.out.println("dpid: " + dpid); |
| 245 | results_str += " dpid: " + dpid + "]"; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | // System.out.println("END\n"); |
| 250 | System.out.println(results_str); |
| 251 | |
| 252 | 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]"; |
| 253 | |
| 254 | |
| 255 | // Pipe<Vertex, Vertex> pipe = Gremlin.compile(gremlin); |
| 256 | // pipe.setStarts(new SingleIterator<Vertex>(v1)); |
| 257 | |
| 258 | // |
| 259 | // XXX: An alternative (faster?) solution that fails to compile |
| 260 | // |
| 261 | MyLoopFunction whileFunction = new MyLoopFunction(dpid_dest); |
| 262 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
Pavlin Radoslavov | 544d313 | 2013-02-13 10:16:52 -0800 | [diff] [blame^] | 263 | ArrayList<ArrayList<Vertex>> results2 = new ArrayList<ArrayList<Vertex>>(); |
| 264 | // TODO: The statement below doesn't compile |
| 265 | // pipe.start(v_src).as("x").out("on").out("link").in("on").dedup().loop("x", whileFunction).path().fill(results2); |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame] | 266 | |
| 267 | // Check the result |
| 268 | assertEquals(results_str, expected_result); |
Pavlin Radoslavov | 544d313 | 2013-02-13 10:16:52 -0800 | [diff] [blame^] | 269 | |
| 270 | // |
| 271 | // Test Shortest-Path computation to non-existing destination |
| 272 | // |
| 273 | results.clear(); |
| 274 | try { |
| 275 | engine.eval(gremlin_nopath); |
| 276 | } catch (ScriptException e) { |
| 277 | System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage()); |
| 278 | return; |
| 279 | } |
| 280 | assertTrue(results.size() == 0); |
Pavlin Radoslavov | 5185fac | 2013-01-29 16:40:10 -0800 | [diff] [blame] | 281 | } |
Jonathan Hart | 627f10c | 2013-01-16 14:20:03 -0800 | [diff] [blame] | 282 | } |