Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Yuta HIGUCHI | 6237cfc | 2016-07-06 21:23:00 -0700 | [diff] [blame] | 22 | import org.onosproject.net.link.LinkDescription; |
| 23 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 24 | import 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. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 28 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Immutable representation of a link identity. |
| 31 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 32 | public final class LinkKey { |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 33 | |
| 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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 61 | 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 HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 75 | } |
| 76 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Creates a link identifier for the specified link. |
| 79 | * |
| 80 | * @param link link descriptor |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 81 | * @return a link identifier |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 82 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 83 | public static LinkKey linkKey(Link link) { |
| 84 | return new LinkKey(link.src(), link.dst()); |
| 85 | } |
| 86 | |
Yuta HIGUCHI | 6237cfc | 2016-07-06 21:23:00 -0700 | [diff] [blame] | 87 | /** |
| 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 HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public int hashCode() { |
Thomas Vachuska | c5ea950 | 2014-11-04 10:25:46 -0800 | [diff] [blame] | 99 | return Objects.hash(src, dst); |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 100 | } |
| 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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 109 | return Objects.equals(this.src, other.src) && |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 110 | Objects.equals(this.dst, other.dst); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 118 | .add("src", src) |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 119 | .add("dst", dst) |
| 120 | .toString(); |
| 121 | } |
| 122 | } |