blob: 68834db1584c8a50ff3ec3b28cd222349f65c8d9 [file] [log] [blame]
Jian Lic7e20a52016-07-18 19:03:49 +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 Lie9af3b42016-08-08 15:50:01 +090018import com.google.common.collect.ImmutableList;
Jian Li115d8602016-08-15 20:21:53 +090019import io.netty.buffer.ByteBuf;
20import 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 Lie9af3b42016-08-08 15:50:01 +090023
Jian Lic7e20a52016-07-18 19:03:49 +090024import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28
29/**
30 * List type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090031 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090032 * List type is defined in draft-ietf-lisp-lcaf-13
33 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-21
34 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090035 * <pre>
36 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090037 * 0 1 2 3
38 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | AFI = 16387 | Rsvd1 | Flags |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Type = 1 | Rsvd2 | 2 + 4 + 2 + 16 |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | AFI = 1 | IPv4 Address ... |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | ... IPv4 Address | AFI = 2 |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | IPv6 Address ... |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | ... IPv6 Address ... |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | ... IPv6 Address ... |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | ... IPv6 Address |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090056 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090057 */
Jian Li115d8602016-08-15 20:21:53 +090058public final class LispListLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090059
Jian Lia7b394d2016-08-21 23:11:46 +090060 private static final short LENGTH = 24;
Jian Lic7e20a52016-07-18 19:03:49 +090061 List<LispAfiAddress> addresses;
62
63 /**
64 * Initializes list type LCAF address.
65 *
66 * @param addresses a set of IPv4 and IPv6 addresses
67 */
68 public LispListLcafAddress(List<LispAfiAddress> addresses) {
69 super(LispCanonicalAddressFormatEnum.LIST, LENGTH);
Jian Lie9af3b42016-08-08 15:50:01 +090070 this.addresses = addresses;
Jian Lic7e20a52016-07-18 19:03:49 +090071 }
72
73 /**
Jian Lia7b394d2016-08-21 23:11:46 +090074 * Initializes list type LCAF address.
75 *
76 * @param reserved1 reserved1 field
77 * @param flag flag
78 * @param reserved2 reserved2 field
79 * @param addresses a set of IPv4 and IPv6 addresses
80 */
81 public LispListLcafAddress(byte reserved1, byte reserved2, byte flag,
82 List<LispAfiAddress> addresses) {
83 super(LispCanonicalAddressFormatEnum.LIST, reserved1, flag, reserved2, LENGTH);
84 this.addresses = addresses;
85 }
86
87 /**
Jian Lic7e20a52016-07-18 19:03:49 +090088 * Obtains a set of AFI addresses including IPv4 and IPv6.
89 *
90 * @return a set of AFI addresses
91 */
92 public List<LispAfiAddress> getAddresses() {
Jian Lie9af3b42016-08-08 15:50:01 +090093 return ImmutableList.copyOf(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090094 }
95
96 @Override
97 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090098 return Objects.hash(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090099 }
100
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj) {
104 return true;
105 }
106
107 if (obj instanceof LispListLcafAddress) {
108 final LispListLcafAddress other = (LispListLcafAddress) obj;
109 return Objects.equals(this.addresses, other.addresses);
110 }
111 return false;
112 }
113
114 @Override
115 public String toString() {
116 return toStringHelper(this)
117 .add("addresses", addresses)
118 .toString();
119 }
Jian Li115d8602016-08-15 20:21:53 +0900120
121 /**
122 * List LCAF address reader class.
123 */
124 public static class ListLcafAddressReader implements LispAddressReader<LispListLcafAddress> {
125
126 @Override
127 public LispListLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
128
Jian Lia7b394d2016-08-21 23:11:46 +0900129 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900130
Jian Li76ea0572016-08-29 12:41:16 +0900131 AfiAddressReader reader = new AfiAddressReader();
132
133 LispAfiAddress ipv4 = reader.readFrom(byteBuf);
134 LispAfiAddress ipv6 = reader.readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900135
Jian Lia7b394d2016-08-21 23:11:46 +0900136 return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(),
137 lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6));
Jian Li115d8602016-08-15 20:21:53 +0900138 }
139 }
GUNiba871702016-08-22 21:06:02 +0900140
141 /**
142 * List LCAF address writer class.
143 */
144 public static class ListLcafAddressWriter implements LispAddressWriter<LispListLcafAddress> {
145
Jian Li76ea0572016-08-29 12:41:16 +0900146 private static final int IPV4_ADDRESS_INDEX = 0;
147 private static final int IPV6_ADDRESS_INDEX = 1;
148
GUNiba871702016-08-22 21:06:02 +0900149 @Override
150 public void writeTo(ByteBuf byteBuf, LispListLcafAddress address) throws LispWriterException {
151
152 LispLcafAddress.serializeCommon(byteBuf, address);
153
154 LispIpv4Address.Ipv4AddressWriter v4Writer = new LispIpv4Address.Ipv4AddressWriter();
155 LispIpv6Address.Ipv6AddressWriter v6Writer = new LispIpv6Address.Ipv6AddressWriter();
156
Jian Li76ea0572016-08-29 12:41:16 +0900157 LispAfiAddress ipv4 = address.getAddresses().get(IPV4_ADDRESS_INDEX);
158 LispAfiAddress ipv6 = address.getAddresses().get(IPV6_ADDRESS_INDEX);
159
160 // IPv4 address
161 byteBuf.writeShort(ipv4.getAfi().getIanaCode());
162 v4Writer.writeTo(byteBuf, (LispIpv4Address) ipv4);
163
164 // IPv6 address
165 byteBuf.writeShort(ipv6.getAfi().getIanaCode());
166 v6Writer.writeTo(byteBuf, (LispIpv6Address) ipv6);
GUNiba871702016-08-22 21:06:02 +0900167 }
168 }
Jian Lic7e20a52016-07-18 19:03:49 +0900169}