blob: 62f1ea06b29137bd935e41aca9d15e5c77249491 [file] [log] [blame]
Jian Li6b77dc02016-07-14 16:04:25 +09001/*
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 */
16package org.onosproject.lisp.msg.types;
17
Jian Li115d8602016-08-15 20:21:53 +090018import io.netty.buffer.ByteBuf;
Jian Li6b77dc02016-07-14 16:04:25 +090019import org.onlab.packet.IpAddress;
Jian Li115d8602016-08-15 20:21:53 +090020import org.onosproject.lisp.msg.exceptions.LispParseError;
GUNiba871702016-08-22 21:06:02 +090021import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li6b77dc02016-07-14 16:04:25 +090022
Jian Lie9af3b42016-08-08 15:50:01 +090023import java.util.Objects;
24
Jian Li6b77dc02016-07-14 16:04:25 +090025import static com.google.common.base.Preconditions.checkArgument;
26
27/**
28 * IPv4 address that is used by LISP Locator.
29 */
30public 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 Lid4e63702016-08-30 18:29:20 +090038 super(address, AddressFamilyIdentifierEnum.IP4);
Jian Li6b77dc02016-07-14 16:04:25 +090039 checkArgument(address.isIp4());
40 }
Jian Lie9af3b42016-08-08 15:50:01 +090041
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 Li115d8602016-08-15 20:21:53 +090060
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 }
GUNiba871702016-08-22 21:06:02 +090078
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 Li6b77dc02016-07-14 16:04:25 +090090}