blob: 2077059d9a11d96bdbc1347695da28f2e87870f6 [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
59 private static final byte LENGTH = 24;
60 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 /**
73 * Obtains a set of AFI addresses including IPv4 and IPv6.
74 *
75 * @return a set of AFI addresses
76 */
77 public List<LispAfiAddress> getAddresses() {
Jian Lie9af3b42016-08-08 15:50:01 +090078 return ImmutableList.copyOf(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090079 }
80
81 @Override
82 public int hashCode() {
Jian Lid56f97e2016-07-19 15:48:15 +090083 return Objects.hash(addresses);
Jian Lic7e20a52016-07-18 19:03:49 +090084 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (this == obj) {
89 return true;
90 }
91
92 if (obj instanceof LispListLcafAddress) {
93 final LispListLcafAddress other = (LispListLcafAddress) obj;
94 return Objects.equals(this.addresses, other.addresses);
95 }
96 return false;
97 }
98
99 @Override
100 public String toString() {
101 return toStringHelper(this)
102 .add("addresses", addresses)
103 .toString();
104 }
Jian Li115d8602016-08-15 20:21:53 +0900105
106 /**
107 * List LCAF address reader class.
108 */
109 public static class ListLcafAddressReader implements LispAddressReader<LispListLcafAddress> {
110
111 @Override
112 public LispListLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {
113
114
115 LispAfiAddress ipv4 = new LispIpAddress.IpAddressReader().readFrom(byteBuf);
116 LispAfiAddress ipv6 = new LispIpAddress.IpAddressReader().readFrom(byteBuf);
117
118 return new LispListLcafAddress(ImmutableList.of(ipv4, ipv6));
119 }
120 }
Jian Lic7e20a52016-07-18 19:03:49 +0900121}