blob: ee0eebd1b9312283f9d23b15e4a650dbf53fa894 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -07003import net.onrc.onos.core.util.Dpid;
4
Jonathan Hart369875b2014-02-13 10:00:31 -08005import java.util.ArrayList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08006import java.util.Collection;
7import java.util.Collections;
8import java.util.HashMap;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08009import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -080010import java.util.LinkedList;
Jonathan Hart369875b2014-02-13 10:00:31 -080011import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -080012import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080013import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080014
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080015/**
16 * Switch Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070017 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018 * 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 Harte37e4e22014-05-13 19:12:02 -070021public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080022
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070023 private Dpid dpid;
Jonathan Harte37e4e22014-05-13 19:12:02 -070024 // These needs to be ConcurrentCollecton if allowing the topology to be
25 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070026 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080027
Jonathan Harte37e4e22014-05-13 19:12:02 -070028 public SwitchImpl(Topology topology, Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070029 this(topology, new Dpid(dpid));
30 }
31
32 public SwitchImpl(Topology topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070033 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070034 this.dpid = dpid;
35 ports = new HashMap<Long, Port>();
36 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 @Override
39 public Long getDpid() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070040 return dpid.value();
Ray Milkey269ffb92014-04-03 14:43:30 -070041 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 @Override
44 public Collection<Port> getPorts() {
45 return Collections.unmodifiableCollection(ports.values());
46 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 @Override
49 public Port getPort(Long number) {
50 return ports.get(number);
51 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080052
Ray Milkey269ffb92014-04-03 14:43:30 -070053 @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 Hart062a2e82014-02-03 09:41:57 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @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 Hart062a2e82014-02-03 09:41:57 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 @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 HIGUCHI25719052014-02-13 14:42:06 -080081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 for (Port port : ports.values()) {
83 for (Device device : port.getDevices()) {
84 devices.add(device);
85 }
86 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080087
Ray Milkey269ffb92014-04-03 14:43:30 -070088 return devices;
89 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 public void addPort(Port port) {
92 this.ports.put(port.getNumber(), port);
93 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 public Port removePort(Port port) {
96 Port p = this.ports.remove(port.getNumber());
97 return p;
98 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 public Port addPort(Long portNumber) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700101 PortImpl port = new PortImpl(topology, this, portNumber);
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 ports.put(port.getNumber(), port);
103 return port;
104 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 @Override
107 public Iterable<Link> getOutgoingLinks() {
108 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700109 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 Link link = port.getOutgoingLink();
111 if (link != null) {
112 links.add(link);
113 }
114 }
115 return links;
116 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 @Override
119 public Iterable<Link> getIncomingLinks() {
120 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700121 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 Link link = port.getIncomingLink();
123 if (link != null) {
124 links.add(link);
125 }
126 }
127 return links;
128 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800129}