Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.routing; |
| 2 | |
| 3 | import java.util.ArrayList; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 6 | import java.util.Iterator; |
| 7 | import java.util.List; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 9 | |
| 10 | import net.floodlightcontroller.core.internal.SwitchStorageImpl; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService; |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.util.DataPath; |
| 17 | import net.floodlightcontroller.util.Dpid; |
| 18 | import net.floodlightcontroller.util.FlowEntry; |
| 19 | import net.floodlightcontroller.util.Port; |
| 20 | import net.floodlightcontroller.util.SwitchPort; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 21 | |
| 22 | import org.openflow.util.HexString; |
| 23 | |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 24 | import com.thinkaurelius.titan.core.TitanGraph; |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 25 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 26 | import com.tinkerpop.blueprints.Vertex; |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 27 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 28 | import com.tinkerpop.pipes.PipeFunction; |
| 29 | import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 30 | |
| 31 | import javax.script.ScriptContext; |
| 32 | import javax.script.ScriptEngine; |
| 33 | import javax.script.ScriptException; |
| 34 | import com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine; |
| 35 | |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | public class TopoRouteService implements IFloodlightModule, ITopoRouteService { |
| 40 | |
| 41 | /** The logger. */ |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 42 | private static Logger log = |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 43 | LoggerFactory.getLogger(TopoRouteService.class); |
| 44 | |
| 45 | @Override |
| 46 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 47 | Collection<Class<? extends IFloodlightService>> l = |
| 48 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 49 | l.add(ITopoRouteService.class); |
| 50 | return l; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 55 | getServiceImpls() { |
| 56 | Map<Class<? extends IFloodlightService>, |
| 57 | IFloodlightService> m = |
| 58 | new HashMap<Class<? extends IFloodlightService>, |
| 59 | IFloodlightService>(); |
| 60 | m.put(ITopoRouteService.class, this); |
| 61 | return m; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Collection<Class<? extends IFloodlightService>> |
| 66 | getModuleDependencies() { |
| 67 | Collection<Class<? extends IFloodlightService>> l = |
| 68 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 69 | // TODO: Add the appropriate dependencies |
| 70 | // l.add(IRestApiService.class); |
| 71 | return l; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void init(FloodlightModuleContext context) |
| 76 | throws FloodlightModuleException { |
| 77 | // TODO: Add the appropriate initialization |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void startUp(FloodlightModuleContext context) { |
| 82 | // TODO: Add the approprate setup |
| 83 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 84 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 85 | ThreadLocal<SwitchStorageImpl> store = new ThreadLocal<SwitchStorageImpl>() { |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 86 | @Override |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 87 | protected SwitchStorageImpl initialValue() { |
| 88 | SwitchStorageImpl swStore = new SwitchStorageImpl(); |
| 89 | // NOTE: This is the file path from global properties |
| 90 | swStore.init("/tmp/cassandra.titan"); |
| 91 | return swStore; |
| 92 | } |
| 93 | }; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 94 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 95 | SwitchStorageImpl swStore = store.get(); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 96 | |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 97 | static class ShortestPathLoopFunction implements PipeFunction<LoopBundle<Vertex>, Boolean> { |
| 98 | String dpid; |
| 99 | public ShortestPathLoopFunction(String dpid) { |
| 100 | super(); |
| 101 | this.dpid = dpid; |
| 102 | } |
| 103 | public Boolean compute(LoopBundle<Vertex> bundle) { |
| 104 | Boolean output = false; |
| 105 | if (! bundle.getObject().getProperty("dpid").equals(dpid)) { |
| 106 | output = true; |
| 107 | } |
| 108 | return output; |
| 109 | } |
| 110 | } |
| 111 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 112 | @Override |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 113 | public DataPath getShortestPath(SwitchPort src, SwitchPort dest) { |
| 114 | DataPath result_data_path = new DataPath(); |
| 115 | |
| 116 | // Initialize the source and destination in the data path to return |
| 117 | result_data_path.setSrcPort(src); |
| 118 | result_data_path.setDstPort(dest); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 119 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 120 | TitanGraph titanGraph = swStore.graph; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 121 | |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 122 | String dpid_src = src.dpid().toString(); |
| 123 | String dpid_dest = dest.dpid().toString(); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 124 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 125 | // |
| 126 | // Implement the Shortest Path between two vertices by using |
| 127 | // the following Gremlin CLI code: |
| 128 | // 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} |
| 129 | // The equivalent code used here is: |
| 130 | // results = []; v_src.as("x").out("on").out("link").in("on").dedup().loop("x"){it.object.dpid != v_dest.dpid}.path().fill(results) |
| 131 | // |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 132 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 133 | // Get the source vertex |
| 134 | Iterator<Vertex> iter = titanGraph.getVertices("dpid", dpid_src).iterator(); |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 135 | if (! iter.hasNext()) { |
| 136 | // titanGraph.stopTransaction(Conclusion.SUCCESS); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 137 | return null; // Source vertex not found |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 138 | } |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 139 | Vertex v_src = iter.next(); |
Pavlin Radoslavov | 2f686c5 | 2013-02-13 11:27:13 -0800 | [diff] [blame] | 140 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 141 | // Get the destination vertex |
| 142 | iter = titanGraph.getVertices("dpid", dpid_dest).iterator(); |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 143 | if (! iter.hasNext()) { |
| 144 | // titanGraph.stopTransaction(Conclusion.SUCCESS); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 145 | return null; // Destination vertex not found |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 146 | } |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 147 | Vertex v_dest = iter.next(); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 148 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 149 | // |
| 150 | // Test whether we are computing a path from/to the same DPID. |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 151 | // If "yes", then just add a single flow entry in the return result. |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 152 | // NOTE: The return value will change in the future to return |
| 153 | // a single hop/entry instead of two. Currently, we need |
| 154 | // both entries to capture the source and destination ports. |
| 155 | // |
| 156 | if (dpid_src.equals(dpid_dest)) { |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 157 | FlowEntry flowEntry = new FlowEntry(); |
| 158 | flowEntry.setDpid(src.dpid()); |
| 159 | flowEntry.setInPort(src.port()); |
| 160 | flowEntry.setOutPort(dest.port()); |
| 161 | result_data_path.flowEntries().add(flowEntry); |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 162 | // titanGraph.stopTransaction(Conclusion.SUCCESS); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 163 | return result_data_path; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 164 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 165 | |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 166 | ShortestPathLoopFunction whileFunction = new ShortestPathLoopFunction(dpid_dest); |
| 167 | GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(); |
| 168 | Collection<List> results = new ArrayList<List>(); |
| 169 | GremlinPipeline<Vertex, List> path; |
| 170 | path = pipe.start(v_src).as("x").out("on").out("link").in("on").dedup().loop("x", whileFunction).path(); |
| 171 | path.fill(results); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 172 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 173 | // |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 174 | // Loop through the result and collect the list |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 175 | // of <dpid, port> tuples. |
| 176 | // |
| 177 | long nodeId = 0; |
| 178 | short portId = 0; |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 179 | Port inPort = new Port(src.port().value()); |
| 180 | Port outPort = new Port(); |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 181 | for (List l : results) { |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 182 | int idx = 0; |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 183 | for (Object o: l) { |
| 184 | Vertex v = (Vertex)(o); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 185 | String type = v.getProperty("type").toString(); |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 186 | // System.out.println("type: " + type); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 187 | if (type.equals("port")) { |
| 188 | String number = v.getProperty("number").toString(); |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 189 | // System.out.println("number: " + number); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 190 | |
| 191 | Object obj = v.getProperty("number"); |
| 192 | // String class_str = obj.getClass().toString(); |
| 193 | if (obj instanceof Short) { |
| 194 | portId = (Short)obj; |
| 195 | } else if (obj instanceof Integer) { |
| 196 | Integer int_nodeId = (Integer)obj; |
| 197 | portId = int_nodeId.shortValue(); |
| 198 | // int int_nodeId = (Integer)obj; |
| 199 | // portId = (short)int_nodeId.; |
| 200 | } |
| 201 | } else if (type.equals("switch")) { |
| 202 | String dpid = v.getProperty("dpid").toString(); |
| 203 | nodeId = HexString.toLong(dpid); |
| 204 | |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 205 | // System.out.println("dpid: " + dpid); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 206 | } |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 207 | idx++; |
| 208 | if (idx == 1) { |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 209 | continue; |
| 210 | } |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 211 | int mod = idx % 3; |
| 212 | if (mod == 0) { |
| 213 | // Setup the incoming port |
| 214 | inPort = new Port(portId); |
| 215 | continue; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 216 | } |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 217 | if (mod == 2) { |
| 218 | // Setup the outgoing port, and add the Flow Entry |
| 219 | outPort = new Port(portId); |
| 220 | |
| 221 | FlowEntry flowEntry = new FlowEntry(); |
| 222 | flowEntry.setDpid(new Dpid(nodeId)); |
| 223 | flowEntry.setInPort(inPort); |
| 224 | flowEntry.setOutPort(outPort); |
| 225 | result_data_path.flowEntries().add(flowEntry); |
| 226 | continue; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (idx > 0) { |
| 231 | // Add the last Flow Entry |
| 232 | FlowEntry flowEntry = new FlowEntry(); |
| 233 | flowEntry.setDpid(new Dpid(nodeId)); |
| 234 | flowEntry.setInPort(inPort); |
| 235 | flowEntry.setOutPort(dest.port()); |
| 236 | result_data_path.flowEntries().add(flowEntry); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 237 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 238 | } |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 239 | // titanGraph.stopTransaction(Conclusion.SUCCESS); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 240 | if (result_data_path.flowEntries().size() > 0) |
| 241 | return result_data_path; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 242 | |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | @Override |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 247 | public Boolean routeExists(SwitchPort src, SwitchPort dest) { |
| 248 | DataPath dataPath = getShortestPath(src, dest); |
| 249 | if (dataPath != null) |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 250 | return true; |
| 251 | return false; |
| 252 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 253 | } |