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; |
| 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; |
| 11 | |
| 12 | import net.floodlightcontroller.util.MACAddress; |
| 13 | |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | public class AbstractNetworkGraph implements NetworkGraph { |
| 18 | @SuppressWarnings("unused") |
| 19 | private static final Logger log = LoggerFactory |
| 20 | .getLogger(AbstractNetworkGraph.class); |
| 21 | |
| 22 | // DPID -> Switch |
| 23 | protected ConcurrentMap<Long, Switch> switches; |
| 24 | |
| 25 | protected ConcurrentMap<InetAddress, Set<Device>> addr2Device; |
| 26 | protected ConcurrentMap<MACAddress, Set<Device>> mac2Device; |
| 27 | |
| 28 | public AbstractNetworkGraph() { |
| 29 | // TODO: Does these object need to be stored in Concurrent Collection? |
| 30 | switches = new ConcurrentHashMap<>(); |
| 31 | addr2Device = new ConcurrentHashMap<>(); |
| 32 | mac2Device = new ConcurrentHashMap<>(); |
| 33 | } |
| 34 | |
| 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); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public Iterable<Switch> getSwitches() { |
| 43 | // TODO Check if it is safe to directly return this Object. |
| 44 | return Collections.unmodifiableCollection(switches.values()); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Iterable<Link> getLinks() { |
| 49 | List<Link> linklist = new LinkedList<>(); |
| 50 | |
| 51 | for (Switch sw : switches.values()) { |
| 52 | Iterable<Link> links = sw.getLinks(); |
| 53 | for (Link l : links) { |
| 54 | linklist.add(l); |
| 55 | } |
| 56 | } |
| 57 | return linklist; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Iterable<Link> getLinksFromSwitch(long dpid) { |
| 62 | Switch sw = getSwitch(dpid); |
| 63 | if (sw == null) { |
| 64 | return Collections.emptyList(); |
| 65 | } |
| 66 | Iterable<Link> links = sw.getLinks(); |
| 67 | if (links instanceof Collection) { |
| 68 | return Collections.unmodifiableCollection((Collection<Link>) links); |
| 69 | } else { |
| 70 | List<Link> linklist = new LinkedList<>(); |
| 71 | for (Link l : links) { |
| 72 | linklist.add(l); |
| 73 | } |
| 74 | return linklist; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Iterable<Device> getDeviceByIp(InetAddress ipAddress) { |
| 80 | Set<Device> devices = addr2Device.get(ipAddress); |
| 81 | if (devices == null) { |
| 82 | return Collections.emptyList(); |
| 83 | } |
| 84 | return Collections.unmodifiableCollection(devices); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Iterable<Device> getDeviceByMac(MACAddress address) { |
| 89 | Set<Device> devices = mac2Device.get(address); |
| 90 | if (devices == null) { |
| 91 | return Collections.emptyList(); |
| 92 | } |
| 93 | return Collections.unmodifiableCollection(devices); |
| 94 | } |
| 95 | |
| 96 | } |