Pavlin Radoslavov | 1a56edd | 2013-06-21 13:30:23 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.ofcontroller.routing; |
| 2 | |
| 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; |
| 7 | |
| 8 | import java.util.Collection; |
| 9 | import java.util.List; |
| 10 | import java.util.ArrayList; |
| 11 | import java.util.Iterator; |
| 12 | |
| 13 | import org.junit.After; |
| 14 | import org.junit.Before; |
| 15 | import org.junit.Ignore; |
| 16 | import org.junit.Test; |
| 17 | |
| 18 | import com.thinkaurelius.titan.core.TitanGraph; |
| 19 | import com.tinkerpop.blueprints.Vertex; |
| 20 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 21 | import com.tinkerpop.pipes.PipeFunction; |
| 22 | import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle; |
| 23 | |
| 24 | import javax.script.ScriptContext; |
| 25 | import javax.script.ScriptEngine; |
| 26 | import javax.script.ScriptException; |
| 27 | import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * A class for testing the TopoRouteService class. |
| 32 | * @see net.onrc.onos.ofcontroller.routing.TopoRouteService |
| 33 | * @author Pavlin Radoslavov (pavlin@onlab.us) |
| 34 | */ |
| 35 | public class TopoRouteServiceTest { |
| 36 | |
| 37 | private TitanGraph titanGraph; |
| 38 | |
| 39 | @Before |
| 40 | public void setUp() throws Exception { |
| 41 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 42 | TestDatabaseManager.populateTestData(titanGraph); |
| 43 | } |
| 44 | |
| 45 | @After |
| 46 | public void tearDown() throws Exception { |
| 47 | titanGraph.shutdown(); |
| 48 | } |
| 49 | |
| 50 | @Ignore @Test |
| 51 | public void testUpdate() { |
| 52 | fail("Not yet implemented"); |
| 53 | } |
| 54 | |
| 55 | static class MyLoopFunction implements PipeFunction<LoopBundle<Vertex>, Boolean> { |
| 56 | String dpid; |
| 57 | public MyLoopFunction(String dpid) { |
| 58 | super(); |
| 59 | this.dpid = dpid; |
| 60 | } |
| 61 | public Boolean compute(LoopBundle<Vertex> bundle) { |
| 62 | Boolean output = false; |
| 63 | if (! bundle.getObject().getProperty("dpid").equals(dpid)) { |
| 64 | output = true; |
| 65 | } |
| 66 | return output; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testShortestPath() { |
| 72 | String dpid_src = "00:00:00:00:00:00:0a:01"; |
| 73 | String dpid_dest = "00:00:00:00:00:00:0a:06"; |
| 74 | |
| 75 | // |
| 76 | // Implement the Shortest Path between two vertices by using |
| 77 | // the following Gremlin CLI code: |
| 78 | // 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} |
| 79 | // The equivalent code used here is: |
| 80 | // results = []; v_src.as("x").out("on").out("link").in("on").dedup().loop("x"){it.object.dpid != v_dest.dpid}.path().fill(results) |
| 81 | // |
| 82 | |
| 83 | // Get the source vertex |
| 84 | Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator(); |
| 85 | if (! iter.hasNext()) |
| 86 | return; // Source vertex not found |
| 87 | Vertex v_src = iter.next(); |
| 88 | |
| 89 | // Get the destination vertex |
| 90 | iter = titanGraph.getVertices("dpid", dpid_dest).iterator(); |
| 91 | if (! iter.hasNext()) |
| 92 | return; // Destination vertex not found |
| 93 | Vertex v_dest = iter.next(); |
| 94 | |
| 95 | // |
| 96 | // Implement the Gremlin script and run it |
| 97 | // |
| 98 | // NOTE: This mechanism is slower. The code is kept here |
| 99 | // for future reference. |
| 100 | // |
| 101 | /* |
| 102 | String gremlin = "v_src.as(\"x\").out(\"on\").out(\"link\").in(\"on\").dedup().loop(\"x\"){it.object.dpid != v_dest.dpid}.path().fill(results)"; |
| 103 | |
| 104 | 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)"; |
| 105 | |
| 106 | ScriptEngine engine = new GremlinGroovyScriptEngine(); |
| 107 | ArrayList<ArrayList<Vertex>> results = new ArrayList<ArrayList<Vertex>>(); |
| 108 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", titanGraph); |
| 109 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_src", v_src); |
| 110 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_dest", v_dest); |
| 111 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("results", results); |
| 112 | |
| 113 | try { |
| 114 | engine.eval(gremlin); |
| 115 | } catch (ScriptException e) { |
| 116 | System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage()); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | for (ArrayList<Vertex> lv : results) { |
| 121 | ... |
| 122 | } |
| 123 | */ |
| 124 | |
| 125 | MyLoopFunction whileFunction = new MyLoopFunction(dpid_dest); |
| 126 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 127 | Collection<List> results = new ArrayList<List>(); |
| 128 | GremlinPipeline<Vertex, List> path; |
| 129 | path = pipe.start(v_src).as("x").out("on").out("link").in("on").dedup().loop("x", whileFunction).path(); |
| 130 | path.fill(results); |
| 131 | |
| 132 | // |
| 133 | // Extract the result and compose it into a string |
| 134 | // |
| 135 | String results_str = ""; |
| 136 | // System.out.println("BEGIN " + results.size()); |
| 137 | for (List l : results) { |
| 138 | for (Object o: l) { |
| 139 | Vertex v = (Vertex)(o); |
| 140 | // System.out.println(v); |
| 141 | String type = v.getProperty("type").toString(); |
| 142 | results_str += "[type: " + type; |
| 143 | // System.out.println("type: " + type); |
| 144 | if (type.equals("port")) { |
| 145 | String number = v.getProperty("number").toString(); |
| 146 | // System.out.println("number: " + number); |
| 147 | results_str += " number: " + number + "]"; |
| 148 | } |
| 149 | if (type.equals("switch")) { |
| 150 | String dpid = v.getProperty("dpid").toString(); |
| 151 | // System.out.println("dpid: " + dpid); |
| 152 | results_str += " dpid: " + dpid + "]"; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | // System.out.println("END\n"); |
| 157 | System.out.println(results_str); |
| 158 | |
| 159 | // |
| 160 | // Check the result |
| 161 | // |
| 162 | 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]"; |
| 163 | |
| 164 | assertEquals(results_str, expected_result); |
| 165 | |
| 166 | // |
| 167 | // Test Shortest-Path computation to non-existing destination |
| 168 | // |
| 169 | results.clear(); |
| 170 | MyLoopFunction noDestWhileFunction = new MyLoopFunction("NO-SUCH-DPID"); |
| 171 | path = pipe.start(v_src).as("x").out("on").out("link").in("on").dedup().loop("x", noDestWhileFunction).path(); |
| 172 | path.fill(results); |
| 173 | assertTrue(results.size() == 0); |
| 174 | } |
| 175 | } |