Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 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 { |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 21 | @SuppressWarnings("unused") |
Pavlin Radoslavov | 6397a7f | 2014-02-18 14:56:52 -0800 | [diff] [blame] | 22 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 23 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 24 | // DPID -> Switch |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 25 | private ConcurrentMap<Long, Switch> switches; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 26 | |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [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 | |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [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 | |
Pavlin Radoslavov | 6397a7f | 2014-02-18 14:56:52 -0800 | [diff] [blame] | 35 | public NetworkGraphImpl() { |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 36 | // TODO: Does these object need to be stored in Concurrent Collection? |
| 37 | switches = new ConcurrentHashMap<>(); |
| 38 | addr2Device = new ConcurrentHashMap<>(); |
| 39 | mac2Device = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 40 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 41 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [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); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 46 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 47 | |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 48 | protected void putSwitch(Switch sw) { |
| 49 | switches.put(sw.getDpid(), sw); |
| 50 | } |
| 51 | |
| 52 | protected void removeSwitch(Long dpid) { |
| 53 | switches.remove(dpid); |
| 54 | } |
| 55 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [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()); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 60 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 61 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 62 | @Override |
Pavlin Radoslavov | 06df22a | 2014-02-18 19:16:27 -0800 | [diff] [blame] | 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 | } |
| 70 | |
| 71 | @Override |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 72 | public Link getLink(Long dpid, Long number) { |
| 73 | Port srcPort = getPort(dpid, number); |
| 74 | if (srcPort == null) |
| 75 | return null; |
| 76 | return srcPort.getOutgoingLink(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid, |
| 81 | Long dstNumber) { |
| 82 | Link link = getLink(srcDpid, srcNumber); |
| 83 | if (link == null) |
| 84 | return null; |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 85 | if (!link.getDstSwitch().getDpid().equals(dstDpid)) |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 86 | return null; |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 87 | if (!link.getDstPort().getNumber().equals(dstNumber)) |
Pavlin Radoslavov | 7c8f69a | 2014-02-19 19:01:45 -0800 | [diff] [blame] | 88 | return null; |
| 89 | return link; |
| 90 | } |
| 91 | |
| 92 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 93 | public Iterable<Link> getLinks() { |
| 94 | List<Link> linklist = new LinkedList<>(); |
| 95 | |
| 96 | for (Switch sw : switches.values()) { |
| 97 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 98 | for (Link l : links) { |
| 99 | linklist.add(l); |
| 100 | } |
| 101 | } |
| 102 | return linklist; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 103 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 104 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 105 | @Override |
Pavlin Radoslavov | 06df22a | 2014-02-18 19:16:27 -0800 | [diff] [blame] | 106 | public Iterable<Device> getDevicesByIp(InetAddress ipAddress) { |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 107 | Set<Device> devices = addr2Device.get(ipAddress); |
| 108 | if (devices == null) { |
Yuta HIGUCHI | 8d9fddf | 2014-02-10 13:32:16 -0800 | [diff] [blame] | 109 | return Collections.emptySet(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 110 | } |
| 111 | return Collections.unmodifiableCollection(devices); |
| 112 | } |
| 113 | |
| 114 | @Override |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -0800 | [diff] [blame] | 115 | public Device getDeviceByMac(MACAddress address) { |
| 116 | return mac2Device.get(address); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 117 | } |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 118 | |
| 119 | protected void putDevice(Device device) { |
| 120 | mac2Device.put(device.getMacAddress(), device); |
| 121 | for (InetAddress ipAddr : device.getIpAddress()) { |
| 122 | Set<Device> devices = addr2Device.get(ipAddr); |
| 123 | if (devices == null) { |
| 124 | devices = new HashSet<>(); |
| 125 | addr2Device.put(ipAddr, devices); |
| 126 | } |
| 127 | devices.add(device); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | protected void removeDevice(Device device) { |
| 132 | mac2Device.remove(device.getMacAddress()); |
| 133 | for (InetAddress ipAddr : device.getIpAddress()) { |
| 134 | Set<Device> devices = addr2Device.get(ipAddr); |
| 135 | if (devices != null) { |
| 136 | devices.remove(device); |
| 137 | if (devices.isEmpty()) |
| 138 | addr2Device.remove(ipAddr); |
| 139 | } |
| 140 | } |
| 141 | } |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 142 | |
| 143 | @Override |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 144 | public void acquireReadLock() { |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 145 | readLock.lock(); |
| 146 | } |
| 147 | |
| 148 | @Override |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 149 | public void releaseReadLock() { |
Jonathan Hart | 26f291b | 2014-02-18 16:57:24 -0800 | [diff] [blame] | 150 | readLock.unlock(); |
| 151 | } |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 152 | |
| 153 | protected void acquireWriteLock() { |
| 154 | writeLock.lock(); |
| 155 | } |
| 156 | |
| 157 | protected void releaseWriteLock() { |
| 158 | writeLock.unlock(); |
| 159 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 160 | } |