2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.types;
18 import java.nio.ByteBuffer;
19 import java.util.Arrays;
20 import java.util.Objects;
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.onlab.packet.IpPrefix;
24 import org.onosproject.bgpio.util.Validation;
26 import com.google.common.base.MoreObjects;
29 * Provides IP Reachability InformationTlv Tlv which contains IP Prefix.
31 public class IPReachabilityInformationTlv implements BgpValueType {
34 * Reference :draft-ietf-idr-ls-distribution-11
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Prefix Length | IP Prefix (variable) //
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 Figure 14: IP Reachability Information TLV Format
47 public static final short TYPE = 265;
48 public static final int ONE_BYTE_LEN = 8;
50 private byte prefixLen;
51 private byte[] ipPrefix;
55 * Constructor to initialize parameters.
57 * @param prefixLen length of IP Prefix
58 * @param ipPrefix IP Prefix
59 * @param length length of value field
61 public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) {
62 this.ipPrefix = ipPrefix;
63 this.prefixLen = prefixLen;
72 public IpPrefix getPrefixValue() {
73 IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen);
78 * Returns IP Prefix length.
80 * @return IP Prefix length
82 public byte getPrefixLen() {
83 return this.prefixLen;
87 public int hashCode() {
88 return Objects.hash(Arrays.hashCode(ipPrefix), prefixLen);
92 public boolean equals(Object obj) {
97 if (obj instanceof IPReachabilityInformationTlv) {
98 IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj;
99 return Objects.equals(prefixLen, other.prefixLen) && Arrays.equals(ipPrefix, other.ipPrefix);
105 public int write(ChannelBuffer cb) {
106 int iLenStartIndex = cb.writerIndex();
108 cb.writeShort(length);
109 cb.writeByte(prefixLen);
110 cb.writeBytes(ipPrefix);
111 return cb.writerIndex() - iLenStartIndex;
115 * Reads the channel buffer and returns object of IPReachabilityInformationTlv.
117 * @param cb ChannelBuffer
118 * @param length of value field
119 * @return object of IPReachabilityInformationTlv
121 public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) {
122 byte preficLen = cb.readByte();
124 if (preficLen == 0) {
125 prefix = new byte[] {0};
127 int len = preficLen / ONE_BYTE_LEN;
128 int reminder = preficLen % ONE_BYTE_LEN;
132 prefix = new byte[len];
133 cb.readBytes(prefix, 0, len);
135 return IPReachabilityInformationTlv.of(preficLen, prefix, length);
138 public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) {
139 return new IPReachabilityInformationTlv(preficLen, prefix, length);
142 public short getType() {
147 public int compareTo(Object o) {
148 if (this.equals(o)) {
151 ByteBuffer value1 = ByteBuffer.wrap(this.ipPrefix);
152 ByteBuffer value2 = ByteBuffer.wrap(((IPReachabilityInformationTlv) o).ipPrefix);
153 return value1.compareTo(value2);
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
160 .add("Length", length)
161 .add("Prefixlength", getPrefixLen())
162 .add("Prefixvalue", getPrefixValue())