Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 2 | |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 4 | import net.onrc.onos.core.util.PortNumber; |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 5 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 7 | import java.util.Collection; |
| 8 | import java.util.Collections; |
| 9 | import java.util.HashMap; |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 10 | import java.util.HashSet; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 11 | import java.util.LinkedList; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 12 | import java.util.List; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 13 | import java.util.Map; |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 14 | import java.util.Set; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 15 | |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 16 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 17 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 18 | /** |
| 19 | * Switch Object stored in In-memory Topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 20 | * <p/> |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 21 | * TODO REMOVE following design memo: This object itself may hold the DBObject, |
| 22 | * but this Object itself will not issue any read/write to the DataStore. |
| 23 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 24 | public class SwitchImpl extends TopologyObject implements Switch { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 25 | |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 26 | private Dpid dpid; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 27 | // These needs to be ConcurrentCollecton if allowing the topology to be |
| 28 | // accessed concurrently |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 29 | private final Map<PortNumber, Port> ports; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 30 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 31 | public SwitchImpl(Topology topology, Long dpid) { |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 32 | this(topology, new Dpid(dpid)); |
| 33 | } |
| 34 | |
| 35 | public SwitchImpl(Topology topology, Dpid dpid) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 36 | super(topology); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | this.dpid = dpid; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 38 | ports = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 40 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 42 | public Dpid getDpid() { |
| 43 | return dpid; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 45 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | @Override |
| 47 | public Collection<Port> getPorts() { |
| 48 | return Collections.unmodifiableCollection(ports.values()); |
| 49 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 50 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 52 | public Port getPort(PortNumber number) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | return ports.get(number); |
| 54 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | @Override |
| 57 | public Iterable<Switch> getNeighbors() { |
| 58 | Set<Switch> neighbors = new HashSet<>(); |
| 59 | for (Link link : getOutgoingLinks()) { |
| 60 | neighbors.add(link.getDstSwitch()); |
| 61 | } |
| 62 | // XXX should incoming considered neighbor? |
| 63 | for (Link link : getIncomingLinks()) { |
| 64 | neighbors.add(link.getSrcSwitch()); |
| 65 | } |
| 66 | return neighbors; |
| 67 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 68 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame^] | 70 | public Link getLinkToNeighbor(Dpid neighborDpid) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | for (Link link : getOutgoingLinks()) { |
| 72 | if (link.getDstSwitch().getDpid().equals(neighborDpid)) { |
| 73 | return link; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public Collection<Device> getDevices() { |
| 81 | // TODO Should switch also store a list of attached devices to avoid |
| 82 | // calculating this every time? |
| 83 | List<Device> devices = new ArrayList<Device>(); |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | for (Port port : ports.values()) { |
| 86 | for (Device device : port.getDevices()) { |
| 87 | devices.add(device); |
| 88 | } |
| 89 | } |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 90 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | return devices; |
| 92 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | public void addPort(Port port) { |
| 95 | this.ports.put(port.getNumber(), port); |
| 96 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | public Port removePort(Port port) { |
| 99 | Port p = this.ports.remove(port.getNumber()); |
| 100 | return p; |
| 101 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | public Port addPort(Long portNumber) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 104 | PortImpl port = new PortImpl(topology, this, portNumber); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | ports.put(port.getNumber(), port); |
| 106 | return port; |
| 107 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 108 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public Iterable<Link> getOutgoingLinks() { |
| 111 | LinkedList<Link> links = new LinkedList<Link>(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 112 | for (Port port : ports.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | Link link = port.getOutgoingLink(); |
| 114 | if (link != null) { |
| 115 | links.add(link); |
| 116 | } |
| 117 | } |
| 118 | return links; |
| 119 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 120 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public Iterable<Link> getIncomingLinks() { |
| 123 | LinkedList<Link> links = new LinkedList<Link>(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 124 | for (Port port : ports.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | Link link = port.getIncomingLink(); |
| 126 | if (link != null) { |
| 127 | links.add(link); |
| 128 | } |
| 129 | } |
| 130 | return links; |
| 131 | } |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 132 | |
| 133 | @Override |
| 134 | public String getStringAttribute(String attr) { |
| 135 | throw new UnsupportedOperationException("Not implemented yet"); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", |
| 140 | justification = "getStringAttribute might return null once implemented") |
| 141 | public String getStringAttribute(String attr, String def) { |
| 142 | final String v = getStringAttribute(attr); |
| 143 | if (v == null) { |
| 144 | return def; |
| 145 | } else { |
| 146 | return v; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Map<String, String> getAllStringAttributes() { |
| 152 | throw new UnsupportedOperationException("Not implemented yet"); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
| 157 | return dpid.toString(); |
| 158 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 159 | } |