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 | |
| 3 | import java.net.InetAddress; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 4 | import java.util.Collections; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 5 | import java.util.HashSet; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 9 | import java.util.concurrent.ConcurrentHashMap; |
| 10 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 11 | import java.util.concurrent.locks.Lock; |
| 12 | import java.util.concurrent.locks.ReadWriteLock; |
| 13 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 14 | |
| 15 | import net.floodlightcontroller.util.MACAddress; |
| 16 | |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
Pavlin Radoslavov | 6397a7f | 2014-02-18 14:56:52 -0800 | [diff] [blame] | 20 | public class NetworkGraphImpl implements NetworkGraph { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | @SuppressWarnings("unused") |
| 22 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 23 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 24 | // DPID -> Switch |
| 25 | private ConcurrentMap<Long, Switch> switches; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 26 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | private ConcurrentMap<InetAddress, Set<Device>> addr2Device; |
| 28 | private ConcurrentMap<MACAddress, Device> mac2Device; |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 29 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); |
| 31 | private Lock readLock = readWriteLock.readLock(); |
| 32 | // TODO use the write lock after refactor |
| 33 | private Lock writeLock = readWriteLock.writeLock(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 34 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | public NetworkGraphImpl() { |
| 36 | // TODO: Does these object need to be stored in Concurrent Collection? |
| 37 | switches = new ConcurrentHashMap<>(); |
| 38 | addr2Device = new ConcurrentHashMap<>(); |
| 39 | mac2Device = new ConcurrentHashMap<>(); |
| 40 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | @Override |
| 43 | public Switch getSwitch(Long dpid) { |
| 44 | // TODO Check if it is safe to directly return this Object. |
| 45 | return switches.get(dpid); |
| 46 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | protected void putSwitch(Switch sw) { |
| 49 | switches.put(sw.getDpid(), sw); |
| 50 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | protected void removeSwitch(Long dpid) { |
| 53 | switches.remove(dpid); |
| 54 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | @Override |
| 57 | public Iterable<Switch> getSwitches() { |
| 58 | // TODO Check if it is safe to directly return this Object. |
| 59 | return Collections.unmodifiableCollection(switches.values()); |
| 60 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public Port getPort(Long dpid, Long number) { |
| 64 | Switch sw = getSwitch(dpid); |
| 65 | if (sw != null) { |
| 66 | return sw.getPort(number); |
| 67 | } |
| 68 | return null; |
| 69 | } |
Pavlin Radoslavov | 06df22a | 2014-02-18 19:16:27 -0800 | [diff] [blame] | 70 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public Link getLink(Long dpid, Long number) { |
| 73 | Port srcPort = getPort(dpid, number); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 74 | if (srcPort == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 76 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | return srcPort.getOutgoingLink(); |
| 78 | } |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid, |
| 82 | Long dstNumber) { |
| 83 | Link link = getLink(srcDpid, srcNumber); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 84 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 86 | } |
| 87 | if (!link.getDstSwitch().getDpid().equals(dstDpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 89 | } |
| 90 | if (!link.getDstPort().getNumber().equals(dstNumber)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 92 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | return link; |
| 94 | } |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public Iterable<Link> getLinks() { |
| 98 | List<Link> linklist = new LinkedList<>(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | for (Switch sw : switches.values()) { |
| 101 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 102 | for (Link l : links) { |
| 103 | linklist.add(l); |
| 104 | } |
| 105 | } |
| 106 | return linklist; |
| 107 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 108 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public Iterable<Device> getDevicesByIp(InetAddress ipAddress) { |
| 111 | Set<Device> devices = addr2Device.get(ipAddress); |
| 112 | if (devices == null) { |
| 113 | return Collections.emptySet(); |
| 114 | } |
| 115 | return Collections.unmodifiableCollection(devices); |
| 116 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 117 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public Device getDeviceByMac(MACAddress address) { |
| 120 | return mac2Device.get(address); |
| 121 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 122 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | protected void putDevice(Device device) { |
| 124 | mac2Device.put(device.getMacAddress(), device); |
| 125 | for (InetAddress ipAddr : device.getIpAddress()) { |
| 126 | Set<Device> devices = addr2Device.get(ipAddr); |
| 127 | if (devices == null) { |
| 128 | devices = new HashSet<>(); |
| 129 | addr2Device.put(ipAddr, devices); |
| 130 | } |
| 131 | devices.add(device); |
| 132 | } |
| 133 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 134 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | protected void removeDevice(Device device) { |
| 136 | mac2Device.remove(device.getMacAddress()); |
| 137 | for (InetAddress ipAddr : device.getIpAddress()) { |
| 138 | Set<Device> devices = addr2Device.get(ipAddr); |
| 139 | if (devices != null) { |
| 140 | devices.remove(device); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 141 | if (devices.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | addr2Device.remove(ipAddr); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame^] | 143 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 147 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public void acquireReadLock() { |
| 150 | readLock.lock(); |
| 151 | } |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 152 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public void releaseReadLock() { |
| 155 | readLock.unlock(); |
| 156 | } |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 157 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | protected void acquireWriteLock() { |
| 159 | writeLock.lock(); |
| 160 | } |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 161 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | protected void releaseWriteLock() { |
| 163 | writeLock.unlock(); |
| 164 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 165 | } |