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