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