blob: a8c824d234f323f74ee61ccf0b20e91182a26d49 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08006import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -08007import java.util.LinkedList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08008import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08009import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080010
Jonathan Hart062a2e82014-02-03 09:41:57 -080011import net.onrc.onos.datastore.topology.RCPort;
12import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI1929b5d2014-02-05 20:14:51 -080013import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
14import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
Jonathan Hart062a2e82014-02-03 09:41:57 -080015
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016/**
17 * Switch Object stored in In-memory Topology.
18 *
19 * TODO REMOVE following design memo: This object itself may hold the DBObject,
20 * but this Object itself will not issue any read/write to the DataStore.
21 */
Jonathan Hart062a2e82014-02-03 09:41:57 -080022public class SwitchImpl extends NetworkGraphObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080023
Toshio Koide2f570c12014-02-06 16:55:32 -080024 private Long dpid;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080025 // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
Toshio Koide2f570c12014-02-06 16:55:32 -080026 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080027
Toshio Koide2f570c12014-02-06 16:55:32 -080028 public SwitchImpl(NetworkGraph graph, Long dpid) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080029 super(graph);
Toshio Koide2f570c12014-02-06 16:55:32 -080030 this.dpid = dpid;
31 ports = new HashMap<Long, Port>();
Jonathan Hart062a2e82014-02-03 09:41:57 -080032 }
33
34 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080035 public Long getDpid() {
Jonathan Hart062a2e82014-02-03 09:41:57 -080036 return dpid;
37 }
38
39 @Override
40 public Collection<Port> getPorts() {
41 return Collections.unmodifiableCollection(ports.values());
42 }
43
44 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080045 public Port getPort(Long number) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080046 return ports.get(number);
47 }
48
49 @Override
Jonathan Hart062a2e82014-02-03 09:41:57 -080050 public Iterable<Switch> getNeighbors() {
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080051 Set<Switch> neighbors = new HashSet<>();
52 for (Link link : getOutgoingLinks()) {
53 neighbors.add(link.getDestinationSwitch());
54 }
55 // XXX should incoming considered neighbor?
56 for (Link link : getIncomingLinks()) {
57 neighbors.add(link.getSourceSwitch());
58 }
59 return neighbors;
Jonathan Hart062a2e82014-02-03 09:41:57 -080060 }
61
62 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080063 public Link getLinkToNeighbor(Long neighborDpid) {
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -080064 for (Link link : getOutgoingLinks()) {
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080065 if (link.getDestinationSwitch().getDpid().equals(neighborDpid) ) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080066 return link;
67 }
68 }
69 return null;
70 }
71
72 @Override
73 public Collection<Device> getDevices() {
74 // TODO Auto-generated method stub
75 return null;
76 }
77
Jonathan Hart062a2e82014-02-03 09:41:57 -080078 public void addPort(Port port) {
79 this.ports.put(port.getNumber(), port);
80 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080081
82 public Port removePort(Port port) {
83 Port p = this.ports.remove(port.getNumber());
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080084 return p;
85 }
86
Toshio Koide2f570c12014-02-06 16:55:32 -080087 public Port addPort(Long portNumber) {
88 PortImpl port = new PortImpl(graph, this, portNumber);
89 ports.put(port.getNumber(), port);
90 return port;
Jonathan Hart062a2e82014-02-03 09:41:57 -080091 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080092
Jonathan Hart062a2e82014-02-03 09:41:57 -080093 public void store() {
94 RCSwitch rcSwitch = new RCSwitch(dpid);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080095
Jonathan Hart062a2e82014-02-03 09:41:57 -080096 for (Port port : ports.values()) {
97 RCPort rcPort = new RCPort(dpid, (long)port.getNumber());
98 rcSwitch.addPortId(rcPort.getId());
99 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800100
101
Jonathan Hart062a2e82014-02-03 09:41:57 -0800102 try {
103 rcSwitch.update();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800104
Jonathan Hart062a2e82014-02-03 09:41:57 -0800105 } catch (ObjectDoesntExistException | WrongVersionException e) {
106 // TODO Auto-generated catch block
107 e.printStackTrace();
108 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800109
Jonathan Hart062a2e82014-02-03 09:41:57 -0800110 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800111
112 @Override
113 public Iterable<Link> getOutgoingLinks() {
114 LinkedList<Link> links = new LinkedList<Link>();
115 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800116 Link link = port.getOutgoingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800117 if (link != null) {
118 links.add(link);
119 }
120 }
121 return links;
122 }
123
124 @Override
125 public Iterable<Link> getIncomingLinks() {
126 LinkedList<Link> links = new LinkedList<Link>();
127 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800128 Link link = port.getIncomingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800129 if (link != null) {
130 links.add(link);
131 }
132 }
133 return links;
134 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800135}