Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Collections; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 9 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
| 10 | import net.onrc.onos.datastore.topology.RCPort; |
| 11 | import net.onrc.onos.datastore.topology.RCSwitch; |
| 12 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 13 | |
| 14 | public class SwitchImpl extends NetworkGraphObject implements Switch { |
| 15 | |
| 16 | private long dpid; |
| 17 | private final Map<Short, Port> ports; |
| 18 | |
| 19 | public SwitchImpl(NetworkGraph graph) { |
| 20 | super(graph); |
| 21 | |
| 22 | ports = new HashMap<Short, Port>(); |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public long getDpid() { |
| 27 | return dpid; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public Collection<Port> getPorts() { |
| 32 | return Collections.unmodifiableCollection(ports.values()); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public Port getPort(short number) { |
| 37 | return ports.get(number); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public Collection<FlowEntry> getFlowEntries() { |
| 42 | // TODO Auto-generated method stub |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Iterable<Switch> getNeighbors() { |
| 48 | // TODO Auto-generated method stub |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Link getLinkToNeighbor(long neighborDpid) { |
| 54 | for (Link link : graph.getLinksFromSwitch(dpid)) { |
| 55 | if (link.getDestinationSwitch().getDpid() == neighborDpid) { |
| 56 | return link; |
| 57 | } |
| 58 | } |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<Device> getDevices() { |
| 64 | // TODO Auto-generated method stub |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | public void setDpid(long dpid) { |
| 69 | this.dpid = dpid; |
| 70 | } |
| 71 | |
| 72 | public void addPort(Port port) { |
| 73 | this.ports.put(port.getNumber(), port); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Iterable<Link> getLinks() { |
| 78 | return graph.getLinksFromSwitch(dpid); |
| 79 | } |
| 80 | |
| 81 | public void store() { |
| 82 | RCSwitch rcSwitch = new RCSwitch(dpid); |
| 83 | |
| 84 | for (Port port : ports.values()) { |
| 85 | RCPort rcPort = new RCPort(dpid, (long)port.getNumber()); |
| 86 | rcSwitch.addPortId(rcPort.getId()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | try { |
| 91 | rcSwitch.update(); |
| 92 | |
| 93 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 94 | // TODO Auto-generated catch block |
| 95 | e.printStackTrace(); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | } |