Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.routing; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import net.floodlightcontroller.core.internal.SwitchStorageImpl; |
| 8 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
| 9 | import net.floodlightcontroller.topology.NodePortTuple; |
| 10 | |
| 11 | import org.openflow.util.HexString; |
| 12 | |
| 13 | import com.thinkaurelius.titan.core.TitanFactory; |
| 14 | import com.thinkaurelius.titan.core.TitanGraph; |
| 15 | // import com.tinkerpop.blueprints.Direction; |
| 16 | import com.tinkerpop.blueprints.Vertex; |
| 17 | // import com.tinkerpop.gremlin.groovy.Gremlin; |
| 18 | // import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 19 | // import com.tinkerpop.pipes.Pipe; |
| 20 | // import com.tinkerpop.pipes.PipeFunction; |
| 21 | // import com.tinkerpop.pipes.branch.LoopPipe; |
| 22 | // import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle; |
| 23 | // import com.tinkerpop.pipes.filter.FilterPipe.Filter; |
| 24 | // import com.tinkerpop.pipes.util.PipesFluentPipeline; |
| 25 | |
| 26 | import com.tinkerpop.blueprints.Element; |
| 27 | |
| 28 | import javax.script.ScriptContext; |
| 29 | import javax.script.ScriptEngine; |
| 30 | import javax.script.ScriptException; |
| 31 | import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine; |
| 32 | |
| 33 | public class TopoRouteService implements ITopoRouteService { |
| 34 | |
| 35 | ThreadLocal<SwitchStorageImpl> store = new ThreadLocal<SwitchStorageImpl>() { |
| 36 | @Override |
| 37 | protected SwitchStorageImpl initialValue() { |
| 38 | SwitchStorageImpl swStore = new SwitchStorageImpl(); |
| 39 | // NOTE: This is the file path from global properties |
| 40 | swStore.init("/tmp/cassandra.titan"); |
| 41 | return swStore; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | SwitchStorageImpl swStore = store.get(); |
| 46 | |
| 47 | @Override |
Pavlin Radoslavov | e9686a3 | 2013-01-29 16:50:03 -0800 | [diff] [blame] | 48 | public List<NodePortTuple> GetShortestPath(NodePortTuple src, |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 49 | NodePortTuple dest) { |
| 50 | List<NodePortTuple> result_list = new ArrayList<NodePortTuple>(); |
| 51 | |
| 52 | TitanGraph titanGraph = swStore.graph; |
| 53 | |
| 54 | String dpid_src = HexString.toHexString(src.getNodeId()); |
| 55 | String dpid_dest = HexString.toHexString(dest.getNodeId()); |
| 56 | |
| 57 | // |
| 58 | // Implement the Shortest Path between two vertices by using |
Pavlin Radoslavov | 5095197 | 2013-02-13 10:14:08 -0800 | [diff] [blame^] | 59 | // the following Gremlin CLI code: |
| 60 | // 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} |
| 61 | // The equivalent code used here is: |
| 62 | // 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 | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 63 | // |
| 64 | |
Ubuntu | d857765 | 2013-02-08 18:54:46 +0000 | [diff] [blame] | 65 | String gremlin = "v_src.as(\"x\").out(\"on\").out(\"link\").in(\"on\").dedup().loop(\"x\"){it.object.dpid != v_dest.dpid}.path().fill(results)"; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 66 | |
| 67 | // Get the source vertex |
| 68 | Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator(); |
| 69 | if (! iter.hasNext()) |
| 70 | return null; // Source vertex not found |
| 71 | Vertex v_src = iter.next(); |
| 72 | |
| 73 | // Get the destination vertex |
| 74 | iter = titanGraph.getVertices("dpid", dpid_dest).iterator(); |
| 75 | if (! iter.hasNext()) |
| 76 | return null; // Destination vertex not found |
| 77 | Vertex v_dest = iter.next(); |
| 78 | |
| 79 | // |
| 80 | // Implement the Gremlin script and run it |
| 81 | // |
| 82 | ScriptEngine engine = new GremlinGroovyScriptEngine(); |
| 83 | |
| 84 | ArrayList<ArrayList<Vertex>> results = new ArrayList<ArrayList<Vertex>>(); |
| 85 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", titanGraph); |
| 86 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_src", v_src); |
| 87 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_dest", v_dest); |
| 88 | engine.getBindings(ScriptContext.ENGINE_SCOPE).put("results", results); |
| 89 | |
| 90 | try { |
| 91 | engine.eval(gremlin); |
| 92 | } catch (ScriptException e) { |
| 93 | System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage()); |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | // |
| 98 | // Loop through the result and return the list |
| 99 | // of <dpid, port> tuples. |
| 100 | // |
| 101 | long nodeId = 0; |
| 102 | short portId = 0; |
| 103 | for (ArrayList<Vertex> lv : results) { |
| 104 | int idx = 0; |
| 105 | for (Vertex v: lv) { |
| 106 | String type = v.getProperty("type").toString(); |
| 107 | System.out.println("type: " + type); |
| 108 | if (type.equals("port")) { |
| 109 | String number = v.getProperty("number").toString(); |
| 110 | System.out.println("number: " + number); |
| 111 | |
| 112 | Object obj = v.getProperty("number"); |
| 113 | // String class_str = obj.getClass().toString(); |
| 114 | if (obj instanceof Short) { |
| 115 | portId = (Short)obj; |
| 116 | } else if (obj instanceof Integer) { |
| 117 | Integer int_nodeId = (Integer)obj; |
| 118 | portId = int_nodeId.shortValue(); |
| 119 | // int int_nodeId = (Integer)obj; |
| 120 | // portId = (short)int_nodeId.; |
| 121 | } |
| 122 | } else if (type.equals("switch")) { |
| 123 | String dpid = v.getProperty("dpid").toString(); |
| 124 | nodeId = HexString.toLong(dpid); |
| 125 | |
| 126 | System.out.println("dpid: " + dpid); |
| 127 | } |
| 128 | if (idx == 0) { |
| 129 | idx++; |
| 130 | continue; |
| 131 | } |
| 132 | int mod = (idx - 1) % 3; |
| 133 | if ((mod == 0) || (mod == 2)) { |
| 134 | result_list.add(new NodePortTuple(nodeId, portId)); |
| 135 | } |
| 136 | idx++; |
| 137 | } |
| 138 | } |
| 139 | if (result_list.size() > 0) |
| 140 | return result_list; |
| 141 | |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Boolean RouteExists(NodePortTuple src, NodePortTuple dest) { |
Pavlin Radoslavov | e9686a3 | 2013-01-29 16:50:03 -0800 | [diff] [blame] | 147 | List<NodePortTuple> route = GetShortestPath(src, dest); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 148 | if (route != null) |
| 149 | return true; |
| 150 | return false; |
| 151 | } |
| 152 | } |