blob: 58e95cb5d243ab680bf47565dcbf810ccd3dbfe5 [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
13import com.thinkaurelius.titan.core.TitanFactory;
14import com.thinkaurelius.titan.core.TitanGraph;
15// import com.tinkerpop.blueprints.Direction;
16import 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
26import com.tinkerpop.blueprints.Element;
27
28import javax.script.ScriptContext;
29import javax.script.ScriptEngine;
30import javax.script.ScriptException;
31import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
32
33public 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 Radoslavove9686a32013-01-29 16:50:03 -080048 public List<NodePortTuple> GetShortestPath(NodePortTuple src,
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080049 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 Radoslavov50951972013-02-13 10:14:08 -080059 // 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 Radoslavov382b22a2013-01-28 09:24:04 -080063 //
64
Ubuntud8577652013-02-08 18:54:46 +000065 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 -080066
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 Radoslavove9686a32013-01-29 16:50:03 -0800147 List<NodePortTuple> route = GetShortestPath(src, dest);
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800148 if (route != null)
149 return true;
150 return false;
151 }
152}