blob: 4952c6fae957b356a348c3f8a4e8ab64e590a67c [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;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07004import net.onrc.onos.core.util.PortNumber;
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -07005
Jonathan Hart369875b2014-02-13 10:00:31 -08006import java.util.ArrayList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08007import java.util.Collection;
8import java.util.Collections;
9import java.util.HashMap;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080010import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -080011import java.util.LinkedList;
Jonathan Hart369875b2014-02-13 10:00:31 -080012import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -080013import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080014import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080015
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070016import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018/**
19 * Switch Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070020 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080021 * TODO REMOVE following design memo: This object itself may hold the DBObject,
22 * but this Object itself will not issue any read/write to the DataStore.
23 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070024public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080025
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070026 private Dpid dpid;
Jonathan Harte37e4e22014-05-13 19:12:02 -070027 // These needs to be ConcurrentCollecton if allowing the topology to be
28 // accessed concurrently
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070029 private final Map<PortNumber, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080030
Jonathan Harte37e4e22014-05-13 19:12:02 -070031 public SwitchImpl(Topology topology, Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070032 this(topology, new Dpid(dpid));
33 }
34
35 public SwitchImpl(Topology topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070036 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070037 this.dpid = dpid;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070038 ports = new HashMap<>();
Ray Milkey269ffb92014-04-03 14:43:30 -070039 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080040
Ray Milkey269ffb92014-04-03 14:43:30 -070041 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070042 public Dpid getDpid() {
43 return dpid;
Ray Milkey269ffb92014-04-03 14:43:30 -070044 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080045
Ray Milkey269ffb92014-04-03 14:43:30 -070046 @Override
47 public Collection<Port> getPorts() {
48 return Collections.unmodifiableCollection(ports.values());
49 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070052 public Port getPort(PortNumber number) {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 return ports.get(number);
54 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @Override
57 public Iterable<Switch> getNeighbors() {
58 Set<Switch> neighbors = new HashSet<>();
59 for (Link link : getOutgoingLinks()) {
60 neighbors.add(link.getDstSwitch());
61 }
62 // XXX should incoming considered neighbor?
63 for (Link link : getIncomingLinks()) {
64 neighbors.add(link.getSrcSwitch());
65 }
66 return neighbors;
67 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080068
Ray Milkey269ffb92014-04-03 14:43:30 -070069 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070070 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070071 for (Link link : getOutgoingLinks()) {
72 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
73 return link;
74 }
75 }
76 return null;
77 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 @Override
80 public Collection<Device> getDevices() {
81 // TODO Should switch also store a list of attached devices to avoid
82 // calculating this every time?
83 List<Device> devices = new ArrayList<Device>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 for (Port port : ports.values()) {
86 for (Device device : port.getDevices()) {
87 devices.add(device);
88 }
89 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 return devices;
92 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 public void addPort(Port port) {
95 this.ports.put(port.getNumber(), port);
96 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 public Port removePort(Port port) {
99 Port p = this.ports.remove(port.getNumber());
100 return p;
101 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 public Port addPort(Long portNumber) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700104 PortImpl port = new PortImpl(topology, this, portNumber);
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 ports.put(port.getNumber(), port);
106 return port;
107 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 @Override
110 public Iterable<Link> getOutgoingLinks() {
111 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700112 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 Link link = port.getOutgoingLink();
114 if (link != null) {
115 links.add(link);
116 }
117 }
118 return links;
119 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 @Override
122 public Iterable<Link> getIncomingLinks() {
123 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700124 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 Link link = port.getIncomingLink();
126 if (link != null) {
127 links.add(link);
128 }
129 }
130 return links;
131 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700132
133 @Override
134 public String getStringAttribute(String attr) {
135 throw new UnsupportedOperationException("Not implemented yet");
136 }
137
138 @Override
139 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
140 justification = "getStringAttribute might return null once implemented")
141 public String getStringAttribute(String attr, String def) {
142 final String v = getStringAttribute(attr);
143 if (v == null) {
144 return def;
145 } else {
146 return v;
147 }
148 }
149
150 @Override
151 public Map<String, String> getAllStringAttributes() {
152 throw new UnsupportedOperationException("Not implemented yet");
153 }
154
155 @Override
156 public String toString() {
157 return dpid.toString();
158 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800159}