blob: 1f871d0a6e0ae87803f5bf374dd9cf0117400097 [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;
Toshio Koide2f570c12014-02-06 16:55:32 -08006import java.util.LinkedList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08007import java.util.Map;
8
Jonathan Hart062a2e82014-02-03 09:41:57 -08009import net.onrc.onos.datastore.topology.RCPort;
10import net.onrc.onos.datastore.topology.RCSwitch;
11import net.onrc.onos.ofcontroller.util.FlowEntry;
Yuta HIGUCHI1929b5d2014-02-05 20:14:51 -080012import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
13import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
Jonathan Hart062a2e82014-02-03 09:41:57 -080014
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080015/**
16 * Switch Object stored in In-memory Topology.
17 *
18 * TODO REMOVE following design memo: This object itself may hold the DBObject,
19 * but this Object itself will not issue any read/write to the DataStore.
20 */
Jonathan Hart062a2e82014-02-03 09:41:57 -080021public class SwitchImpl extends NetworkGraphObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080022
Toshio Koide2f570c12014-02-06 16:55:32 -080023 private Long dpid;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080024 // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
Toshio Koide2f570c12014-02-06 16:55:32 -080025 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080026
Toshio Koide2f570c12014-02-06 16:55:32 -080027 public SwitchImpl(NetworkGraph graph, Long dpid) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080028 super(graph);
Toshio Koide2f570c12014-02-06 16:55:32 -080029 this.dpid = dpid;
30 ports = new HashMap<Long, Port>();
Jonathan Hart062a2e82014-02-03 09:41:57 -080031 }
32
33 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080034 public Long getDpid() {
Jonathan Hart062a2e82014-02-03 09:41:57 -080035 return dpid;
36 }
37
38 @Override
39 public Collection<Port> getPorts() {
40 return Collections.unmodifiableCollection(ports.values());
41 }
42
43 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080044 public Port getPort(Long number) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080045 return ports.get(number);
46 }
47
48 @Override
49 public Collection<FlowEntry> getFlowEntries() {
50 // TODO Auto-generated method stub
51 return null;
52 }
53
54 @Override
55 public Iterable<Switch> getNeighbors() {
56 // TODO Auto-generated method stub
57 return null;
58 }
59
60 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080061 public Link getLinkToNeighbor(Long neighborDpid) {
62 for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080063 if (link.getDestinationSwitch().getDpid() == neighborDpid) {
64 return link;
65 }
66 }
67 return null;
68 }
69
70 @Override
71 public Collection<Device> getDevices() {
72 // TODO Auto-generated method stub
73 return null;
74 }
75
Jonathan Hart062a2e82014-02-03 09:41:57 -080076 public void addPort(Port port) {
77 this.ports.put(port.getNumber(), port);
78 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080079
80 public Port removePort(Port port) {
81 Port p = this.ports.remove(port.getNumber());
82 // XXX Do we need to validate instance equality?
83 assert( p == port );
84 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}