blob: 5d287a849c443361afde10ba18a9e0553ec199ee [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;
5import java.util.Collections;
6import java.util.HashMap;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08007import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -08008import java.util.LinkedList;
Jonathan Hart369875b2014-02-13 10:00:31 -08009import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -080010import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080011import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080012
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080013/**
14 * Switch Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070015 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016 * TODO REMOVE following design memo: This object itself may hold the DBObject,
17 * but this Object itself will not issue any read/write to the DataStore.
18 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070019public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 private Long dpid;
Jonathan Harte37e4e22014-05-13 19:12:02 -070022 // These needs to be ConcurrentCollecton if allowing the topology to be
23 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070024 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080025
Jonathan Harte37e4e22014-05-13 19:12:02 -070026 public SwitchImpl(Topology topology, Long dpid) {
27 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070028 this.dpid = dpid;
29 ports = new HashMap<Long, Port>();
30 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080031
Ray Milkey269ffb92014-04-03 14:43:30 -070032 @Override
33 public Long getDpid() {
34 return dpid;
35 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 @Override
38 public Collection<Port> getPorts() {
39 return Collections.unmodifiableCollection(ports.values());
40 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
43 public Port getPort(Long number) {
44 return ports.get(number);
45 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 @Override
48 public Iterable<Switch> getNeighbors() {
49 Set<Switch> neighbors = new HashSet<>();
50 for (Link link : getOutgoingLinks()) {
51 neighbors.add(link.getDstSwitch());
52 }
53 // XXX should incoming considered neighbor?
54 for (Link link : getIncomingLinks()) {
55 neighbors.add(link.getSrcSwitch());
56 }
57 return neighbors;
58 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 @Override
61 public Link getLinkToNeighbor(Long neighborDpid) {
62 for (Link link : getOutgoingLinks()) {
63 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
64 return link;
65 }
66 }
67 return null;
68 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 @Override
71 public Collection<Device> getDevices() {
72 // TODO Should switch also store a list of attached devices to avoid
73 // calculating this every time?
74 List<Device> devices = new ArrayList<Device>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 for (Port port : ports.values()) {
77 for (Device device : port.getDevices()) {
78 devices.add(device);
79 }
80 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 return devices;
83 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 public void addPort(Port port) {
86 this.ports.put(port.getNumber(), port);
87 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 public Port removePort(Port port) {
90 Port p = this.ports.remove(port.getNumber());
91 return p;
92 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 public Port addPort(Long portNumber) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070095 PortImpl port = new PortImpl(topology, this, portNumber);
Ray Milkey269ffb92014-04-03 14:43:30 -070096 ports.put(port.getNumber(), port);
97 return port;
98 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 @Override
101 public Iterable<Link> getOutgoingLinks() {
102 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700103 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 Link link = port.getOutgoingLink();
105 if (link != null) {
106 links.add(link);
107 }
108 }
109 return links;
110 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800111
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 @Override
113 public Iterable<Link> getIncomingLinks() {
114 LinkedList<Link> links = new LinkedList<Link>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700115 for (Port port : ports.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 Link link = port.getIncomingLink();
117 if (link != null) {
118 links.add(link);
119 }
120 }
121 return links;
122 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800123}