HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.floodlightlistener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 6 | import java.util.List; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 7 | import java.util.Map; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 8 | import java.util.concurrent.ScheduledExecutorService; |
| 9 | import java.util.concurrent.TimeUnit; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 10 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFSwitch; |
| 13 | import net.floodlightcontroller.core.IOFSwitchListener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 18 | import net.floodlightcontroller.core.util.SingletonTask; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 19 | import net.floodlightcontroller.devicemanager.IDevice; |
| 20 | import net.floodlightcontroller.devicemanager.IDeviceListener; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.routing.Link; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 23 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 24 | import net.onrc.onos.graph.GraphDBConnection; |
| 25 | import net.onrc.onos.graph.GraphDBOperation; |
| 26 | import net.onrc.onos.graph.IDBConnection; |
| 27 | import net.onrc.onos.graph.LocalTopologyEventListener; |
HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 29 | import net.onrc.onos.ofcontroller.core.ILinkStorage; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener; |
| 33 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 34 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
| 35 | import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl; |
HIGUCHI Yuta | ed49ef7 | 2013-06-12 11:34:10 -0700 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener; |
| 38 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.proxyarp.ArpMessage; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 41 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 42 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 43 | import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback; |
| 44 | import net.onrc.onos.registry.controller.RegistryException; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 45 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 46 | import org.openflow.protocol.OFPhysicalPort; |
| 47 | import org.openflow.util.HexString; |
| 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| 51 | import com.google.common.net.InetAddresses; |
| 52 | |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 53 | public class NetworkGraphPublisher implements IDeviceListener, |
| 54 | IOFSwitchListener, |
| 55 | IOFSwitchPortListener, |
| 56 | ILinkDiscoveryListener, |
| 57 | IFloodlightModule, |
| 58 | INetworkGraphService { |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 59 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 60 | protected IDeviceStorage devStore; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 61 | protected ISwitchStorage swStore; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 62 | protected ILinkStorage linkStore; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 63 | protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 64 | //protected IDeviceService deviceService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 65 | protected IControllerRegistryService registryService; |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 66 | protected GraphDBOperation op; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 67 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 68 | protected static final String DBConfigFile = "dbconf"; |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 69 | protected static final String CleanupEnabled = "EnableCleanup"; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 70 | protected IThreadPoolService threadPool; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 71 | protected IFloodlightProviderService floodlightProvider; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 72 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 73 | protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 74 | protected SingletonTask cleanupTask; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 75 | protected ILinkDiscoveryService linkDiscovery; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 76 | |
| 77 | protected IDatagridService datagridService; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 80 | * Cleanup and synch switch state from registry |
| 81 | */ |
| 82 | protected class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 83 | @Override |
| 84 | public void run() { |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 85 | String old = Thread.currentThread().getName(); |
Yuta HIGUCHI | 1412e8c | 2014-01-03 17:19:01 -0800 | [diff] [blame] | 86 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 87 | try { |
| 88 | log.debug("Running cleanup thread"); |
| 89 | switchCleanup(); |
| 90 | } |
| 91 | catch (Exception e) { |
| 92 | log.error("Error in cleanup thread", e); |
| 93 | } finally { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 94 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 95 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 96 | TimeUnit.SECONDS); |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 97 | Thread.currentThread().setName(old); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void controlChanged(long dpid, boolean hasControl) { |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 103 | if (hasControl) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 104 | log.debug("got control to set inactive sw {}", HexString.toHexString(dpid)); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 105 | try { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 106 | // Get the affected ports |
| 107 | List<Short> ports = swStore.getPorts(HexString.toHexString(dpid)); |
| 108 | // Get the affected links |
| 109 | List<Link> links = linkStore.getLinks(HexString.toHexString(dpid)); |
| 110 | // Get the affected reverse links |
| 111 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid)); |
| 112 | links.addAll(reverseLinks); |
| 113 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 114 | //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) { |
| 115 | if (swStore.deactivateSwitch(HexString.toHexString(dpid))) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 116 | registryService.releaseControl(dpid); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 117 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 118 | // TODO publish UPDATE_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 119 | // |
| 120 | // NOTE: Here we explicitly send |
| 121 | // notification to remove the |
| 122 | // switch, because it is inactive |
| 123 | // |
| 124 | TopologyElement topologyElement = |
| 125 | new TopologyElement(dpid); |
| 126 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 127 | |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 128 | // Publish: remove the affected ports |
| 129 | for (Short port : ports) { |
| 130 | TopologyElement topologyElementPort = |
| 131 | new TopologyElement(dpid, port); |
| 132 | datagridService.notificationSendTopologyElementRemoved(topologyElementPort); |
| 133 | } |
| 134 | // Publish: remove the affected links |
| 135 | for (Link link : links) { |
| 136 | TopologyElement topologyElementLink = |
| 137 | new TopologyElement(link.getSrc(), |
| 138 | link.getSrcPort(), |
| 139 | link.getDst(), |
| 140 | link.getDstPort()); |
| 141 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 142 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 143 | } |
| 144 | } catch (Exception e) { |
| 145 | log.error("Error in SwitchCleanup:controlChanged ", e); |
| 146 | } |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 147 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 150 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 151 | protected void switchCleanup() { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 152 | op.close(); |
| 153 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 154 | |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 155 | log.debug("Checking for inactive switches"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 156 | // For each switch check if a controller exists in controller registry |
| 157 | for (ISwitchObject sw: switches) { |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 158 | //log.debug("checking if switch is inactive: {}", sw.getDPID()); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 159 | try { |
| 160 | long dpid = HexString.toLong(sw.getDPID()); |
| 161 | String controller = registryService.getControllerForSwitch(dpid); |
| 162 | if (controller == null) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 163 | log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 164 | registryService.requestControl(dpid, new SwitchCleanup()); |
Umesh Krishnaswamy | 255b988 | 2013-04-02 19:55:43 -0700 | [diff] [blame] | 165 | //} else { |
| 166 | // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 167 | } |
| 168 | } catch (NumberFormatException e) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 169 | log.debug("Caught NumberFormatException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 170 | e.printStackTrace(); |
| 171 | } catch (RegistryException e) { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 172 | log.debug("Caught RegistryException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 173 | e.printStackTrace(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 174 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 175 | } |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 176 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 177 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 178 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 179 | @Override |
| 180 | public void linkDiscoveryUpdate(LDUpdate update) { |
| 181 | Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort()); |
| 182 | //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 183 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 184 | switch (update.getOperation()) { |
| 185 | case LINK_REMOVED: |
| 186 | log.debug("LinkDiscoveryUpdate(): Removing link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 187 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 188 | if (linkStore.deleteLink(lt)) { |
| 189 | // TODO publish DELETE_LINK event here |
| 190 | TopologyElement topologyElement = |
| 191 | new TopologyElement(update.getSrc(), |
| 192 | update.getSrcPort(), |
| 193 | update.getDst(), |
| 194 | update.getDstPort()); |
| 195 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 196 | } |
| 197 | break; |
| 198 | case LINK_UPDATED: |
| 199 | log.debug("LinkDiscoveryUpdate(): Updating link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 200 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 201 | LinkInfo linfo = linkStore.getLinkInfo(lt); |
| 202 | // TODO update "linfo" using portState derived using "update" |
| 203 | if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) { |
| 204 | // TODO publish UPDATE_LINK event here |
| 205 | // |
| 206 | // TODO NOTE: Here we assume that updated |
| 207 | // link is UP. |
| 208 | // |
| 209 | TopologyElement topologyElement = |
| 210 | new TopologyElement(update.getSrc(), |
| 211 | update.getSrcPort(), |
| 212 | update.getDst(), |
| 213 | update.getDstPort()); |
| 214 | datagridService.notificationSendTopologyElementUpdated(topologyElement); |
| 215 | } |
| 216 | break; |
| 217 | case LINK_ADDED: |
| 218 | log.debug("LinkDiscoveryUpdate(): Adding link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 219 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 220 | if (linkStore.addLink(lt)) { |
| 221 | // TODO publish ADD_LINK event here |
| 222 | TopologyElement topologyElement = |
| 223 | new TopologyElement(update.getSrc(), |
| 224 | update.getSrcPort(), |
| 225 | update.getDst(), |
| 226 | update.getDstPort()); |
| 227 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
| 228 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 229 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 236 | |
| 237 | @Override |
| 238 | public void addedSwitch(IOFSwitch sw) { |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 239 | if (registryService.hasControl(sw.getId())) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 240 | if (swStore.addSwitch(sw)) { |
| 241 | // TODO publish ADD_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 242 | TopologyElement topologyElement = |
| 243 | new TopologyElement(sw.getId()); |
Pavlin Radoslavov | 649c97d | 2013-11-04 16:00:23 -0800 | [diff] [blame] | 244 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 245 | |
| 246 | // Publish: add the ports |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 247 | // TODO: Add only ports that are UP? |
| 248 | for (OFPhysicalPort port : sw.getPorts()) { |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 249 | TopologyElement topologyElementPort = |
| 250 | new TopologyElement(sw.getId(), port.getPortNumber()); |
| 251 | datagridService.notificationSendTopologyElementAdded(topologyElementPort); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 252 | |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 253 | // Allow links to be discovered on this port now that it's |
| 254 | // in the database |
| 255 | linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber()); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 256 | } |
Pavlin Radoslavov | 649c97d | 2013-11-04 16:00:23 -0800 | [diff] [blame] | 257 | |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 258 | // Add all links that might be connected already |
| 259 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 260 | // Add all reverse links as well |
| 261 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 262 | links.addAll(reverseLinks); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 263 | |
| 264 | // Publish: add the links |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 265 | for (Link link : links) { |
| 266 | TopologyElement topologyElementLink = |
| 267 | new TopologyElement(link.getSrc(), |
| 268 | link.getSrcPort(), |
| 269 | link.getDst(), |
| 270 | link.getDstPort()); |
| 271 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 272 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 273 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 274 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void removedSwitch(IOFSwitch sw) { |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 279 | /* |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 280 | if (registryService.hasControl(sw.getId())) { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 281 | // Get the affected ports |
| 282 | List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId())); |
| 283 | // Get the affected links |
| 284 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 285 | // Get the affected reverse links |
| 286 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 287 | links.addAll(reverseLinks); |
| 288 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 289 | if (swStore.deleteSwitch(sw.getStringId())) { |
| 290 | // TODO publish DELETE_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 291 | TopologyElement topologyElement = |
| 292 | new TopologyElement(sw.getId()); |
| 293 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 294 | |
| 295 | // Publish: remove the affected ports |
| 296 | for (Short port : ports) { |
| 297 | TopologyElement topologyElementPort = |
| 298 | new TopologyElement(sw.getId(), port); |
| 299 | datagridService.notificationSendTopologyElementRemoved(topologyElementPort); |
| 300 | } |
| 301 | // Publish: remove the affected links |
| 302 | for (Link link : links) { |
| 303 | TopologyElement topologyElementLink = |
| 304 | new TopologyElement(link.getSrc(), |
| 305 | link.getSrcPort(), |
| 306 | link.getDst(), |
| 307 | link.getDstPort()); |
| 308 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 309 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 312 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void switchPortChanged(Long switchId) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 317 | // NOTE: Event not needed here. This callback always coincide with add/remove callback. |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 320 | |
| 321 | @Override |
| 322 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 323 | if (swStore.addPort(HexString.toHexString(switchId), port)) { |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 324 | // Allow links to be discovered on this port now that it's |
| 325 | // in the database |
| 326 | linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber()); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 327 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 328 | // TODO publish ADD_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 329 | TopologyElement topologyElement = |
| 330 | new TopologyElement(switchId, port.getPortNumber()); |
| 331 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 332 | |
| 333 | // Add all links that might be connected already |
| 334 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 335 | // Add all reverse links as well |
| 336 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 337 | links.addAll(reverseLinks); |
| 338 | |
| 339 | // Publish: add the links |
| 340 | for (Link link : links) { |
| 341 | TopologyElement topologyElementLink = |
| 342 | new TopologyElement(link.getSrc(), |
| 343 | link.getSrcPort(), |
| 344 | link.getDst(), |
| 345 | link.getDstPort()); |
| 346 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 347 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 348 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 353 | // Remove all links that might be connected already |
| 354 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 355 | // Remove all reverse links as well |
| 356 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 357 | links.addAll(reverseLinks); |
| 358 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 359 | if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) { |
| 360 | // TODO publish DELETE_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 361 | TopologyElement topologyElement = |
| 362 | new TopologyElement(switchId, port.getPortNumber()); |
| 363 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 364 | |
| 365 | // Publish: remove the links |
| 366 | for (Link link : links) { |
| 367 | TopologyElement topologyElementLink = |
| 368 | new TopologyElement(link.getSrc(), |
| 369 | link.getSrcPort(), |
| 370 | link.getDst(), |
| 371 | link.getDstPort()); |
| 372 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 373 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 374 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 377 | @Override |
| 378 | public String getName() { |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 379 | return "NetworkGraphPublisher"; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public void deviceAdded(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 384 | log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString()); |
| 385 | devStore.addDevice(device); |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 386 | for (int intIpv4Address : device.getIPv4Addresses()) { |
| 387 | datagridService.sendArpRequest( |
| 388 | ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address))); |
| 389 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void deviceRemoved(IDevice device) { |
| 394 | // TODO Auto-generated method stub |
Jonathan Hart | 4fce4ed | 2013-11-01 21:29:21 -0700 | [diff] [blame] | 395 | devStore.removeDevice(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void deviceMoved(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 400 | devStore.changeDeviceAttachments(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void deviceIPV4AddrChanged(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 405 | devStore.changeDeviceIPv4Address(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void deviceVlanChanged(IDevice device) { |
| 410 | // TODO Auto-generated method stub |
| 411 | } |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 412 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 413 | |
| 414 | @Override |
| 415 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 416 | Collection<Class<? extends IFloodlightService>> l = |
| 417 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 418 | l.add(INetworkGraphService.class); |
| 419 | return l; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 424 | Map<Class<? extends IFloodlightService>, |
| 425 | IFloodlightService> m = |
| 426 | new HashMap<Class<? extends IFloodlightService>, |
| 427 | IFloodlightService>(); |
| 428 | m.put(INetworkGraphService.class, this); |
| 429 | return m; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 434 | Collection<Class<? extends IFloodlightService>> l = |
| 435 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 436 | l.add(IFloodlightProviderService.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 437 | //l.add(IDeviceService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 438 | l.add(IDatagridService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 439 | l.add(IThreadPoolService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 440 | return l; |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public void init(FloodlightModuleContext context) |
| 445 | throws FloodlightModuleException { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 446 | Map<String, String> configMap = context.getConfigParams(this); |
| 447 | String conf = configMap.get(DBConfigFile); |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 448 | op = new GraphDBOperation(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 449 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 450 | floodlightProvider = |
| 451 | context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 452 | //deviceService = context.getServiceImpl(IDeviceService.class); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 453 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 454 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 455 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 456 | datagridService = context.getServiceImpl(IDatagridService.class); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 457 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 458 | devStore = new DeviceStorageImpl(); |
| 459 | devStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 460 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 461 | swStore = new SwitchStorageImpl(); |
| 462 | swStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 463 | |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 464 | linkStore = new LinkStorageImpl(); |
| 465 | linkStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 466 | |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 467 | log.debug("Initializing NetworkGraphPublisher module with {}", conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 468 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void startUp(FloodlightModuleContext context) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 473 | Map<String, String> configMap = context.getConfigParams(this); |
| 474 | String cleanupNeeded = configMap.get(CleanupEnabled); |
| 475 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 476 | //deviceService.addListener(this); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 477 | floodlightProvider.addOFSwitchListener(this); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 478 | linkDiscovery.addListener(this); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 479 | |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 480 | log.debug("Adding EventListener"); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 481 | IDBConnection conn = op.getDBConnection(); |
| 482 | conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn)); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 483 | // Setup the Cleanup task. |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 484 | if (cleanupNeeded == null || !cleanupNeeded.equals("False")) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 485 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
| 486 | cleanupTask = new SingletonTask(ses, new SwitchCleanup()); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 487 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS); |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 488 | } |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 489 | |
| 490 | // |
| 491 | // NOTE: No need to register with the Datagrid Service, |
| 492 | // because we don't need to receive any notifications from it. |
| 493 | // |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | } |