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