blob: 4425a7deed2f96925135c62ed25117f17cac05e5 [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;
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -08006import java.util.HashSet;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08007import java.util.LinkedList;
8import java.util.List;
9import java.util.Set;
10import java.util.concurrent.ConcurrentHashMap;
11import java.util.concurrent.ConcurrentMap;
12
13import net.floodlightcontroller.util.MACAddress;
14
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080018public class NetworkGraphImpl implements NetworkGraph {
Toshio Koide2f570c12014-02-06 16:55:32 -080019 @SuppressWarnings("unused")
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080020 private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080021
Toshio Koide2f570c12014-02-06 16:55:32 -080022 // DPID -> Switch
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080023 private ConcurrentMap<Long, Switch> switches;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080024
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080025 private ConcurrentMap<InetAddress, Set<Device>> addr2Device;
26 private ConcurrentMap<MACAddress, Device> mac2Device;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080027
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080028 public NetworkGraphImpl() {
Toshio Koide2f570c12014-02-06 16:55:32 -080029 // TODO: Does these object need to be stored in Concurrent Collection?
30 switches = new ConcurrentHashMap<>();
31 addr2Device = new ConcurrentHashMap<>();
32 mac2Device = new ConcurrentHashMap<>();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080033 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080034
Toshio Koide2f570c12014-02-06 16:55:32 -080035 @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 HIGUCHI80829d12014-02-05 20:16:56 -080039 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080040
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080041 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 Koide2f570c12014-02-06 16:55:32 -080049 @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 HIGUCHI80829d12014-02-05 20:16:56 -080053 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080054
Toshio Koide2f570c12014-02-06 16:55:32 -080055 @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 HIGUCHI80829d12014-02-05 20:16:56 -080066 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080067
Toshio Koide2f570c12014-02-06 16:55:32 -080068 @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 HIGUCHI4bfdd532014-02-07 13:47:36 -080085
Toshio Koide2f570c12014-02-06 16:55:32 -080086 @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 HIGUCHI8d9fddf2014-02-10 13:32:16 -0800109 return Collections.emptySet();
Toshio Koide2f570c12014-02-06 16:55:32 -0800110 }
111 return Collections.unmodifiableCollection(devices);
112 }
113
114 @Override
Toshio Koide6c9b7e82014-02-07 18:01:32 -0800115 public Device getDeviceByMac(MACAddress address) {
116 return mac2Device.get(address);
Toshio Koide2f570c12014-02-06 16:55:32 -0800117 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800118
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 HIGUCHI80829d12014-02-05 20:16:56 -0800142}