Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.vtnrsc; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | |
| 25 | /** |
| 26 | * Immutable representation of a IP address for the port, Include the IP address |
| 27 | * and subnet identity. |
| 28 | */ |
| 29 | public final class FixedIp { |
| 30 | private final SubnetId subnetId; |
| 31 | private final IpAddress ip; |
| 32 | // Public construction is prohibited |
| 33 | private FixedIp(SubnetId subnetId, IpAddress ip) { |
| 34 | checkNotNull(subnetId, "SubnetId cannot be null"); |
| 35 | checkNotNull(ip, "IpAddress cannot be null"); |
| 36 | this.subnetId = subnetId; |
| 37 | this.ip = ip; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the FixedIp subnet identifier. |
| 42 | * |
| 43 | * @return subnet identifier |
| 44 | */ |
| 45 | public SubnetId subnetId() { |
| 46 | return subnetId; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the FixedIp IP address. |
| 51 | * |
| 52 | * @return IP address |
| 53 | */ |
| 54 | public IpAddress ip() { |
| 55 | return ip; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates a fixed ip using the supplied fixedIp. |
| 60 | * |
| 61 | * @param subnetId subnet identity |
| 62 | * @param ip IP address |
| 63 | * @return FixedIp |
| 64 | */ |
| 65 | public static FixedIp fixedIp(SubnetId subnetId, IpAddress ip) { |
| 66 | return new FixedIp(subnetId, ip); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | return Objects.hash(subnetId, ip); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean equals(Object obj) { |
| 76 | if (this == obj) { |
| 77 | return true; |
| 78 | } |
| 79 | if (obj instanceof FixedIp) { |
| 80 | final FixedIp that = (FixedIp) obj; |
| 81 | return Objects.equals(this.subnetId, that.subnetId) |
| 82 | && Objects.equals(this.ip, that.ip); |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return toStringHelper(this).add("subnetId", subnetId).add("ip", ip) |
| 90 | .toString(); |
| 91 | } |
| 92 | |
| 93 | } |