blob: ad13a791f0a446dad518c28046bf2d2af1401a4f [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Jonathan Hart369875b2014-02-13 10:00:31 -08003import java.util.ArrayList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08004import java.util.Collection;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08005import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -08006import java.util.LinkedList;
Jonathan Hart369875b2014-02-13 10:00:31 -08007import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -08008import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08009import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080010
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070011import net.onrc.onos.core.util.Dpid;
12import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070013import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14
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 Hart062a2e82014-02-03 09:41:57 -080024
Jonathan Harte37e4e22014-05-13 19:12:02 -070025 public SwitchImpl(Topology topology, Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070026 this(topology, new Dpid(dpid));
27 }
28
29 public SwitchImpl(Topology topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070030 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070031 this.dpid = dpid;
Ray Milkey269ffb92014-04-03 14:43:30 -070032 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080033
Ray Milkey269ffb92014-04-03 14:43:30 -070034 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070035 public Dpid getDpid() {
36 return dpid;
Ray Milkey269ffb92014-04-03 14:43:30 -070037 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080038
Ray Milkey269ffb92014-04-03 14:43:30 -070039 @Override
40 public Collection<Port> getPorts() {
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070041 return topology.getPorts(getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070042 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070045 public Port getPort(PortNumber number) {
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070046 return topology.getPort(getDpid(), number);
Ray Milkey269ffb92014-04-03 14:43:30 -070047 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 @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 Hart062a2e82014-02-03 09:41:57 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070063 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070064 for (Link link : getOutgoingLinks()) {
65 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
66 return link;
67 }
68 }
69 return null;
70 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 @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 HIGUCHI25719052014-02-13 14:42:06 -080077
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070078 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070079 for (Device device : port.getDevices()) {
80 devices.add(device);
81 }
82 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 return devices;
85 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 public Port addPort(Long portNumber) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070088 PortImpl port = new PortImpl(topology, this, portNumber);
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070089 ((TopologyImpl) topology).putPort(port);
Ray Milkey269ffb92014-04-03 14:43:30 -070090 return port;
91 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 @Override
94 public Iterable<Link> getOutgoingLinks() {
95 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070096 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -070097 Link link = port.getOutgoingLink();
98 if (link != null) {
99 links.add(link);
100 }
101 }
102 return links;
103 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800104
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 @Override
106 public Iterable<Link> getIncomingLinks() {
107 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700108 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 Link link = port.getIncomingLink();
110 if (link != null) {
111 links.add(link);
112 }
113 }
114 return links;
115 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700116
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 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800143}