Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 5 | import java.util.ArrayList; |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 6 | import java.util.Collection; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 7 | import java.util.Collections; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 8 | import java.util.HashMap; |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 9 | import java.util.Iterator; |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 10 | import java.util.LinkedList; |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 11 | import java.util.List; |
| 12 | import java.util.Map; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 13 | import java.util.Map.Entry; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 14 | import java.util.SortedSet; |
| 15 | import java.util.TreeSet; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentHashMap; |
| 17 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 18 | import java.util.concurrent.locks.Lock; |
| 19 | import java.util.concurrent.locks.ReadWriteLock; |
| 20 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 21 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 22 | import javax.annotation.concurrent.GuardedBy; |
| 23 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.util.LinkTuple; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.OnosInstanceId; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 31 | |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 35 | import com.google.common.collect.HashMultimap; |
| 36 | import com.google.common.collect.Multimap; |
| 37 | import com.google.common.collect.Multimaps; |
| 38 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 39 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 40 | // TODO add TopologyManager, etc. to populate Mastership information. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Class to represent an instance of Topology Snapshot. |
| 43 | */ |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 44 | public class TopologyImpl implements MutableTopology, MutableInternalTopology { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 45 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 46 | private static final Logger log = LoggerFactory.getLogger(TopologyImpl.class); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 47 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 48 | // TODO Revisit Map types after implementing CoW/lock-free |
| 49 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 50 | // Mastership info |
| 51 | // Dpid -> [ (InstanceID, Role) ] |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 52 | private final Map<Dpid, SortedSet<MastershipData>> mastership; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | // DPID -> Switch |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 55 | private final ConcurrentMap<Dpid, SwitchData> switches; |
| 56 | private final ConcurrentMap<Dpid, ConcurrentMap<PortNumber, PortData>> ports; |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 57 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 58 | // Index from Port to Host |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 59 | private final Multimap<SwitchPort, HostData> hosts; |
| 60 | private final ConcurrentMap<MACAddress, HostData> mac2Host; |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 61 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 62 | // SwitchPort -> (type -> Link) |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 63 | private final ConcurrentMap<SwitchPort, ConcurrentMap<String, LinkData>> outgoingLinks; |
| 64 | private final ConcurrentMap<SwitchPort, ConcurrentMap<String, LinkData>> incomingLinks; |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 65 | |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 66 | private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); |
| 67 | private final Lock readLock = readWriteLock.readLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | // TODO use the write lock after refactor |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 69 | private final Lock writeLock = readWriteLock.writeLock(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 70 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Create an empty Topology. |
| 73 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 74 | public TopologyImpl() { |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 75 | mastership = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | // TODO: Does these object need to be stored in Concurrent Collection? |
| 77 | switches = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 78 | ports = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 79 | hosts = Multimaps.synchronizedMultimap( |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 80 | HashMultimap.<SwitchPort, HostData>create()); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 81 | mac2Host = new ConcurrentHashMap<>(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 82 | outgoingLinks = new ConcurrentHashMap<>(); |
| 83 | incomingLinks = new ConcurrentHashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 85 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Create a shallow copy of given Topology. |
| 88 | * |
| 89 | * @param original Topology |
| 90 | */ |
| 91 | public TopologyImpl(TopologyImpl original) { |
| 92 | original.acquireReadLock(); |
| 93 | try { |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 94 | // shallow copy Set in Map |
| 95 | this.mastership = new HashMap<>(original.mastership.size()); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 96 | for (Entry<Dpid, SortedSet<MastershipData>> e |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 97 | : original.mastership.entrySet()) { |
| 98 | this.mastership.put(e.getKey(), new TreeSet<>(e.getValue())); |
| 99 | } |
| 100 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 101 | this.switches = new ConcurrentHashMap<>(original.switches); |
| 102 | |
| 103 | // shallow copy Map in Map |
| 104 | this.ports = new ConcurrentHashMap<>(original.ports.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 105 | for (Entry<Dpid, ConcurrentMap<PortNumber, PortData>> entry |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 106 | : original.ports.entrySet()) { |
| 107 | this.ports.put(entry.getKey(), new ConcurrentHashMap<>(entry.getValue())); |
| 108 | } |
| 109 | |
| 110 | this.hosts = Multimaps.synchronizedMultimap( |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 111 | HashMultimap.<SwitchPort, HostData>create(original.hosts)); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 112 | this.mac2Host = new ConcurrentHashMap<>(original.mac2Host); |
| 113 | |
| 114 | // shallow copy Map in Map |
| 115 | this.outgoingLinks = new ConcurrentHashMap<>(original.outgoingLinks.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 116 | for (Entry<SwitchPort, ConcurrentMap<String, LinkData>> entry |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 117 | : original.outgoingLinks.entrySet()) { |
| 118 | this.outgoingLinks.put(entry.getKey(), new ConcurrentHashMap<>(entry.getValue())); |
| 119 | } |
| 120 | |
| 121 | // shallow copy Map in Map |
| 122 | this.incomingLinks = new ConcurrentHashMap<>(original.incomingLinks.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 123 | for (Entry<SwitchPort, ConcurrentMap<String, LinkData>> entry |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 124 | : original.incomingLinks.entrySet()) { |
| 125 | this.incomingLinks.put(entry.getKey(), new ConcurrentHashMap<>(entry.getValue())); |
| 126 | } |
| 127 | } finally { |
| 128 | original.releaseReadLock(); |
| 129 | } |
| 130 | } |
| 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 133 | public Switch getSwitch(Dpid dpid) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 134 | final SwitchData sw = switches.get(dpid); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 135 | if (sw != null) { |
| 136 | return new SwitchImpl(this, dpid); |
| 137 | } else { |
| 138 | return null; |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 139 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 141 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public Iterable<Switch> getSwitches() { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 144 | List<Switch> list = new ArrayList<>(switches.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 145 | for (SwitchData elm : switches.values()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 146 | list.add(new SwitchImpl(this, elm.getDpid())); |
| 147 | } |
| 148 | return list; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 150 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 151 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 152 | public Port getPort(Dpid dpid, PortNumber number) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 153 | ConcurrentMap<PortNumber, PortData> portMap = ports.get(dpid); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 154 | if (portMap != null) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 155 | final PortData port = portMap.get(number); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 156 | if (port != null) { |
| 157 | return new PortImpl(this, port.getSwitchPort()); |
| 158 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 159 | } |
| 160 | return null; |
| 161 | } |
Pavlin Radoslavov | 06df22a | 2014-02-18 19:16:27 -0800 | [diff] [blame] | 162 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 164 | public Port getPort(SwitchPort port) { |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 165 | return getPort(port.getDpid(), port.getPortNumber()); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 169 | public Collection<Port> getPorts(Dpid dpid) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 170 | ConcurrentMap<PortNumber, PortData> portMap = ports.get(dpid); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 171 | if (portMap == null) { |
| 172 | return Collections.emptyList(); |
| 173 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 174 | List<Port> list = new ArrayList<>(portMap.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 175 | for (PortData elm : portMap.values()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 176 | list.add(new PortImpl(this, elm.getSwitchPort())); |
| 177 | } |
| 178 | return list; |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 182 | public Link getOutgoingLink(Dpid dpid, PortNumber number) { |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 183 | return getOutgoingLink(new SwitchPort(dpid, number)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | } |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 185 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 187 | public Link getOutgoingLink(SwitchPort port) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 188 | Map<String, LinkData> links = outgoingLinks.get(port); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 189 | return getPacketLinkIfExists(links); |
| 190 | } |
| 191 | |
| 192 | // TODO remove when we no longer need packet fall back behavior |
| 193 | /** |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 194 | * Gets the "packet" link if such exists, |
| 195 | * if not return whichever link is found first. |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 196 | * |
| 197 | * @param links Collection of links to search from |
| 198 | * @return Link instance found or null if no link exists |
| 199 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 200 | private Link getPacketLinkIfExists(Map<String, LinkData> links) { |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 201 | |
| 202 | if (links == null) { |
| 203 | return null; |
| 204 | } |
| 205 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 206 | LinkData link = links.get(TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 207 | if (link != null) { |
| 208 | // return packet link |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 209 | return new LinkImpl(this, link.getLinkTuple()); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 210 | } else { |
| 211 | // return whatever found |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 212 | Iterator<LinkData> it = links.values().iterator(); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 213 | if (it.hasNext()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 214 | return new LinkImpl(this, it.next().getLinkTuple()); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Link getOutgoingLink(Dpid dpid, PortNumber number, String type) { |
| 222 | return getOutgoingLink(new SwitchPort(dpid, number), type); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Link getOutgoingLink(SwitchPort port, String type) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 227 | Map<String, LinkData> links = outgoingLinks.get(port); |
| 228 | final LinkData link = links.get(type); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 229 | if (link != null) { |
| 230 | return new LinkImpl(this, link.getLinkTuple()); |
| 231 | } |
| 232 | return null; |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Collection<Link> getOutgoingLinks(SwitchPort port) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 237 | ConcurrentMap<String, LinkData> typeMap = outgoingLinks.get(port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 238 | if (typeMap == null) { |
| 239 | return Collections.emptyList(); |
| 240 | } |
| 241 | return toLinkImpls(typeMap.values()); |
| 242 | } |
| 243 | |
| 244 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 245 | * Converts collection of LinkData to collection of LinkImpls. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 246 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 247 | * @param links collection of LinkData |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 248 | * @return collection of LinkImpls |
| 249 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 250 | private Collection<Link> toLinkImpls(final Collection<LinkData> links) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 251 | if (links == null) { |
| 252 | return Collections.emptyList(); |
| 253 | } |
| 254 | List<Link> list = new ArrayList<>(links.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 255 | for (LinkData elm : links) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 256 | list.add(new LinkImpl(this, elm.getLinkTuple())); |
| 257 | } |
| 258 | return list; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public Link getIncomingLink(Dpid dpid, PortNumber number) { |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 263 | return getIncomingLink(new SwitchPort(dpid, number)); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public Link getIncomingLink(SwitchPort port) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 268 | Map<String, LinkData> links = incomingLinks.get(port); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 269 | return getPacketLinkIfExists(links); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Link getIncomingLink(Dpid dpid, PortNumber number, String type) { |
| 274 | return getIncomingLink(new SwitchPort(dpid, number), type); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public Link getIncomingLink(SwitchPort port, String type) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 279 | Map<String, LinkData> links = incomingLinks.get(port); |
| 280 | final LinkData link = links.get(type); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 281 | if (link != null) { |
| 282 | return new LinkImpl(this, link.getLinkTuple()); |
| 283 | } |
| 284 | return null; |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Collection<Link> getIncomingLinks(SwitchPort port) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 289 | ConcurrentMap<String, LinkData> typeMap = incomingLinks.get(port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 290 | if (typeMap == null) { |
| 291 | return Collections.emptyList(); |
| 292 | } |
| 293 | return toLinkImpls(typeMap.values()); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public Link getLink(Dpid srcDpid, PortNumber srcNumber, |
| 298 | Dpid dstDpid, PortNumber dstNumber) { |
| 299 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 300 | final SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstNumber); |
| 301 | Collection<Link> links = getOutgoingLinks(new SwitchPort(srcDpid, srcNumber)); |
| 302 | for (Link link : links) { |
| 303 | if (link == null) { |
| 304 | continue; |
| 305 | } |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 306 | if (link.getDstPort().getSwitchPort().equals(dstSwitchPort)) { |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 307 | return link; |
| 308 | } |
| 309 | } |
| 310 | return null; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public Link getLink(Dpid srcDpid, PortNumber srcNumber, |
| 315 | Dpid dstDpid, PortNumber dstNumber, |
| 316 | String type) { |
| 317 | |
| 318 | Link link = getOutgoingLink(srcDpid, srcNumber, type); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 319 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 321 | } |
| 322 | if (!link.getDstSwitch().getDpid().equals(dstDpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 324 | } |
| 325 | if (!link.getDstPort().getNumber().equals(dstNumber)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 326 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 327 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 328 | return link; |
| 329 | } |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 330 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public Iterable<Link> getLinks() { |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 333 | List<Link> links = new ArrayList<>(); |
| 334 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 335 | for (Map<String, LinkData> portLinks : outgoingLinks.values()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 336 | if (portLinks == null) { |
| 337 | continue; |
| 338 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 339 | for (LinkData elm : portLinks.values()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 340 | links.add(new LinkImpl(this, elm.getLinkTuple())); |
| 341 | } |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 342 | } |
| 343 | return links; |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 344 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 345 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 346 | @Override |
| 347 | public Host getHostByMac(MACAddress address) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 348 | HostData host = mac2Host.get(address); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 349 | if (host != null) { |
| 350 | return new HostImpl(this, address); |
| 351 | } |
| 352 | return null; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public Iterable<Host> getHosts() { |
| 357 | return toHostImpls(mac2Host.values()); |
| 358 | } |
| 359 | |
| 360 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 361 | * Converts collection of HostData to collection of HostImpl. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 362 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 363 | * @param events collection of HostData |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 364 | * @return collection of HostImpl |
| 365 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 366 | private List<Host> toHostImpls(Collection<HostData> events) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 367 | if (events == null) { |
| 368 | return Collections.emptyList(); |
| 369 | } |
| 370 | List<Host> list = new ArrayList<>(events.size()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 371 | for (HostData elm : events) { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 372 | list.add(new HostImpl(this, elm.getMac(), elm.getIp())); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 373 | } |
| 374 | return list; |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public Collection<Host> getHosts(SwitchPort port) { |
| 379 | return toHostImpls(hosts.get(port)); |
| 380 | } |
| 381 | |
| 382 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 383 | public SwitchData getSwitchData(final Dpid dpid) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 384 | return this.switches.get(dpid); |
| 385 | } |
| 386 | |
| 387 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 388 | public Collection<SwitchData> getAllSwitchDataEntries() { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 389 | return Collections.unmodifiableCollection(switches.values()); |
| 390 | } |
| 391 | |
| 392 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 393 | public PortData getPortData(final SwitchPort port) { |
| 394 | return getPortData(port.getDpid(), port.getPortNumber()); |
Yuta HIGUCHI | 45ccade | 2014-08-18 17:09:19 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 398 | public PortData getPortData(final Dpid dpid, PortNumber portNumber) { |
| 399 | ConcurrentMap<PortNumber, PortData> portMap = this.ports.get(dpid); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 400 | if (portMap != null) { |
Yuta HIGUCHI | 45ccade | 2014-08-18 17:09:19 -0700 | [diff] [blame] | 401 | return portMap.get(portNumber); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 402 | } |
| 403 | return null; |
| 404 | } |
| 405 | |
| 406 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 407 | public Collection<PortData> getPortDataEntries(final Dpid dpid) { |
| 408 | ConcurrentMap<PortNumber, PortData> portList = ports.get(dpid); |
Yuta HIGUCHI | 45ccade | 2014-08-18 17:09:19 -0700 | [diff] [blame] | 409 | if (portList == null) { |
| 410 | return Collections.emptyList(); |
| 411 | } |
| 412 | return Collections.unmodifiableCollection(portList.values()); |
| 413 | } |
| 414 | |
| 415 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 416 | public Collection<PortData> getAllPortDataEntries() { |
| 417 | List<PortData> events = new LinkedList<>(); |
| 418 | for (ConcurrentMap<PortNumber, PortData> cm : ports.values()) { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 419 | events.addAll(cm.values()); |
| 420 | } |
| 421 | return Collections.unmodifiableCollection(events); |
| 422 | } |
| 423 | |
| 424 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 425 | public LinkData getLinkData(final LinkTuple linkId) { |
| 426 | ConcurrentMap<String, LinkData> links = this.outgoingLinks.get(linkId.getSrc()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 427 | if (links == null) { |
| 428 | return null; |
| 429 | } |
| 430 | |
| 431 | // TODO Should we look for Packet link first? |
| 432 | // Not unless invariant is broken. |
| 433 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 434 | for (LinkData link : links.values()) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 435 | if (link.getDst().equals(linkId.getDst())) { |
| 436 | return link; |
| 437 | } |
| 438 | } |
| 439 | return null; |
| 440 | } |
| 441 | |
| 442 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 443 | public LinkData getLinkData(final LinkTuple linkId, final String type) { |
| 444 | ConcurrentMap<String, LinkData> links = this.outgoingLinks.get(linkId.getSrc()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 445 | if (links == null) { |
| 446 | return null; |
| 447 | } |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 448 | LinkData link = links.get(type); |
Yuta HIGUCHI | 4f32884 | 2014-08-28 17:23:09 -0700 | [diff] [blame] | 449 | if (link.getDst().equals(linkId.getDst())) { |
| 450 | return link; |
| 451 | } |
| 452 | return null; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 456 | public Collection<LinkData> getLinkDataEntriesFrom(SwitchPort srcPort) { |
| 457 | ConcurrentMap<String, LinkData> links = this.outgoingLinks.get(srcPort); |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 458 | if (links == null) { |
| 459 | return Collections.emptyList(); |
| 460 | } |
| 461 | return Collections.unmodifiableCollection(links.values()); |
| 462 | } |
| 463 | |
| 464 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 465 | public Collection<LinkData> getLinkDataEntriesTo(SwitchPort dstPort) { |
| 466 | ConcurrentMap<String, LinkData> links = this.incomingLinks.get(dstPort); |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 467 | if (links == null) { |
| 468 | return Collections.emptyList(); |
| 469 | } |
| 470 | return Collections.unmodifiableCollection(links.values()); |
| 471 | } |
| 472 | |
| 473 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 474 | public Collection<LinkData> getLinkDataEntries(final LinkTuple linkId) { |
| 475 | ConcurrentMap<String, LinkData> links = this.outgoingLinks.get(linkId.getSrc()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 476 | if (links == null) { |
| 477 | return Collections.emptyList(); |
| 478 | } |
| 479 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 480 | List<LinkData> linkDataEntries = new ArrayList<>(); |
| 481 | for (LinkData e : links.values()) { |
Yuta HIGUCHI | 4f32884 | 2014-08-28 17:23:09 -0700 | [diff] [blame] | 482 | if (e.getDst().equals(linkId.getDst())) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 483 | linkDataEntries.add(e); |
Yuta HIGUCHI | 4f32884 | 2014-08-28 17:23:09 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 487 | // unless invariant is broken, this should contain at most 1 element. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 488 | return linkDataEntries; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 492 | public Collection<LinkData> getAllLinkDataEntries() { |
| 493 | List<LinkData> events = new LinkedList<>(); |
| 494 | for (ConcurrentMap<String, LinkData> cm : outgoingLinks.values()) { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 495 | events.addAll(cm.values()); |
| 496 | } |
| 497 | return Collections.unmodifiableCollection(events); |
| 498 | } |
| 499 | |
| 500 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 501 | public HostData getHostData(final MACAddress mac) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 502 | return this.mac2Host.get(mac); |
| 503 | } |
| 504 | |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 505 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 506 | public Collection<HostData> getHostDataEntries(SwitchPort port) { |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 507 | return Collections.unmodifiableCollection(hosts.get(port)); |
| 508 | } |
| 509 | |
| 510 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 511 | public Collection<HostData> getAllHostDataEntries() { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 512 | return Collections.unmodifiableCollection(mac2Host.values()); |
| 513 | } |
| 514 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 515 | @Override |
| 516 | public OnosInstanceId getSwitchMaster(Dpid dpid) { |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 517 | final SortedSet<MastershipData> candidates = mastership.get(dpid); |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 518 | if (candidates == null) { |
| 519 | return null; |
| 520 | } |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 521 | for (MastershipData candidate : candidates) { |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 522 | if (candidate.getRole() == Role.MASTER) { |
| 523 | return candidate.getOnosInstanceId(); |
| 524 | } |
| 525 | } |
| 526 | return null; |
| 527 | } |
| 528 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 529 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 530 | * Puts a SwitchData. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 531 | * |
| 532 | * @param sw Switch to add. (Will be frozen if not already) |
| 533 | */ |
| 534 | @GuardedBy("writeLock") |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 535 | protected void putSwitch(SwitchData sw) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 536 | // TODO isFrozen check once we implement CoW/lock-free |
| 537 | switches.put(sw.getDpid(), sw.freeze()); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 538 | ports.putIfAbsent(sw.getDpid(), new ConcurrentHashMap<PortNumber, PortData>()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 542 | * Removes a SwitchData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 543 | * <p/> |
| 544 | * Will also remove ports, if it has not been removed already. |
| 545 | * |
| 546 | * @param dpid Switch DPID |
| 547 | */ |
| 548 | @GuardedBy("writeLock") |
| 549 | protected void removeSwitch(Dpid dpid) { |
| 550 | // TODO isFrozen check once we implement CoW/lock-free |
| 551 | switches.remove(dpid); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 552 | ConcurrentMap<PortNumber, PortData> removedPorts = ports.remove(dpid); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 553 | if (removedPorts != null && !removedPorts.isEmpty()) { |
| 554 | log.warn("Some ports were removed as side-effect of #removeSwitch({})", dpid); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 559 | * Puts a PortData. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 560 | * |
| 561 | * @param port Port to add. (Will be frozen if not already) |
| 562 | */ |
| 563 | @GuardedBy("writeLock") |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 564 | protected void putPort(PortData port) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 565 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 566 | ConcurrentMap<PortNumber, PortData> portMap = ports.get(port.getDpid()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 567 | if (portMap == null) { |
| 568 | portMap = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 569 | ConcurrentMap<PortNumber, PortData> existing |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 570 | = ports.putIfAbsent(port.getDpid(), portMap); |
| 571 | if (existing != null) { |
| 572 | // port map was added concurrently, using theirs |
| 573 | portMap = existing; |
| 574 | } |
| 575 | } |
| 576 | portMap.put(port.getPortNumber(), port.freeze()); |
| 577 | } |
| 578 | |
| 579 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 580 | * Removes a PortData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 581 | * |
| 582 | * @param port SwitchPort to remove |
| 583 | */ |
| 584 | @GuardedBy("writeLock") |
| 585 | protected void removePort(SwitchPort port) { |
| 586 | removePort(port.getDpid(), port.getPortNumber()); |
| 587 | } |
| 588 | |
| 589 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 590 | * Removes a PortData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 591 | * <p/> |
| 592 | * Will also remove ports, if it has not been removed already. |
| 593 | * |
| 594 | * @param dpid Switch DPID |
| 595 | * @param number PortNumber |
| 596 | */ |
| 597 | @GuardedBy("writeLock") |
| 598 | protected void removePort(Dpid dpid, PortNumber number) { |
| 599 | // TODO sanity check Host attachment point. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 600 | ConcurrentMap<PortNumber, PortData> portMap = ports.get(dpid); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 601 | if (portMap != null) { |
| 602 | portMap.remove(number); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 607 | * Puts a LinkData. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 608 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 609 | * @param link LinkData |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 610 | */ |
| 611 | @GuardedBy("writeLock") |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 612 | protected void putLink(LinkData link) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 613 | // TODO Do sanity check? |
| 614 | // - There cannot be 2 links in same direction between a port pair. |
| 615 | putLinkMap(outgoingLinks, link.getSrc(), link); |
| 616 | putLinkMap(incomingLinks, link.getDst(), link); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 619 | /** |
| 620 | * Helper method to update outgoingLinks, incomingLinks. |
| 621 | * |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 622 | * @param linkMap outgoingLinks or incomingLinks to update |
| 623 | * @param port {@code linkMap} key to update |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 624 | * @param link Link to add |
| 625 | */ |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 626 | @GuardedBy("writeLock") |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 627 | private void putLinkMap(ConcurrentMap<SwitchPort, ConcurrentMap<String, LinkData>> linkMap, |
| 628 | SwitchPort port, LinkData link) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 629 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 630 | ConcurrentMap<String, LinkData> linksOnPort = linkMap.get(port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 631 | if (linksOnPort == null) { |
| 632 | linksOnPort = new ConcurrentHashMap<>(4); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 633 | ConcurrentMap<String, LinkData> existing |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 634 | = linkMap.putIfAbsent( |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 635 | port, |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 636 | linksOnPort); |
| 637 | |
| 638 | if (existing != null) { |
| 639 | linksOnPort = existing; |
| 640 | } |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 641 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 642 | linksOnPort.put(link.getType(), link); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 645 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 646 | * Removes a LinkData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 647 | * |
| 648 | * @param link Link to remove |
| 649 | * @param type type of link to remove |
| 650 | */ |
| 651 | @GuardedBy("writeLock") |
| 652 | protected void removeLink(LinkTuple link, String type) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 653 | ConcurrentMap<String, LinkData> portLinks |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 654 | = outgoingLinks.get(link.getSrc()); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 655 | if (portLinks != null) { |
| 656 | // no conditional update here |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 657 | portLinks.remove(type); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 658 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 659 | portLinks |
| 660 | = incomingLinks.get(link.getDst()); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 661 | if (portLinks != null) { |
| 662 | // no conditional update here |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 663 | portLinks.remove(type); |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 664 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 665 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 666 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 667 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 668 | * Removes a LinkData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 669 | * |
| 670 | * @param link Link to remove |
| 671 | */ |
| 672 | @GuardedBy("writeLock") |
| 673 | protected void removeLink(LinkTuple link) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 674 | Collection<LinkData> links = getLinkDataEntries(link); |
| 675 | for (LinkData l : links) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 676 | removeLink(link, l.getType()); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 677 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 681 | * Puts a HostData. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 682 | * <p/> |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 683 | * Removes attachment points for previous HostData and update |
| 684 | * them with new HostData |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 685 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 686 | * @param host HostData |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 687 | */ |
| 688 | @GuardedBy("writeLock") |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 689 | protected void putHost(HostData host) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 690 | // Host cannot be simply put() to replace instance since it has mobility. |
| 691 | // Simply remove -> put for now. |
| 692 | |
| 693 | // remove old attachment points |
| 694 | removeHost(host.getMac()); |
| 695 | |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 696 | // add new attachment points |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 697 | for (SwitchPort port : host.getAttachmentPoints()) { |
| 698 | hosts.put(port, host); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 699 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 700 | mac2Host.put(host.getMac(), host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 701 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 702 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 703 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 704 | * Removes a HostData from this snapshot. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 705 | * |
| 706 | * @param mac MACAddress of the Host to remove |
| 707 | */ |
| 708 | @GuardedBy("writeLock") |
| 709 | protected void removeHost(MACAddress mac) { |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 710 | HostData host = mac2Host.remove(mac); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 711 | if (host != null) { |
| 712 | for (SwitchPort port : host.getAttachmentPoints()) { |
| 713 | hosts.remove(port, host); |
| 714 | } |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 715 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 716 | } |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 717 | |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 718 | /** |
| 719 | * Puts a mastership change event. |
| 720 | * |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 721 | * @param master MastershipData |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 722 | */ |
| 723 | @GuardedBy("writeLock") |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 724 | protected void putSwitchMastershipData(MastershipData master) { |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 725 | checkNotNull(master); |
| 726 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 727 | SortedSet<MastershipData> candidates |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 728 | = mastership.get(master.getDpid()); |
| 729 | if (candidates == null) { |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 730 | // SortedSet, customized so that MASTER MastershipData appear |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 731 | // earlier during iteration. |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 732 | candidates = new TreeSet<>(new MastershipData.MasterFirstComparator()); |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // always replace |
| 736 | candidates.remove(master); |
| 737 | candidates.add(master); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Removes a mastership change event. |
| 742 | * <p> |
| 743 | * Note: Only Dpid and OnosInstanceId will be used to identify the |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 744 | * {@link MastershipData} to remove. |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 745 | * |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 746 | * @param master {@link MastershipData} to remove. (Role is ignored) |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 747 | */ |
| 748 | @GuardedBy("writeLock") |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 749 | protected void removeSwitchMastershipData(MastershipData master) { |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 750 | checkNotNull(master); |
| 751 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 752 | SortedSet<MastershipData> candidates |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 753 | = mastership.get(master.getDpid()); |
| 754 | if (candidates == null) { |
| 755 | // nothing to do |
| 756 | return; |
| 757 | } |
| 758 | candidates.remove(master); |
| 759 | } |
| 760 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 761 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 762 | @Override |
| 763 | public void acquireReadLock() { |
| 764 | readLock.lock(); |
| 765 | } |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 766 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 767 | @Override |
| 768 | public void releaseReadLock() { |
| 769 | readLock.unlock(); |
| 770 | } |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 771 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 772 | protected void acquireWriteLock() { |
| 773 | writeLock.lock(); |
| 774 | } |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 775 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 776 | protected void releaseWriteLock() { |
| 777 | writeLock.unlock(); |
| 778 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 779 | } |