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