blob: 2bba015140e0bc7e02b626324f61b2792eb41d90 [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;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08004import java.util.concurrent.ConcurrentHashMap;
5import java.util.concurrent.ConcurrentMap;
Jonathan Hart26f291b2014-02-18 16:57:24 -08006import java.util.concurrent.locks.Lock;
7import java.util.concurrent.locks.ReadWriteLock;
8import java.util.concurrent.locks.ReentrantReadWriteLock;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -08009
10import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070011import net.onrc.onos.core.util.Dpid;
12import net.onrc.onos.core.util.PortNumber;
Jonathan Hart25bd53e2014-04-30 23:44:09 -070013import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080014
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Jonathan Harte37e4e22014-05-13 19:12:02 -070018public class TopologyImpl implements Topology {
Ray Milkey269ffb92014-04-03 14:43:30 -070019 @SuppressWarnings("unused")
Jonathan Harte37e4e22014-05-13 19:12:02 -070020 private static final Logger log = LoggerFactory.getLogger(TopologyImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080021
Ray Milkey269ffb92014-04-03 14:43:30 -070022 // DPID -> Switch
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070023 private final ConcurrentMap<Dpid, Switch> switches;
Jonathan Hart25bd53e2014-04-30 23:44:09 -070024 private final ConcurrentMap<MACAddress, Device> mac2Device;
25
26 private final ConcurrentMap<SwitchPort, Link> outgoingLinks;
27 private final ConcurrentMap<SwitchPort, Link> incomingLinks;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080028
Ray Milkey269ffb92014-04-03 14:43:30 -070029 private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
30 private Lock readLock = readWriteLock.readLock();
31 // TODO use the write lock after refactor
32 private Lock writeLock = readWriteLock.writeLock();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080033
Jonathan Harte37e4e22014-05-13 19:12:02 -070034 public TopologyImpl() {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 // TODO: Does these object need to be stored in Concurrent Collection?
36 switches = new ConcurrentHashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070037 mac2Device = new ConcurrentHashMap<>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070038 outgoingLinks = new ConcurrentHashMap<>();
39 incomingLinks = new ConcurrentHashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070040 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070043 public Switch getSwitch(Dpid dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070044 // 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) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070053 switches.remove(new Dpid(dpid));
54 }
55
56 protected void removeSwitch(Dpid dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070057 switches.remove(dpid);
58 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 @Override
61 public Iterable<Switch> getSwitches() {
62 // TODO Check if it is safe to directly return this Object.
63 return Collections.unmodifiableCollection(switches.values());
64 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070067 public Port getPort(Dpid dpid, PortNumber number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070068 Switch sw = getSwitch(dpid);
69 if (sw != null) {
70 return sw.getPort(number);
71 }
72 return null;
73 }
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070076 public Port getPort(SwitchPort port) {
77 return getPort(port.dpid(), port.port());
Jonathan Hart25bd53e2014-04-30 23:44:09 -070078 }
79
80 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070081 public Link getOutgoingLink(Dpid dpid, PortNumber number) {
82 return outgoingLinks.get(new SwitchPort(dpid, number));
Ray Milkey269ffb92014-04-03 14:43:30 -070083 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070086 public Link getOutgoingLink(SwitchPort port) {
87 return outgoingLinks.get(port);
88 }
89
90 @Override
91 public Link getIncomingLink(Dpid dpid, PortNumber number) {
92 return incomingLinks.get(new SwitchPort(dpid, number));
93 }
94
95 @Override
96 public Link getIncomingLink(SwitchPort port) {
97 return incomingLinks.get(port);
98 }
99
100 @Override
101 public Link getLink(Dpid srcDpid, PortNumber srcNumber,
102 Dpid dstDpid, PortNumber dstNumber) {
103
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700104 Link link = getOutgoingLink(srcDpid, srcNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700105 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700107 }
108 if (!link.getDstSwitch().getDpid().equals(dstDpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700110 }
111 if (!link.getDstPort().getNumber().equals(dstNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700113 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 return link;
115 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 @Override
118 public Iterable<Link> getLinks() {
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700119 return Collections.unmodifiableCollection(outgoingLinks.values());
120 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800121
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700122 protected void putLink(Link link) {
123 outgoingLinks.put(link.getSrcPort().asSwitchPort(), link);
124 incomingLinks.put(link.getDstPort().asSwitchPort(), link);
125 }
126
127 protected void removeLink(Link link) {
128 outgoingLinks.remove(link.getSrcPort().asSwitchPort(), link);
129 incomingLinks.remove(link.getDstPort().asSwitchPort(), link);
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800131
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 public Device getDeviceByMac(MACAddress address) {
134 return mac2Device.get(address);
135 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800136
TeruU65bc5ff2014-04-23 22:22:32 -0700137 @Override
138 public Iterable<Device> getDevices() {
139 return Collections.unmodifiableCollection(mac2Device.values());
140 }
141
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 protected void putDevice(Device device) {
143 mac2Device.put(device.getMacAddress(), device);
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800145
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 protected void removeDevice(Device device) {
147 mac2Device.remove(device.getMacAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800149
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 @Override
151 public void acquireReadLock() {
152 readLock.lock();
153 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800154
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 @Override
156 public void releaseReadLock() {
157 readLock.unlock();
158 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800159
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 protected void acquireWriteLock() {
161 writeLock.lock();
162 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800163
Ray Milkey269ffb92014-04-03 14:43:30 -0700164 protected void releaseWriteLock() {
165 writeLock.unlock();
166 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800167}