blob: 56c96e0a5e8115d49e8935e97379f5041755baa4 [file] [log] [blame]
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -07001package org.onlab.onos.net;
2
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -07005import java.util.Objects;
6
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -07007import org.onlab.onos.net.link.LinkDescription;
8
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -07009import com.google.common.base.MoreObjects;
10
11// TODO Consider renaming.
12// it's an identifier for a Link, but it's not ElementId, so not using LinkId.
tom95329eb2014-10-06 08:40:06 -070013
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070014/**
15 * Immutable representation of a link identity.
16 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070017public final class LinkKey {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070018
19 private final ConnectPoint src;
20 private final ConnectPoint dst;
21
22 /**
23 * Returns source connection point.
24 *
25 * @return source connection point
26 */
27 public ConnectPoint src() {
28 return src;
29 }
30
31 /**
32 * Returns destination connection point.
33 *
34 * @return destination connection point
35 */
36 public ConnectPoint dst() {
37 return dst;
38 }
39
40 /**
41 * Creates a link identifier with source and destination connection point.
42 *
43 * @param src source connection point
44 * @param dst destination connection point
45 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070046 private LinkKey(ConnectPoint src, ConnectPoint dst) {
47 this.src = checkNotNull(src);
48 this.dst = checkNotNull(dst);
49 }
50
51 /**
52 * Creates a link identifier with source and destination connection point.
53 *
54 * @param src source connection point
55 * @param dst destination connection point
56 * @return a link identifier
57 */
58 public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) {
59 return new LinkKey(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070060 }
61
tom95329eb2014-10-06 08:40:06 -070062 /**
63 * Creates a link identifier for the specified link.
64 *
65 * @param link link descriptor
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070066 * @return a link identifier
tom95329eb2014-10-06 08:40:06 -070067 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070068 public static LinkKey linkKey(Link link) {
69 return new LinkKey(link.src(), link.dst());
70 }
71
72 /**
73 * Creates a link identifier for the specified link.
74 *
75 * @param desc link description
76 * @return a link identifier
77 */
78 public static LinkKey linkKey(LinkDescription desc) {
79 return new LinkKey(desc.src(), desc.dst());
tom95329eb2014-10-06 08:40:06 -070080 }
81
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070082 @Override
83 public int hashCode() {
84 return Objects.hash(src(), dst);
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj instanceof LinkKey) {
93 final LinkKey other = (LinkKey) obj;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070094 return Objects.equals(this.src, other.src) &&
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070095 Objects.equals(this.dst, other.dst);
96 }
97 return false;
98 }
99
100 @Override
101 public String toString() {
102 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700103 .add("src", src)
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700104 .add("dst", dst)
105 .toString();
106 }
107}