blob: 1e161a2af2d1767cd8996f4b3bf002305e6effb3 [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>>();
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 Koide9aa4c0f2014-08-11 16:06:44 -070030 * 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 Koidea03915e2014-07-01 18:39:52 -070035 */
36 public Tree(Path path) {
37 super();
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070038 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 Koidea03915e2014-07-01 18:39:52 -070063 }
64
65 private void addPort(SwitchPort port) {
Toshio Koide9aa4c0f2014-08-11 16:06:44 -070066 checkNotNull(port);
Toshio Koidea03915e2014-07-01 18:39:52 -070067 if (!ports.containsKey(port.dpid())) {
68 ports.put(port.dpid(), new HashSet<PortNumber>());
69 }
70 ports.get(port.dpid()).add(port.port());
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 Koide9aa4c0f2014-08-11 16:06:44 -070083 checkNotNull(link);
Toshio Koidea03915e2014-07-01 18:39:52 -070084 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}