blob: 08326a5bda0f3d43741c463d158de487111de08d [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 Li5e505c62016-12-05 02:44:24 +090023import org.onosproject.lisp.msg.types.LispIpv4Address.Ipv4AddressWriter;
24import org.onosproject.lisp.msg.types.LispIpv6Address.Ipv6AddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090025
Jian Lic7e20a52016-07-18 19:03:49 +090026import java.util.List;
27import java.util.Objects;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
Jian Lid4e63702016-08-30 18:29:20 +090030import static com.google.common.base.Preconditions.checkArgument;
Jian Lic7e20a52016-07-18 19:03:49 +090031
32/**
33 * List type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090034 * <p>
Jian Li89c9ca92016-11-11 04:09:02 +090035 * List type is defined in draft-ietf-lisp-lcaf-20
36 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20#page-22
Jian Lic7e20a52016-07-18 19:03:49 +090037 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090038 * <pre>
39 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090040 * 0 1 2 3
41 * 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
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | AFI = 16387 | Rsvd1 | Flags |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li89c9ca92016-11-11 04:09:02 +090045 * | Type = 1 | Rsvd2 | Length |
Jian Lic7e20a52016-07-18 19:03:49 +090046 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | AFI = 1 | IPv4 Address ... |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | ... IPv4 Address | AFI = 2 |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | IPv6 Address ... |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | ... IPv6 Address ... |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | ... IPv6 Address ... |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | ... IPv6 Address |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090059 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090060 */
Jian Li115d8602016-08-15 20:21:53 +090061public final class LispListLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090062
Jian Lia7b394d2016-08-21 23:11:46 +090063 private static final short LENGTH = 24;
Jian Lic7e20a52016-07-18 19:03:49 +090064 List<LispAfiAddress> addresses;
65
66 /**
67 * Initializes list type LCAF address.
68 *
69 * @param addresses a set of IPv4 and IPv6 addresses
70 */
71 public LispListLcafAddress(List<LispAfiAddress> addresses) {
72 super(LispCanonicalAddressFormatEnum.LIST, LENGTH);
Jian Lid4e63702016-08-30 18:29:20 +090073
74 checkArgument(checkAddressValidity(addresses), "Malformed addresses, please " +
75 "specify IPv4 address first, and then specify IPv6 address");
76
Jian Lie9af3b42016-08-08 15:50:01 +090077 this.addresses = addresses;
Jian Lic7e20a52016-07-18 19:03:49 +090078 }
79
80 /**
Jian Lia7b394d2016-08-21 23:11:46 +090081 * Initializes list type LCAF address.
82 *
83 * @param reserved1 reserved1 field
Jian Li89c9ca92016-11-11 04:09:02 +090084 * @param flag flag
Jian Lia7b394d2016-08-21 23:11:46 +090085 * @param reserved2 reserved2 field
86 * @param addresses a set of IPv4 and IPv6 addresses
87 */
88 public LispListLcafAddress(byte reserved1, byte reserved2, byte flag,
89 List<LispAfiAddress> addresses) {
90 super(LispCanonicalAddressFormatEnum.LIST, reserved1, flag, reserved2, LENGTH);
Jian Lid4e63702016-08-30 18:29:20 +090091
92 checkArgument(checkAddressValidity(addresses), "Malformed addresses, please " +
93 "specify IPv4 address first, and then specify IPv6 address");
94
Jian Lia7b394d2016-08-21 23:11:46 +090095 this.addresses = addresses;
96 }
97
98 /**
Jian Lid4e63702016-08-30 18:29:20 +090099 * Checks the validity of a collection of input addresses.
100 *
101 * @param addresses a collection of addresses
102 * @return validity result
103 */
104 private boolean checkAddressValidity(List<LispAfiAddress> addresses) {
105 // we need to make sure that the address collection is comprised of
106 // one IPv4 address and one IPv6 address
107
108 if (addresses == null || addresses.size() != 2) {
109 return false;
110 }
111
112 LispAfiAddress ipv4 = addresses.get(0);
113 LispAfiAddress ipv6 = addresses.get(1);
114
115 if (ipv4.getAfi() != AddressFamilyIdentifierEnum.IP4) {
116 return false;
117 }
118
119 if (ipv6.getAfi() != AddressFamilyIdentifierEnum.IP6) {
120 return false;
121 }
122
123 return true;
124 }
125
126 /**
Jian Lic7e20a52016-07-18 19:03:49 +0900127 * Obtains a set of AFI addresses including IPv4 and IPv6.
128 *
129 * @return a set of AFI addresses
130 */
131 public List<LispAfiAddress> getAddresses() {
Jian Lie9af3b42016-08-08 15:50:01 +0900132 return ImmutableList.copyOf(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +0900133 }
134
135 @Override
136 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +0900137 return Objects.hash(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +0900138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
145
146 if (obj instanceof LispListLcafAddress) {
147 final LispListLcafAddress other = (LispListLcafAddress) obj;
148 return Objects.equals(this.addresses, other.addresses);
149 }
150 return false;
151 }
152
153 @Override
154 public String toString() {
155 return toStringHelper(this)
156 .add("addresses", addresses)
157 .toString();
158 }
Jian Li115d8602016-08-15 20:21:53 +0900159
160 /**
161 * List LCAF address reader class.
162 */
163 public static class ListLcafAddressReader implements LispAddressReader<LispListLcafAddress> {
164
165 @Override
166 public LispListLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
167
Jian Lia7b394d2016-08-21 23:11:46 +0900168 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900169
Jian Li76ea0572016-08-29 12:41:16 +0900170 AfiAddressReader reader = new AfiAddressReader();
171
172 LispAfiAddress ipv4 = reader.readFrom(byteBuf);
173 LispAfiAddress ipv6 = reader.readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900174
Jian Lia7b394d2016-08-21 23:11:46 +0900175 return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(),
Jian Li89c9ca92016-11-11 04:09:02 +0900176 lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6));
Jian Li115d8602016-08-15 20:21:53 +0900177 }
178 }
GUNiba871702016-08-22 21:06:02 +0900179
180 /**
181 * List LCAF address writer class.
182 */
183 public static class ListLcafAddressWriter implements LispAddressWriter<LispListLcafAddress> {
184
Jian Li76ea0572016-08-29 12:41:16 +0900185 private static final int IPV4_ADDRESS_INDEX = 0;
186 private static final int IPV6_ADDRESS_INDEX = 1;
187
GUNiba871702016-08-22 21:06:02 +0900188 @Override
189 public void writeTo(ByteBuf byteBuf, LispListLcafAddress address) throws LispWriterException {
190
Jian Lief0f7232016-11-15 19:55:46 +0900191 int lcafIndex = byteBuf.writerIndex();
GUNiba871702016-08-22 21:06:02 +0900192 LispLcafAddress.serializeCommon(byteBuf, address);
193
Jian Li5e505c62016-12-05 02:44:24 +0900194 Ipv4AddressWriter v4Writer = new Ipv4AddressWriter();
195 Ipv6AddressWriter v6Writer = new Ipv6AddressWriter();
GUNiba871702016-08-22 21:06:02 +0900196
Jian Li76ea0572016-08-29 12:41:16 +0900197 LispAfiAddress ipv4 = address.getAddresses().get(IPV4_ADDRESS_INDEX);
198 LispAfiAddress ipv6 = address.getAddresses().get(IPV6_ADDRESS_INDEX);
199
200 // IPv4 address
201 byteBuf.writeShort(ipv4.getAfi().getIanaCode());
202 v4Writer.writeTo(byteBuf, (LispIpv4Address) ipv4);
203
204 // IPv6 address
205 byteBuf.writeShort(ipv6.getAfi().getIanaCode());
206 v6Writer.writeTo(byteBuf, (LispIpv6Address) ipv6);
Jian Lief0f7232016-11-15 19:55:46 +0900207
208 LispLcafAddress.updateLength(lcafIndex, byteBuf);
GUNiba871702016-08-22 21:06:02 +0900209 }
210 }
Jian Lic7e20a52016-07-18 19:03:49 +0900211}