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; |
| 6 | import java.util.LinkedList; |
| 7 | |
| 8 | import net.floodlightcontroller.util.MACAddress; |
| 9 | |
| 10 | /** |
| 11 | * @author Toshio Koide (t-koide@onlab.us) |
| 12 | */ |
| 13 | public class MutableNetworkGraph implements NetworkGraph { |
| 14 | protected HashMap<Long, SwitchImpl> switches; |
| 15 | |
| 16 | public MutableNetworkGraph() { |
| 17 | switches = new HashMap<Long, SwitchImpl>(); |
| 18 | } |
| 19 | |
| 20 | // Switch operations |
| 21 | |
| 22 | public Switch addSwitch(Long switchId) { |
| 23 | if (switches.containsKey(switchId)) { |
| 24 | return null; // should throw exception |
| 25 | } |
| 26 | SwitchImpl sw = new SwitchImpl(this, switchId); |
| 27 | switches.put(sw.getDpid(), sw); |
| 28 | return sw; |
| 29 | |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public Switch getSwitch(Long dpid) { |
| 34 | return switches.get(dpid); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public Iterable<? extends Switch> getSwitches() { |
| 39 | return switches.values(); |
| 40 | } |
| 41 | |
| 42 | // Link operations |
| 43 | |
| 44 | public Link addLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 45 | return new LinkImpl( |
| 46 | this, |
| 47 | getSwitch(srcDpid).getPort(srcPortNo), |
| 48 | getSwitch(dstDpid).getPort(dstPortNo)); |
| 49 | } |
| 50 | |
| 51 | public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 52 | Link[] links = new Link[2]; |
| 53 | links[0] = addLink(srcDpid, srcPortNo, dstDpid, dstPortNo); |
| 54 | links[1] = addLink(dstDpid, dstPortNo, srcDpid, srcPortNo); |
| 55 | |
| 56 | return links; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Collection<Link> getLinks() { |
| 61 | LinkedList<Link> links = new LinkedList<Link>(); |
| 62 | for (Switch sw: switches.values()) { |
| 63 | for (Port port: sw.getPorts()) { |
| 64 | Link link = port.getOutgoingLink(); |
| 65 | if (link != null) { |
| 66 | links.add(link); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return links; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Iterable<Link> getOutgoingLinksFromSwitch(Long dpid) { |
| 75 | LinkedList<Link> links = new LinkedList<Link>(); |
| 76 | for (Port port: getSwitch(dpid).getPorts()) { |
| 77 | Link link = port.getOutgoingLink(); |
| 78 | if (link != null) { |
| 79 | links.add(link); |
| 80 | } |
| 81 | } |
| 82 | return links; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Iterable<Link> getIncomingLinksFromSwitch(Long dpid) { |
| 87 | LinkedList<Link> links = new LinkedList<Link>(); |
| 88 | for (Port port: getSwitch(dpid).getPorts()) { |
| 89 | Link link = port.getIncomingLink(); |
| 90 | if (link != null) { |
| 91 | links.add(link); |
| 92 | } |
| 93 | } |
| 94 | return links; |
| 95 | } |
| 96 | |
| 97 | // Device operations |
| 98 | |
| 99 | @Override |
| 100 | public Iterable<Device> getDeviceByIp(InetAddress ipAddress) { |
| 101 | // TODO Auto-generated method stub |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Iterable<Device> getDeviceByMac(MACAddress address) { |
| 107 | // TODO Auto-generated method stub |
| 108 | return null; |
| 109 | } |
| 110 | } |