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 { |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 18 | @SuppressWarnings("unused") |
| 19 | private static final Logger log = LoggerFactory.getLogger(AbstractNetworkGraph.class); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 20 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 21 | // DPID -> Switch |
| 22 | protected ConcurrentMap<Long, Switch> switches; |
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 | protected ConcurrentMap<InetAddress, Set<Device>> addr2Device; |
| 25 | protected ConcurrentMap<MACAddress, Set<Device>> mac2Device; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 26 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 27 | public AbstractNetworkGraph() { |
| 28 | // TODO: Does these object need to be stored in Concurrent Collection? |
| 29 | switches = new ConcurrentHashMap<>(); |
| 30 | addr2Device = new ConcurrentHashMap<>(); |
| 31 | mac2Device = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 32 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 33 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 34 | @Override |
| 35 | public Switch getSwitch(Long dpid) { |
| 36 | // TODO Check if it is safe to directly return this Object. |
| 37 | return switches.get(dpid); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 38 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 39 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 40 | @Override |
| 41 | public Iterable<Switch> getSwitches() { |
| 42 | // TODO Check if it is safe to directly return this Object. |
| 43 | return Collections.unmodifiableCollection(switches.values()); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 44 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 45 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 46 | @Override |
| 47 | public Iterable<Link> getLinks() { |
| 48 | List<Link> linklist = new LinkedList<>(); |
| 49 | |
| 50 | for (Switch sw : switches.values()) { |
| 51 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 52 | for (Link l : links) { |
| 53 | linklist.add(l); |
| 54 | } |
| 55 | } |
| 56 | return linklist; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 57 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 58 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 59 | @Override |
| 60 | public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) { |
| 61 | Switch sw = getSwitch(dpid); |
| 62 | if (sw == null) { |
| 63 | return Collections.emptyList(); |
| 64 | } |
| 65 | Iterable<Link> links = sw.getOutgoingLinks(); |
| 66 | if (links instanceof Collection) { |
| 67 | return Collections.unmodifiableCollection((Collection<Link>) links); |
| 68 | } else { |
| 69 | List<Link> linklist = new LinkedList<>(); |
| 70 | for (Link l : links) { |
| 71 | linklist.add(l); |
| 72 | } |
| 73 | return linklist; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) { |
| 79 | Switch sw = getSwitch(dpid); |
| 80 | if (sw == null) { |
| 81 | return Collections.emptyList(); |
| 82 | } |
| 83 | Iterable<Link> links = sw.getIncomingLinks(); |
| 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 | } |
| 94 | |
| 95 | |
| 96 | @Override |
| 97 | public Iterable<Device> getDeviceByIp(InetAddress ipAddress) { |
| 98 | Set<Device> devices = addr2Device.get(ipAddress); |
| 99 | if (devices == null) { |
| 100 | return Collections.emptyList(); |
| 101 | } |
| 102 | return Collections.unmodifiableCollection(devices); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Iterable<Device> getDeviceByMac(MACAddress address) { |
| 107 | Set<Device> devices = mac2Device.get(address); |
| 108 | if (devices == null) { |
| 109 | return Collections.emptyList(); |
| 110 | } |
| 111 | return Collections.unmodifiableCollection(devices); |
| 112 | } |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 113 | } |