Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.types; |
| 17 | |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 21 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 23 | |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | |
| 29 | /** |
| 30 | * List type LCAF address class. |
Jian Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 31 | * <p> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 32 | * 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 Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 35 | * <pre> |
| 36 | * {@literal |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 37 | * 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 Li | 8fc2d2f | 2016-08-08 14:43:53 +0900 | [diff] [blame] | 56 | * }</pre> |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 57 | */ |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 58 | public final class LispListLcafAddress extends LispLcafAddress { |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 59 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 60 | private static final short LENGTH = 24; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 61 | 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 Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 70 | this.addresses = addresses; |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 74 | * 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 Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 88 | * 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 Li | e9af3b4 | 2016-08-08 15:50:01 +0900 | [diff] [blame] | 93 | return ImmutableList.copyOf(addresses); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
Jian Li | d56f97e | 2016-07-19 15:48:15 +0900 | [diff] [blame] | 98 | return Objects.hash(addresses); |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 99 | } |
| 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 Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 120 | |
| 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 Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 129 | LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 130 | |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 131 | AfiAddressReader reader = new AfiAddressReader(); |
| 132 | |
| 133 | LispAfiAddress ipv4 = reader.readFrom(byteBuf); |
| 134 | LispAfiAddress ipv6 = reader.readFrom(byteBuf); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 135 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 136 | return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(), |
| 137 | lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6)); |
Jian Li | 115d860 | 2016-08-15 20:21:53 +0900 | [diff] [blame] | 138 | } |
| 139 | } |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * List LCAF address writer class. |
| 143 | */ |
| 144 | public static class ListLcafAddressWriter implements LispAddressWriter<LispListLcafAddress> { |
| 145 | |
Jian Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 146 | private static final int IPV4_ADDRESS_INDEX = 0; |
| 147 | private static final int IPV6_ADDRESS_INDEX = 1; |
| 148 | |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 149 | @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 Li | 76ea057 | 2016-08-29 12:41:16 +0900 | [diff] [blame] | 157 | 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); |
GUNi | ba87170 | 2016-08-22 21:06:02 +0900 | [diff] [blame] | 167 | } |
| 168 | } |
Jian Li | c7e20a5 | 2016-07-18 19:03:49 +0900 | [diff] [blame] | 169 | } |