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