blob: fd331195cbe10b08488ce26926278db6baaf7539 [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;
Jian Lie9af3b42016-08-08 15:50:01 +090022
Jian Lic7e20a52016-07-18 19:03:49 +090023import java.util.List;
24import java.util.Objects;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27
28/**
29 * List type LCAF address class.
Jian Li8fc2d2f2016-08-08 14:43:53 +090030 * <p>
Jian Lic7e20a52016-07-18 19:03:49 +090031 * List type is defined in draft-ietf-lisp-lcaf-13
32 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-21
33 *
Jian Li8fc2d2f2016-08-08 14:43:53 +090034 * <pre>
35 * {@literal
Jian Lic7e20a52016-07-18 19:03:49 +090036 * 0 1 2 3
37 * 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
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | AFI = 16387 | Rsvd1 | Flags |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Type = 1 | Rsvd2 | 2 + 4 + 2 + 16 |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | AFI = 1 | IPv4 Address ... |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | ... IPv4 Address | AFI = 2 |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | IPv6 Address ... |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | ... IPv6 Address ... |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | ... IPv6 Address ... |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | ... IPv6 Address |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jian Li8fc2d2f2016-08-08 14:43:53 +090055 * }</pre>
Jian Lic7e20a52016-07-18 19:03:49 +090056 */
Jian Li115d8602016-08-15 20:21:53 +090057public final class LispListLcafAddress extends LispLcafAddress {
Jian Lic7e20a52016-07-18 19:03:49 +090058
Jian Lia7b394d2016-08-21 23:11:46 +090059 private static final short LENGTH = 24;
Jian Lic7e20a52016-07-18 19:03:49 +090060 List<LispAfiAddress> addresses;
61
62 /**
63 * Initializes list type LCAF address.
64 *
65 * @param addresses a set of IPv4 and IPv6 addresses
66 */
67 public LispListLcafAddress(List<LispAfiAddress> addresses) {
68 super(LispCanonicalAddressFormatEnum.LIST, LENGTH);
Jian Lie9af3b42016-08-08 15:50:01 +090069 this.addresses = addresses;
Jian Lic7e20a52016-07-18 19:03:49 +090070 }
71
72 /**
Jian Lia7b394d2016-08-21 23:11:46 +090073 * Initializes list type LCAF address.
74 *
75 * @param reserved1 reserved1 field
76 * @param flag flag
77 * @param reserved2 reserved2 field
78 * @param addresses a set of IPv4 and IPv6 addresses
79 */
80 public LispListLcafAddress(byte reserved1, byte reserved2, byte flag,
81 List<LispAfiAddress> addresses) {
82 super(LispCanonicalAddressFormatEnum.LIST, reserved1, flag, reserved2, LENGTH);
83 this.addresses = addresses;
84 }
85
86 /**
Jian Lic7e20a52016-07-18 19:03:49 +090087 * Obtains a set of AFI addresses including IPv4 and IPv6.
88 *
89 * @return a set of AFI addresses
90 */
91 public List<LispAfiAddress> getAddresses() {
Jian Lie9af3b42016-08-08 15:50:01 +090092 return ImmutableList.copyOf(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090093 }
94
95 @Override
96 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090097 return Objects.hash(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090098 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105
106 if (obj instanceof LispListLcafAddress) {
107 final LispListLcafAddress other = (LispListLcafAddress) obj;
108 return Objects.equals(this.addresses, other.addresses);
109 }
110 return false;
111 }
112
113 @Override
114 public String toString() {
115 return toStringHelper(this)
116 .add("addresses", addresses)
117 .toString();
118 }
Jian Li115d8602016-08-15 20:21:53 +0900119
120 /**
121 * List LCAF address reader class.
122 */
123 public static class ListLcafAddressReader implements LispAddressReader<LispListLcafAddress> {
124
125 @Override
126 public LispListLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
127
Jian Lia7b394d2016-08-21 23:11:46 +0900128 LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900129
Jian Lia7b394d2016-08-21 23:11:46 +0900130 LispAfiAddress ipv4 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
131 LispAfiAddress ipv6 = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);
Jian Li115d8602016-08-15 20:21:53 +0900132
Jian Lia7b394d2016-08-21 23:11:46 +0900133 return new LispListLcafAddress(lcafAddress.getReserved1(), lcafAddress.getReserved2(),
134 lcafAddress.getFlag(), ImmutableList.of(ipv4, ipv6));
Jian Li115d8602016-08-15 20:21:53 +0900135 }
136 }
Jian Lic7e20a52016-07-18 19:03:49 +0900137}