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