blob: a69e0d34265367e6bb8b86918666c000ffc6b293 [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.api.flowmanager;
2
Toshio Koide9aa4c0f2014-08-11 16:06:44 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Toshio Koidea03915e2014-07-01 18:39:52 -07005import java.util.HashMap;
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Set;
9
10import net.onrc.onos.core.util.Dpid;
11import net.onrc.onos.core.util.PortNumber;
12import net.onrc.onos.core.util.SwitchPort;
13
14/**
15 * A directed connected tree topology representation used by TreeFlow.
16 */
17public class Tree extends FlowLinks {
18 Map<Dpid, Set<PortNumber>> ports = new HashMap<Dpid, Set<PortNumber>>();
Toshio Koidea03915e2014-07-01 18:39:52 -070019
20 /**
21 * Creates new instance.
22 */
23 public Tree() {
24 super();
25 }
26
27 /**
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070028 * 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 Koidea03915e2014-07-01 18:39:52 -070033 */
34 public Tree(Path path) {
35 super();
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070036 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 Koidea03915e2014-07-01 18:39:52 -070061 }
62
63 private void addPort(SwitchPort port) {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070064 checkNotNull(port);
Pavlin Radoslavov3d322f42014-08-18 14:58:55 -070065 if (!ports.containsKey(port.getDpid())) {
66 ports.put(port.getDpid(), new HashSet<PortNumber>());
Toshio Koidea03915e2014-07-01 18:39:52 -070067 }
Pavlin Radoslavov3d322f42014-08-18 14:58:55 -070068 ports.get(port.getDpid()).add(port.getPortNumber());
Toshio Koidea03915e2014-07-01 18:39:52 -070069 }
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 Koide9aa4c0f2014-08-11 16:06:44 -070081 checkNotNull(link);
Toshio Koidea03915e2014-07-01 18:39:52 -070082 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 Koidea03915e2014-07-01 18:39:52 -0700103 return true;
104 }
105
106 /**
107 * Checks if specified dpid exists in this tree.
108 *
Toshio Koide43df85b2014-08-20 20:19:57 -0700109 * @param dpid DPID to be checked
110 * @return true if found, false otherwise
Toshio Koidea03915e2014-07-01 18:39:52 -0700111 */
112 public boolean hasDpid(Dpid dpid) {
113 return ports.containsKey(dpid);
114 }
115
116 /**
Toshio Koide43df85b2014-08-20 20:19:57 -0700117 * 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 Koidea03915e2014-07-01 18:39:52 -0700128 * Checks if specified switch-port exists in this tree.
129 *
Toshio Koide43df85b2014-08-20 20:19:57 -0700130 * @param port SwitchPort object to be checked
131 * @return true if found, false otherwise
Toshio Koidea03915e2014-07-01 18:39:52 -0700132 */
133 public boolean hasSwitchPort(SwitchPort port) {
Toshio Koide43df85b2014-08-20 20:19:57 -0700134 Set<PortNumber> portNumbers = getPortNumbersOf(port.getDpid());
135 return portNumbers != null && portNumbers.contains(port.getPortNumber());
Toshio Koidea03915e2014-07-01 18:39:52 -0700136 }
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}