blob: 415b9246033ce97edd18cd7c0843c1eae415e04e [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 HIGUCHIdb1b8302014-06-26 10:50:39 -070015import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080017/**
18 * Switch Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070019 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020 * 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 Harte37e4e22014-05-13 19:12:02 -070023public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080024
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070025 private Dpid dpid;
Jonathan Harte37e4e22014-05-13 19:12:02 -070026 // These needs to be ConcurrentCollecton if allowing the topology to be
27 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070028 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080029
Jonathan Harte37e4e22014-05-13 19:12:02 -070030 public SwitchImpl(Topology topology, Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070031 this(topology, new Dpid(dpid));
32 }
33
34 public SwitchImpl(Topology topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070035 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070036 this.dpid = dpid;
37 ports = new HashMap<Long, Port>();
38 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080039
Ray Milkey269ffb92014-04-03 14:43:30 -070040 @Override
41 public Long getDpid() {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070042 return dpid.value();
Ray Milkey269ffb92014-04-03 14:43:30 -070043 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Override
46 public Collection<Port> getPorts() {
47 return Collections.unmodifiableCollection(ports.values());
48 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 @Override
51 public Port getPort(Long number) {
52 return ports.get(number);
53 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 @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 Hart062a2e82014-02-03 09:41:57 -080067
Ray Milkey269ffb92014-04-03 14:43:30 -070068 @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 Hart062a2e82014-02-03 09:41:57 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 @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 HIGUCHI25719052014-02-13 14:42:06 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 for (Port port : ports.values()) {
85 for (Device device : port.getDevices()) {
86 devices.add(device);
87 }
88 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 return devices;
91 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 public void addPort(Port port) {
94 this.ports.put(port.getNumber(), port);
95 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080096
Ray Milkey269ffb92014-04-03 14:43:30 -070097 public Port removePort(Port port) {
98 Port p = this.ports.remove(port.getNumber());
99 return p;
100 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 public Port addPort(Long portNumber) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700103 PortImpl port = new PortImpl(topology, this, portNumber);
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 ports.put(port.getNumber(), port);
105 return port;
106 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 @Override
109 public Iterable<Link> getOutgoingLinks() {
110 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700111 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 Link link = port.getOutgoingLink();
113 if (link != null) {
114 links.add(link);
115 }
116 }
117 return links;
118 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800119
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 @Override
121 public Iterable<Link> getIncomingLinks() {
122 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700123 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 Link link = port.getIncomingLink();
125 if (link != null) {
126 links.add(link);
127 }
128 }
129 return links;
130 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700131
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 Hart062a2e82014-02-03 09:41:57 -0800158}