blob: 902ffa6d7fe8ee30b1ad378fae62b33dae55c28a [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08002
3import java.net.InetAddress;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08004import java.util.Collections;
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -08005import java.util.HashSet;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08006import java.util.LinkedList;
7import java.util.List;
8import java.util.Set;
9import java.util.concurrent.ConcurrentHashMap;
10import java.util.concurrent.ConcurrentMap;
Jonathan Hart26f291b2014-02-18 16:57:24 -080011import java.util.concurrent.locks.Lock;
12import java.util.concurrent.locks.ReadWriteLock;
13import java.util.concurrent.locks.ReentrantReadWriteLock;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080014
15import net.floodlightcontroller.util.MACAddress;
16
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080020public class NetworkGraphImpl implements NetworkGraph {
Toshio Koide2f570c12014-02-06 16:55:32 -080021 @SuppressWarnings("unused")
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080022 private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080023
Toshio Koide2f570c12014-02-06 16:55:32 -080024 // DPID -> Switch
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080025 private ConcurrentMap<Long, Switch> switches;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080026
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080027 private ConcurrentMap<InetAddress, Set<Device>> addr2Device;
28 private ConcurrentMap<MACAddress, Device> mac2Device;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080029
Jonathan Hart26f291b2014-02-18 16:57:24 -080030 private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
31 private Lock readLock = readWriteLock.readLock();
32 // TODO use the write lock after refactor
33 private Lock writeLock = readWriteLock.writeLock();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080034
Pavlin Radoslavov6397a7f2014-02-18 14:56:52 -080035 public NetworkGraphImpl() {
Toshio Koide2f570c12014-02-06 16:55:32 -080036 // TODO: Does these object need to be stored in Concurrent Collection?
37 switches = new ConcurrentHashMap<>();
38 addr2Device = new ConcurrentHashMap<>();
39 mac2Device = new ConcurrentHashMap<>();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080040 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080041
Toshio Koide2f570c12014-02-06 16:55:32 -080042 @Override
43 public Switch getSwitch(Long dpid) {
44 // TODO Check if it is safe to directly return this Object.
45 return switches.get(dpid);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080046 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080047
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080048 protected void putSwitch(Switch sw) {
49 switches.put(sw.getDpid(), sw);
50 }
51
52 protected void removeSwitch(Long dpid) {
53 switches.remove(dpid);
54 }
55
Toshio Koide2f570c12014-02-06 16:55:32 -080056 @Override
57 public Iterable<Switch> getSwitches() {
58 // TODO Check if it is safe to directly return this Object.
59 return Collections.unmodifiableCollection(switches.values());
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080060 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080061
Toshio Koide2f570c12014-02-06 16:55:32 -080062 @Override
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080063 public Port getPort(Long dpid, Long number) {
64 Switch sw = getSwitch(dpid);
65 if (sw != null) {
66 return sw.getPort(number);
67 }
68 return null;
69 }
70
71 @Override
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080072 public Link getLink(Long dpid, Long number) {
73 Port srcPort = getPort(dpid, number);
74 if (srcPort == null)
75 return null;
76 return srcPort.getOutgoingLink();
77 }
78
79 @Override
80 public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid,
81 Long dstNumber) {
82 Link link = getLink(srcDpid, srcNumber);
83 if (link == null)
84 return null;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080085 if (!link.getDstSwitch().getDpid().equals(dstDpid))
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080086 return null;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080087 if (!link.getDstPort().getNumber().equals(dstNumber))
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080088 return null;
89 return link;
90 }
91
92 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080093 public Iterable<Link> getLinks() {
94 List<Link> linklist = new LinkedList<>();
95
96 for (Switch sw : switches.values()) {
97 Iterable<Link> links = sw.getOutgoingLinks();
98 for (Link l : links) {
99 linklist.add(l);
100 }
101 }
102 return linklist;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800103 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800104
Toshio Koide2f570c12014-02-06 16:55:32 -0800105 @Override
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -0800106 public Iterable<Device> getDevicesByIp(InetAddress ipAddress) {
Toshio Koide2f570c12014-02-06 16:55:32 -0800107 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 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800142
143 @Override
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800144 public void acquireReadLock() {
Jonathan Hart26f291b2014-02-18 16:57:24 -0800145 readLock.lock();
146 }
147
148 @Override
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800149 public void releaseReadLock() {
Jonathan Hart26f291b2014-02-18 16:57:24 -0800150 readLock.unlock();
151 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800152
153 protected void acquireWriteLock() {
154 writeLock.lock();
155 }
156
157 protected void releaseWriteLock() {
158 writeLock.unlock();
159 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800160}