Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.topology; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 5 | import java.util.HashMap; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 6 | import java.util.Map; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 7 | |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 8 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 9 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 11 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 13 | |
| 14 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 15 | import net.onrc.onos.graph.GraphDBOperation; |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 16 | import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.util.DataPath; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 19 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 22 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 23 | /** |
Pavlin Radoslavov | 1278ac7 | 2013-10-16 04:43:49 -0700 | [diff] [blame] | 24 | * A class for implementing Topology Network Service. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 25 | */ |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 26 | public class TopologyManager implements IFloodlightModule, |
| 27 | ITopologyNetService { |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 28 | private static Logger log = LoggerFactory.getLogger(TopologyManager.class); |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 29 | protected IFloodlightProviderService floodlightProvider; |
| 30 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 31 | protected GraphDBOperation dbHandler; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 32 | |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 33 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 34 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 35 | * Default constructor. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 36 | */ |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 37 | public TopologyManager() { |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 40 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 41 | * Constructor for given database configuration file. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 42 | * |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 43 | * @param config the database configuration file to use for |
| 44 | * the initialization. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 45 | */ |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 46 | public TopologyManager(String config) { |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 47 | this.init(config); |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 50 | /** |
Pavlin Radoslavov | 0367d35 | 2013-10-19 11:04:43 -0700 | [diff] [blame] | 51 | * Constructor for a given database operation handler. |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 52 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 53 | * @param dbHandler the database operation handler to use for the |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 54 | * initialization. |
| 55 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 56 | public TopologyManager(GraphDBOperation dbHandler) { |
| 57 | this.dbHandler = dbHandler; |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 61 | * Init the module. |
| 62 | * |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 63 | * @param config the database configuration file to use for |
| 64 | * the initialization. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 65 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 66 | public void init(String config) { |
| 67 | try { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 68 | dbHandler = new GraphDBOperation(config); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 69 | } catch (Exception e) { |
| 70 | log.error(e.getMessage()); |
| 71 | } |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 74 | /** |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 75 | * Shutdown the Topology Manager operation. |
| 76 | */ |
| 77 | public void finalize() { |
| 78 | close(); |
| 79 | } |
| 80 | |
| 81 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 82 | * Close the service. It will close the corresponding database connection. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 83 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 84 | public void close() { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 85 | dbHandler.close(); |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 86 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 87 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 88 | /** |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 89 | * Get the collection of offered module services. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 90 | * |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 91 | * @return the collection of offered module services. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 92 | */ |
Pavlin Radoslavov | 5d8d77e | 2013-10-18 18:49:25 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 95 | Collection<Class<? extends IFloodlightService>> l = |
| 96 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 97 | l.add(ITopologyNetService.class); |
| 98 | return l; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the collection of implemented services. |
| 103 | * |
| 104 | * @return the collection of implemented services. |
| 105 | */ |
| 106 | @Override |
| 107 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 108 | getServiceImpls() { |
| 109 | Map<Class<? extends IFloodlightService>, |
| 110 | IFloodlightService> m = |
| 111 | new HashMap<Class<? extends IFloodlightService>, |
| 112 | IFloodlightService>(); |
| 113 | m.put(ITopologyNetService.class, this); |
| 114 | return m; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the collection of modules this module depends on. |
| 119 | * |
| 120 | * @return the collection of modules this module depends on. |
| 121 | */ |
| 122 | @Override |
| 123 | public Collection<Class<? extends IFloodlightService>> |
| 124 | getModuleDependencies() { |
| 125 | Collection<Class<? extends IFloodlightService>> l = |
| 126 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 127 | l.add(IFloodlightProviderService.class); |
| 128 | l.add(INetworkGraphService.class); |
| 129 | l.add(IDatagridService.class); |
| 130 | return l; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Initialize the module. |
| 135 | * |
| 136 | * @param context the module context to use for the initialization. |
| 137 | */ |
| 138 | @Override |
| 139 | public void init(FloodlightModuleContext context) |
| 140 | throws FloodlightModuleException { |
| 141 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 142 | |
| 143 | String conf = ""; |
| 144 | this.init(conf); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Startup module operation. |
| 149 | * |
| 150 | * @param context the module context to use for the startup. |
| 151 | */ |
| 152 | @Override |
| 153 | public void startUp(FloodlightModuleContext context) { |
| 154 | |
Pavlin Radoslavov | cec899a | 2013-06-27 15:47:50 -0700 | [diff] [blame] | 155 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 156 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Fetch the Switch and Ports info from the Titan Graph |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 159 | * and return it for fast access during the shortest path |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 160 | * computation. |
| 161 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 162 | * After fetching the state, method @ref getTopologyShortestPath() |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 163 | * can be used for fast shortest path computation. |
| 164 | * |
| 165 | * Note: There is certain cost to fetch the state, hence it should |
| 166 | * be used only when there is a large number of shortest path |
| 167 | * computations that need to be done on the same topology. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 168 | * Typically, a single call to @ref newDatabaseTopology() |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 169 | * should be followed by a large number of calls to |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 170 | * method @ref getTopologyShortestPath(). |
| 171 | * After the last @ref getTopologyShortestPath() call, |
| 172 | * method @ref dropTopology() should be used to release |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 173 | * the internal state that is not needed anymore: |
| 174 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 175 | * Topology topology = topologyManager.newDatabaseTopology(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 176 | * for (int i = 0; i < 10000; i++) { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 177 | * dataPath = topologyManager.getTopologyShortestPath(topology, ...); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 178 | * ... |
| 179 | * } |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 180 | * topologyManager.dropTopology(shortestPathTopo); |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 181 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 182 | * @return the allocated topology handler. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 183 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 184 | public Topology newDatabaseTopology() { |
| 185 | Topology topology = new Topology(); |
| 186 | topology.readFromDatabase(dbHandler); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 187 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 188 | return topology; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 192 | * Release the topology that was populated by |
| 193 | * method @ref newDatabaseTopology(). |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 194 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 195 | * See the documentation for method @ref newDatabaseTopology() |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 196 | * for additional information and usage. |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 197 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 198 | * @param topology the topology to release. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 199 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 200 | public void dropTopology(Topology topology) { |
| 201 | topology = null; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the shortest path from a source to a destination by |
| 206 | * using the pre-populated local topology state prepared |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 207 | * by method @ref newDatabaseTopology(). |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 208 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 209 | * See the documentation for method @ref newDatabaseTopology() |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 210 | * for additional information and usage. |
| 211 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 212 | * @param topology the topology handler to use. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 213 | * @param src the source in the shortest path computation. |
| 214 | * @param dest the destination in the shortest path computation. |
| 215 | * @return the data path with the computed shortest path if |
| 216 | * found, otherwise null. |
| 217 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 218 | public DataPath getTopologyShortestPath(Topology topology, |
| 219 | SwitchPort src, SwitchPort dest) { |
| 220 | return ShortestPath.getTopologyShortestPath(topology, src, dest); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 224 | * Get the shortest path from a source to a destination by using |
| 225 | * the underlying database. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 226 | * |
| 227 | * @param src the source in the shortest path computation. |
| 228 | * @param dest the destination in the shortest path computation. |
| 229 | * @return the data path with the computed shortest path if |
| 230 | * found, otherwise null. |
| 231 | */ |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 232 | @Override |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 233 | public DataPath getDatabaseShortestPath(SwitchPort src, SwitchPort dest) { |
| 234 | return ShortestPath.getDatabaseShortestPath(dbHandler, src, dest); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 237 | /** |
| 238 | * Test whether a route exists from a source to a destination. |
| 239 | * |
| 240 | * @param src the source node for the test. |
| 241 | * @param dest the destination node for the test. |
| 242 | * @return true if a route exists, otherwise false. |
| 243 | */ |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 244 | @Override |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 245 | public Boolean routeExists(SwitchPort src, SwitchPort dest) { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 246 | DataPath dataPath = getDatabaseShortestPath(src, dest); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 247 | return (dataPath != null); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 248 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 249 | } |