blob: 0b0d5fbc5a404a0cdacd68890c012248f45127d0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070017
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
Yuta HIGUCHI6237cfc2016-07-06 21:23:00 -070022import org.onosproject.net.link.LinkDescription;
23
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070024import com.google.common.base.MoreObjects;
25
26// TODO Consider renaming.
27// it's an identifier for a Link, but it's not ElementId, so not using LinkId.
tom95329eb2014-10-06 08:40:06 -070028
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070029/**
30 * Immutable representation of a link identity.
31 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070032public final class LinkKey {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070033
34 private final ConnectPoint src;
35 private final ConnectPoint dst;
36
37 /**
38 * Returns source connection point.
39 *
40 * @return source connection point
41 */
42 public ConnectPoint src() {
43 return src;
44 }
45
46 /**
47 * Returns destination connection point.
48 *
49 * @return destination connection point
50 */
51 public ConnectPoint dst() {
52 return dst;
53 }
54
55 /**
56 * Creates a link identifier with source and destination connection point.
57 *
58 * @param src source connection point
59 * @param dst destination connection point
60 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070061 private LinkKey(ConnectPoint src, ConnectPoint dst) {
62 this.src = checkNotNull(src);
63 this.dst = checkNotNull(dst);
64 }
65
66 /**
67 * Creates a link identifier with source and destination connection point.
68 *
69 * @param src source connection point
70 * @param dst destination connection point
71 * @return a link identifier
72 */
73 public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) {
74 return new LinkKey(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070075 }
76
tom95329eb2014-10-06 08:40:06 -070077 /**
78 * Creates a link identifier for the specified link.
79 *
80 * @param link link descriptor
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070081 * @return a link identifier
tom95329eb2014-10-06 08:40:06 -070082 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070083 public static LinkKey linkKey(Link link) {
84 return new LinkKey(link.src(), link.dst());
85 }
86
Yuta HIGUCHI6237cfc2016-07-06 21:23:00 -070087 /**
88 * Creates a link identifier for the specified link.
89 *
90 * @param link {@link Description}
91 * @return a link identifier
92 */
93 public static LinkKey linkKey(LinkDescription link) {
94 return new LinkKey(link.src(), link.dst());
95 }
96
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070097 @Override
98 public int hashCode() {
Thomas Vachuskac5ea9502014-11-04 10:25:46 -080099 return Objects.hash(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (this == obj) {
105 return true;
106 }
107 if (obj instanceof LinkKey) {
108 final LinkKey other = (LinkKey) obj;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700109 return Objects.equals(this.src, other.src) &&
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700110 Objects.equals(this.dst, other.dst);
111 }
112 return false;
113 }
114
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(getClass())
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700118 .add("src", src)
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700119 .add("dst", dst)
120 .toString();
121 }
122}