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; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 7 | import java.util.Map; |
| 8 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 9 | import net.onrc.onos.datastore.topology.RCPort; |
| 10 | import net.onrc.onos.datastore.topology.RCSwitch; |
| 11 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 12 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 13 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 14 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 15 | /** |
| 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 Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 21 | public class SwitchImpl extends NetworkGraphObject implements Switch { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 22 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 23 | private Long dpid; |
| 24 | private final Map<Long, Port> ports; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 25 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 26 | public SwitchImpl(NetworkGraph graph, Long dpid) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 27 | super(graph); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 28 | this.dpid = dpid; |
| 29 | ports = new HashMap<Long, Port>(); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 33 | public Long getDpid() { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 34 | return dpid; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public Collection<Port> getPorts() { |
| 39 | return Collections.unmodifiableCollection(ports.values()); |
| 40 | } |
| 41 | |
| 42 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 43 | public Port getPort(Long number) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 44 | 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 Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 60 | public Link getLinkToNeighbor(Long neighborDpid) { |
| 61 | for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 62 | 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 Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 75 | public void addPort(Port port) { |
| 76 | this.ports.put(port.getNumber(), port); |
| 77 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame^] | 78 | |
| 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 Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 86 | public Port addPort(Long portNumber) { |
| 87 | PortImpl port = new PortImpl(graph, this, portNumber); |
| 88 | ports.put(port.getNumber(), port); |
| 89 | return port; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 90 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 91 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 92 | public void store() { |
| 93 | RCSwitch rcSwitch = new RCSwitch(dpid); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 95 | for (Port port : ports.values()) { |
| 96 | RCPort rcPort = new RCPort(dpid, (long)port.getNumber()); |
| 97 | rcSwitch.addPortId(rcPort.getId()); |
| 98 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 99 | |
| 100 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 101 | try { |
| 102 | rcSwitch.update(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 103 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 104 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 105 | // TODO Auto-generated catch block |
| 106 | e.printStackTrace(); |
| 107 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 108 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 109 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 110 | |
| 111 | @Override |
| 112 | public Iterable<Link> getOutgoingLinks() { |
| 113 | LinkedList<Link> links = new LinkedList<Link>(); |
| 114 | for (Port port: getPorts()) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame^] | 115 | Link link = port.getOutgoingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 116 | 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 HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame^] | 127 | Link link = port.getIncomingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 128 | if (link != null) { |
| 129 | links.add(link); |
| 130 | } |
| 131 | } |
| 132 | return links; |
| 133 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 134 | } |