blob: 3552b45351c35b56f8ffef4d59e9f144ffb77718 [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 Lia7b394d2016-08-21 23:11:46 +0900131 LispAfiAddress ipv4 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
132 LispAfiAddress ipv6 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900133
Jian Lia7b394d2016-08-21 23:11:46 +0900134 return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(),
135 lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6));
Jian Li115d8602016-08-15 20:21:53 +0900136 }
137 }
GUNiba871702016-08-22 21:06:02 +0900138
139 /**
140 * List LCAF address writer class.
141 */
142 public static class ListLcafAddressWriter implements LispAddressWriter<LispListLcafAddress> {
143
144 @Override
145 public void writeTo(ByteBuf byteBuf, LispListLcafAddress address) throws LispWriterException {
146
147 LispLcafAddress.serializeCommon(byteBuf, address);
148
149 LispIpv4Address.Ipv4AddressWriter v4Writer = new LispIpv4Address.Ipv4AddressWriter();
150 LispIpv6Address.Ipv6AddressWriter v6Writer = new LispIpv6Address.Ipv6AddressWriter();
151
152 v4Writer.writeTo(byteBuf, (LispIpv4Address) address.getAddresses().get(0));
153 v6Writer.writeTo(byteBuf, (LispIpv6Address) address.getAddresses().get(1));
154 }
155 }
Jian Lic7e20a52016-07-18 19:03:49 +0900156}