blob: d28e8bbe6b91538c412b3b4f499b11b45c7a886c [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 {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 @SuppressWarnings("unused")
22 private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080023
Ray Milkey269ffb92014-04-03 14:43:30 -070024 // DPID -> Switch
25 private ConcurrentMap<Long, Switch> switches;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 private ConcurrentMap<InetAddress, Set<Device>> addr2Device;
28 private ConcurrentMap<MACAddress, Device> mac2Device;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 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
Ray Milkey269ffb92014-04-03 14:43:30 -070035 public NetworkGraphImpl() {
36 // TODO: Does these object need to be stored in Concurrent Collection?
37 switches = new ConcurrentHashMap<>();
38 addr2Device = new ConcurrentHashMap<>();
39 mac2Device = new ConcurrentHashMap<>();
40 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
43 public Switch getSwitch(Long dpid) {
44 // TODO Check if it is safe to directly return this Object.
45 return switches.get(dpid);
46 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 protected void putSwitch(Switch sw) {
49 switches.put(sw.getDpid(), sw);
50 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 protected void removeSwitch(Long dpid) {
53 switches.remove(dpid);
54 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @Override
57 public Iterable<Switch> getSwitches() {
58 // TODO Check if it is safe to directly return this Object.
59 return Collections.unmodifiableCollection(switches.values());
60 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 @Override
63 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 }
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 @Override
72 public Link getLink(Long dpid, Long number) {
73 Port srcPort = getPort(dpid, number);
Ray Milkeyb29e6262014-04-09 16:02:14 -070074 if (srcPort == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070076 }
Ray Milkey269ffb92014-04-03 14:43:30 -070077 return srcPort.getOutgoingLink();
78 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 @Override
81 public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid,
82 Long dstNumber) {
83 Link link = getLink(srcDpid, srcNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -070084 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 }
87 if (!link.getDstSwitch().getDpid().equals(dstDpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070089 }
90 if (!link.getDstPort().getNumber().equals(dstNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 }
Ray Milkey269ffb92014-04-03 14:43:30 -070093 return link;
94 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @Override
97 public Iterable<Link> getLinks() {
98 List<Link> linklist = new LinkedList<>();
Toshio Koide2f570c12014-02-06 16:55:32 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 for (Switch sw : switches.values()) {
101 Iterable<Link> links = sw.getOutgoingLinks();
102 for (Link l : links) {
103 linklist.add(l);
104 }
105 }
106 return linklist;
107 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 @Override
110 public Iterable<Device> getDevicesByIp(InetAddress ipAddress) {
111 Set<Device> devices = addr2Device.get(ipAddress);
112 if (devices == null) {
113 return Collections.emptySet();
114 }
115 return Collections.unmodifiableCollection(devices);
116 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 @Override
119 public Device getDeviceByMac(MACAddress address) {
120 return mac2Device.get(address);
121 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800122
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 protected void putDevice(Device device) {
124 mac2Device.put(device.getMacAddress(), device);
125 for (InetAddress ipAddr : device.getIpAddress()) {
126 Set<Device> devices = addr2Device.get(ipAddr);
127 if (devices == null) {
128 devices = new HashSet<>();
129 addr2Device.put(ipAddr, devices);
130 }
131 devices.add(device);
132 }
133 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800134
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 protected void removeDevice(Device device) {
136 mac2Device.remove(device.getMacAddress());
137 for (InetAddress ipAddr : device.getIpAddress()) {
138 Set<Device> devices = addr2Device.get(ipAddr);
139 if (devices != null) {
140 devices.remove(device);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700141 if (devices.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 addr2Device.remove(ipAddr);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700143 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 }
145 }
146 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 @Override
149 public void acquireReadLock() {
150 readLock.lock();
151 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800152
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 @Override
154 public void releaseReadLock() {
155 readLock.unlock();
156 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800157
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 protected void acquireWriteLock() {
159 writeLock.lock();
160 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800161
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 protected void releaseWriteLock() {
163 writeLock.unlock();
164 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800165}