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