blob: b20ab6b7d71ff697eba518657a4480cffa7d4ecc [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08002
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08003import java.util.Collections;
4import java.util.LinkedList;
5import java.util.List;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08006import java.util.concurrent.ConcurrentHashMap;
7import java.util.concurrent.ConcurrentMap;
Jonathan Hart26f291b2014-02-18 16:57:24 -08008import java.util.concurrent.locks.Lock;
9import java.util.concurrent.locks.ReadWriteLock;
10import java.util.concurrent.locks.ReentrantReadWriteLock;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080011
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 {
Ray Milkey269ffb92014-04-03 14:43:30 -070018 @SuppressWarnings("unused")
19 private static final Logger log = LoggerFactory.getLogger(NetworkGraphImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 // DPID -> Switch
22 private ConcurrentMap<Long, Switch> switches;
Ray Milkey269ffb92014-04-03 14:43:30 -070023 private ConcurrentMap<MACAddress, Device> mac2Device;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080024
Ray Milkey269ffb92014-04-03 14:43:30 -070025 private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
26 private Lock readLock = readWriteLock.readLock();
27 // TODO use the write lock after refactor
28 private Lock writeLock = readWriteLock.writeLock();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 public NetworkGraphImpl() {
31 // TODO: Does these object need to be stored in Concurrent Collection?
32 switches = new ConcurrentHashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070033 mac2Device = new ConcurrentHashMap<>();
34 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080035
Ray Milkey269ffb92014-04-03 14:43:30 -070036 @Override
37 public Switch getSwitch(Long dpid) {
38 // TODO Check if it is safe to directly return this Object.
39 return switches.get(dpid);
40 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 protected void putSwitch(Switch sw) {
43 switches.put(sw.getDpid(), sw);
44 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080045
Ray Milkey269ffb92014-04-03 14:43:30 -070046 protected void removeSwitch(Long dpid) {
47 switches.remove(dpid);
48 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 @Override
51 public Iterable<Switch> getSwitches() {
52 // TODO Check if it is safe to directly return this Object.
53 return Collections.unmodifiableCollection(switches.values());
54 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @Override
57 public Port getPort(Long dpid, Long number) {
58 Switch sw = getSwitch(dpid);
59 if (sw != null) {
60 return sw.getPort(number);
61 }
62 return null;
63 }
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 @Override
66 public Link getLink(Long dpid, Long number) {
67 Port srcPort = getPort(dpid, number);
Ray Milkeyb29e6262014-04-09 16:02:14 -070068 if (srcPort == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070069 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070070 }
Ray Milkey269ffb92014-04-03 14:43:30 -070071 return srcPort.getOutgoingLink();
72 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 @Override
75 public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid,
76 Long dstNumber) {
77 Link link = getLink(srcDpid, srcNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -070078 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070079 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070080 }
81 if (!link.getDstSwitch().getDpid().equals(dstDpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070082 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070083 }
84 if (!link.getDstPort().getNumber().equals(dstNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070085 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 }
Ray Milkey269ffb92014-04-03 14:43:30 -070087 return link;
88 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 @Override
91 public Iterable<Link> getLinks() {
92 List<Link> linklist = new LinkedList<>();
Toshio Koide2f570c12014-02-06 16:55:32 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 for (Switch sw : switches.values()) {
95 Iterable<Link> links = sw.getOutgoingLinks();
96 for (Link l : links) {
97 linklist.add(l);
98 }
99 }
100 return linklist;
101 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 public Device getDeviceByMac(MACAddress address) {
105 return mac2Device.get(address);
106 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800107
TeruU65bc5ff2014-04-23 22:22:32 -0700108 @Override
109 public Iterable<Device> getDevices() {
110 return Collections.unmodifiableCollection(mac2Device.values());
111 }
112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 protected void putDevice(Device device) {
114 mac2Device.put(device.getMacAddress(), device);
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 protected void removeDevice(Device device) {
118 mac2Device.remove(device.getMacAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 @Override
122 public void acquireReadLock() {
123 readLock.lock();
124 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800125
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 @Override
127 public void releaseReadLock() {
128 readLock.unlock();
129 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 protected void acquireWriteLock() {
132 writeLock.lock();
133 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800134
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 protected void releaseWriteLock() {
136 writeLock.unlock();
137 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800138}