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 | 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 | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 24 | // These needs to be ConcurrentCollecton if allowing the topology to be |
| 25 | // accessed concurrently |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | private final Map<Long, Port> ports; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 27 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 28 | public SwitchImpl(Topology topology, Long dpid) { |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 29 | this(topology, new Dpid(dpid)); |
| 30 | } |
| 31 | |
| 32 | public SwitchImpl(Topology topology, Dpid dpid) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 33 | super(topology); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | this.dpid = dpid; |
| 35 | ports = new HashMap<Long, Port>(); |
| 36 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 37 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 38 | @Override |
| 39 | public Long getDpid() { |
Sho SHIMIZU | 83e3c1f | 2014-06-13 15:57:26 -0700 | [diff] [blame] | 40 | return dpid.value(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 42 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public Collection<Port> getPorts() { |
| 45 | return Collections.unmodifiableCollection(ports.values()); |
| 46 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public Port getPort(Long number) { |
| 50 | return ports.get(number); |
| 51 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | @Override |
| 54 | public Iterable<Switch> getNeighbors() { |
| 55 | Set<Switch> neighbors = new HashSet<>(); |
| 56 | for (Link link : getOutgoingLinks()) { |
| 57 | neighbors.add(link.getDstSwitch()); |
| 58 | } |
| 59 | // XXX should incoming considered neighbor? |
| 60 | for (Link link : getIncomingLinks()) { |
| 61 | neighbors.add(link.getSrcSwitch()); |
| 62 | } |
| 63 | return neighbors; |
| 64 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 65 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public Link getLinkToNeighbor(Long neighborDpid) { |
| 68 | for (Link link : getOutgoingLinks()) { |
| 69 | if (link.getDstSwitch().getDpid().equals(neighborDpid)) { |
| 70 | return link; |
| 71 | } |
| 72 | } |
| 73 | return null; |
| 74 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 75 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public Collection<Device> getDevices() { |
| 78 | // TODO Should switch also store a list of attached devices to avoid |
| 79 | // calculating this every time? |
| 80 | List<Device> devices = new ArrayList<Device>(); |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 81 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | for (Port port : ports.values()) { |
| 83 | for (Device device : port.getDevices()) { |
| 84 | devices.add(device); |
| 85 | } |
| 86 | } |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | return devices; |
| 89 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 90 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | public void addPort(Port port) { |
| 92 | this.ports.put(port.getNumber(), port); |
| 93 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 94 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | public Port removePort(Port port) { |
| 96 | Port p = this.ports.remove(port.getNumber()); |
| 97 | return p; |
| 98 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | public Port addPort(Long portNumber) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 101 | PortImpl port = new PortImpl(topology, this, portNumber); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | ports.put(port.getNumber(), port); |
| 103 | return port; |
| 104 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 105 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public Iterable<Link> getOutgoingLinks() { |
| 108 | LinkedList<Link> links = new LinkedList<Link>(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 109 | for (Port port : ports.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | Link link = port.getOutgoingLink(); |
| 111 | if (link != null) { |
| 112 | links.add(link); |
| 113 | } |
| 114 | } |
| 115 | return links; |
| 116 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 117 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public Iterable<Link> getIncomingLinks() { |
| 120 | LinkedList<Link> links = new LinkedList<Link>(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 121 | for (Port port : ports.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | Link link = port.getIncomingLink(); |
| 123 | if (link != null) { |
| 124 | links.add(link); |
| 125 | } |
| 126 | } |
| 127 | return links; |
| 128 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 129 | } |