blob: cae29ce5bf759e4efe046b28f05066402bd6436f [file] [log] [blame]
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -07001package net.onrc.onos.ofcontroller.topology;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -08002
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -07003import java.util.ArrayList;
4import java.util.Collection;
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -08005import java.util.HashMap;
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -08006import java.util.Map;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -08007
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -07008import net.floodlightcontroller.core.IFloodlightProviderService;
9import net.floodlightcontroller.core.module.FloodlightModuleContext;
10import net.floodlightcontroller.core.module.FloodlightModuleException;
11import net.floodlightcontroller.core.module.IFloodlightModule;
12import net.floodlightcontroller.core.module.IFloodlightService;
Naoki Shiotab32edf52013-12-12 14:09:36 -080013import net.floodlightcontroller.restserver.IRestApiService;
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070014import net.onrc.onos.datagrid.IDatagridService;
yoshitomob292c622013-11-23 14:35:58 -080015import net.onrc.onos.graph.DBOperation;
16import net.onrc.onos.graph.GraphDBManager;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -080017import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
Naoki Shiotab32edf52013-12-12 14:09:36 -080018import net.onrc.onos.ofcontroller.topology.web.OnosTopologyWebRoutable;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070019import net.onrc.onos.ofcontroller.util.DataPath;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -070020import net.onrc.onos.ofcontroller.util.FlowEntry;
21import net.onrc.onos.ofcontroller.util.FlowPath;
22import net.onrc.onos.ofcontroller.util.Port;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070023import net.onrc.onos.ofcontroller.util.SwitchPort;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080024
Pankaj Berde15193092013-03-21 17:30:14 -070025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080027
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -070028/**
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -070029 * A class for obtaining Topology Snapshot
30 * and PathComputation.
31 *
32 * TODO: PathComputation part should be refactored out to separate class.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070033 */
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070034public class TopologyManager implements IFloodlightModule,
35 ITopologyNetService {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070036 private final static Logger log = LoggerFactory.getLogger(TopologyManager.class);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070037 protected IFloodlightProviderService floodlightProvider;
38
yoshif7424e42013-11-25 22:07:40 -080039 protected static final String DBConfigFile = "dbconf";
40 protected static final String GraphDBStore = "graph_db_store";
41
yoshitomob292c622013-11-23 14:35:58 -080042 protected DBOperation dbHandler;
Naoki Shiotab32edf52013-12-12 14:09:36 -080043 protected IRestApiService restApi;
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080044
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070045
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070046 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070047 * Default constructor.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070048 */
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070049 public TopologyManager() {
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080050 }
51
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070052 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070053 * Constructor for given database configuration file.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070054 *
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070055 * @param config the database configuration file to use for
56 * the initialization.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070057 */
yoshif7424e42013-11-25 22:07:40 -080058 public TopologyManager(FloodlightModuleContext context) {
59 Map<String, String> configMap = context.getConfigParams(this);
60 String conf = configMap.get(DBConfigFile);
61 String dbStore = configMap.get(GraphDBStore);
62 this.init(dbStore,conf);
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080063 }
64
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070065 /**
Pavlin Radoslavov0367d352013-10-19 11:04:43 -070066 * Constructor for a given database operation handler.
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070067 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070068 * @param dbHandler the database operation handler to use for the
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070069 * initialization.
70 */
yoshitomob292c622013-11-23 14:35:58 -080071 public TopologyManager(DBOperation dbHandler) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070072 this.dbHandler = dbHandler;
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070073 }
74
75 /**
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070076 * Init the module.
77 *
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070078 * @param config the database configuration file to use for
79 * the initialization.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070080 */
yoshitomob292c622013-11-23 14:35:58 -080081 public void init(final String dbStore, String config) {
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070082 try {
Yoshi Muroi5804ce92014-02-08 03:58:04 -080083 dbHandler = GraphDBManager.getDBOperation();
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070084 } catch (Exception e) {
85 log.error(e.getMessage());
86 }
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080087 }
88
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070089 /**
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070090 * Shutdown the Topology Manager operation.
91 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080092 @Override
93 protected void finalize() {
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070094 close();
95 }
96
97 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070098 * Close the service. It will close the corresponding database connection.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070099 */
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700100 public void close() {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700101 dbHandler.close();
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -0800102 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800103
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -0700104 /**
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700105 * Get the collection of offered module services.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -0700106 *
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700107 * @return the collection of offered module services.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -0700108 */
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700109 @Override
110 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800111 Collection<Class<? extends IFloodlightService>> l =
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700112 new ArrayList<Class<? extends IFloodlightService>>();
113 l.add(ITopologyNetService.class);
114 return l;
115 }
116
117 /**
118 * Get the collection of implemented services.
119 *
120 * @return the collection of implemented services.
121 */
122 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800123 public Map<Class<? extends IFloodlightService>, IFloodlightService>
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700124 getServiceImpls() {
125 Map<Class<? extends IFloodlightService>,
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800126 IFloodlightService> m =
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700127 new HashMap<Class<? extends IFloodlightService>,
128 IFloodlightService>();
129 m.put(ITopologyNetService.class, this);
130 return m;
131 }
132
133 /**
134 * Get the collection of modules this module depends on.
135 *
136 * @return the collection of modules this module depends on.
137 */
138 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800139 public Collection<Class<? extends IFloodlightService>>
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700140 getModuleDependencies() {
141 Collection<Class<? extends IFloodlightService>> l =
142 new ArrayList<Class<? extends IFloodlightService>>();
143 l.add(IFloodlightProviderService.class);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700144 l.add(IDatagridService.class);
145 return l;
146 }
147
148 /**
149 * Initialize the module.
150 *
151 * @param context the module context to use for the initialization.
152 */
153 @Override
154 public void init(FloodlightModuleContext context)
155 throws FloodlightModuleException {
156 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Naoki Shiotab32edf52013-12-12 14:09:36 -0800157 restApi = context.getServiceImpl(IRestApiService.class);
yoshif7424e42013-11-25 22:07:40 -0800158 Map<String, String> configMap = context.getConfigParams(this);
159 String conf = configMap.get(DBConfigFile);
160 String dbStore = configMap.get(GraphDBStore);
161 this.init(dbStore, conf);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700162 }
163
164 /**
165 * Startup module operation.
166 *
167 * @param context the module context to use for the startup.
168 */
169 @Override
170 public void startUp(FloodlightModuleContext context) {
Naoki Shiotab32edf52013-12-12 14:09:36 -0800171 restApi.addRestletRoutable(new OnosTopologyWebRoutable());
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700172
Pavlin Radoslavovcec899a2013-06-27 15:47:50 -0700173 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800174
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700175 /**
176 * Fetch the Switch and Ports info from the Titan Graph
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000177 * and return it for fast access during the shortest path
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700178 * computation.
179 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700180 * After fetching the state, method @ref getTopologyShortestPath()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700181 * can be used for fast shortest path computation.
182 *
183 * Note: There is certain cost to fetch the state, hence it should
184 * be used only when there is a large number of shortest path
185 * computations that need to be done on the same topology.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700186 * Typically, a single call to @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700187 * should be followed by a large number of calls to
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700188 * method @ref getTopologyShortestPath().
189 * After the last @ref getTopologyShortestPath() call,
190 * method @ref dropTopology() should be used to release
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700191 * the internal state that is not needed anymore:
192 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700193 * Topology topology = topologyManager.newDatabaseTopology();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700194 * for (int i = 0; i < 10000; i++) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700195 * dataPath = topologyManager.getTopologyShortestPath(topology, ...);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700196 * ...
197 * }
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700198 * topologyManager.dropTopology(shortestPathTopo);
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000199 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700200 * @return the allocated topology handler.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700201 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800202 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700203 public Topology newDatabaseTopology() {
204 Topology topology = new Topology();
205 topology.readFromDatabase(dbHandler);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700206
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700207 return topology;
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700208 }
209
210 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700211 * Release the topology that was populated by
212 * method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700213 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700214 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700215 * for additional information and usage.
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000216 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700217 * @param topology the topology to release.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700218 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800219 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700220 public void dropTopology(Topology topology) {
Naoki Shiotadf051d42014-01-20 16:12:41 -0800221 // nothing to do
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700222 }
223
224 /**
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700225 * Compute the network path for a Flow.
226 *
227 * @param topology the topology handler to use.
228 * @param flowPath the Flow to compute the network path for.
229 * @return the data path with the computed path if found, otherwise null.
230 */
231 public static DataPath computeNetworkPath(Topology topology,
232 FlowPath flowPath) {
233 //
234 // Compute the network path based on the desired Flow Path type
235 //
236 switch (flowPath.flowPathType()) {
237 case FP_TYPE_SHORTEST_PATH: {
238 SwitchPort src = flowPath.dataPath().srcPort();
239 SwitchPort dest = flowPath.dataPath().dstPort();
240 return ShortestPath.getTopologyShortestPath(topology, src, dest);
241 }
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800242
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700243 case FP_TYPE_EXPLICIT_PATH:
244 return flowPath.dataPath();
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800245
246 case FP_TYPE_UNKNOWN:
247 return null;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700248 }
249
250 return null;
251 }
252
253 /**
254 * Test whether two Flow Entries represent same points in a data path.
255 *
256 * NOTE: Two Flow Entries represent same points in a data path if
257 * the Switch DPID, incoming port and outgoing port are same.
258 *
259 * NOTE: This method is specialized for shortest-path unicast paths,
260 * and probably should be moved somewhere else.
261 *
262 * @param oldFlowEntry the first Flow Entry to compare.
263 * @param newFlowEntry the second Flow Entry to compare.
264 * @return true if the two Flow Entries represent same points in a
265 * data path, otherwise false.
266 */
267 public static boolean isSameFlowEntryDataPath(FlowEntry oldFlowEntry,
268 FlowEntry newFlowEntry) {
269 // Test the DPID
270 if (oldFlowEntry.dpid().value() != newFlowEntry.dpid().value())
271 return false;
272
273 // Test the inPort
274 do {
275 Port oldPort = oldFlowEntry.inPort();
276 Port newPort = newFlowEntry.inPort();
277 if ((oldPort != null) && (newPort != null) &&
278 (oldPort.value() == newPort.value())) {
279 break;
280 }
281 if ((oldPort == null) && (newPort == null))
282 break;
283 return false; // inPort is different
284 } while (false);
285
286 // Test the outPort
287 do {
288 Port oldPort = oldFlowEntry.outPort();
289 Port newPort = newFlowEntry.outPort();
290 if ((oldPort != null) && (newPort != null) &&
291 (oldPort.value() == newPort.value())) {
292 break;
293 }
294 if ((oldPort == null) && (newPort == null))
295 break;
296 return false; // outPort is different
297 } while (false);
298
299 return true;
300 }
301
302 /**
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700303 * Get the shortest path from a source to a destination by
304 * using the pre-populated local topology state prepared
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700305 * by method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700306 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700307 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700308 * for additional information and usage.
309 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700310 * @param topology the topology handler to use.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700311 * @param src the source in the shortest path computation.
312 * @param dest the destination in the shortest path computation.
313 * @return the data path with the computed shortest path if
314 * found, otherwise null.
315 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800316 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700317 public DataPath getTopologyShortestPath(Topology topology,
318 SwitchPort src, SwitchPort dest) {
319 return ShortestPath.getTopologyShortestPath(topology, src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700320 }
321
322 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700323 * Get the shortest path from a source to a destination by using
324 * the underlying database.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700325 *
326 * @param src the source in the shortest path computation.
327 * @param dest the destination in the shortest path computation.
328 * @return the data path with the computed shortest path if
329 * found, otherwise null.
330 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800331 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700332 public DataPath getDatabaseShortestPath(SwitchPort src, SwitchPort dest) {
333 return ShortestPath.getDatabaseShortestPath(dbHandler, src, dest);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800334 }
335
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700336 /**
337 * Test whether a route exists from a source to a destination.
338 *
339 * @param src the source node for the test.
340 * @param dest the destination node for the test.
341 * @return true if a route exists, otherwise false.
342 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800343 @Override
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -0800344 public Boolean routeExists(SwitchPort src, SwitchPort dest) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700345 DataPath dataPath = getDatabaseShortestPath(src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700346 return (dataPath != null);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800347 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800348}