blob: f326f4cc19e7d8bb138ccf9562b0f993898ca176 [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;
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070017import net.onrc.onos.ofcontroller.floodlightlistener.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 {
yoshice479d62014-01-22 12:04:26 -080083 dbHandler = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
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);
144 l.add(INetworkGraphService.class);
145 l.add(IDatagridService.class);
146 return l;
147 }
148
149 /**
150 * Initialize the module.
151 *
152 * @param context the module context to use for the initialization.
153 */
154 @Override
155 public void init(FloodlightModuleContext context)
156 throws FloodlightModuleException {
157 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Naoki Shiotab32edf52013-12-12 14:09:36 -0800158 restApi = context.getServiceImpl(IRestApiService.class);
yoshif7424e42013-11-25 22:07:40 -0800159 Map<String, String> configMap = context.getConfigParams(this);
160 String conf = configMap.get(DBConfigFile);
161 String dbStore = configMap.get(GraphDBStore);
162 this.init(dbStore, conf);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700163 }
164
165 /**
166 * Startup module operation.
167 *
168 * @param context the module context to use for the startup.
169 */
170 @Override
171 public void startUp(FloodlightModuleContext context) {
Naoki Shiotab32edf52013-12-12 14:09:36 -0800172 restApi.addRestletRoutable(new OnosTopologyWebRoutable());
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700173
Pavlin Radoslavovcec899a2013-06-27 15:47:50 -0700174 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800175
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700176 /**
177 * Fetch the Switch and Ports info from the Titan Graph
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000178 * and return it for fast access during the shortest path
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700179 * computation.
180 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700181 * After fetching the state, method @ref getTopologyShortestPath()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700182 * can be used for fast shortest path computation.
183 *
184 * Note: There is certain cost to fetch the state, hence it should
185 * be used only when there is a large number of shortest path
186 * computations that need to be done on the same topology.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700187 * Typically, a single call to @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700188 * should be followed by a large number of calls to
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700189 * method @ref getTopologyShortestPath().
190 * After the last @ref getTopologyShortestPath() call,
191 * method @ref dropTopology() should be used to release
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700192 * the internal state that is not needed anymore:
193 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700194 * Topology topology = topologyManager.newDatabaseTopology();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700195 * for (int i = 0; i < 10000; i++) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700196 * dataPath = topologyManager.getTopologyShortestPath(topology, ...);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700197 * ...
198 * }
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700199 * topologyManager.dropTopology(shortestPathTopo);
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000200 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700201 * @return the allocated topology handler.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700202 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800203 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700204 public Topology newDatabaseTopology() {
205 Topology topology = new Topology();
206 topology.readFromDatabase(dbHandler);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700207
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700208 return topology;
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700209 }
210
211 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700212 * Release the topology that was populated by
213 * method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700214 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700215 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700216 * for additional information and usage.
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000217 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700218 * @param topology the topology to release.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700219 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800220 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700221 public void dropTopology(Topology topology) {
Naoki Shiotadf051d42014-01-20 16:12:41 -0800222 // nothing to do
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700223 }
224
225 /**
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700226 * Compute the network path for a Flow.
227 *
228 * @param topology the topology handler to use.
229 * @param flowPath the Flow to compute the network path for.
230 * @return the data path with the computed path if found, otherwise null.
231 */
232 public static DataPath computeNetworkPath(Topology topology,
233 FlowPath flowPath) {
234 //
235 // Compute the network path based on the desired Flow Path type
236 //
237 switch (flowPath.flowPathType()) {
238 case FP_TYPE_SHORTEST_PATH: {
239 SwitchPort src = flowPath.dataPath().srcPort();
240 SwitchPort dest = flowPath.dataPath().dstPort();
241 return ShortestPath.getTopologyShortestPath(topology, src, dest);
242 }
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800243
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700244 case FP_TYPE_EXPLICIT_PATH:
245 return flowPath.dataPath();
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800246
247 case FP_TYPE_UNKNOWN:
248 return null;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700249 }
250
251 return null;
252 }
253
254 /**
255 * Test whether two Flow Entries represent same points in a data path.
256 *
257 * NOTE: Two Flow Entries represent same points in a data path if
258 * the Switch DPID, incoming port and outgoing port are same.
259 *
260 * NOTE: This method is specialized for shortest-path unicast paths,
261 * and probably should be moved somewhere else.
262 *
263 * @param oldFlowEntry the first Flow Entry to compare.
264 * @param newFlowEntry the second Flow Entry to compare.
265 * @return true if the two Flow Entries represent same points in a
266 * data path, otherwise false.
267 */
268 public static boolean isSameFlowEntryDataPath(FlowEntry oldFlowEntry,
269 FlowEntry newFlowEntry) {
270 // Test the DPID
271 if (oldFlowEntry.dpid().value() != newFlowEntry.dpid().value())
272 return false;
273
274 // Test the inPort
275 do {
276 Port oldPort = oldFlowEntry.inPort();
277 Port newPort = newFlowEntry.inPort();
278 if ((oldPort != null) && (newPort != null) &&
279 (oldPort.value() == newPort.value())) {
280 break;
281 }
282 if ((oldPort == null) && (newPort == null))
283 break;
284 return false; // inPort is different
285 } while (false);
286
287 // Test the outPort
288 do {
289 Port oldPort = oldFlowEntry.outPort();
290 Port newPort = newFlowEntry.outPort();
291 if ((oldPort != null) && (newPort != null) &&
292 (oldPort.value() == newPort.value())) {
293 break;
294 }
295 if ((oldPort == null) && (newPort == null))
296 break;
297 return false; // outPort is different
298 } while (false);
299
300 return true;
301 }
302
303 /**
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700304 * Get the shortest path from a source to a destination by
305 * using the pre-populated local topology state prepared
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700306 * by method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700307 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700308 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700309 * for additional information and usage.
310 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700311 * @param topology the topology handler to use.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700312 * @param src the source in the shortest path computation.
313 * @param dest the destination in the shortest path computation.
314 * @return the data path with the computed shortest path if
315 * found, otherwise null.
316 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800317 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700318 public DataPath getTopologyShortestPath(Topology topology,
319 SwitchPort src, SwitchPort dest) {
320 return ShortestPath.getTopologyShortestPath(topology, src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700321 }
322
323 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700324 * Get the shortest path from a source to a destination by using
325 * the underlying database.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700326 *
327 * @param src the source in the shortest path computation.
328 * @param dest the destination in the shortest path computation.
329 * @return the data path with the computed shortest path if
330 * found, otherwise null.
331 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800332 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700333 public DataPath getDatabaseShortestPath(SwitchPort src, SwitchPort dest) {
334 return ShortestPath.getDatabaseShortestPath(dbHandler, src, dest);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800335 }
336
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700337 /**
338 * Test whether a route exists from a source to a destination.
339 *
340 * @param src the source node for the test.
341 * @param dest the destination node for the test.
342 * @return true if a route exists, otherwise false.
343 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800344 @Override
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -0800345 public Boolean routeExists(SwitchPort src, SwitchPort dest) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700346 DataPath dataPath = getDatabaseShortestPath(src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700347 return (dataPath != null);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800348 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800349}