blob: f751938264840da8d6b76cfcb4196d0b37c4af1a [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
7import com.google.common.base.MoreObjects;
8
9// TODO Consider renaming.
10// it's an identifier for a Link, but it's not ElementId, so not using LinkId.
tom95329eb2014-10-06 08:40:06 -070011
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070012/**
13 * Immutable representation of a link identity.
14 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070015public final class LinkKey {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070016
17 private final ConnectPoint src;
18 private final ConnectPoint dst;
19
20 /**
21 * Returns source connection point.
22 *
23 * @return source connection point
24 */
25 public ConnectPoint src() {
26 return src;
27 }
28
29 /**
30 * Returns destination connection point.
31 *
32 * @return destination connection point
33 */
34 public ConnectPoint dst() {
35 return dst;
36 }
37
38 /**
39 * Creates a link identifier with source and destination connection point.
40 *
41 * @param src source connection point
42 * @param dst destination connection point
43 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070044 private LinkKey(ConnectPoint src, ConnectPoint dst) {
45 this.src = checkNotNull(src);
46 this.dst = checkNotNull(dst);
47 }
48
49 /**
50 * Creates a link identifier with source and destination connection point.
51 *
52 * @param src source connection point
53 * @param dst destination connection point
54 * @return a link identifier
55 */
56 public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) {
57 return new LinkKey(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070058 }
59
tom95329eb2014-10-06 08:40:06 -070060 /**
61 * Creates a link identifier for the specified link.
62 *
63 * @param link link descriptor
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070064 * @return a link identifier
tom95329eb2014-10-06 08:40:06 -070065 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070066 public static LinkKey linkKey(Link link) {
67 return new LinkKey(link.src(), link.dst());
68 }
69
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070070 @Override
71 public int hashCode() {
72 return Objects.hash(src(), dst);
73 }
74
75 @Override
76 public boolean equals(Object obj) {
77 if (this == obj) {
78 return true;
79 }
80 if (obj instanceof LinkKey) {
81 final LinkKey other = (LinkKey) obj;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070082 return Objects.equals(this.src, other.src) &&
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070083 Objects.equals(this.dst, other.dst);
84 }
85 return false;
86 }
87
88 @Override
89 public String toString() {
90 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070091 .add("src", src)
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070092 .add("dst", dst)
93 .toString();
94 }
95}