blob: 5a2566f46d697fc1879d6543f40a6b6b7d3f5c81 [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
Simon Huntf01826c2017-05-09 17:28:13 -070018import com.google.common.base.MoreObjects;
19import org.onosproject.net.link.LinkDescription;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070020
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070021import java.util.Objects;
22
Simon Huntf01826c2017-05-09 17:28:13 -070023import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070024
25// TODO Consider renaming.
26// it's an identifier for a Link, but it's not ElementId, so not using LinkId.
tom95329eb2014-10-06 08:40:06 -070027
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070028/**
29 * Immutable representation of a link identity.
30 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070031public final class LinkKey {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070032
Simon Huntf01826c2017-05-09 17:28:13 -070033 private static final String DELIM = "-";
34
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070035 private final ConnectPoint src;
36 private final ConnectPoint dst;
37
38 /**
Simon Huntf01826c2017-05-09 17:28:13 -070039 * Creates a link identifier with source and destination connection point.
40 *
41 * @param src source connection point
42 * @param dst destination connection point
43 */
44 private LinkKey(ConnectPoint src, ConnectPoint dst) {
45 this.src = checkNotNull(src);
46 this.dst = checkNotNull(dst);
47 }
48
49 /**
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070050 * Returns source connection point.
51 *
52 * @return source connection point
53 */
54 public ConnectPoint src() {
55 return src;
56 }
57
58 /**
59 * Returns destination connection point.
60 *
61 * @return destination connection point
62 */
63 public ConnectPoint dst() {
64 return dst;
65 }
66
67 /**
Simon Huntf01826c2017-05-09 17:28:13 -070068 * Returns a string suitable to be used as an identifier.
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070069 *
Simon Huntf01826c2017-05-09 17:28:13 -070070 * @return string as identifier
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070071 */
Simon Huntf01826c2017-05-09 17:28:13 -070072 public String asId() {
73 return src + DELIM + dst;
74 }
75
76 @Override
77 public int hashCode() {
78 return Objects.hash(src, dst);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj instanceof LinkKey) {
87 final LinkKey other = (LinkKey) obj;
88 return Objects.equals(this.src, other.src) &&
89 Objects.equals(this.dst, other.dst);
90 }
91 return false;
92 }
93
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .add("src", src)
98 .add("dst", dst)
99 .toString();
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700100 }
101
102 /**
103 * Creates a link identifier with source and destination connection point.
104 *
105 * @param src source connection point
106 * @param dst destination connection point
107 * @return a link identifier
108 */
109 public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) {
110 return new LinkKey(src, dst);
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700111 }
112
tom95329eb2014-10-06 08:40:06 -0700113 /**
114 * Creates a link identifier for the specified link.
115 *
116 * @param link link descriptor
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700117 * @return a link identifier
tom95329eb2014-10-06 08:40:06 -0700118 */
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700119 public static LinkKey linkKey(Link link) {
120 return new LinkKey(link.src(), link.dst());
121 }
122
Yuta HIGUCHI6237cfc2016-07-06 21:23:00 -0700123 /**
124 * Creates a link identifier for the specified link.
125 *
126 * @param link {@link Description}
127 * @return a link identifier
128 */
129 public static LinkKey linkKey(LinkDescription link) {
130 return new LinkKey(link.src(), link.dst());
131 }
132
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700133}