blob: 142453a336077c1bc4582baea5f3676e75f596c4 [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
59 // the following Gremlin code:
60 // results = []; v_src.as('x').out.out.in.has("type", "switch").dedup().loop('x'){it.object.dpid != v_dest.dpid & it.loops < 10}.path().fill(results)
61 //
62
Ubuntud8577652013-02-08 18:54:46 +000063// String gremlin = "v_src.as(\"x\").out.out.in.has(\"type\", \"switch\").dedup().loop(\"x\"){it.object.dpid != v_dest.dpid & it.loops < 10}.path().fill(results)";
64 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 -080065
66 // Get the source vertex
67 Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator();
68 if (! iter.hasNext())
69 return null; // Source vertex not found
70 Vertex v_src = iter.next();
71
72 // Get the destination vertex
73 iter = titanGraph.getVertices("dpid", dpid_dest).iterator();
74 if (! iter.hasNext())
75 return null; // Destination vertex not found
76 Vertex v_dest = iter.next();
77
78 //
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}