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; |
Yuta HIGUCHI | 1929b5d | 2014-02-05 20:14:51 -0800 | [diff] [blame] | 11 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 12 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 13 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 14 | /** |
| 15 | * Switch Object stored in In-memory Topology. |
| 16 | * |
| 17 | * TODO REMOVE following design memo: This object itself may hold the DBObject, |
| 18 | * but this Object itself will not issue any read/write to the DataStore. |
| 19 | */ |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 20 | public class SwitchImpl extends NetworkGraphObject implements Switch { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 21 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 22 | private Long dpid; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 23 | // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 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 |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 48 | public Iterable<Switch> getNeighbors() { |
| 49 | // TODO Auto-generated method stub |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 54 | public Link getLinkToNeighbor(Long neighborDpid) { |
| 55 | for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 56 | if (link.getDestinationSwitch().getDpid() == neighborDpid) { |
| 57 | return link; |
| 58 | } |
| 59 | } |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Collection<Device> getDevices() { |
| 65 | // TODO Auto-generated method stub |
| 66 | return null; |
| 67 | } |
| 68 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 69 | public void addPort(Port port) { |
| 70 | this.ports.put(port.getNumber(), port); |
| 71 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 72 | |
| 73 | public Port removePort(Port port) { |
| 74 | Port p = this.ports.remove(port.getNumber()); |
| 75 | // XXX Do we need to validate instance equality? |
| 76 | assert( p == port ); |
| 77 | return p; |
| 78 | } |
| 79 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 80 | public Port addPort(Long portNumber) { |
| 81 | PortImpl port = new PortImpl(graph, this, portNumber); |
| 82 | ports.put(port.getNumber(), port); |
| 83 | return port; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 84 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 85 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 86 | public void store() { |
| 87 | RCSwitch rcSwitch = new RCSwitch(dpid); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 88 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 89 | for (Port port : ports.values()) { |
| 90 | RCPort rcPort = new RCPort(dpid, (long)port.getNumber()); |
| 91 | rcSwitch.addPortId(rcPort.getId()); |
| 92 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 93 | |
| 94 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 95 | try { |
| 96 | rcSwitch.update(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 97 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 98 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 99 | // TODO Auto-generated catch block |
| 100 | e.printStackTrace(); |
| 101 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 102 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 103 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public Iterable<Link> getOutgoingLinks() { |
| 107 | LinkedList<Link> links = new LinkedList<Link>(); |
| 108 | for (Port port: getPorts()) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 109 | Link link = port.getOutgoingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 110 | if (link != null) { |
| 111 | links.add(link); |
| 112 | } |
| 113 | } |
| 114 | return links; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Iterable<Link> getIncomingLinks() { |
| 119 | LinkedList<Link> links = new LinkedList<Link>(); |
| 120 | for (Port port: getPorts()) { |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 121 | Link link = port.getIncomingLink(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 122 | if (link != null) { |
| 123 | links.add(link); |
| 124 | } |
| 125 | } |
| 126 | return links; |
| 127 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 128 | } |