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; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame^] | 24 | // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 25 | private final Map<Long, Port> ports; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 26 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 27 | public SwitchImpl(NetworkGraph graph, Long dpid) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 28 | super(graph); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 29 | this.dpid = dpid; |
| 30 | ports = new HashMap<Long, Port>(); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 34 | public Long getDpid() { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 35 | return dpid; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Collection<Port> getPorts() { |
| 40 | return Collections.unmodifiableCollection(ports.values()); |
| 41 | } |
| 42 | |
| 43 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 44 | public Port getPort(Long number) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 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 |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 61 | public Link getLinkToNeighbor(Long neighborDpid) { |
| 62 | for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 76 | public void addPort(Port port) { |
| 77 | this.ports.put(port.getNumber(), port); |
| 78 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 79 | |
| 80 | public Port removePort(Port port) { |
| 81 | Port p = this.ports.remove(port.getNumber()); |
| 82 | // XXX Do we need to validate instance equality? |
| 83 | assert( p == port ); |
| 84 | return p; |
| 85 | } |
| 86 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 87 | public Port addPort(Long portNumber) { |
| 88 | PortImpl port = new PortImpl(graph, this, portNumber); |
| 89 | ports.put(port.getNumber(), port); |
| 90 | return port; |
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 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | public Iterable<Link> getOutgoingLinks() { |
| 114 | LinkedList<Link> links = new LinkedList<Link>(); |
| 115 | for (Port port: getPorts()) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 116 | Link link = port.getOutgoingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 117 | if (link != null) { |
| 118 | links.add(link); |
| 119 | } |
| 120 | } |
| 121 | return links; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Iterable<Link> getIncomingLinks() { |
| 126 | LinkedList<Link> links = new LinkedList<Link>(); |
| 127 | for (Port port: getPorts()) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 128 | Link link = port.getIncomingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 129 | if (link != null) { |
| 130 | links.add(link); |
| 131 | } |
| 132 | } |
| 133 | return links; |
| 134 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 135 | } |