blob: c1d1a1a212a4705d560e7406832f6c10ebbd31e2 [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 }
Toshio Koide2f570c12014-02-06 16:55:32 -080078
79 public Port addPort(Long portNumber) {
80 PortImpl port = new PortImpl(graph, this, portNumber);
81 ports.put(port.getNumber(), port);
82 return port;
Jonathan Hart062a2e82014-02-03 09:41:57 -080083 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080084
Jonathan Hart062a2e82014-02-03 09:41:57 -080085 public void store() {
86 RCSwitch rcSwitch = new RCSwitch(dpid);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080087
Jonathan Hart062a2e82014-02-03 09:41:57 -080088 for (Port port : ports.values()) {
89 RCPort rcPort = new RCPort(dpid, (long)port.getNumber());
90 rcSwitch.addPortId(rcPort.getId());
91 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080092
93
Jonathan Hart062a2e82014-02-03 09:41:57 -080094 try {
95 rcSwitch.update();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080096
Jonathan Hart062a2e82014-02-03 09:41:57 -080097 } catch (ObjectDoesntExistException | WrongVersionException e) {
98 // TODO Auto-generated catch block
99 e.printStackTrace();
100 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800101
Jonathan Hart062a2e82014-02-03 09:41:57 -0800102 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800103
104 @Override
105 public Iterable<Link> getOutgoingLinks() {
106 LinkedList<Link> links = new LinkedList<Link>();
107 for (Port port: getPorts()) {
108 Link link = port.getOutgoingLink();
109 if (link != null) {
110 links.add(link);
111 }
112 }
113 return links;
114 }
115
116 @Override
117 public Iterable<Link> getIncomingLinks() {
118 LinkedList<Link> links = new LinkedList<Link>();
119 for (Port port: getPorts()) {
120 Link link = port.getIncomingLink();
121 if (link != null) {
122 links.add(link);
123 }
124 }
125 return links;
126 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800127}