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