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 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 78 | |
| 79 | public Port addPort(Long portNumber) { |
| 80 | PortImpl port = new PortImpl(graph, this, portNumber); |
| 81 | ports.put(port.getNumber(), port); |
| 82 | return port; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 83 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 84 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 85 | public void store() { |
| 86 | RCSwitch rcSwitch = new RCSwitch(dpid); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 87 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 88 | for (Port port : ports.values()) { |
| 89 | RCPort rcPort = new RCPort(dpid, (long)port.getNumber()); |
| 90 | rcSwitch.addPortId(rcPort.getId()); |
| 91 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 92 | |
| 93 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 94 | try { |
| 95 | rcSwitch.update(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 96 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 97 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 98 | // TODO Auto-generated catch block |
| 99 | e.printStackTrace(); |
| 100 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 101 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 102 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 103 | |
| 104 | @Override |
| 105 | public Iterable<Link> getOutgoingLinks() { |
| 106 | LinkedList<Link> links = new LinkedList<Link>(); |
| 107 | for (Port port: getPorts()) { |
| 108 | Link link = port.getOutgoingLink(); |
| 109 | if (link != null) { |
| 110 | links.add(link); |
| 111 | } |
| 112 | } |
| 113 | return links; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Iterable<Link> getIncomingLinks() { |
| 118 | LinkedList<Link> links = new LinkedList<Link>(); |
| 119 | for (Port port: getPorts()) { |
| 120 | Link link = port.getIncomingLink(); |
| 121 | if (link != null) { |
| 122 | links.add(link); |
| 123 | } |
| 124 | } |
| 125 | return links; |
| 126 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 127 | } |