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