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