blob: b9379a87451dbe9562b51f4824e4e5b5f99cc69d [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;
24 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080025
Toshio Koide2f570c12014-02-06 16:55:32 -080026 public SwitchImpl(NetworkGraph graph, Long dpid) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080027 super(graph);
Toshio Koide2f570c12014-02-06 16:55:32 -080028 this.dpid = dpid;
29 ports = new HashMap<Long, Port>();
Jonathan Hart062a2e82014-02-03 09:41:57 -080030 }
31
32 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080033 public Long getDpid() {
Jonathan Hart062a2e82014-02-03 09:41:57 -080034 return dpid;
35 }
36
37 @Override
38 public Collection<Port> getPorts() {
39 return Collections.unmodifiableCollection(ports.values());
40 }
41
42 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080043 public Port getPort(Long number) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080044 return ports.get(number);
45 }
46
47 @Override
48 public Collection<FlowEntry> getFlowEntries() {
49 // TODO Auto-generated method stub
50 return null;
51 }
52
53 @Override
54 public Iterable<Switch> getNeighbors() {
55 // TODO Auto-generated method stub
56 return null;
57 }
58
59 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080060 public Link getLinkToNeighbor(Long neighborDpid) {
61 for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080062 if (link.getDestinationSwitch().getDpid() == neighborDpid) {
63 return link;
64 }
65 }
66 return null;
67 }
68
69 @Override
70 public Collection<Device> getDevices() {
71 // TODO Auto-generated method stub
72 return null;
73 }
74
Jonathan Hart062a2e82014-02-03 09:41:57 -080075 public void addPort(Port port) {
76 this.ports.put(port.getNumber(), port);
77 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080078
79 public Port removePort(Port port) {
80 Port p = this.ports.remove(port.getNumber());
81 // XXX Do we need to validate instance equality?
82 assert( p == port );
83 return p;
84 }
85
Toshio Koide2f570c12014-02-06 16:55:32 -080086 public Port addPort(Long portNumber) {
87 PortImpl port = new PortImpl(graph, this, portNumber);
88 ports.put(port.getNumber(), port);
89 return port;
Jonathan Hart062a2e82014-02-03 09:41:57 -080090 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080091
Jonathan Hart062a2e82014-02-03 09:41:57 -080092 public void store() {
93 RCSwitch rcSwitch = new RCSwitch(dpid);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080094
Jonathan Hart062a2e82014-02-03 09:41:57 -080095 for (Port port : ports.values()) {
96 RCPort rcPort = new RCPort(dpid, (long)port.getNumber());
97 rcSwitch.addPortId(rcPort.getId());
98 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080099
100
Jonathan Hart062a2e82014-02-03 09:41:57 -0800101 try {
102 rcSwitch.update();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800103
Jonathan Hart062a2e82014-02-03 09:41:57 -0800104 } catch (ObjectDoesntExistException | WrongVersionException e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800108
Jonathan Hart062a2e82014-02-03 09:41:57 -0800109 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800110
111 @Override
112 public Iterable<Link> getOutgoingLinks() {
113 LinkedList<Link> links = new LinkedList<Link>();
114 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800115 Link link = port.getOutgoingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800116 if (link != null) {
117 links.add(link);
118 }
119 }
120 return links;
121 }
122
123 @Override
124 public Iterable<Link> getIncomingLinks() {
125 LinkedList<Link> links = new LinkedList<Link>();
126 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800127 Link link = port.getIncomingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800128 if (link != null) {
129 links.add(link);
130 }
131 }
132 return links;
133 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800134}