Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 6 | import java.util.HashMap; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 7 | import java.util.LinkedList; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 8 | import java.util.List; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 9 | import java.util.Map; |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 10 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | import java.util.concurrent.ConcurrentMap; |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 13 | import java.util.concurrent.LinkedBlockingQueue; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 14 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 17 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 19 | import net.floodlightcontroller.core.IOFSwitch.PortChangeType; |
| 20 | import net.floodlightcontroller.core.IOFSwitchListener; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 22 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.core.util.SingletonTask; |
| 26 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 27 | |
| 28 | import net.onrc.onos.api.batchoperation.BatchOperationEntry; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 30 | import net.onrc.onos.core.datagrid.IEventChannel; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.hostmanager.Host; |
| 32 | import net.onrc.onos.core.hostmanager.IHostListener; |
| 33 | import net.onrc.onos.core.hostmanager.IHostService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener; |
| 35 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.linkdiscovery.Link; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.registry.RegistryException; |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.util.OnosInstanceId; |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.util.PortNumberUtils; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 43 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 44 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 45 | import org.apache.commons.lang3.concurrent.ConcurrentUtils; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 47 | import org.projectfloodlight.openflow.util.HexString; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 50 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 51 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 52 | * Class for publishing topology-related events. |
| 53 | * |
| 54 | * The events are received from the discovery modules, reformatted and |
| 55 | * published to the other ONOS instances. |
| 56 | * |
| 57 | * TODO: Add a synchronization mechanism when publishing the events to |
| 58 | * preserve the ordering and to avoid mismatch in the local "published" state, |
| 59 | * because each of the caller (the discovery modules) might be running |
| 60 | * on a different thread. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 61 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 62 | public class TopologyPublisher implements IOFSwitchListener, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | ILinkDiscoveryListener, |
| 64 | IFloodlightModule, |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 65 | IHostListener, |
| 66 | ITopologyPublisherService { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 67 | private static final Logger log = |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 68 | LoggerFactory.getLogger(TopologyPublisher.class); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 69 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 70 | private IFloodlightProviderService floodlightProvider; |
| 71 | private ILinkDiscoveryService linkDiscovery; |
| 72 | private IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 73 | private ITopologyService topologyService; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 74 | private IDatagridService datagridService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 75 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 76 | private IHostService hostService; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 77 | |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 78 | private MutableTopology mutableTopology; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 79 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 80 | private static final String ENABLE_CLEANUP_PROPERTY = "EnableCleanup"; |
| 81 | private boolean cleanupEnabled = true; |
| 82 | private static final int CLEANUP_TASK_INTERVAL = 60; // in seconds |
| 83 | private SingletonTask cleanupTask; |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 84 | private DelayedOperationsHandler delayedOperationsHandler = |
| 85 | new DelayedOperationsHandler(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 86 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 87 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
| 88 | |
| 89 | // |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 90 | // Local state for keeping track of locally published events so we can |
| 91 | // cleanup properly when an entry is removed. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 92 | // |
| 93 | // We keep all Port, (incoming) Link and Host events per Switch DPID: |
| 94 | // - If a switch goes down, we remove all corresponding Port, Link and |
| 95 | // Host events. |
| 96 | // - If a port on a switch goes down, we remove all corresponding Link |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 97 | // and Host events attached to this port. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 98 | // |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 99 | // TODO: What to do if the Mastership changes? |
| 100 | // - Cleanup state from publishedFoo maps, but do not send REMOVE events? |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 101 | // |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 102 | private ConcurrentMap<Dpid, MastershipEvent> publishedMastershipEvents = |
| 103 | new ConcurrentHashMap<>(); |
| 104 | private ConcurrentMap<Dpid, SwitchEvent> publishedSwitchEvents = |
| 105 | new ConcurrentHashMap<>(); |
| 106 | private ConcurrentMap<Dpid, ConcurrentMap<ByteBuffer, PortEvent>> |
| 107 | publishedPortEvents = new ConcurrentHashMap<>(); |
| 108 | private ConcurrentMap<Dpid, ConcurrentMap<ByteBuffer, LinkEvent>> |
| 109 | publishedLinkEvents = new ConcurrentHashMap<>(); |
| 110 | private ConcurrentMap<Dpid, ConcurrentMap<ByteBuffer, HostEvent>> |
| 111 | publishedHostEvents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 112 | |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 113 | private BlockingQueue<TopologyBatchOperation> delayedOperations = |
| 114 | new LinkedBlockingQueue<>(); |
| 115 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 116 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 117 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 118 | * Gets the ONOS Instance ID. |
| 119 | * |
| 120 | * @return the ONOS Instance ID. |
| 121 | */ |
| 122 | private OnosInstanceId getOnosInstanceId() { |
| 123 | return registryService.getOnosInstanceId(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Cleanup old switches from the topology. Old switches are those which |
| 128 | * have no controller in the registry. |
| 129 | * |
| 130 | * TODO: The overall switch cleanup mechanism needs refactoring/redesign. |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 131 | */ |
| 132 | private class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 133 | @Override |
| 134 | public void run() { |
| 135 | String old = Thread.currentThread().getName(); |
| 136 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 137 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 138 | try { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 139 | if (log.isTraceEnabled()) { |
| 140 | log.trace("Running cleanup thread"); |
| 141 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 142 | switchCleanup(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 143 | } finally { |
| 144 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 145 | TimeUnit.SECONDS); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 146 | Thread.currentThread().setName(old); |
| 147 | } |
| 148 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 149 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 150 | /** |
| 151 | * First half of the switch cleanup operation. This method will attempt |
| 152 | * to get control of any switch it sees without a controller via the |
| 153 | * registry. |
| 154 | */ |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 155 | private void switchCleanup() { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 156 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 157 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 158 | if (log.isTraceEnabled()) { |
| 159 | log.trace("Checking for inactive switches"); |
| 160 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 161 | // For each switch check if a controller exists in controller |
| 162 | // registry |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | for (Switch sw : switches) { |
Praseed Balakrishnan | e82adc6 | 2014-08-04 10:59:24 -0700 | [diff] [blame] | 164 | // FIXME How to handle case where Switch has never been |
| 165 | // registered to ZK |
| 166 | if (sw.getConfigState() == ConfigState.CONFIGURED) { |
| 167 | continue; |
| 168 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 169 | try { |
| 170 | String controller = |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 171 | registryService.getControllerForSwitch(sw.getDpid().value()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 172 | if (controller == null) { |
| 173 | log.debug("Requesting control to set switch {} INACTIVE", |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 174 | sw.getDpid()); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 175 | registryService.requestControl(sw.getDpid().value(), |
| 176 | this); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 177 | } |
| 178 | } catch (RegistryException e) { |
| 179 | log.error("Caught RegistryException in cleanup thread", e); |
| 180 | } |
| 181 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Second half of the switch cleanup operation. If the registry grants |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 186 | * control of a switch, we can be sure no other instance is writing |
| 187 | * this switch to the topology, so we can remove it now. |
| 188 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 189 | * @param dpid the dpid of the switch we requested control for |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 190 | * @param hasControl whether we got control or not |
| 191 | */ |
| 192 | @Override |
| 193 | public void controlChanged(long dpid, boolean hasControl) { |
| 194 | if (hasControl) { |
| 195 | log.debug("Got control to set switch {} INACTIVE", |
| 196 | HexString.toHexString(dpid)); |
Jonathan Hart | e02cf54 | 2014-04-02 16:24:44 -0700 | [diff] [blame] | 197 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 198 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(dpid)); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 199 | publishRemoveSwitchEvent(switchEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 200 | registryService.releaseControl(dpid); |
| 201 | } |
| 202 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 203 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 204 | |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 205 | /** |
| 206 | * A class to deal with Topology Operations that couldn't be pushed |
| 207 | * to the Global Log writer, because they need to be delayed. |
| 208 | * For example, a link cannot be pushed before the switches on both |
| 209 | * ends are in the Global Log. |
| 210 | * |
| 211 | * TODO: This is an ugly hack that should go away: right now we have to |
| 212 | * keep trying periodically. |
| 213 | * TODO: Currently, we retry only ADD Link Events, everything else |
| 214 | * is thrown away. |
| 215 | */ |
| 216 | private class DelayedOperationsHandler extends Thread { |
| 217 | private static final long RETRY_INTERVAL_MS = 10; // 10ms |
| 218 | |
| 219 | @Override |
| 220 | public void run() { |
| 221 | List<TopologyBatchOperation> operations = new LinkedList<>(); |
| 222 | |
| 223 | this.setName("TopologyPublisher.DelayedOperationsHandler " + |
| 224 | this.getId()); |
| 225 | // |
| 226 | // The main loop |
| 227 | // |
| 228 | while (true) { |
| 229 | try { |
| 230 | // |
| 231 | // Block-waiting for an operation to be added, sleep |
| 232 | // and try to publish it again. |
| 233 | // |
| 234 | TopologyBatchOperation firstTbo = delayedOperations.take(); |
| 235 | Thread.sleep(RETRY_INTERVAL_MS); |
| 236 | operations.add(firstTbo); |
| 237 | delayedOperations.drainTo(operations); |
| 238 | |
| 239 | // Retry only the appropriate operations |
| 240 | for (TopologyBatchOperation tbo : operations) { |
| 241 | for (BatchOperationEntry< |
| 242 | TopologyBatchOperation.Operator, |
| 243 | TopologyEvent> boe : tbo.getOperations()) { |
| 244 | TopologyBatchOperation.Operator oper = |
| 245 | boe.getOperator(); |
| 246 | switch (oper) { |
| 247 | case ADD: |
| 248 | TopologyEvent topologyEvent = boe.getTarget(); |
| 249 | LinkEvent linkEvent = |
| 250 | topologyEvent.getLinkEvent(); |
| 251 | // |
| 252 | // Test whether the Link Event still can be |
| 253 | // published. |
| 254 | // TODO: The implementation below has a bug: |
| 255 | // If it happens that the same Link Event was |
| 256 | // removed in the middle of checking, we might |
| 257 | // incorrectly publish it again from here. |
| 258 | // |
| 259 | if (linkEvent == null) { |
| 260 | break; |
| 261 | } |
| 262 | ConcurrentMap<ByteBuffer, LinkEvent> |
| 263 | linkEvents = publishedLinkEvents.get( |
| 264 | linkEvent.getDst().getDpid()); |
| 265 | if (linkEvents == null) { |
| 266 | break; |
| 267 | } |
| 268 | if (linkEvents.get(linkEvent.getIDasByteBuffer()) == null) { |
| 269 | break; |
| 270 | } |
| 271 | publishAddLinkEvent(linkEvent); |
| 272 | break; |
| 273 | case REMOVE: |
| 274 | break; |
| 275 | default: |
| 276 | log.error("Unknown Topology Batch Operation {}", oper); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } catch (InterruptedException exception) { |
| 282 | log.debug("Exception processing delayed operations: ", |
| 283 | exception); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 289 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 290 | public void linkAdded(Link link) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 291 | LinkEvent linkEvent = new LinkEvent( |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 292 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 293 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 294 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 295 | // FIXME should be merging, with existing attrs, etc.. |
| 296 | // TODO define attr name as constant somewhere. |
| 297 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 298 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 299 | TopologyElement.TYPE_PACKET_LAYER); |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 300 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 301 | ConfigState.NOT_CONFIGURED.toString()); |
| 302 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 303 | AdminStatus.ACTIVE.toString()); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 304 | linkEvent.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 305 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 306 | publishAddLinkEvent(linkEvent); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void linkRemoved(Link link) { |
| 311 | LinkEvent linkEvent = new LinkEvent( |
| 312 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 313 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 314 | |
| 315 | // FIXME should be merging, with existing attrs, etc.. |
| 316 | // TODO define attr name as constant somewhere. |
| 317 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 318 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 319 | TopologyElement.TYPE_PACKET_LAYER); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 320 | linkEvent.freeze(); |
| 321 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 322 | publishRemoveLinkEvent(linkEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 323 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 324 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 325 | /* ***************** |
| 326 | * IOFSwitchListener |
| 327 | * *****************/ |
| 328 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 329 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 330 | public void switchActivatedMaster(long swId) { |
| 331 | IOFSwitch sw = floodlightProvider.getSwitch(swId); |
| 332 | final Dpid dpid = new Dpid(swId); |
| 333 | if (sw == null) { |
| 334 | log.warn("Added switch not available {} ", dpid); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | controllerRoleChanged(dpid, Role.MASTER); |
| 339 | |
| 340 | SwitchEvent switchEvent = new SwitchEvent(dpid); |
| 341 | // FIXME should be merging, with existing attrs, etc.. |
| 342 | // TODO define attr name as constant somewhere. |
| 343 | // TODO populate appropriate attributes. |
| 344 | switchEvent.createStringAttribute(TopologyElement.TYPE, |
| 345 | TopologyElement.TYPE_PACKET_LAYER); |
| 346 | switchEvent.createStringAttribute("ConnectedSince", |
| 347 | sw.getConnectedSince().toString()); |
| 348 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 349 | ConfigState.NOT_CONFIGURED.toString()); |
| 350 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 351 | AdminStatus.ACTIVE.toString()); |
| 352 | switchEvent.freeze(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 353 | // The Port events |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 354 | List<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 355 | for (OFPortDesc port : sw.getPorts()) { |
| 356 | PortEvent portEvent = new PortEvent(dpid, |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 357 | PortNumberUtils.openFlow(port)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 358 | // FIXME should be merging, with existing attrs, etc.. |
| 359 | // TODO define attr name as constant somewhere. |
| 360 | // TODO populate appropriate attributes. |
| 361 | portEvent.createStringAttribute("name", port.getName()); |
| 362 | portEvent.createStringAttribute(TopologyElement.TYPE, |
| 363 | TopologyElement.TYPE_PACKET_LAYER); |
| 364 | portEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 365 | ConfigState.NOT_CONFIGURED.toString()); |
| 366 | portEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 367 | AdminStatus.ACTIVE.toString()); |
| 368 | |
| 369 | portEvent.freeze(); |
| 370 | portEvents.add(portEvent); |
| 371 | } |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 372 | publishAddSwitchEvent(switchEvent, portEvents); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void switchActivatedEqual(long swId) { |
| 377 | final Dpid dpid = new Dpid(swId); |
| 378 | controllerRoleChanged(dpid, Role.EQUAL); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public void switchMasterToEqual(long swId) { |
| 383 | final Dpid dpid = new Dpid(swId); |
| 384 | controllerRoleChanged(dpid, Role.EQUAL); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public void switchEqualToMaster(long swId) { |
| 389 | // for now treat as switchActivatedMaster |
| 390 | switchActivatedMaster(swId); |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void switchDisconnected(long swId) { |
| 395 | final Dpid dpid = new Dpid(swId); |
| 396 | |
| 397 | log.debug("Local switch disconnected: dpid = {} role = {}", dpid); |
| 398 | |
| 399 | Role role = Role.SLAVE; // TODO: Should be Role.UNKNOWN |
| 400 | |
| 401 | MastershipEvent mastershipEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 402 | new MastershipEvent(dpid, getOnosInstanceId(), role); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 403 | // FIXME should be merging, with existing attrs, etc.. |
| 404 | // TODO define attr name as constant somewhere. |
| 405 | // TODO populate appropriate attributes. |
| 406 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
| 407 | TopologyElement.TYPE_ALL_LAYERS); |
| 408 | mastershipEvent.freeze(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 409 | publishRemoveSwitchMastershipEvent(mastershipEvent); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void switchPortChanged(long swId, OFPortDesc port, |
| 414 | PortChangeType changeType) { |
| 415 | switch (changeType) { |
| 416 | case ADD: |
| 417 | switchPortAdded(swId, port); |
| 418 | break; |
| 419 | case DELETE: |
| 420 | switchPortRemoved(swId, port); |
| 421 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 422 | case UP: |
Pavlin Radoslavov | 7946c61 | 2014-08-13 15:06:00 -0700 | [diff] [blame] | 423 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 424 | switchPortAdded(swId, port); |
| 425 | break; |
| 426 | case DOWN: |
| 427 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 428 | switchPortRemoved(swId, port); |
| 429 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 430 | case OTHER_UPDATE: |
| 431 | default: |
| 432 | // XXX S what is the right set of port change handlers? |
| 433 | log.debug("Topology publisher does not handle these port updates: {}", |
| 434 | changeType); |
| 435 | } |
| 436 | } |
| 437 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 438 | /** |
| 439 | * Prepares an event for adding a port on a switch. |
| 440 | * |
| 441 | * @param switchId the switch ID (DPID) |
| 442 | * @param port the port to add |
| 443 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 444 | private void switchPortAdded(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 445 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 446 | final PortEvent portEvent = new PortEvent(dpid, |
| 447 | PortNumberUtils.openFlow(port)); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 448 | // FIXME should be merging, with existing attrs, etc.. |
| 449 | // TODO define attr name as constant somewhere. |
| 450 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 451 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 452 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 453 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 454 | portEvent.freeze(); |
| 455 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 456 | publishAddPortEvent(portEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 457 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 458 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 459 | /** |
| 460 | * Prepares an event for removing a port on a switch. |
| 461 | * |
| 462 | * @param switchId the switch ID (DPID) |
| 463 | * @param port the port to remove |
| 464 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 465 | private void switchPortRemoved(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 466 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 467 | |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 468 | final PortEvent portEvent = new PortEvent(dpid, |
| 469 | PortNumberUtils.openFlow(port)); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 470 | // FIXME should be merging, with existing attrs, etc.. |
| 471 | // TODO define attr name as constant somewhere. |
| 472 | // TODO populate appropriate attributes. |
| 473 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 474 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 475 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 476 | portEvent.freeze(); |
| 477 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 478 | publishRemovePortEvent(portEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 479 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 480 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 481 | @Override |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 482 | public String getName() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 483 | return "topologyPublisher"; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 484 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 485 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 486 | /* ***************** |
| 487 | * IFloodlightModule |
| 488 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 489 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 490 | @Override |
| 491 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 492 | List<Class<? extends IFloodlightService>> services = |
| 493 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 494 | services.add(ITopologyPublisherService.class); |
| 495 | return services; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 496 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 497 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 498 | @Override |
| 499 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 500 | getServiceImpls() { |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 501 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 502 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 503 | impls.put(ITopologyPublisherService.class, this); |
| 504 | return impls; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 505 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 506 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 507 | @Override |
| 508 | public Collection<Class<? extends IFloodlightService>> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 509 | getModuleDependencies() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 510 | Collection<Class<? extends IFloodlightService>> l = |
| 511 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 512 | l.add(IFloodlightProviderService.class); |
| 513 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 514 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 515 | l.add(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 516 | l.add(IDatagridService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 517 | l.add(ITopologyService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 518 | l.add(IHostService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 519 | return l; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 520 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 521 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 522 | @Override |
| 523 | public void init(FloodlightModuleContext context) |
| 524 | throws FloodlightModuleException { |
| 525 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 526 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 527 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 528 | datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 529 | hostService = context.getServiceImpl(IHostService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 530 | topologyService = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 531 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 532 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 533 | @Override |
| 534 | public void startUp(FloodlightModuleContext context) { |
| 535 | floodlightProvider.addOFSwitchListener(this); |
| 536 | linkDiscovery.addListener(this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 537 | hostService.addHostListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 538 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 539 | eventChannel = datagridService.createChannel( |
| 540 | TopologyManager.EVENT_CHANNEL_NAME, |
| 541 | byte[].class, |
| 542 | TopologyEvent.class); |
| 543 | |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 544 | mutableTopology = topologyService.getTopology(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 545 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 546 | // Run the cleanup thread |
| 547 | String enableCleanup = |
| 548 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 549 | if (enableCleanup != null |
| 550 | && enableCleanup.equalsIgnoreCase("false")) { |
| 551 | cleanupEnabled = false; |
| 552 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 553 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 554 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not "); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 555 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 556 | if (cleanupEnabled) { |
| 557 | IThreadPoolService threadPool = |
| 558 | context.getServiceImpl(IThreadPoolService.class); |
| 559 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 560 | new SwitchCleanup()); |
| 561 | // Run the cleanup task immediately on startup |
| 562 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 563 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 564 | |
| 565 | // Run the Delayed Operations Handler thread |
| 566 | delayedOperationsHandler.start(); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 567 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 568 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 569 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 570 | public void hostAdded(Host host) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 571 | log.debug("Host added with MAC {}", host.getMacAddress()); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 572 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 573 | SwitchPort sp = new SwitchPort(host.getSwitchDPID(), host.getSwitchPort()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 574 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 575 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 576 | HostEvent event = new HostEvent(host.getMacAddress()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 577 | event.setAttachmentPoints(spLists); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 578 | event.setLastSeenTime(host.getLastSeenTimestamp().getTime()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 579 | // Does not use vlan info now. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 580 | event.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 581 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 582 | publishAddHostEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 583 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 584 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 585 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 586 | public void hostRemoved(Host host) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 587 | log.debug("Host removed with MAC {}", host.getMacAddress()); |
| 588 | |
| 589 | // |
| 590 | // Remove all previously added HostEvent for this MAC address |
| 591 | // |
| 592 | // TODO: Currently, the caller of hostRemoved() might not include |
| 593 | // the correct set of Attachment Points in the HostEvent entry itself. |
| 594 | // Also, we might have multiple HostEvent entries for the same |
| 595 | // host (MAC address), each containing a single (different) Attachment |
| 596 | // Point. |
| 597 | // Hence, here we have to cleanup all HostEvent entries for this |
| 598 | // particular host, based on its MAC address. |
| 599 | // |
| 600 | List<HostEvent> removeHostEvents = new LinkedList<>(); |
| 601 | for (ConcurrentMap<ByteBuffer, HostEvent> cm : publishedHostEvents.values()) { |
| 602 | for (HostEvent hostEvent : cm.values()) { |
| 603 | if (hostEvent.getMac().equals(host.getMacAddress())) { |
| 604 | removeHostEvents.add(hostEvent); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | for (HostEvent event : removeHostEvents) { |
| 609 | publishRemoveHostEvent(event); |
| 610 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 611 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 612 | |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 613 | @Override |
| 614 | public boolean publish(TopologyBatchOperation tbo) { |
| 615 | publishTopologyOperations(tbo); |
| 616 | return true; |
| 617 | } |
| 618 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 619 | /** |
| 620 | * Prepares the Controller role changed event for a switch. |
| 621 | * |
| 622 | * @param dpid the switch DPID |
| 623 | * @param role the new role of the controller |
| 624 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 625 | private void controllerRoleChanged(Dpid dpid, Role role) { |
| 626 | log.debug("Local switch controller mastership role changed: dpid = {} role = {}", |
| 627 | dpid, role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 628 | MastershipEvent mastershipEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 629 | new MastershipEvent(dpid, getOnosInstanceId(), role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 630 | // FIXME should be merging, with existing attrs, etc.. |
| 631 | // TODO define attr name as constant somewhere. |
| 632 | // TODO populate appropriate attributes. |
| 633 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 634 | TopologyElement.TYPE_ALL_LAYERS); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 635 | mastershipEvent.freeze(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 636 | publishAddSwitchMastershipEvent(mastershipEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 640 | * Publishes ADD Mastership Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 641 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 642 | * @param mastershipEvent the mastership event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 643 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 644 | private void publishAddSwitchMastershipEvent( |
| 645 | MastershipEvent mastershipEvent) { |
| 646 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 647 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 648 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 649 | new TopologyEvent(mastershipEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 650 | tbo.appendAddOperation(topologyEvent); |
| 651 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 652 | publishedMastershipEvents.put(mastershipEvent.getDpid(), |
| 653 | mastershipEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 657 | * Publishes REMOVE Mastership Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 658 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 659 | * @param mastershipEvent the mastership event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 660 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 661 | private void publishRemoveSwitchMastershipEvent( |
| 662 | MastershipEvent mastershipEvent) { |
| 663 | if (publishedMastershipEvents.get(mastershipEvent.getDpid()) == null) { |
| 664 | return; // Nothing to do |
| 665 | } |
| 666 | |
| 667 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 668 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 669 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 670 | new TopologyEvent(mastershipEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 671 | tbo.appendRemoveOperation(topologyEvent); |
| 672 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 673 | publishedMastershipEvents.remove(mastershipEvent.getDpid()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 677 | * Publishes ADD Switch Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 678 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 679 | * @param switchEvent the switch event to publish |
| 680 | * @param portEvents the corresponding port events for the switch to |
| 681 | * publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 682 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 683 | private void publishAddSwitchEvent(SwitchEvent switchEvent, |
| 684 | Collection<PortEvent> portEvents) { |
| 685 | if (!registryService.hasControl(switchEvent.getOriginDpid().value())) { |
| 686 | log.debug("Not the master for switch {}. Suppressed switch add event {}.", |
| 687 | switchEvent.getOriginDpid(), switchEvent); |
| 688 | return; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 691 | // Keep track of the old Port Events that should be removed |
| 692 | ConcurrentMap<ByteBuffer, PortEvent> oldPortEvents = |
| 693 | publishedPortEvents.get(switchEvent.getDpid()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 694 | if (oldPortEvents == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 695 | oldPortEvents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 698 | // Publish the information for the switch |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 699 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 700 | TopologyEvent topologyEvent = |
| 701 | new TopologyEvent(switchEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 702 | tbo.appendAddOperation(topologyEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 703 | |
| 704 | // Publish the information for each port |
| 705 | ConcurrentMap<ByteBuffer, PortEvent> newPortEvents = |
| 706 | new ConcurrentHashMap<>(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 707 | for (PortEvent portEvent : portEvents) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 708 | topologyEvent = |
| 709 | new TopologyEvent(portEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 710 | tbo.appendAddOperation(topologyEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 711 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 712 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 713 | newPortEvents.put(id, portEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 714 | oldPortEvents.remove(id); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 715 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 716 | publishTopologyOperations(tbo); |
| 717 | publishedSwitchEvents.put(switchEvent.getDpid(), switchEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 718 | publishedPortEvents.put(switchEvent.getDpid(), newPortEvents); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 719 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 720 | // Cleanup for each of the old removed port |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 721 | for (PortEvent portEvent : oldPortEvents.values()) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 722 | publishRemovePortEvent(portEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 727 | * Publishes REMOVE Switch Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 728 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 729 | * @param switchEvent the switch event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 730 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 731 | private void publishRemoveSwitchEvent(SwitchEvent switchEvent) { |
| 732 | // |
| 733 | // TODO: Removed the check for now, because currently this method is |
| 734 | // also called by the SwitchCleanup thread, and in that case |
| 735 | // the Switch Event was published by some other ONOS instance. |
| 736 | // |
| 737 | /* |
| 738 | if (publishedSwitchEvents.get(switchEvent.getDpid()) == null) { |
| 739 | return; // Nothing to do |
| 740 | } |
| 741 | */ |
| 742 | |
| 743 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 744 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 745 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 746 | new TopologyEvent(switchEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 747 | tbo.appendRemoveOperation(topologyEvent); |
| 748 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 749 | publishedSwitchEvents.remove(switchEvent.getDpid()); |
| 750 | |
| 751 | // Cleanup for each port |
| 752 | ConcurrentMap<ByteBuffer, PortEvent> portEvents = |
| 753 | publishedPortEvents.get(switchEvent.getDpid()); |
| 754 | if (portEvents != null) { |
| 755 | for (PortEvent portEvent : portEvents.values()) { |
| 756 | publishRemovePortEvent(portEvent); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | publishedPortEvents.remove(switchEvent.getDpid()); |
| 761 | publishedLinkEvents.remove(switchEvent.getDpid()); |
| 762 | publishedHostEvents.remove(switchEvent.getDpid()); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Publishes ADD Port Event. |
| 767 | * |
| 768 | * @param portEvent the port event to publish |
| 769 | */ |
| 770 | private void publishAddPortEvent(PortEvent portEvent) { |
| 771 | if (!registryService.hasControl(portEvent.getOriginDpid().value())) { |
| 772 | log.debug("Not the master for switch {}. Suppressed port add event {}.", |
| 773 | portEvent.getOriginDpid(), portEvent); |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 778 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 779 | TopologyEvent topologyEvent = |
| 780 | new TopologyEvent(portEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 781 | tbo.appendAddOperation(topologyEvent); |
| 782 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 783 | |
| 784 | // Store the new Port Event in the local cache |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 785 | ConcurrentMap<ByteBuffer, PortEvent> portEvents = |
| 786 | ConcurrentUtils.putIfAbsent(publishedPortEvents, |
| 787 | portEvent.getDpid(), |
| 788 | new ConcurrentHashMap<ByteBuffer, PortEvent>()); |
| 789 | portEvents.put(portEvent.getIDasByteBuffer(), portEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 793 | * Publishes REMOVE Port Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 794 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 795 | * @param portEvent the port event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 796 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 797 | private void publishRemovePortEvent(PortEvent portEvent) { |
| 798 | ConcurrentMap<ByteBuffer, PortEvent> portEvents = |
| 799 | publishedPortEvents.get(portEvent.getDpid()); |
| 800 | if (portEvents == null) { |
| 801 | return; // Nothing to do |
| 802 | } |
| 803 | if (portEvents.get(portEvent.getIDasByteBuffer()) == null) { |
| 804 | return; // Nothing to do |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 807 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 808 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 809 | TopologyEvent topologyEvent = |
| 810 | new TopologyEvent(portEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 811 | tbo.appendRemoveOperation(topologyEvent); |
| 812 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 813 | |
| 814 | // Cleanup for the incoming link(s) |
| 815 | ConcurrentMap<ByteBuffer, LinkEvent> linkEvents = |
| 816 | publishedLinkEvents.get(portEvent.getDpid()); |
| 817 | if (linkEvents != null) { |
| 818 | for (LinkEvent linkEvent : linkEvents.values()) { |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 819 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 820 | publishRemoveLinkEvent(linkEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Cleanup for the connected hosts |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 826 | ConcurrentMap<ByteBuffer, HostEvent> hostEvents = |
| 827 | publishedHostEvents.get(portEvent.getDpid()); |
| 828 | if (hostEvents != null) { |
| 829 | for (HostEvent hostEvent : hostEvents.values()) { |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 830 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 831 | if (swp.equals(portEvent.getSwitchPort())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 832 | publishRemoveHostEvent(hostEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 836 | } |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 837 | |
| 838 | portEvents.remove(portEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 842 | * Publishes ADD Link Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 843 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 844 | * @param linkEvent the link event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 845 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 846 | private void publishAddLinkEvent(LinkEvent linkEvent) { |
| 847 | if (!registryService.hasControl(linkEvent.getOriginDpid().value())) { |
| 848 | log.debug("Not the master for dst switch {}. Suppressed link add event {}.", |
| 849 | linkEvent.getOriginDpid(), linkEvent); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 854 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 855 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 856 | new TopologyEvent(linkEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 857 | tbo.appendAddOperation(topologyEvent); |
| 858 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 859 | |
| 860 | // Store the new Link Event in the local cache |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 861 | ConcurrentMap<ByteBuffer, LinkEvent> linkEvents = |
| 862 | ConcurrentUtils.putIfAbsent(publishedLinkEvents, |
| 863 | linkEvent.getDst().getDpid(), |
| 864 | new ConcurrentHashMap<ByteBuffer, LinkEvent>()); |
| 865 | linkEvents.put(linkEvent.getIDasByteBuffer(), linkEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 869 | * Publishes REMOVE Link Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 870 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 871 | * @param linkEvent the link event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 872 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 873 | private void publishRemoveLinkEvent(LinkEvent linkEvent) { |
| 874 | ConcurrentMap<ByteBuffer, LinkEvent> linkEvents = |
| 875 | publishedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 876 | if (linkEvents == null) { |
| 877 | return; // Nothing to do |
| 878 | } |
| 879 | if (linkEvents.get(linkEvent.getIDasByteBuffer()) == null) { |
| 880 | return; // Nothing to do |
| 881 | } |
| 882 | |
| 883 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 884 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 885 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 886 | new TopologyEvent(linkEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 887 | tbo.appendRemoveOperation(topologyEvent); |
| 888 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 889 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 890 | linkEvents.remove(linkEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 894 | * Publishes ADD Host Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 895 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 896 | * @param hostEvent the host event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 897 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 898 | private void publishAddHostEvent(HostEvent hostEvent) { |
| 899 | // |
| 900 | // NOTE: The implementation below assumes that there is just one |
| 901 | // attachment point stored in hostEvent. Currently, this assumption |
| 902 | // is true based on the existing implementation of the caller |
| 903 | // hostAdded(). |
| 904 | // |
| 905 | |
| 906 | if (!registryService.hasControl(hostEvent.getOriginDpid().value())) { |
| 907 | log.debug("Not the master for attachment switch {}. Suppressed host add event {}.", |
| 908 | hostEvent.getOriginDpid(), hostEvent); |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 913 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 914 | TopologyEvent topologyEvent = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 915 | new TopologyEvent(hostEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 916 | tbo.appendAddOperation(topologyEvent); |
| 917 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 918 | |
| 919 | // Store the new Host Event in the local cache |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 920 | ConcurrentMap<ByteBuffer, HostEvent> hostEvents = |
| 921 | ConcurrentUtils.putIfAbsent(publishedHostEvents, |
| 922 | hostEvent.getOriginDpid(), |
| 923 | new ConcurrentHashMap<ByteBuffer, HostEvent>()); |
| 924 | hostEvents.put(hostEvent.getIDasByteBuffer(), hostEvent); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 928 | * Publishes REMOVE Host Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 929 | * |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 930 | * @param hostEvent the host event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 931 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 932 | private void publishRemoveHostEvent(HostEvent hostEvent) { |
| 933 | ConcurrentMap<ByteBuffer, HostEvent> hostEvents = |
| 934 | publishedHostEvents.get(hostEvent.getOriginDpid()); |
| 935 | if (hostEvents == null) { |
| 936 | return; // Nothing to do |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 937 | } |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 938 | if (hostEvents.get(hostEvent.getIDasByteBuffer()) == null) { |
| 939 | return; // Nothing to do |
| 940 | } |
| 941 | |
| 942 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 943 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 944 | TopologyEvent topologyEvent = |
| 945 | new TopologyEvent(hostEvent, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 946 | tbo.appendRemoveOperation(topologyEvent); |
| 947 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 948 | |
| 949 | hostEvents.remove(hostEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 950 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 951 | |
| 952 | /** |
| 953 | * Publishes Topology Operations. |
| 954 | * |
| 955 | * @param tbo the Topology Operations to publish |
| 956 | */ |
| 957 | private void publishTopologyOperations(TopologyBatchOperation tbo) { |
| 958 | // TODO: This flag should be configurable |
| 959 | boolean isGlobalLogWriter = false; |
| 960 | |
| 961 | log.debug("Publishing: {}", tbo); |
| 962 | |
| 963 | if (isGlobalLogWriter) { |
| 964 | if (!topologyService.publish(tbo)) { |
| 965 | log.debug("Cannot publish: {}", tbo); |
| 966 | delayedOperations.add(tbo); |
| 967 | } |
| 968 | } else { |
| 969 | // TODO: For now we publish each TopologyEvent independently |
| 970 | for (BatchOperationEntry<TopologyBatchOperation.Operator, |
| 971 | TopologyEvent> boe : tbo.getOperations()) { |
| 972 | TopologyBatchOperation.Operator oper = boe.getOperator(); |
| 973 | TopologyEvent topologyEvent = boe.getTarget(); |
| 974 | switch (oper) { |
| 975 | case ADD: |
| 976 | eventChannel.addEntry(topologyEvent.getID(), |
| 977 | topologyEvent); |
| 978 | break; |
| 979 | case REMOVE: |
| 980 | eventChannel.removeEntry(topologyEvent.getID()); |
| 981 | break; |
| 982 | default: |
| 983 | log.error("Unknown Topology Batch Operation {}", oper); |
| 984 | break; |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 989 | } |