blob: 71ef16ec38e90e22911b5fb6ae1524bf044f300b [file] [log] [blame]
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
3import java.net.InetAddress;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Set;
9import java.util.concurrent.ConcurrentHashMap;
10import java.util.concurrent.ConcurrentMap;
11
12import net.floodlightcontroller.util.MACAddress;
13
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
17public class AbstractNetworkGraph implements NetworkGraph {
Toshio Koide2f570c12014-02-06 16:55:32 -080018 @SuppressWarnings("unused")
19 private static final Logger log = LoggerFactory.getLogger(AbstractNetworkGraph.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080020
Toshio Koide2f570c12014-02-06 16:55:32 -080021 // DPID -> Switch
22 protected ConcurrentMap<Long, Switch> switches;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080023
Toshio Koide2f570c12014-02-06 16:55:32 -080024 protected ConcurrentMap<InetAddress, Set<Device>> addr2Device;
25 protected ConcurrentMap<MACAddress, Set<Device>> mac2Device;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080026
Toshio Koide2f570c12014-02-06 16:55:32 -080027 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 HIGUCHI80829d12014-02-05 20:16:56 -080032 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080033
Toshio Koide2f570c12014-02-06 16:55:32 -080034 @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 HIGUCHI80829d12014-02-05 20:16:56 -080038 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080039
Toshio Koide2f570c12014-02-06 16:55:32 -080040 @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 HIGUCHI80829d12014-02-05 20:16:56 -080044 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080045
Toshio Koide2f570c12014-02-06 16:55:32 -080046 @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 HIGUCHI80829d12014-02-05 20:16:56 -080057 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080058
Toshio Koide2f570c12014-02-06 16:55:32 -080059 @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 HIGUCHI80829d12014-02-05 20:16:56 -0800113}