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; |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 6 | import java.util.HashSet; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 7 | import java.util.Map; |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 8 | import java.util.Set; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 9 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 10 | import net.onrc.onos.datastore.topology.RCPort; |
| 11 | import net.onrc.onos.datastore.topology.RCSwitch; |
| 12 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 13 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 14 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 15 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 16 | /** |
| 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 Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 22 | public class SwitchImpl extends NetworkGraphObject implements Switch { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 23 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 24 | private long dpid; |
| 25 | private final Map<Short, Port> ports; |
| 26 | |
| 27 | public SwitchImpl(NetworkGraph graph) { |
| 28 | super(graph); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 29 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 30 | ports = new HashMap<Short, Port>(); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public long getDpid() { |
| 35 | return dpid; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Collection<Port> getPorts() { |
| 40 | return Collections.unmodifiableCollection(ports.values()); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public Port getPort(short number) { |
| 45 | 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 |
| 61 | public Link getLinkToNeighbor(long neighborDpid) { |
| 62 | for (Link link : graph.getLinksFromSwitch(dpid)) { |
| 63 | 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 | |
| 76 | public void setDpid(long dpid) { |
| 77 | this.dpid = dpid; |
| 78 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 79 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 80 | public void addPort(Port port) { |
| 81 | this.ports.put(port.getNumber(), port); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Iterable<Link> getLinks() { |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 86 | Set<Link> links = new HashSet<>(); |
| 87 | for( Port p : ports.values()) { |
| 88 | links.add(p.getLink()); |
| 89 | } |
| 90 | return links; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 91 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 92 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 93 | public void store() { |
| 94 | RCSwitch rcSwitch = new RCSwitch(dpid); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 95 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 96 | for (Port port : ports.values()) { |
| 97 | RCPort rcPort = new RCPort(dpid, (long)port.getNumber()); |
| 98 | rcSwitch.addPortId(rcPort.getId()); |
| 99 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 100 | |
| 101 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 102 | try { |
| 103 | rcSwitch.update(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 104 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 105 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 106 | // TODO Auto-generated catch block |
| 107 | e.printStackTrace(); |
| 108 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 109 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 110 | } |
| 111 | } |