blob: 874bf37c66ac838169574af4a47701b3ac1f41f2 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
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
Jonathan Hart062a2e82014-02-03 09:41:57 -080013import net.onrc.onos.datastore.topology.RCPort;
14import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI1929b5d2014-02-05 20:14:51 -080015import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
16import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
Jonathan Hart062a2e82014-02-03 09:41:57 -080017
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018/**
19 * Switch Object stored in In-memory Topology.
20 *
21 * 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 Hart062a2e82014-02-03 09:41:57 -080024public class SwitchImpl extends NetworkGraphObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080025
Toshio Koide2f570c12014-02-06 16:55:32 -080026 private Long dpid;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080027 // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
Toshio Koide2f570c12014-02-06 16:55:32 -080028 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080029
Toshio Koide2f570c12014-02-06 16:55:32 -080030 public SwitchImpl(NetworkGraph graph, Long dpid) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080031 super(graph);
Toshio Koide2f570c12014-02-06 16:55:32 -080032 this.dpid = dpid;
33 ports = new HashMap<Long, Port>();
Jonathan Hart062a2e82014-02-03 09:41:57 -080034 }
35
36 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080037 public Long getDpid() {
Jonathan Hart062a2e82014-02-03 09:41:57 -080038 return dpid;
39 }
40
41 @Override
42 public Collection<Port> getPorts() {
43 return Collections.unmodifiableCollection(ports.values());
44 }
45
46 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080047 public Port getPort(Long number) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080048 return ports.get(number);
49 }
50
51 @Override
Jonathan Hart062a2e82014-02-03 09:41:57 -080052 public Iterable<Switch> getNeighbors() {
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080053 Set<Switch> neighbors = new HashSet<>();
54 for (Link link : getOutgoingLinks()) {
55 neighbors.add(link.getDestinationSwitch());
56 }
57 // XXX should incoming considered neighbor?
58 for (Link link : getIncomingLinks()) {
59 neighbors.add(link.getSourceSwitch());
60 }
61 return neighbors;
Jonathan Hart062a2e82014-02-03 09:41:57 -080062 }
63
64 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080065 public Link getLinkToNeighbor(Long neighborDpid) {
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080066 for (Link link : getOutgoingLinks()) {
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080067 if (link.getDestinationSwitch().getDpid().equals(neighborDpid) ) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080068 return link;
69 }
70 }
71 return null;
72 }
73
74 @Override
75 public Collection<Device> getDevices() {
Jonathan Hart369875b2014-02-13 10:00:31 -080076 // TODO Should switch also store a list of attached devices to avoid
77 // calculating this every time?
78 List<Device> devices = new ArrayList<Device>();
79
80 for (Port port : ports.values()) {
81 for (Device device : port.getDevices()) {
82 devices.add(device);
83 }
84 }
85
86 return devices;
Jonathan Hart062a2e82014-02-03 09:41:57 -080087 }
88
Jonathan Hart062a2e82014-02-03 09:41:57 -080089 public void addPort(Port port) {
90 this.ports.put(port.getNumber(), port);
91 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080092
93 public Port removePort(Port port) {
94 Port p = this.ports.remove(port.getNumber());
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080095 return p;
96 }
97
Toshio Koide2f570c12014-02-06 16:55:32 -080098 public Port addPort(Long portNumber) {
99 PortImpl port = new PortImpl(graph, this, portNumber);
100 ports.put(port.getNumber(), port);
101 return port;
Jonathan Hart062a2e82014-02-03 09:41:57 -0800102 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800103
Jonathan Hart062a2e82014-02-03 09:41:57 -0800104 public void store() {
105 RCSwitch rcSwitch = new RCSwitch(dpid);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800106
Jonathan Hart062a2e82014-02-03 09:41:57 -0800107 for (Port port : ports.values()) {
108 RCPort rcPort = new RCPort(dpid, (long)port.getNumber());
109 rcSwitch.addPortId(rcPort.getId());
110 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800111
112
Jonathan Hart062a2e82014-02-03 09:41:57 -0800113 try {
114 rcSwitch.update();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800115
Jonathan Hart062a2e82014-02-03 09:41:57 -0800116 } catch (ObjectDoesntExistException | WrongVersionException e) {
117 // TODO Auto-generated catch block
118 e.printStackTrace();
119 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800120
Jonathan Hart062a2e82014-02-03 09:41:57 -0800121 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800122
123 @Override
124 public Iterable<Link> getOutgoingLinks() {
125 LinkedList<Link> links = new LinkedList<Link>();
126 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800127 Link link = port.getOutgoingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800128 if (link != null) {
129 links.add(link);
130 }
131 }
132 return links;
133 }
134
135 @Override
136 public Iterable<Link> getIncomingLinks() {
137 LinkedList<Link> links = new LinkedList<Link>();
138 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800139 Link link = port.getIncomingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800140 if (link != null) {
141 links.add(link);
142 }
143 }
144 return links;
145 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800146}