Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.lisp.msg.types; |
| 17 | |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame^] | 18 | import io.netty.buffer.ByteBuf; |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame^] | 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 21 | |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * IPv4 address that is used by LISP Locator. |
| 28 | */ |
| 29 | public class LispIpv4Address extends LispIpAddress { |
| 30 | |
| 31 | /** |
| 32 | * Initializes LISP locator's IPv4 address. |
| 33 | * |
| 34 | * @param address IP address |
| 35 | */ |
| 36 | public LispIpv4Address(IpAddress address) { |
| 37 | super(address, AddressFamilyIdentifierEnum.IP); |
| 38 | checkArgument(address.isIp4()); |
| 39 | } |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 40 | |
| 41 | @Override |
| 42 | public boolean equals(Object obj) { |
| 43 | if (this == obj) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | if (obj instanceof LispIpv4Address) { |
| 48 | final LispIpv4Address other = (LispIpv4Address) obj; |
| 49 | return Objects.equals(this.address, other.address) && |
| 50 | Objects.equals(this.getAfi(), other.getAfi()); |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int hashCode() { |
| 57 | return Objects.hash(address, getAfi()); |
| 58 | } |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame^] | 59 | |
| 60 | /** |
| 61 | * IPv4 address reader class. |
| 62 | */ |
| 63 | public static class Ipv4AddressReader implements LispAddressReader<LispIpv4Address> { |
| 64 | |
| 65 | private static final int SIZE_OF_IPV4_ADDRESS = 4; |
| 66 | |
| 67 | @Override |
| 68 | public LispIpv4Address readFrom(ByteBuf byteBuf) throws LispParseError { |
| 69 | |
| 70 | byte[] ipByte = new byte[SIZE_OF_IPV4_ADDRESS]; |
| 71 | byteBuf.readBytes(ipByte); |
| 72 | IpAddress ipAddress = IpAddress.valueOf(IpAddress.Version.INET, ipByte); |
| 73 | |
| 74 | return new LispIpv4Address(ipAddress); |
| 75 | } |
| 76 | } |
Jian Li | 6b77dc0 | 2016-07-14 16:04:25 +0900 | [diff] [blame] | 77 | } |