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