blob: 1cc5829be5e40372b9911178ff6aea00048485c4 [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;
Jian Li6b77dc02016-07-14 16:04:25 +090021
Jian Lie9af3b42016-08-08 15:50:01 +090022import java.util.Objects;
23
Jian Li6b77dc02016-07-14 16:04:25 +090024import static com.google.common.base.Preconditions.checkArgument;
25
26/**
27 * IPv4 address that is used by LISP Locator.
28 */
29public 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 Lie9af3b42016-08-08 15:50:01 +090040
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 Li115d8602016-08-15 20:21:53 +090059
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 Li6b77dc02016-07-14 16:04:25 +090077}