blob: 3fe053c789fa2784c0f2c59581e719184445ca55 [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 Hart062a2e82014-02-03 09:41:57 -080019public class SwitchImpl extends NetworkGraphObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 private Long dpid;
22 // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
23 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080024
Ray Milkey269ffb92014-04-03 14:43:30 -070025 public SwitchImpl(NetworkGraph graph, Long dpid) {
26 super(graph);
27 this.dpid = dpid;
28 ports = new HashMap<Long, Port>();
29 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080030
Ray Milkey269ffb92014-04-03 14:43:30 -070031 @Override
32 public Long getDpid() {
33 return dpid;
34 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080035
Ray Milkey269ffb92014-04-03 14:43:30 -070036 @Override
37 public Collection<Port> getPorts() {
38 return Collections.unmodifiableCollection(ports.values());
39 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080040
Ray Milkey269ffb92014-04-03 14:43:30 -070041 @Override
42 public Port getPort(Long number) {
43 return ports.get(number);
44 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080045
Ray Milkey269ffb92014-04-03 14:43:30 -070046 @Override
47 public Iterable<Switch> getNeighbors() {
48 Set<Switch> neighbors = new HashSet<>();
49 for (Link link : getOutgoingLinks()) {
50 neighbors.add(link.getDstSwitch());
51 }
52 // XXX should incoming considered neighbor?
53 for (Link link : getIncomingLinks()) {
54 neighbors.add(link.getSrcSwitch());
55 }
56 return neighbors;
57 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 @Override
60 public Link getLinkToNeighbor(Long neighborDpid) {
61 for (Link link : getOutgoingLinks()) {
62 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
63 return link;
64 }
65 }
66 return null;
67 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080068
Ray Milkey269ffb92014-04-03 14:43:30 -070069 @Override
70 public Collection<Device> getDevices() {
71 // TODO Should switch also store a list of attached devices to avoid
72 // calculating this every time?
73 List<Device> devices = new ArrayList<Device>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 for (Port port : ports.values()) {
76 for (Device device : port.getDevices()) {
77 devices.add(device);
78 }
79 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 return devices;
82 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 public void addPort(Port port) {
85 this.ports.put(port.getNumber(), port);
86 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080087
Ray Milkey269ffb92014-04-03 14:43:30 -070088 public Port removePort(Port port) {
89 Port p = this.ports.remove(port.getNumber());
90 return p;
91 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 public Port addPort(Long portNumber) {
94 PortImpl port = new PortImpl(graph, this, portNumber);
95 ports.put(port.getNumber(), port);
96 return port;
97 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 @Override
100 public Iterable<Link> getOutgoingLinks() {
101 LinkedList<Link> links = new LinkedList<Link>();
102 for (Port port : getPorts()) {
103 Link link = port.getOutgoingLink();
104 if (link != null) {
105 links.add(link);
106 }
107 }
108 return links;
109 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 @Override
112 public Iterable<Link> getIncomingLinks() {
113 LinkedList<Link> links = new LinkedList<Link>();
114 for (Port port : getPorts()) {
115 Link link = port.getIncomingLink();
116 if (link != null) {
117 links.add(link);
118 }
119 }
120 return links;
121 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800122}