blob: be207c6e69face487b8f44baf832cbbf93fc3744 [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;
Pankaj Berde38646d62013-06-21 11:34:04 -070015import net.onrc.onos.graph.GraphDBOperation;
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070016import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService;
Naoki Shiotab32edf52013-12-12 14:09:36 -080017import net.onrc.onos.ofcontroller.topology.web.OnosTopologyWebRoutable;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070018import net.onrc.onos.ofcontroller.util.DataPath;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -070019import net.onrc.onos.ofcontroller.util.FlowEntry;
20import net.onrc.onos.ofcontroller.util.FlowPath;
21import net.onrc.onos.ofcontroller.util.Port;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070022import net.onrc.onos.ofcontroller.util.SwitchPort;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080023
Pankaj Berde15193092013-03-21 17:30:14 -070024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080026
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -070027/**
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -070028 * A class for obtaining Topology Snapshot
29 * and PathComputation.
30 *
31 * TODO: PathComputation part should be refactored out to separate class.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070032 */
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070033public class TopologyManager implements IFloodlightModule,
34 ITopologyNetService {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070035 private final static Logger log = LoggerFactory.getLogger(TopologyManager.class);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070036 protected IFloodlightProviderService floodlightProvider;
37
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070038 protected GraphDBOperation dbHandler;
Naoki Shiotab32edf52013-12-12 14:09:36 -080039 protected IRestApiService restApi;
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080040
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070041
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070042 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070043 * Default constructor.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070044 */
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070045 public TopologyManager() {
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080046 }
47
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070048 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070049 * Constructor for given database configuration file.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070050 *
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070051 * @param config the database configuration file to use for
52 * the initialization.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070053 */
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070054 public TopologyManager(String config) {
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070055 this.init(config);
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080056 }
57
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070058 /**
Pavlin Radoslavov0367d352013-10-19 11:04:43 -070059 * Constructor for a given database operation handler.
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070060 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070061 * @param dbHandler the database operation handler to use for the
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070062 * initialization.
63 */
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070064 public TopologyManager(GraphDBOperation dbHandler) {
65 this.dbHandler = dbHandler;
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070066 }
67
68 /**
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070069 * Init the module.
70 *
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070071 * @param config the database configuration file to use for
72 * the initialization.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070073 */
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070074 public void init(String config) {
75 try {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070076 dbHandler = new GraphDBOperation(config);
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070077 } catch (Exception e) {
78 log.error(e.getMessage());
79 }
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080080 }
81
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070082 /**
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070083 * Shutdown the Topology Manager operation.
84 */
85 public void finalize() {
86 close();
87 }
88
89 /**
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070090 * Close the service. It will close the corresponding database connection.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070091 */
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -070092 public void close() {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070093 dbHandler.close();
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -080094 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080095
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070096 /**
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070097 * Get the collection of offered module services.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -070098 *
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -070099 * @return the collection of offered module services.
Pavlin Radoslavovc5d2fbc2013-06-27 18:15:56 -0700100 */
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700101 @Override
102 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
103 Collection<Class<? extends IFloodlightService>> l =
104 new ArrayList<Class<? extends IFloodlightService>>();
105 l.add(ITopologyNetService.class);
106 return l;
107 }
108
109 /**
110 * Get the collection of implemented services.
111 *
112 * @return the collection of implemented services.
113 */
114 @Override
115 public Map<Class<? extends IFloodlightService>, IFloodlightService>
116 getServiceImpls() {
117 Map<Class<? extends IFloodlightService>,
118 IFloodlightService> m =
119 new HashMap<Class<? extends IFloodlightService>,
120 IFloodlightService>();
121 m.put(ITopologyNetService.class, this);
122 return m;
123 }
124
125 /**
126 * Get the collection of modules this module depends on.
127 *
128 * @return the collection of modules this module depends on.
129 */
130 @Override
131 public Collection<Class<? extends IFloodlightService>>
132 getModuleDependencies() {
133 Collection<Class<? extends IFloodlightService>> l =
134 new ArrayList<Class<? extends IFloodlightService>>();
135 l.add(IFloodlightProviderService.class);
136 l.add(INetworkGraphService.class);
137 l.add(IDatagridService.class);
138 return l;
139 }
140
141 /**
142 * Initialize the module.
143 *
144 * @param context the module context to use for the initialization.
145 */
146 @Override
147 public void init(FloodlightModuleContext context)
148 throws FloodlightModuleException {
149 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Naoki Shiotab32edf52013-12-12 14:09:36 -0800150 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700151
152 String conf = "";
153 this.init(conf);
154 }
155
156 /**
157 * Startup module operation.
158 *
159 * @param context the module context to use for the startup.
160 */
161 @Override
162 public void startUp(FloodlightModuleContext context) {
Naoki Shiotab32edf52013-12-12 14:09:36 -0800163 restApi.addRestletRoutable(new OnosTopologyWebRoutable());
Pavlin Radoslavov5d8d77e2013-10-18 18:49:25 -0700164
Pavlin Radoslavovcec899a2013-06-27 15:47:50 -0700165 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800166
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700167 /**
168 * Fetch the Switch and Ports info from the Titan Graph
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000169 * and return it for fast access during the shortest path
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700170 * computation.
171 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700172 * After fetching the state, method @ref getTopologyShortestPath()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700173 * can be used for fast shortest path computation.
174 *
175 * Note: There is certain cost to fetch the state, hence it should
176 * be used only when there is a large number of shortest path
177 * computations that need to be done on the same topology.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700178 * Typically, a single call to @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700179 * should be followed by a large number of calls to
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700180 * method @ref getTopologyShortestPath().
181 * After the last @ref getTopologyShortestPath() call,
182 * method @ref dropTopology() should be used to release
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700183 * the internal state that is not needed anymore:
184 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700185 * Topology topology = topologyManager.newDatabaseTopology();
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700186 * for (int i = 0; i < 10000; i++) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700187 * dataPath = topologyManager.getTopologyShortestPath(topology, ...);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700188 * ...
189 * }
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700190 * topologyManager.dropTopology(shortestPathTopo);
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000191 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700192 * @return the allocated topology handler.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700193 */
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700194 public Topology newDatabaseTopology() {
195 Topology topology = new Topology();
196 topology.readFromDatabase(dbHandler);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700197
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700198 return topology;
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700199 }
200
201 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700202 * Release the topology that was populated by
203 * method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700204 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700205 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700206 * for additional information and usage.
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000207 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700208 * @param topology the topology to release.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700209 */
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700210 public void dropTopology(Topology topology) {
211 topology = null;
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700212 }
213
214 /**
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700215 * Compute the network path for a Flow.
216 *
217 * @param topology the topology handler to use.
218 * @param flowPath the Flow to compute the network path for.
219 * @return the data path with the computed path if found, otherwise null.
220 */
221 public static DataPath computeNetworkPath(Topology topology,
222 FlowPath flowPath) {
223 //
224 // Compute the network path based on the desired Flow Path type
225 //
226 switch (flowPath.flowPathType()) {
227 case FP_TYPE_SHORTEST_PATH: {
228 SwitchPort src = flowPath.dataPath().srcPort();
229 SwitchPort dest = flowPath.dataPath().dstPort();
230 return ShortestPath.getTopologyShortestPath(topology, src, dest);
231 }
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800232
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700233 case FP_TYPE_EXPLICIT_PATH:
234 return flowPath.dataPath();
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800235
236 case FP_TYPE_UNKNOWN:
237 return null;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700238 }
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800239
240 return null;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700241 }
242
243 /**
244 * Test whether two Flow Entries represent same points in a data path.
245 *
246 * NOTE: Two Flow Entries represent same points in a data path if
247 * the Switch DPID, incoming port and outgoing port are same.
248 *
249 * NOTE: This method is specialized for shortest-path unicast paths,
250 * and probably should be moved somewhere else.
251 *
252 * @param oldFlowEntry the first Flow Entry to compare.
253 * @param newFlowEntry the second Flow Entry to compare.
254 * @return true if the two Flow Entries represent same points in a
255 * data path, otherwise false.
256 */
257 public static boolean isSameFlowEntryDataPath(FlowEntry oldFlowEntry,
258 FlowEntry newFlowEntry) {
259 // Test the DPID
260 if (oldFlowEntry.dpid().value() != newFlowEntry.dpid().value())
261 return false;
262
263 // Test the inPort
264 do {
265 Port oldPort = oldFlowEntry.inPort();
266 Port newPort = newFlowEntry.inPort();
267 if ((oldPort != null) && (newPort != null) &&
268 (oldPort.value() == newPort.value())) {
269 break;
270 }
271 if ((oldPort == null) && (newPort == null))
272 break;
273 return false; // inPort is different
274 } while (false);
275
276 // Test the outPort
277 do {
278 Port oldPort = oldFlowEntry.outPort();
279 Port newPort = newFlowEntry.outPort();
280 if ((oldPort != null) && (newPort != null) &&
281 (oldPort.value() == newPort.value())) {
282 break;
283 }
284 if ((oldPort == null) && (newPort == null))
285 break;
286 return false; // outPort is different
287 } while (false);
288
289 return true;
290 }
291
292 /**
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700293 * Get the shortest path from a source to a destination by
294 * using the pre-populated local topology state prepared
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700295 * by method @ref newDatabaseTopology().
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700296 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700297 * See the documentation for method @ref newDatabaseTopology()
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700298 * for additional information and usage.
299 *
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700300 * @param topology the topology handler to use.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700301 * @param src the source in the shortest path computation.
302 * @param dest the destination in the shortest path computation.
303 * @return the data path with the computed shortest path if
304 * found, otherwise null.
305 */
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700306 public DataPath getTopologyShortestPath(Topology topology,
307 SwitchPort src, SwitchPort dest) {
308 return ShortestPath.getTopologyShortestPath(topology, src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700309 }
310
311 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700312 * Get the shortest path from a source to a destination by using
313 * the underlying database.
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700314 *
315 * @param src the source in the shortest path computation.
316 * @param dest the destination in the shortest path computation.
317 * @return the data path with the computed shortest path if
318 * found, otherwise null.
319 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800320 @Override
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700321 public DataPath getDatabaseShortestPath(SwitchPort src, SwitchPort dest) {
322 return ShortestPath.getDatabaseShortestPath(dbHandler, src, dest);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800323 }
324
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700325 /**
326 * Test whether a route exists from a source to a destination.
327 *
328 * @param src the source node for the test.
329 * @param dest the destination node for the test.
330 * @return true if a route exists, otherwise false.
331 */
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800332 @Override
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -0800333 public Boolean routeExists(SwitchPort src, SwitchPort dest) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700334 DataPath dataPath = getDatabaseShortestPath(src, dest);
Pavlin Radoslavova5f167b2013-03-21 11:39:27 -0700335 return (dataPath != null);
Pavlin Radoslavovf34c2902013-02-22 10:33:34 -0800336 }
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -0800337}