blob: ed3ad4393b8b326f88fa38b735171b52e4a1c480 [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 * IPv6 address that is used by LISP Locator.
29 */
30public class LispIpv6Address extends LispIpAddress {
31
32 /**
33 * Initializes LISP locator's IPv6 address.
34 *
35 * @param address IP address
36 */
37 public LispIpv6Address(IpAddress address) {
38 super(address, AddressFamilyIdentifierEnum.IP);
39 checkArgument(address.isIp6());
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 LispIpv6Address) {
49 final LispIpv6Address other = (LispIpv6Address) 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 * IPv6 address reader class.
63 */
64 public static class Ipv6AddressReader implements LispAddressReader<LispIpv6Address> {
65
66 private static final int SIZE_OF_IPV6_ADDRESS = 16;
67
68 @Override
69 public LispIpv6Address readFrom(ByteBuf byteBuf) throws LispParseError {
70
71 byte[] ipByte = new byte[SIZE_OF_IPV6_ADDRESS];
72 byteBuf.readBytes(ipByte);
73 IpAddress ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, ipByte);
74
75 return new LispIpv6Address(ipAddress);
76 }
77 }
GUNiba871702016-08-22 21:06:02 +090078
79 /**
80 * Ipv6 address writer class.
81 */
82 public static class Ipv6AddressWriter implements LispAddressWriter<LispIpv6Address> {
83
84 @Override
85 public void writeTo(ByteBuf byteBuf, LispIpv6Address address) throws LispWriterException {
86 byte[] ipByte = address.getAddress().getIp6Address().toOctets();
87 byteBuf.writeBytes(ipByte);
88 }
89 }
Jian Li6b77dc02016-07-14 16:04:25 +090090}