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