blob: 628ad7cc1738a157ee6acc94adb22c90b0d9c269 [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
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080056 public Port getPort(Long dpid, Long number) {
57 Switch sw = getSwitch(dpid);
58 if (sw != null) {
59 return sw.getPort(number);
60 }
61 return null;
62 }
63
64 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080065 public Iterable<Link> getLinks() {
66 List<Link> linklist = new LinkedList<>();
67
68 for (Switch sw : switches.values()) {
69 Iterable<Link> links = sw.getOutgoingLinks();
70 for (Link l : links) {
71 linklist.add(l);
72 }
73 }
74 return linklist;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080075 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080076
Toshio Koide2f570c12014-02-06 16:55:32 -080077 @Override
78 public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) {
79 Switch sw = getSwitch(dpid);
80 if (sw == null) {
81 return Collections.emptyList();
82 }
83 Iterable<Link> links = sw.getOutgoingLinks();
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 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080094
Toshio Koide2f570c12014-02-06 16:55:32 -080095 @Override
96 public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) {
97 Switch sw = getSwitch(dpid);
98 if (sw == null) {
99 return Collections.emptyList();
100 }
101 Iterable<Link> links = sw.getIncomingLinks();
102 if (links instanceof Collection) {
103 return Collections.unmodifiableCollection((Collection<Link>) links);
104 } else {
105 List<Link> linklist = new LinkedList<>();
106 for (Link l : links) {
107 linklist.add(l);
108 }
109 return linklist;
110 }
111 }
112
113
114 @Override
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -0800115 public Iterable<Device> getDevicesByIp(InetAddress ipAddress) {
Toshio Koide2f570c12014-02-06 16:55:32 -0800116 Set<Device> devices = addr2Device.get(ipAddress);
117 if (devices == null) {
Yuta HIGUCHI8d9fddf2014-02-10 13:32:16 -0800118 return Collections.emptySet();
Toshio Koide2f570c12014-02-06 16:55:32 -0800119 }
120 return Collections.unmodifiableCollection(devices);
121 }
122
123 @Override
Toshio Koide6c9b7e82014-02-07 18:01:32 -0800124 public Device getDeviceByMac(MACAddress address) {
125 return mac2Device.get(address);
Toshio Koide2f570c12014-02-06 16:55:32 -0800126 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800127
128 protected void putDevice(Device device) {
129 mac2Device.put(device.getMacAddress(), device);
130 for (InetAddress ipAddr : device.getIpAddress()) {
131 Set<Device> devices = addr2Device.get(ipAddr);
132 if (devices == null) {
133 devices = new HashSet<>();
134 addr2Device.put(ipAddr, devices);
135 }
136 devices.add(device);
137 }
138 }
139
140 protected void removeDevice(Device device) {
141 mac2Device.remove(device.getMacAddress());
142 for (InetAddress ipAddr : device.getIpAddress()) {
143 Set<Device> devices = addr2Device.get(ipAddr);
144 if (devices != null) {
145 devices.remove(device);
146 if (devices.isEmpty())
147 addr2Device.remove(ipAddr);
148 }
149 }
150 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800151}