blob: a26abaaaca87962750054eb72aed844c19594064 [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;
21import org.onosproject.lisp.msg.exceptions.LispReaderException;
GUNiba871702016-08-22 21:06:02 +090022import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li3a99e712016-12-16 21:23:16 +090023import org.onosproject.lisp.msg.types.LispIpv4Address.Ipv4AddressWriter;
24import org.onosproject.lisp.msg.types.LispIpv6Address.Ipv6AddressWriter;
Jian Li6b77dc02016-07-14 16:04:25 +090025
26/**
27 * IP address that is used by LISP Locator.
28 */
29public abstract class LispIpAddress extends LispAfiAddress {
30
31 protected final IpAddress address;
32
33 /**
34 * Initializes LISP locator's IP address with AFI enum.
35 *
36 * @param address IP address
37 * @param afi AFI enum
38 */
39 protected LispIpAddress(IpAddress address, AddressFamilyIdentifierEnum afi) {
40 super(afi);
41 this.address = address;
42 }
43
44 /**
45 * Obtains LISP locator's IP address.
46 *
47 * @return IP address
48 */
49 public IpAddress getAddress() {
50 return address;
51 }
52
53 @Override
54 public int hashCode() {
55 return address.hashCode();
56 }
57
58 @Override
59 public boolean equals(Object obj) {
60 return address.equals(obj);
61 }
62
63 @Override
64 public String toString() {
65 return address.toString();
66 }
Jian Li115d8602016-08-15 20:21:53 +090067
68 /**
GUNiba871702016-08-22 21:06:02 +090069 * IP address reader class.
Jian Li115d8602016-08-15 20:21:53 +090070 */
71 public static class IpAddressReader implements LispAddressReader<LispIpAddress> {
72
73 @Override
Jian Li3a99e712016-12-16 21:23:16 +090074 public LispIpAddress readFrom(ByteBuf byteBuf)
75 throws LispParseError, LispReaderException {
Jian Li115d8602016-08-15 20:21:53 +090076
77 // AFI code -> 16 bits
78 short afiCode = (short) byteBuf.readUnsignedShort();
79
80 if (afiCode == 1) {
81 return new LispIpv4Address.Ipv4AddressReader().readFrom(byteBuf);
82 } else if (afiCode == 2) {
83 return new LispIpv6Address.Ipv6AddressReader().readFrom(byteBuf);
84 }
85
86 return null;
87 }
88 }
GUNiba871702016-08-22 21:06:02 +090089
90 /**
91 * IP address writer class.
92 */
93 public static class IpAddressWriter implements LispAddressWriter<LispIpAddress> {
94
95 @Override
Jian Li3a99e712016-12-16 21:23:16 +090096 public void writeTo(ByteBuf byteBuf, LispIpAddress address)
97 throws LispWriterException {
GUNiba871702016-08-22 21:06:02 +090098 if (address.getAddress().isIp4()) {
Jian Li3a99e712016-12-16 21:23:16 +090099 new Ipv4AddressWriter().writeTo(byteBuf, (LispIpv4Address) address);
GUNiba871702016-08-22 21:06:02 +0900100 }
101 if (address.getAddress().isIp6()) {
Jian Li3a99e712016-12-16 21:23:16 +0900102 new Ipv6AddressWriter().writeTo(byteBuf, (LispIpv6Address) address);
GUNiba871702016-08-22 21:06:02 +0900103 }
104 }
105 }
Jian Li6b77dc02016-07-14 16:04:25 +0900106}