blob: c4e461d58bf6b1dd467561c93e8b46de64db9bd8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070016package org.onlab.onos.net;
17
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070020import java.util.Objects;
21
22import com.google.common.base.MoreObjects;
23
24// TODO Consider renaming.
25// it's an identifier for a Link, but it's not ElementId, so not using LinkId.
tom95329eb2014-10-06 08:40:06 -070026
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070027/**
28 * Immutable representation of a link identity.
29 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070030public final class LinkKey {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070031
32 private final ConnectPoint src;
33 private final ConnectPoint dst;
34
35 /**
36 * Returns source connection point.
37 *
38 * @return source connection point
39 */
40 public ConnectPoint src() {
41 return src;
42 }
43
44 /**
45 * Returns destination connection point.
46 *
47 * @return destination connection point
48 */
49 public ConnectPoint dst() {
50 return dst;
51 }
52
53 /**
54 * Creates a link identifier with source and destination connection point.
55 *
56 * @param src source connection point
57 * @param dst destination connection point
58 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070059 private LinkKey(ConnectPoint src, ConnectPoint dst) {
60 this.src = checkNotNull(src);
61 this.dst = checkNotNull(dst);
62 }
63
64 /**
65 * Creates a link identifier with source and destination connection point.
66 *
67 * @param src source connection point
68 * @param dst destination connection point
69 * @return a link identifier
70 */
71 public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) {
72 return new LinkKey(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070073 }
74
tom95329eb2014-10-06 08:40:06 -070075 /**
76 * Creates a link identifier for the specified link.
77 *
78 * @param link link descriptor
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070079 * @return a link identifier
tom95329eb2014-10-06 08:40:06 -070080 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070081 public static LinkKey linkKey(Link link) {
82 return new LinkKey(link.src(), link.dst());
83 }
84
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070085 @Override
86 public int hashCode() {
Thomas Vachuskac5ea9502014-11-04 10:25:46 -080087 return Objects.hash(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070088 }
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 }
95 if (obj instanceof LinkKey) {
96 final LinkKey other = (LinkKey) obj;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070097 return Objects.equals(this.src, other.src) &&
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070098 Objects.equals(this.dst, other.dst);
99 }
100 return false;
101 }
102
103 @Override
104 public String toString() {
105 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700106 .add("src", src)
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700107 .add("dst", dst)
108 .toString();
109 }
110}