Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
| 3 | import java.net.InetAddress; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -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; |
| 8 | |
| 9 | import net.floodlightcontroller.util.MACAddress; |
| 10 | |
| 11 | /** |
| 12 | * @author Toshio Koide (t-koide@onlab.us) |
| 13 | */ |
| 14 | public class MutableNetworkGraph implements NetworkGraph { |
| 15 | protected HashMap<Long, SwitchImpl> switches; |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -0800 | [diff] [blame^] | 16 | protected HashMap<MACAddress, Device> devices; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 17 | |
| 18 | public MutableNetworkGraph() { |
| 19 | switches = new HashMap<Long, SwitchImpl>(); |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -0800 | [diff] [blame^] | 20 | devices = new HashMap<MACAddress, Device>(); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Switch operations |
| 24 | |
| 25 | public Switch addSwitch(Long switchId) { |
| 26 | if (switches.containsKey(switchId)) { |
| 27 | return null; // should throw exception |
| 28 | } |
| 29 | SwitchImpl sw = new SwitchImpl(this, switchId); |
| 30 | switches.put(sw.getDpid(), sw); |
| 31 | return sw; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public Switch getSwitch(Long dpid) { |
| 37 | return switches.get(dpid); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public Iterable<? extends Switch> getSwitches() { |
| 42 | return switches.values(); |
| 43 | } |
| 44 | |
| 45 | // Link operations |
| 46 | |
| 47 | public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 48 | return new LinkImpl( |
| 49 | this, |
| 50 | getSwitch(srcDpid).getPort(srcPortNo), |
| 51 | getSwitch(dstDpid).getPort(dstPortNo)); |
| 52 | } |
| 53 | |
| 54 | public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 55 | Link[] links = new Link[2]; |
| 56 | links[0] = addLink(srcDpid, srcPortNo, dstDpid, dstPortNo); |
| 57 | links[1] = addLink(dstDpid, dstPortNo, srcDpid, srcPortNo); |
| 58 | |
| 59 | return links; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<Link> getLinks() { |
| 64 | LinkedList<Link> links = new LinkedList<Link>(); |
| 65 | for (Switch sw: switches.values()) { |
| 66 | for (Port port: sw.getPorts()) { |
| 67 | Link link = port.getOutgoingLink(); |
| 68 | if (link != null) { |
| 69 | links.add(link); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return links; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) { |
| 78 | LinkedList<Link> links = new LinkedList<Link>(); |
| 79 | for (Port port: getSwitch(dpid).getPorts()) { |
| 80 | Link link = port.getOutgoingLink(); |
| 81 | if (link != null) { |
| 82 | links.add(link); |
| 83 | } |
| 84 | } |
| 85 | return links; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) { |
| 90 | LinkedList<Link> links = new LinkedList<Link>(); |
| 91 | for (Port port: getSwitch(dpid).getPorts()) { |
| 92 | Link link = port.getIncomingLink(); |
| 93 | if (link != null) { |
| 94 | links.add(link); |
| 95 | } |
| 96 | } |
| 97 | return links; |
| 98 | } |
| 99 | |
| 100 | // Device operations |
| 101 | |
| 102 | @Override |
| 103 | public Iterable<Device> getDeviceByIp(InetAddress ipAddress) { |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -0800 | [diff] [blame^] | 104 | // TODO optimize it |
| 105 | HashSet<Device> result = new HashSet<Device>(); |
| 106 | for (Device d: devices.values()) { |
| 107 | Collection<InetAddress> addrs = d.getIpAddress(); |
| 108 | for (InetAddress addr: addrs) { |
| 109 | if (addr.equals(ipAddress)) { |
| 110 | result.add(d); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return result; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Toshio Koide | 6c9b7e8 | 2014-02-07 18:01:32 -0800 | [diff] [blame^] | 119 | public Device getDeviceByMac(MACAddress address) { |
| 120 | return devices.get(address); |
| 121 | } |
| 122 | |
| 123 | public void addDevice(Device device) { |
| 124 | devices.put(device.getMacAddress(), device); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 125 | } |
| 126 | } |