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 | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 6 | import java.util.HashSet; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 7 | import java.util.LinkedList; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 9 | import java.util.Set; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 10 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 11 | import net.onrc.onos.datastore.topology.RCPort; |
| 12 | import net.onrc.onos.datastore.topology.RCSwitch; |
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 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 24 | private Long dpid; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 25 | // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 26 | private final Map<Long, Port> ports; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 27 | |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 28 | public SwitchImpl(NetworkGraph graph, Long dpid) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 29 | super(graph); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 30 | this.dpid = dpid; |
| 31 | ports = new HashMap<Long, Port>(); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 35 | public Long getDpid() { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 36 | return dpid; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public Collection<Port> getPorts() { |
| 41 | return Collections.unmodifiableCollection(ports.values()); |
| 42 | } |
| 43 | |
| 44 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 45 | public Port getPort(Long number) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 46 | return ports.get(number); |
| 47 | } |
| 48 | |
| 49 | @Override |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 50 | public Iterable<Switch> getNeighbors() { |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 51 | Set<Switch> neighbors = new HashSet<>(); |
| 52 | for (Link link : getOutgoingLinks()) { |
| 53 | neighbors.add(link.getDestinationSwitch()); |
| 54 | } |
| 55 | // XXX should incoming considered neighbor? |
| 56 | for (Link link : getIncomingLinks()) { |
| 57 | neighbors.add(link.getSourceSwitch()); |
| 58 | } |
| 59 | return neighbors; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 63 | public Link getLinkToNeighbor(Long neighborDpid) { |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 64 | for (Link link : getOutgoingLinks()) { |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 65 | if (link.getDestinationSwitch().getDpid().equals(neighborDpid) ) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 66 | return link; |
| 67 | } |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Collection<Device> getDevices() { |
| 74 | // TODO Auto-generated method stub |
| 75 | return null; |
| 76 | } |
| 77 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 78 | public void addPort(Port port) { |
| 79 | this.ports.put(port.getNumber(), port); |
| 80 | } |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 81 | |
| 82 | public Port removePort(Port port) { |
| 83 | Port p = this.ports.remove(port.getNumber()); |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 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 | } |