blob: be7849483cf350d81ba65fa5c041723da8e45ff1 [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
48 public List<NodePortTuple> GetShortestpath(NodePortTuple src,
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
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
63 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
65 // Get the source vertex
66 Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator();
67 if (! iter.hasNext())
68 return null; // Source vertex not found
69 Vertex v_src = iter.next();
70
71 // Get the destination vertex
72 iter = titanGraph.getVertices("dpid", dpid_dest).iterator();
73 if (! iter.hasNext())
74 return null; // Destination vertex not found
75 Vertex v_dest = iter.next();
76
77 //
78 // Implement the Gremlin script and run it
79 //
80 ScriptEngine engine = new GremlinGroovyScriptEngine();
81
82 ArrayList<ArrayList<Vertex>> results = new ArrayList<ArrayList<Vertex>>();
83 engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", titanGraph);
84 engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_src", v_src);
85 engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v_dest", v_dest);
86 engine.getBindings(ScriptContext.ENGINE_SCOPE).put("results", results);
87
88 try {
89 engine.eval(gremlin);
90 } catch (ScriptException e) {
91 System.err.println("Caught ScriptException running Gremlin script: " + e.getMessage());
92 return null;
93 }
94
95 //
96 // Loop through the result and return the list
97 // of <dpid, port> tuples.
98 //
99 long nodeId = 0;
100 short portId = 0;
101 for (ArrayList<Vertex> lv : results) {
102 int idx = 0;
103 for (Vertex v: lv) {
104 String type = v.getProperty("type").toString();
105 System.out.println("type: " + type);
106 if (type.equals("port")) {
107 String number = v.getProperty("number").toString();
108 System.out.println("number: " + number);
109
110 Object obj = v.getProperty("number");
111 // String class_str = obj.getClass().toString();
112 if (obj instanceof Short) {
113 portId = (Short)obj;
114 } else if (obj instanceof Integer) {
115 Integer int_nodeId = (Integer)obj;
116 portId = int_nodeId.shortValue();
117 // int int_nodeId = (Integer)obj;
118 // portId = (short)int_nodeId.;
119 }
120 } else if (type.equals("switch")) {
121 String dpid = v.getProperty("dpid").toString();
122 nodeId = HexString.toLong(dpid);
123
124 System.out.println("dpid: " + dpid);
125 }
126 if (idx == 0) {
127 idx++;
128 continue;
129 }
130 int mod = (idx - 1) % 3;
131 if ((mod == 0) || (mod == 2)) {
132 result_list.add(new NodePortTuple(nodeId, portId));
133 }
134 idx++;
135 }
136 }
137 if (result_list.size() > 0)
138 return result_list;
139
140 return null;
141 }
142
143 @Override
144 public Boolean RouteExists(NodePortTuple src, NodePortTuple dest) {
145 List<NodePortTuple> route = GetShortestpath(src, dest);
146 if (route != null)
147 return true;
148 return false;
149 }
150}