blob: d48e22205c71a15fa28310d98f67f105cb428ba1 [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;
Jonathan Hart25bd53e2014-04-30 23:44:09 -070011import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080012
13import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Jonathan Harte37e4e22014-05-13 19:12:02 -070016public class TopologyImpl implements Topology {
Ray Milkey269ffb92014-04-03 14:43:30 -070017 @SuppressWarnings("unused")
Jonathan Harte37e4e22014-05-13 19:12:02 -070018 private static final Logger log = LoggerFactory.getLogger(TopologyImpl.class);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080019
Ray Milkey269ffb92014-04-03 14:43:30 -070020 // DPID -> Switch
Jonathan Hart25bd53e2014-04-30 23:44:09 -070021 private final ConcurrentMap<Long, Switch> switches;
22 private final ConcurrentMap<MACAddress, Device> mac2Device;
23
24 private final ConcurrentMap<SwitchPort, Link> outgoingLinks;
25 private final ConcurrentMap<SwitchPort, Link> incomingLinks;
Yuta HIGUCHI5d2d8d42014-02-20 22:22:53 -080026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
28 private Lock readLock = readWriteLock.readLock();
29 // TODO use the write lock after refactor
30 private Lock writeLock = readWriteLock.writeLock();
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080031
Jonathan Harte37e4e22014-05-13 19:12:02 -070032 public TopologyImpl() {
Ray Milkey269ffb92014-04-03 14:43:30 -070033 // TODO: Does these object need to be stored in Concurrent Collection?
34 switches = new ConcurrentHashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070035 mac2Device = new ConcurrentHashMap<>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070036 outgoingLinks = new ConcurrentHashMap<>();
37 incomingLinks = new ConcurrentHashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070038 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080039
Ray Milkey269ffb92014-04-03 14:43:30 -070040 @Override
41 public Switch getSwitch(Long dpid) {
42 // TODO Check if it is safe to directly return this Object.
43 return switches.get(dpid);
44 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080045
Ray Milkey269ffb92014-04-03 14:43:30 -070046 protected void putSwitch(Switch sw) {
47 switches.put(sw.getDpid(), sw);
48 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 protected void removeSwitch(Long dpid) {
51 switches.remove(dpid);
52 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 @Override
55 public Iterable<Switch> getSwitches() {
56 // TODO Check if it is safe to directly return this Object.
57 return Collections.unmodifiableCollection(switches.values());
58 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 @Override
61 public Port getPort(Long dpid, Long number) {
62 Switch sw = getSwitch(dpid);
63 if (sw != null) {
64 return sw.getPort(number);
65 }
66 return null;
67 }
Pavlin Radoslavov06df22a2014-02-18 19:16:27 -080068
Ray Milkey269ffb92014-04-03 14:43:30 -070069 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -070070 public Link getOutgoingLink(Long dpid, Long number) {
71 return outgoingLinks.get(new SwitchPort(dpid, number.shortValue()));
72 }
73
74 @Override
75 public Link getIncomingLink(Long dpid, Long number) {
76 return incomingLinks.get(new SwitchPort(dpid, number.shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -070077 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 @Override
80 public Link getLink(Long srcDpid, Long srcNumber, Long dstDpid,
81 Long dstNumber) {
Jonathan Hart25bd53e2014-04-30 23:44:09 -070082 Link link = getOutgoingLink(srcDpid, srcNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -070083 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070084 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070085 }
86 if (!link.getDstSwitch().getDpid().equals(dstDpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070087 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070088 }
89 if (!link.getDstPort().getNumber().equals(dstNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070090 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -070091 }
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return link;
93 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 @Override
96 public Iterable<Link> getLinks() {
Jonathan Hart25bd53e2014-04-30 23:44:09 -070097 return Collections.unmodifiableCollection(outgoingLinks.values());
98 }
Toshio Koide2f570c12014-02-06 16:55:32 -080099
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700100 protected void putLink(Link link) {
101 outgoingLinks.put(link.getSrcPort().asSwitchPort(), link);
102 incomingLinks.put(link.getDstPort().asSwitchPort(), link);
103 }
104
105 protected void removeLink(Link link) {
106 outgoingLinks.remove(link.getSrcPort().asSwitchPort(), link);
107 incomingLinks.remove(link.getDstPort().asSwitchPort(), link);
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800109
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 public Device getDeviceByMac(MACAddress address) {
112 return mac2Device.get(address);
113 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800114
TeruU65bc5ff2014-04-23 22:22:32 -0700115 @Override
116 public Iterable<Device> getDevices() {
117 return Collections.unmodifiableCollection(mac2Device.values());
118 }
119
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 protected void putDevice(Device device) {
121 mac2Device.put(device.getMacAddress(), device);
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 }
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800123
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 protected void removeDevice(Device device) {
125 mac2Device.remove(device.getMacAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800127
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 @Override
129 public void acquireReadLock() {
130 readLock.lock();
131 }
Jonathan Hart26f291b2014-02-18 16:57:24 -0800132
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 @Override
134 public void releaseReadLock() {
135 readLock.unlock();
136 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800137
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 protected void acquireWriteLock() {
139 writeLock.lock();
140 }
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800141
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 protected void releaseWriteLock() {
143 writeLock.unlock();
144 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800145}