Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.api.flowmanager; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.Map; |
| 6 | import java.util.Set; |
| 7 | |
| 8 | import net.onrc.onos.core.util.Dpid; |
| 9 | import net.onrc.onos.core.util.PortNumber; |
| 10 | import net.onrc.onos.core.util.SwitchPort; |
| 11 | |
| 12 | /** |
| 13 | * A directed connected tree topology representation used by TreeFlow. |
| 14 | */ |
| 15 | public class Tree extends FlowLinks { |
| 16 | Map<Dpid, Set<PortNumber>> ports = new HashMap<Dpid, Set<PortNumber>>(); |
| 17 | Map<SwitchPort, FlowLink> inLinks = new HashMap<SwitchPort, FlowLink>(); |
| 18 | Map<SwitchPort, FlowLink> outLinks = new HashMap<SwitchPort, FlowLink>(); |
| 19 | |
| 20 | /** |
| 21 | * Creates new instance. |
| 22 | */ |
| 23 | public Tree() { |
| 24 | super(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Creates new instance using Path object. |
| 29 | */ |
| 30 | public Tree(Path path) { |
| 31 | super(); |
| 32 | // TODO implement |
| 33 | } |
| 34 | |
| 35 | private void addPort(SwitchPort port) { |
| 36 | if (!ports.containsKey(port.dpid())) { |
| 37 | ports.put(port.dpid(), new HashSet<PortNumber>()); |
| 38 | } |
| 39 | ports.get(port.dpid()).add(port.port()); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Adds FlowLink object to this tree. |
| 44 | * <p> |
| 45 | * This method checks specified FlowLink object to keep this Tree object a |
| 46 | * connected tree. |
| 47 | * |
| 48 | * @param link FlowLink object to be added. |
| 49 | * @return true if succeeded, false otherwise. |
| 50 | */ |
| 51 | public boolean addLink(FlowLink link) { |
| 52 | if (links.size() > 0) { |
| 53 | if (!hasDpid(link.getSrcDpid()) && !hasDpid(link.getDstDpid())) { |
| 54 | // no attaching point |
| 55 | return false; |
| 56 | } |
| 57 | if (hasDpid(link.getSrcDpid()) && hasDpid(link.getDstDpid())) { |
| 58 | // loop or duplicated paths |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (hasSwitchPort(link.getSrcSwitchPort()) |
| 64 | || hasSwitchPort(link.getDstSwitchPort())) { |
| 65 | // some port has already been occupied by another link |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | links.add(link); |
| 70 | addPort(link.getSrcSwitchPort()); |
| 71 | addPort(link.getDstSwitchPort()); |
| 72 | |
| 73 | inLinks.put(link.getDstSwitchPort(), link); |
| 74 | outLinks.put(link.getSrcSwitchPort(), link); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Checks if specified dpid exists in this tree. |
| 81 | * |
| 82 | * @param dpid DPID to be checked. |
| 83 | * @return true if found, false otherwise. |
| 84 | */ |
| 85 | public boolean hasDpid(Dpid dpid) { |
| 86 | return ports.containsKey(dpid); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks if specified switch-port exists in this tree. |
| 91 | * |
| 92 | * @param port SwitchPort object to be checked. |
| 93 | * @return true if found, false otherwise. |
| 94 | */ |
| 95 | public boolean hasSwitchPort(SwitchPort port) { |
| 96 | return inLinks.containsKey(port) || outLinks.containsKey(port); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
| 101 | // TODO think if this is correct. |
| 102 | return super.hashCode(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object o) { |
| 107 | // TODO think if this is correct. |
| 108 | // - has to check equality using a set, not a list? |
| 109 | return super.equals(o); |
| 110 | } |
| 111 | } |