blob: 88cedaca3554bd1477c569d318a5703b6a94cae6 [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
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080017public class NetworkGraphImpl implements NetworkGraph {
Toshio Koide2f570c12014-02-06 16:55:32 -080018 @SuppressWarnings("unused")
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080019 private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.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;
Toshio Koide6c9b7e82014-02-07 18:01:32 -080025 protected ConcurrentMap<MACAddress, Device> mac2Device;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080026
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080027 public NetworkGraphImpl() {
Toshio Koide2f570c12014-02-06 16:55:32 -080028 // 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 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080076
Toshio Koide2f570c12014-02-06 16:55:32 -080077 @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) {
Yuta HIGUCHI8d9fddf2014-02-10 13:32:16 -0800100 return Collections.emptySet();
Toshio Koide2f570c12014-02-06 16:55:32 -0800101 }
102 return Collections.unmodifiableCollection(devices);
103 }
104
105 @Override
Toshio Koide6c9b7e82014-02-07 18:01:32 -0800106 public Device getDeviceByMac(MACAddress address) {
107 return mac2Device.get(address);
Toshio Koide2f570c12014-02-06 16:55:32 -0800108 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800109}