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