Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.flowmanager; |
| 2 | |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.Map; |
| 8 | import java.util.Set; |
| 9 | |
| 10 | import net.onrc.onos.core.util.Dpid; |
| 11 | import net.onrc.onos.core.util.PortNumber; |
| 12 | import net.onrc.onos.core.util.SwitchPort; |
| 13 | |
| 14 | /** |
| 15 | * A directed connected tree topology representation used by TreeFlow. |
| 16 | */ |
| 17 | public class Tree extends FlowLinks { |
| 18 | Map<Dpid, Set<PortNumber>> ports = new HashMap<Dpid, Set<PortNumber>>(); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Creates new instance. |
| 22 | */ |
| 23 | public Tree() { |
| 24 | super(); |
| 25 | } |
| 26 | |
| 27 | /** |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 28 | * Creates new instance from the specified Path object. |
| 29 | * |
| 30 | * @param path the Path object |
| 31 | * @throws IllegalArgumentException if the path object does not form a |
| 32 | * single connected directed path topology |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 33 | */ |
| 34 | public Tree(Path path) { |
| 35 | super(); |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 36 | checkNotNull(path); |
| 37 | |
| 38 | for (FlowLink link : path) { |
| 39 | if (!addLink(link)) { |
| 40 | throw new IllegalArgumentException(); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates new instance using FlowLinks object. |
| 47 | * |
| 48 | * @param links the FlowLinks object |
| 49 | * @throws IllegalArgumentException if the links object does not form a |
| 50 | * single connected directed tree topology |
| 51 | */ |
| 52 | public Tree(FlowLinks links) { |
| 53 | super(); |
| 54 | checkNotNull(links); |
| 55 | |
| 56 | for (FlowLink link : links) { |
| 57 | if (!addLink(link)) { |
| 58 | throw new IllegalArgumentException(); |
| 59 | } |
| 60 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private void addPort(SwitchPort port) { |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 64 | checkNotNull(port); |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 65 | if (!ports.containsKey(port.getDpid())) { |
| 66 | ports.put(port.getDpid(), new HashSet<PortNumber>()); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 67 | } |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 68 | ports.get(port.getDpid()).add(port.getPortNumber()); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Adds FlowLink object to this tree. |
| 73 | * <p> |
| 74 | * This method checks specified FlowLink object to keep this Tree object a |
| 75 | * connected tree. |
| 76 | * |
| 77 | * @param link FlowLink object to be added. |
| 78 | * @return true if succeeded, false otherwise. |
| 79 | */ |
| 80 | public boolean addLink(FlowLink link) { |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 81 | checkNotNull(link); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 82 | if (links.size() > 0) { |
| 83 | if (!hasDpid(link.getSrcDpid()) && !hasDpid(link.getDstDpid())) { |
| 84 | // no attaching point |
| 85 | return false; |
| 86 | } |
| 87 | if (hasDpid(link.getSrcDpid()) && hasDpid(link.getDstDpid())) { |
| 88 | // loop or duplicated paths |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (hasSwitchPort(link.getSrcSwitchPort()) |
| 94 | || hasSwitchPort(link.getDstSwitchPort())) { |
| 95 | // some port has already been occupied by another link |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | links.add(link); |
| 100 | addPort(link.getSrcSwitchPort()); |
| 101 | addPort(link.getDstSwitchPort()); |
| 102 | |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Checks if specified dpid exists in this tree. |
| 108 | * |
Toshio Koide | 43df85b | 2014-08-20 20:19:57 -0700 | [diff] [blame] | 109 | * @param dpid DPID to be checked |
| 110 | * @return true if found, false otherwise |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 111 | */ |
| 112 | public boolean hasDpid(Dpid dpid) { |
| 113 | return ports.containsKey(dpid); |
| 114 | } |
| 115 | |
| 116 | /** |
Toshio Koide | 43df85b | 2014-08-20 20:19:57 -0700 | [diff] [blame] | 117 | * Gets a set of {@link PortNumber}s associated with specified DPID. |
| 118 | * |
| 119 | * @param dpid the DPID |
| 120 | * @return a set of {@link PortNumber}s associated with the DPID, or null if |
| 121 | * the dpid does not exist in this tree |
| 122 | */ |
| 123 | public Set<PortNumber> getPortNumbersOf(Dpid dpid) { |
| 124 | return ports.get(dpid); |
| 125 | } |
| 126 | |
| 127 | /** |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 128 | * Checks if specified switch-port exists in this tree. |
| 129 | * |
Toshio Koide | 43df85b | 2014-08-20 20:19:57 -0700 | [diff] [blame] | 130 | * @param port SwitchPort object to be checked |
| 131 | * @return true if found, false otherwise |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 132 | */ |
| 133 | public boolean hasSwitchPort(SwitchPort port) { |
Toshio Koide | 43df85b | 2014-08-20 20:19:57 -0700 | [diff] [blame] | 134 | Set<PortNumber> portNumbers = getPortNumbersOf(port.getDpid()); |
| 135 | return portNumbers != null && portNumbers.contains(port.getPortNumber()); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
| 140 | // TODO think if this is correct. |
| 141 | return super.hashCode(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean equals(Object o) { |
| 146 | // TODO think if this is correct. |
| 147 | // - has to check equality using a set, not a list? |
| 148 | return super.equals(o); |
| 149 | } |
| 150 | } |