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 |
| 56 | public Iterable<Link> getLinks() { |
| 57 | List<Link> linklist = new LinkedList<>(); |
| 58 | |
| 59 | for (Switch sw : switches.values()) { |
| 60 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 61 | for (Link l : links) { |
| 62 | linklist.add(l); |
| 63 | } |
| 64 | } |
| 65 | return linklist; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 66 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 67 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 68 | @Override |
| 69 | public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) { |
| 70 | Switch sw = getSwitch(dpid); |
| 71 | if (sw == null) { |
| 72 | return Collections.emptyList(); |
| 73 | } |
| 74 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 75 | if (links instanceof Collection) { |
| 76 | return Collections.unmodifiableCollection((Collection<Link>) links); |
| 77 | } else { |
| 78 | List<Link> linklist = new LinkedList<>(); |
| 79 | for (Link l : links) { |
| 80 | linklist.add(l); |
| 81 | } |
| 82 | return linklist; |
| 83 | } |
| 84 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 85 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 86 | @Override |
| 87 | public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) { |
| 88 | Switch sw = getSwitch(dpid); |
| 89 | if (sw == null) { |
| 90 | return Collections.emptyList(); |
| 91 | } |
| 92 | Iterable<Link> links = sw.getIncomingLinks(); |
| 93 | if (links instanceof Collection) { |
| 94 | return Collections.unmodifiableCollection((Collection<Link>) links); |
| 95 | } else { |
| 96 | List<Link> linklist = new LinkedList<>(); |
| 97 | for (Link l : links) { |
| 98 | linklist.add(l); |
| 99 | } |
| 100 | return linklist; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | @Override |
| 106 | public Iterable<Device> getDeviceByIp(InetAddress ipAddress) { |
| 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 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 142 | } |