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