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()); |
Fahad Naeem Khan | 1f8fb2e | 2014-09-25 16:39:18 -0700 | [diff] [blame] | 380 | //setting name from config file. |
| 381 | switchData.createStringAttribute("name", |
| 382 | ret.getSwitchConfig().getName()); |
| 383 | //set the switch socketAddress and port |
| 384 | switchData.createStringAttribute("remoteAddress", |
| 385 | sw.getChannelSocketAddress().toString().substring(1)); |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 386 | if (ret.getConfigState() == NetworkConfigState.ACCEPT_ADD) { |
| 387 | Map<String, String> attr = ret.getSwitchConfig().getPublishAttributes(); |
| 388 | for (Entry<String, String> e : attr.entrySet()) { |
| 389 | switchData.createStringAttribute(e.getKey(), e.getValue()); |
| 390 | } |
| 391 | switchData.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 392 | ConfigState.CONFIGURED.toString()); |
| 393 | } else { |
| 394 | switchData.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 395 | ConfigState.NOT_CONFIGURED.toString()); |
| 396 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 397 | switchData.freeze(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 398 | // The Port events |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 399 | List<PortData> portDataEntries = new ArrayList<PortData>(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 400 | for (OFPortDesc port : sw.getPorts()) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 401 | PortData portData = new PortData(dpid, |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 402 | PortNumberUtils.openFlow(port)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 403 | // FIXME should be merging, with existing attrs, etc.. |
| 404 | // TODO define attr name as constant somewhere. |
| 405 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 406 | portData.createStringAttribute("name", port.getName()); |
| 407 | portData.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 408 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 409 | portData.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 410 | ConfigState.NOT_CONFIGURED.toString()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 411 | portData.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 412 | AdminStatus.ACTIVE.toString()); |
| 413 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 414 | portData.freeze(); |
| 415 | portDataEntries.add(portData); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 416 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 417 | publishAddSwitchEvent(switchData, portDataEntries); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public void switchActivatedEqual(long swId) { |
| 422 | final Dpid dpid = new Dpid(swId); |
| 423 | controllerRoleChanged(dpid, Role.EQUAL); |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public void switchMasterToEqual(long swId) { |
| 428 | final Dpid dpid = new Dpid(swId); |
| 429 | controllerRoleChanged(dpid, Role.EQUAL); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void switchEqualToMaster(long swId) { |
| 434 | // for now treat as switchActivatedMaster |
| 435 | switchActivatedMaster(swId); |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public void switchDisconnected(long swId) { |
| 440 | final Dpid dpid = new Dpid(swId); |
| 441 | |
| 442 | log.debug("Local switch disconnected: dpid = {} role = {}", dpid); |
| 443 | |
| 444 | Role role = Role.SLAVE; // TODO: Should be Role.UNKNOWN |
| 445 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 446 | MastershipData mastershipData = |
| 447 | new MastershipData(dpid, getOnosInstanceId(), role); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 448 | // FIXME should be merging, with existing attrs, etc.. |
| 449 | // TODO define attr name as constant somewhere. |
| 450 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 451 | mastershipData.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 452 | TopologyElement.TYPE_ALL_LAYERS); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 453 | mastershipData.freeze(); |
| 454 | publishRemoveSwitchMastershipEvent(mastershipData); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void switchPortChanged(long swId, OFPortDesc port, |
| 459 | PortChangeType changeType) { |
| 460 | switch (changeType) { |
| 461 | case ADD: |
| 462 | switchPortAdded(swId, port); |
| 463 | break; |
| 464 | case DELETE: |
| 465 | switchPortRemoved(swId, port); |
| 466 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 467 | case UP: |
Pavlin Radoslavov | 7946c61 | 2014-08-13 15:06:00 -0700 | [diff] [blame] | 468 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 469 | switchPortAdded(swId, port); |
| 470 | break; |
| 471 | case DOWN: |
| 472 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 473 | switchPortRemoved(swId, port); |
| 474 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 475 | case OTHER_UPDATE: |
| 476 | default: |
| 477 | // XXX S what is the right set of port change handlers? |
| 478 | log.debug("Topology publisher does not handle these port updates: {}", |
| 479 | changeType); |
| 480 | } |
| 481 | } |
| 482 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Prepares an event for adding a port on a switch. |
| 485 | * |
| 486 | * @param switchId the switch ID (DPID) |
| 487 | * @param port the port to add |
| 488 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 489 | private void switchPortAdded(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 490 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 491 | final PortData portData = new PortData(dpid, |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 492 | PortNumberUtils.openFlow(port)); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 493 | // FIXME should be merging, with existing attrs, etc.. |
| 494 | // TODO define attr name as constant somewhere. |
| 495 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 496 | portData.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 497 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 498 | portData.createStringAttribute("name", port.getName()); |
| 499 | portData.freeze(); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 500 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 501 | publishAddPortEvent(portData); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 502 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 503 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 504 | /** |
| 505 | * Prepares an event for removing a port on a switch. |
| 506 | * |
| 507 | * @param switchId the switch ID (DPID) |
| 508 | * @param port the port to remove |
| 509 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 510 | private void switchPortRemoved(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 511 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 512 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 513 | final PortData portData = new PortData(dpid, |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 514 | PortNumberUtils.openFlow(port)); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 515 | // FIXME should be merging, with existing attrs, etc.. |
| 516 | // TODO define attr name as constant somewhere. |
| 517 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 518 | portData.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 519 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 520 | portData.createStringAttribute("name", port.getName()); |
| 521 | portData.freeze(); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 522 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 523 | publishRemovePortEvent(portData); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 524 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 525 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 526 | @Override |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 527 | public String getName() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 528 | return "topologyPublisher"; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 529 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 530 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 531 | /* ***************** |
| 532 | * IFloodlightModule |
| 533 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 534 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 535 | @Override |
| 536 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 537 | List<Class<? extends IFloodlightService>> services = |
| 538 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 539 | services.add(ITopologyPublisherService.class); |
| 540 | return services; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 541 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 542 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 543 | @Override |
| 544 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 545 | getServiceImpls() { |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 546 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 547 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 548 | impls.put(ITopologyPublisherService.class, this); |
| 549 | return impls; |
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 Collection<Class<? extends IFloodlightService>> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 554 | getModuleDependencies() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 555 | Collection<Class<? extends IFloodlightService>> l = |
| 556 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 557 | l.add(IFloodlightProviderService.class); |
| 558 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 559 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 560 | l.add(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 561 | l.add(IDatagridService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 562 | l.add(ITopologyService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 563 | l.add(IHostService.class); |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 564 | l.add(INetworkConfigService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 565 | return l; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 566 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 567 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 568 | @Override |
| 569 | public void init(FloodlightModuleContext context) |
| 570 | throws FloodlightModuleException { |
| 571 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 572 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 573 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 574 | datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 575 | hostService = context.getServiceImpl(IHostService.class); |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 576 | networkConfigService = context.getServiceImpl(INetworkConfigService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 577 | topologyService = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 578 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 579 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 580 | @Override |
| 581 | public void startUp(FloodlightModuleContext context) { |
| 582 | floodlightProvider.addOFSwitchListener(this); |
| 583 | linkDiscovery.addListener(this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 584 | hostService.addHostListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 585 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 586 | eventChannel = datagridService.createChannel( |
| 587 | TopologyManager.EVENT_CHANNEL_NAME, |
| 588 | byte[].class, |
| 589 | TopologyEvent.class); |
| 590 | |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 591 | mutableTopology = topologyService.getTopology(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 592 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 593 | // Run the cleanup thread |
| 594 | String enableCleanup = |
| 595 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 596 | if (enableCleanup != null |
| 597 | && enableCleanup.equalsIgnoreCase("false")) { |
| 598 | cleanupEnabled = false; |
| 599 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 600 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 601 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not "); |
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 | if (cleanupEnabled) { |
| 604 | IThreadPoolService threadPool = |
| 605 | context.getServiceImpl(IThreadPoolService.class); |
| 606 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 607 | new SwitchCleanup()); |
| 608 | // Run the cleanup task immediately on startup |
| 609 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 610 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 611 | |
| 612 | // Run the Delayed Operations Handler thread |
| 613 | delayedOperationsHandler.start(); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 614 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 615 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 616 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 617 | public void hostAdded(Host host) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 618 | log.debug("Host added with MAC {}", host.getMacAddress()); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 619 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 620 | SwitchPort sp = new SwitchPort(host.getSwitchDPID(), host.getSwitchPort()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 621 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 622 | spLists.add(sp); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 623 | HostData hostData = new HostData(host.getMacAddress(), host.getIpAddress()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 624 | hostData.setAttachmentPoints(spLists); |
| 625 | hostData.setLastSeenTime(host.getLastSeenTimestamp().getTime()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 626 | // Does not use vlan info now. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 627 | hostData.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 628 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 629 | publishAddHostEvent(hostData); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 630 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 631 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 632 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 633 | public void hostRemoved(Host host) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 634 | log.debug("Host removed with MAC {}", host.getMacAddress()); |
| 635 | |
| 636 | // |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 637 | // Remove all previously added HostData for this MAC address |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 638 | // |
| 639 | // TODO: Currently, the caller of hostRemoved() might not include |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 640 | // the correct set of Attachment Points in the HostData entry itself. |
| 641 | // Also, we might have multiple HostData entries for the same |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 642 | // host (MAC address), each containing a single (different) Attachment |
| 643 | // Point. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 644 | // Hence, here we have to cleanup all HostData entries for this |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 645 | // particular host, based on its MAC address. |
| 646 | // |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 647 | List<HostData> removeHostDataEntries = new LinkedList<>(); |
| 648 | for (ConcurrentMap<ByteBuffer, HostData> cm : publishedHostDataEntries.values()) { |
| 649 | for (HostData hostData : cm.values()) { |
| 650 | if (hostData.getMac().equals(host.getMacAddress())) { |
| 651 | removeHostDataEntries.add(hostData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 655 | for (HostData hostData : removeHostDataEntries) { |
| 656 | publishRemoveHostEvent(hostData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 657 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 658 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 659 | |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 660 | @Override |
| 661 | public boolean publish(TopologyBatchOperation tbo) { |
| 662 | publishTopologyOperations(tbo); |
| 663 | return true; |
| 664 | } |
| 665 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 666 | /** |
| 667 | * Prepares the Controller role changed event for a switch. |
| 668 | * |
| 669 | * @param dpid the switch DPID |
| 670 | * @param role the new role of the controller |
| 671 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 672 | private void controllerRoleChanged(Dpid dpid, Role role) { |
| 673 | log.debug("Local switch controller mastership role changed: dpid = {} role = {}", |
| 674 | dpid, role); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 675 | MastershipData mastershipData = |
| 676 | new MastershipData(dpid, getOnosInstanceId(), role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 677 | // FIXME should be merging, with existing attrs, etc.. |
| 678 | // TODO define attr name as constant somewhere. |
| 679 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 680 | mastershipData.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 681 | TopologyElement.TYPE_ALL_LAYERS); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 682 | mastershipData.freeze(); |
| 683 | publishAddSwitchMastershipEvent(mastershipData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 687 | * Publishes ADD Mastership Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 688 | * |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 689 | * @param mastershipData the mastership event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 690 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 691 | private void publishAddSwitchMastershipEvent( |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 692 | MastershipData mastershipData) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 693 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 694 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 695 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 696 | new TopologyEvent(mastershipData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 697 | tbo.appendAddOperation(topologyEvent); |
| 698 | publishTopologyOperations(tbo); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 699 | publishedMastershipDataEntries.put(mastershipData.getDpid(), |
| 700 | mastershipData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 704 | * Publishes REMOVE Mastership Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 705 | * |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 706 | * @param mastershipData the mastership event to publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 707 | */ |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 708 | private void publishRemoveSwitchMastershipEvent( |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 709 | MastershipData mastershipData) { |
| 710 | if (publishedMastershipDataEntries.get(mastershipData.getDpid()) == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 711 | return; // Nothing to do |
| 712 | } |
| 713 | |
| 714 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 715 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 716 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 717 | new TopologyEvent(mastershipData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 718 | tbo.appendRemoveOperation(topologyEvent); |
| 719 | publishTopologyOperations(tbo); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 720 | publishedMastershipDataEntries.remove(mastershipData.getDpid()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 724 | * Publishes ADD Switch Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 725 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 726 | * @param switchData the switch event to publish |
| 727 | * @param portDataEntries the corresponding port events for the switch to |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 728 | * publish |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 729 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 730 | private void publishAddSwitchEvent(SwitchData switchData, |
| 731 | Collection<PortData> portDataEntries) { |
| 732 | if (!registryService.hasControl(switchData.getOriginDpid().value())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 733 | log.debug("Not the master for switch {}. Suppressed switch add event {}.", |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 734 | switchData.getOriginDpid(), switchData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 735 | return; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 738 | // Keep track of the old Port Events that should be removed |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 739 | ConcurrentMap<ByteBuffer, PortData> oldPortDataEntries = |
| 740 | publishedPortDataEntries.get(switchData.getDpid()); |
| 741 | if (oldPortDataEntries == null) { |
| 742 | oldPortDataEntries = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 745 | // Publish the information for the switch |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 746 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 747 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 748 | new TopologyEvent(switchData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 749 | tbo.appendAddOperation(topologyEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 750 | |
| 751 | // Publish the information for each port |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 752 | ConcurrentMap<ByteBuffer, PortData> newPortDataEntries = |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 753 | new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 754 | for (PortData portData : portDataEntries) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 755 | topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 756 | new TopologyEvent(portData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 757 | tbo.appendAddOperation(topologyEvent); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 758 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 759 | ByteBuffer id = portData.getIDasByteBuffer(); |
| 760 | newPortDataEntries.put(id, portData); |
| 761 | oldPortDataEntries.remove(id); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 762 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 763 | publishTopologyOperations(tbo); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 764 | publishedSwitchDataEntries.put(switchData.getDpid(), switchData); |
| 765 | publishedPortDataEntries.put(switchData.getDpid(), newPortDataEntries); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 766 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 767 | // Cleanup for each of the old removed port |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 768 | for (PortData portData : oldPortDataEntries.values()) { |
| 769 | publishRemovePortEvent(portData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 774 | * Publishes REMOVE Switch Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 775 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 776 | * @param switchData the switch event to publish |
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 | private void publishRemoveSwitchEvent(SwitchData switchData) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 779 | // |
| 780 | // TODO: Removed the check for now, because currently this method is |
| 781 | // also called by the SwitchCleanup thread, and in that case |
| 782 | // the Switch Event was published by some other ONOS instance. |
| 783 | // |
| 784 | /* |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 785 | if (publishedSwitchDataEntries.get(switchData.getDpid()) == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 786 | return; // Nothing to do |
| 787 | } |
| 788 | */ |
| 789 | |
| 790 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 791 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 792 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 793 | new TopologyEvent(switchData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 794 | tbo.appendRemoveOperation(topologyEvent); |
| 795 | publishTopologyOperations(tbo); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 796 | publishedSwitchDataEntries.remove(switchData.getDpid()); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 797 | |
| 798 | // Cleanup for each port |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 799 | ConcurrentMap<ByteBuffer, PortData> portDataEntries = |
| 800 | publishedPortDataEntries.get(switchData.getDpid()); |
| 801 | if (portDataEntries != null) { |
| 802 | for (PortData portData : portDataEntries.values()) { |
| 803 | publishRemovePortEvent(portData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 807 | publishedPortDataEntries.remove(switchData.getDpid()); |
| 808 | publishedLinkDataEntries.remove(switchData.getDpid()); |
| 809 | publishedHostDataEntries.remove(switchData.getDpid()); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Publishes ADD Port Event. |
| 814 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 815 | * @param portData the port event to publish |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 816 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 817 | private void publishAddPortEvent(PortData portData) { |
| 818 | if (!registryService.hasControl(portData.getOriginDpid().value())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 819 | log.debug("Not the master for switch {}. Suppressed port add event {}.", |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 820 | portData.getOriginDpid(), portData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 821 | return; |
| 822 | } |
| 823 | |
| 824 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 825 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 826 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 827 | new TopologyEvent(portData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 828 | tbo.appendAddOperation(topologyEvent); |
| 829 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 830 | |
| 831 | // Store the new Port Event in the local cache |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 832 | ConcurrentMap<ByteBuffer, PortData> portDataEntries = |
| 833 | ConcurrentUtils.putIfAbsent(publishedPortDataEntries, |
| 834 | portData.getDpid(), |
| 835 | new ConcurrentHashMap<ByteBuffer, PortData>()); |
| 836 | portDataEntries.put(portData.getIDasByteBuffer(), portData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 840 | * Publishes REMOVE Port Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 841 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 842 | * @param portData the port event to publish |
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 | private void publishRemovePortEvent(PortData portData) { |
| 845 | ConcurrentMap<ByteBuffer, PortData> portDataEntries = |
| 846 | publishedPortDataEntries.get(portData.getDpid()); |
| 847 | if (portDataEntries == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 848 | return; // Nothing to do |
| 849 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 850 | if (portDataEntries.get(portData.getIDasByteBuffer()) == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 851 | return; // Nothing to do |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 854 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 855 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 856 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 857 | new TopologyEvent(portData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 858 | tbo.appendRemoveOperation(topologyEvent); |
| 859 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 860 | |
| 861 | // Cleanup for the incoming link(s) |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 862 | ConcurrentMap<ByteBuffer, LinkData> linkDataEntries = |
| 863 | publishedLinkDataEntries.get(portData.getDpid()); |
| 864 | if (linkDataEntries != null) { |
| 865 | for (LinkData linkData : linkDataEntries.values()) { |
| 866 | if (linkData.getDst().equals(portData.getSwitchPort())) { |
| 867 | publishRemoveLinkEvent(linkData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // Cleanup for the connected hosts |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 873 | ConcurrentMap<ByteBuffer, HostData> hostDataEntries = |
| 874 | publishedHostDataEntries.get(portData.getDpid()); |
| 875 | if (hostDataEntries != null) { |
| 876 | for (HostData hostData : hostDataEntries.values()) { |
| 877 | for (SwitchPort swp : hostData.getAttachmentPoints()) { |
| 878 | if (swp.equals(portData.getSwitchPort())) { |
| 879 | publishRemoveHostEvent(hostData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | } |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 883 | } |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 884 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 885 | portDataEntries.remove(portData.getIDasByteBuffer()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 889 | * Publishes ADD Link Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 890 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 891 | * @param linkData the link event to publish |
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 | private void publishAddLinkEvent(LinkData linkData) { |
| 894 | if (!registryService.hasControl(linkData.getOriginDpid().value())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 895 | log.debug("Not the master for dst switch {}. Suppressed link add event {}.", |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 896 | linkData.getOriginDpid(), linkData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
| 900 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 901 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 902 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 903 | new TopologyEvent(linkData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 904 | tbo.appendAddOperation(topologyEvent); |
| 905 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 906 | |
| 907 | // Store the new Link Event in the local cache |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 908 | ConcurrentMap<ByteBuffer, LinkData> linkDataEntries = |
| 909 | ConcurrentUtils.putIfAbsent(publishedLinkDataEntries, |
| 910 | linkData.getDst().getDpid(), |
| 911 | new ConcurrentHashMap<ByteBuffer, LinkData>()); |
| 912 | linkDataEntries.put(linkData.getIDasByteBuffer(), linkData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 916 | * Publishes REMOVE Link Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 917 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 918 | * @param linkData the link event to publish |
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 | private void publishRemoveLinkEvent(LinkData linkData) { |
| 921 | ConcurrentMap<ByteBuffer, LinkData> linkDataEntries = |
| 922 | publishedLinkDataEntries.get(linkData.getDst().getDpid()); |
| 923 | if (linkDataEntries == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 924 | return; // Nothing to do |
| 925 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 926 | if (linkDataEntries.get(linkData.getIDasByteBuffer()) == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 927 | return; // Nothing to do |
| 928 | } |
| 929 | |
| 930 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 931 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 932 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 933 | new TopologyEvent(linkData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 934 | tbo.appendRemoveOperation(topologyEvent); |
| 935 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 936 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 937 | linkDataEntries.remove(linkData.getIDasByteBuffer()); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 941 | * Publishes ADD Host Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 942 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 943 | * @param hostData the host event to publish |
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 | private void publishAddHostEvent(HostData hostData) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 946 | // |
| 947 | // NOTE: The implementation below assumes that there is just one |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 948 | // attachment point stored in hostData. Currently, this assumption |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 949 | // is true based on the existing implementation of the caller |
| 950 | // hostAdded(). |
| 951 | // |
| 952 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 953 | if (!registryService.hasControl(hostData.getOriginDpid().value())) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 954 | log.debug("Not the master for attachment switch {}. Suppressed host add event {}.", |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 955 | hostData.getOriginDpid(), hostData); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | |
| 959 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 960 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 961 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 962 | new TopologyEvent(hostData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 963 | tbo.appendAddOperation(topologyEvent); |
| 964 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 965 | |
| 966 | // Store the new Host Event in the local cache |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 967 | ConcurrentMap<ByteBuffer, HostData> hostDataEntries = |
| 968 | ConcurrentUtils.putIfAbsent(publishedHostDataEntries, |
| 969 | hostData.getOriginDpid(), |
| 970 | new ConcurrentHashMap<ByteBuffer, HostData>()); |
| 971 | hostDataEntries.put(hostData.getIDasByteBuffer(), hostData); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /** |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 975 | * Publishes REMOVE Host Event. |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 976 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 977 | * @param hostData the host event to publish |
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 | private void publishRemoveHostEvent(HostData hostData) { |
| 980 | ConcurrentMap<ByteBuffer, HostData> hostDataEntries = |
| 981 | publishedHostDataEntries.get(hostData.getOriginDpid()); |
| 982 | if (hostDataEntries == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 983 | return; // Nothing to do |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 984 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 985 | if (hostDataEntries.get(hostData.getIDasByteBuffer()) == null) { |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 986 | return; // Nothing to do |
| 987 | } |
| 988 | |
| 989 | // Publish the information |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 990 | TopologyBatchOperation tbo = new TopologyBatchOperation(); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 991 | TopologyEvent topologyEvent = |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 992 | new TopologyEvent(hostData, getOnosInstanceId()); |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 993 | tbo.appendRemoveOperation(topologyEvent); |
| 994 | publishTopologyOperations(tbo); |
Pavlin Radoslavov | 000fbae | 2014-08-19 19:42:58 -0700 | [diff] [blame] | 995 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 996 | hostDataEntries.remove(hostData.getIDasByteBuffer()); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 997 | } |
Pavlin Radoslavov | 2440967 | 2014-08-20 16:45:11 -0700 | [diff] [blame] | 998 | |
| 999 | /** |
| 1000 | * Publishes Topology Operations. |
| 1001 | * |
| 1002 | * @param tbo the Topology Operations to publish |
| 1003 | */ |
| 1004 | private void publishTopologyOperations(TopologyBatchOperation tbo) { |
| 1005 | // TODO: This flag should be configurable |
| 1006 | boolean isGlobalLogWriter = false; |
| 1007 | |
| 1008 | log.debug("Publishing: {}", tbo); |
| 1009 | |
| 1010 | if (isGlobalLogWriter) { |
| 1011 | if (!topologyService.publish(tbo)) { |
| 1012 | log.debug("Cannot publish: {}", tbo); |
| 1013 | delayedOperations.add(tbo); |
| 1014 | } |
| 1015 | } else { |
| 1016 | // TODO: For now we publish each TopologyEvent independently |
| 1017 | for (BatchOperationEntry<TopologyBatchOperation.Operator, |
| 1018 | TopologyEvent> boe : tbo.getOperations()) { |
| 1019 | TopologyBatchOperation.Operator oper = boe.getOperator(); |
| 1020 | TopologyEvent topologyEvent = boe.getTarget(); |
| 1021 | switch (oper) { |
| 1022 | case ADD: |
| 1023 | eventChannel.addEntry(topologyEvent.getID(), |
| 1024 | topologyEvent); |
| 1025 | break; |
| 1026 | case REMOVE: |
| 1027 | eventChannel.removeEntry(topologyEvent.getID()); |
| 1028 | break; |
| 1029 | default: |
| 1030 | log.error("Unknown Topology Batch Operation {}", oper); |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 1036 | } |