HIGUCHI Yuta | 5a5ef6b | 2013-06-12 14:34:55 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.onoslistener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Map; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 6 | import java.util.concurrent.ScheduledExecutorService; |
| 7 | import java.util.concurrent.TimeUnit; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 8 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 9 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 10 | import org.openflow.util.HexString; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
| 14 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 15 | import net.floodlightcontroller.core.IOFSwitch; |
| 16 | import net.floodlightcontroller.core.IOFSwitchListener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.util.SingletonTask; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.devicemanager.IDevice; |
| 23 | import net.floodlightcontroller.devicemanager.IDeviceListener; |
| 24 | import net.floodlightcontroller.devicemanager.IDeviceService; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryListener; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryService; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 27 | import net.floodlightcontroller.linkdiscovery.internal.LinkStorageImpl; |
| 28 | import net.floodlightcontroller.routing.Link; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 29 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 31 | import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION; |
| 32 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 33 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 34 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
HIGUCHI Yuta | ed49ef7 | 2013-06-12 11:34:10 -0700 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
| 36 | import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl; |
HIGUCHI Yuta | d3fa44d | 2013-06-12 11:53:09 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.devicemanager.IDeviceStorage; |
HIGUCHI Yuta | 351edcb | 2013-06-12 11:58:13 -0700 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.devicemanager.internal.DeviceStorageImpl; |
HIGUCHI Yuta | 80c3ab8 | 2013-06-12 13:17:05 -0700 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkStorage; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 40 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 41 | import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback; |
| 42 | import net.onrc.onos.registry.controller.RegistryException; |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 43 | import net.onrc.onos.util.GraphDBConnection; |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 44 | import net.onrc.onos.util.GraphDBOperation; |
| 45 | import net.onrc.onos.util.IDBConnection; |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 46 | import net.onrc.onos.util.LocalTopologyEventListener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 47 | |
| 48 | public class OnosPublisher implements IDeviceListener, IOFSwitchListener, |
| 49 | ILinkDiscoveryListener, IFloodlightModule { |
| 50 | |
| 51 | protected IDeviceStorage devStore; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 52 | protected ISwitchStorage swStore; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 53 | protected ILinkStorage linkStore; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 54 | protected static Logger log; |
| 55 | protected IDeviceService deviceService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 56 | protected IControllerRegistryService registryService; |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 57 | protected GraphDBOperation op; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 58 | |
| 59 | protected static final String DBConfigFile = "dbconf"; |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 60 | protected static final String CleanupEnabled = "EnableCleanup"; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 61 | protected IThreadPoolService threadPool; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 62 | protected IFloodlightProviderService floodlightProvider; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 63 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 64 | protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 65 | protected SingletonTask cleanupTask; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 66 | protected ILinkDiscoveryService linkDiscovery; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Cleanup and synch switch state from registry |
| 70 | */ |
| 71 | protected class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 72 | @Override |
| 73 | public void run() { |
| 74 | try { |
| 75 | log.debug("Running cleanup thread"); |
| 76 | switchCleanup(); |
| 77 | } |
| 78 | catch (Exception e) { |
| 79 | log.error("Error in cleanup thread", e); |
| 80 | } finally { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 81 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 82 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 83 | TimeUnit.SECONDS); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void controlChanged(long dpid, boolean hasControl) { |
| 89 | // TODO Auto-generated method stub |
| 90 | |
| 91 | if (hasControl) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 92 | log.debug("got control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 93 | swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE); |
| 94 | registryService.releaseControl(dpid); |
| 95 | } |
| 96 | } |
| 97 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 98 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 99 | protected void switchCleanup() { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 100 | op.close(); |
| 101 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 102 | |
| 103 | log.debug("Checking for inactive switches"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 104 | // For each switch check if a controller exists in controller registry |
| 105 | for (ISwitchObject sw: switches) { |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 106 | //log.debug("checking if switch is inactive: {}", sw.getDPID()); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 107 | try { |
| 108 | long dpid = HexString.toLong(sw.getDPID()); |
| 109 | String controller = registryService.getControllerForSwitch(dpid); |
| 110 | if (controller == null) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 111 | log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 112 | registryService.requestControl(dpid, new SwitchCleanup()); |
Umesh Krishnaswamy | 255b988 | 2013-04-02 19:55:43 -0700 | [diff] [blame] | 113 | //} else { |
| 114 | // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 115 | } |
| 116 | } catch (NumberFormatException e) { |
| 117 | // TODO Auto-generated catch block |
| 118 | e.printStackTrace(); |
| 119 | } catch (RegistryException e) { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 120 | log.debug("Caught RegistryException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 121 | e.printStackTrace(); |
| 122 | } |
| 123 | } |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 124 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 125 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public void linkDiscoveryUpdate(LDUpdate update) { |
| 129 | // TODO Auto-generated method stub |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 130 | Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort()); |
| 131 | log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 132 | switch (update.getOperation()) { |
| 133 | |
| 134 | case LINK_REMOVED: |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 135 | linkStore.update(lt, DM_OPERATION.DELETE); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 136 | // TODO: Move network map link removal here |
| 137 | // reconcile paths here |
| 138 | // IPortObject srcPort = conn.utils().searchPort(conn, HexString.toHexString(update.getSrc()), update.getSrcPort()); |
| 139 | break; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 140 | case LINK_UPDATED: |
| 141 | linkStore.update(lt, DM_OPERATION.UPDATE); |
| 142 | break; |
| 143 | case LINK_ADDED: |
| 144 | linkStore.update(lt, DM_OPERATION.INSERT); |
| 145 | break; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 146 | |
| 147 | default: |
| 148 | break; |
| 149 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 150 | |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void addedSwitch(IOFSwitch sw) { |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 155 | |
| 156 | if (registryService.hasControl(sw.getId())) { |
| 157 | swStore.update(sw.getStringId(), SwitchState.ACTIVE, DM_OPERATION.UPDATE); |
| 158 | for (OFPhysicalPort port: sw.getPorts()) { |
| 159 | swStore.addPort(sw.getStringId(), port); |
| 160 | } |
| 161 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 162 | |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removedSwitch(IOFSwitch sw) { |
| 167 | // TODO Auto-generated method stub |
| 168 | |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void switchPortChanged(Long switchId) { |
| 173 | // TODO Auto-generated method stub |
| 174 | |
| 175 | } |
| 176 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
| 180 | // TODO Auto-generated method stub |
| 181 | swStore.addPort(HexString.toHexString(switchId), port); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
| 186 | // TODO Auto-generated method stub |
| 187 | swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber()); |
| 188 | } |
| 189 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 190 | @Override |
| 191 | public String getName() { |
| 192 | return "OnosPublisher"; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void deviceAdded(IDevice device) { |
| 197 | // TODO Auto-generated method stub |
| 198 | log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString()); |
| 199 | devStore.addDevice(device); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void deviceRemoved(IDevice device) { |
| 204 | // TODO Auto-generated method stub |
| 205 | |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void deviceMoved(IDevice device) { |
| 210 | // TODO Auto-generated method stub |
| 211 | devStore.changeDeviceAttachments(device); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void deviceIPV4AddrChanged(IDevice device) { |
| 217 | // TODO Auto-generated method stub |
| 218 | devStore.changeDeviceIPv4Address(device); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void deviceVlanChanged(IDevice device) { |
| 224 | // TODO Auto-generated method stub |
| 225 | } |
| 226 | |
| 227 | |
| 228 | @Override |
| 229 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 230 | // TODO Auto-generated method stub |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 236 | // TODO Auto-generated method stub |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 242 | Collection<Class<? extends IFloodlightService>> l = |
| 243 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 244 | l.add(IFloodlightProviderService.class); |
| 245 | l.add(IDeviceService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 246 | l.add(IThreadPoolService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 247 | return l; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void init(FloodlightModuleContext context) |
| 252 | throws FloodlightModuleException { |
| 253 | // TODO Auto-generated method stub |
| 254 | Map<String, String> configMap = context.getConfigParams(this); |
| 255 | String conf = configMap.get(DBConfigFile); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 256 | op = new GraphDBOperation(GraphDBConnection.getInstance(conf)); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 257 | |
| 258 | log = LoggerFactory.getLogger(OnosPublisher.class); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 259 | floodlightProvider = |
| 260 | context.getServiceImpl(IFloodlightProviderService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 261 | deviceService = context.getServiceImpl(IDeviceService.class); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 262 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 263 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 264 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 265 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 266 | devStore = new DeviceStorageImpl(); |
| 267 | devStore.init(conf); |
| 268 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 269 | swStore = new SwitchStorageImpl(); |
| 270 | swStore.init(conf); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 271 | |
| 272 | linkStore = new LinkStorageImpl(); |
| 273 | linkStore.init(conf); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 274 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 275 | log.debug("Initializing OnosPublisher module with {}", conf); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void startUp(FloodlightModuleContext context) { |
| 281 | // TODO Auto-generated method stub |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 282 | Map<String, String> configMap = context.getConfigParams(this); |
| 283 | String cleanupNeeded = configMap.get(CleanupEnabled); |
| 284 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 285 | deviceService.addListener(this); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 286 | floodlightProvider.addOFSwitchListener(this); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 287 | linkDiscovery.addListener(this); |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 288 | |
| 289 | log.debug("Adding EventListener"); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 290 | IDBConnection conn = op.getDBConnection(); |
| 291 | conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 292 | // Setup the Cleanup task. |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 293 | if (cleanupNeeded == null || !cleanupNeeded.equals("False")) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 294 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
| 295 | cleanupTask = new SingletonTask(ses, new SwitchCleanup()); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 296 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS); |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 297 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | } |