Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onosproject.net.link.LinkDescription; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 20 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 24 | |
| 25 | // TODO Consider renaming. |
| 26 | // 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] | 27 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Immutable representation of a link identity. |
| 30 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 31 | public final class LinkKey { |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 32 | |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 33 | private static final String DELIM = "-"; |
| 34 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 35 | private final ConnectPoint src; |
| 36 | private final ConnectPoint dst; |
| 37 | |
| 38 | /** |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 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 | */ |
| 44 | private LinkKey(ConnectPoint src, ConnectPoint dst) { |
| 45 | this.src = checkNotNull(src); |
| 46 | this.dst = checkNotNull(dst); |
| 47 | } |
| 48 | |
| 49 | /** |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 50 | * 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 Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 68 | * Returns a string suitable to be used as an identifier. |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 69 | * |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 70 | * @return string as identifier |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 71 | */ |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 72 | 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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 100 | } |
| 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 HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Creates a link identifier for the specified link. |
| 115 | * |
| 116 | * @param link link descriptor |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 117 | * @return a link identifier |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 118 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 119 | public static LinkKey linkKey(Link link) { |
| 120 | return new LinkKey(link.src(), link.dst()); |
| 121 | } |
| 122 | |
Yuta HIGUCHI | 6237cfc | 2016-07-06 21:23:00 -0700 | [diff] [blame] | 123 | /** |
| 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 HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 133 | } |