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.util.Arrays;
19 import java.util.Objects;
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onlab.packet.IpPrefix;
23 import org.onosproject.bgpio.util.Validation;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.MoreObjects;
30 * Provides IP Reachability InformationTlv Tlv which contains IP Prefix.
32 public class IPReachabilityInformationTlv implements BGPValueType {
35 * Reference :draft-ietf-idr-ls-distribution-11
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Prefix Length | IP Prefix (variable) //
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 Figure 14: IP Reachability Information TLV Format
48 protected static final Logger log = LoggerFactory.getLogger(IPReachabilityInformationTlv.class);
50 public static final short TYPE = 265;
51 public static final int ONE_BYTE_LEN = 8;
52 private byte prefixLen;
53 private byte[] ipPrefix;
57 * Constructor to initialize parameters.
59 * @param prefixLen length of IP Prefix
60 * @param ipPrefix IP Prefix
61 * @param length length of value field
63 public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) {
64 this.ipPrefix = ipPrefix;
65 this.prefixLen = prefixLen;
74 public IpPrefix getPrefixValue() {
75 IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen);
80 * Returns IP Prefix length.
82 * @return IP Prefix length
84 public byte getPrefixLen() {
85 return this.prefixLen;
89 public int hashCode() {
90 return Objects.hash(Arrays.hashCode(ipPrefix), prefixLen);
94 public boolean equals(Object obj) {
99 if (obj instanceof IPReachabilityInformationTlv) {
100 IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj;
101 return Objects.equals(prefixLen, other.prefixLen) && Arrays.equals(ipPrefix, other.ipPrefix);
107 public int write(ChannelBuffer cb) {
108 int iLenStartIndex = cb.writerIndex();
110 cb.writeShort(length);
111 cb.writeByte(prefixLen);
112 cb.writeBytes(ipPrefix);
113 return cb.writerIndex() - iLenStartIndex;
117 * Reads the channel buffer and returns object of IPReachabilityInformationTlv.
119 * @param cb ChannelBuffer
120 * @param length of value field
121 * @return object of IPReachabilityInformationTlv
123 public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) {
124 byte preficLen = cb.readByte();
126 if (preficLen == 0) {
127 prefix = new byte[] {0};
129 int len = preficLen / ONE_BYTE_LEN;
130 int reminder = preficLen % ONE_BYTE_LEN;
134 prefix = new byte[len];
135 cb.readBytes(prefix, 0, len);
137 return IPReachabilityInformationTlv.of(preficLen, prefix, length);
140 public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) {
141 return new IPReachabilityInformationTlv(preficLen, prefix, length);
144 public short getType() {
149 public String toString() {
150 return MoreObjects.toStringHelper(getClass())
152 .add("Length", length)
153 .add("Prefixlength", getPrefixLen())
154 .add("Prefixvalue", getPrefixValue())