blob: 85fb748b4ebeedb963404534bf7c959b1c980e88 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
2 * Copyright 2015 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.bgpio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.IpPrefix;
22import org.onosproject.bgpio.util.Validation;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides IP Reachability InformationTlv Tlv which contains IP Prefix.
30 */
31public class IPReachabilityInformationTlv implements BGPValueType {
32
33 /*
34 * Reference :draft-ietf-idr-ls-distribution-11
35
36 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 | Type | Length |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Prefix Length | IP Prefix (variable) //
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
44 Figure 14: IP Reachability Information TLV Format
45 */
46
47 protected static final Logger log = LoggerFactory.getLogger(IPReachabilityInformationTlv.class);
48
49 public static final short TYPE = 265;
50 public static final int ONE_BYTE_LEN = 8;
51 private byte prefixLen;
52 private byte[] ipPrefix;
53 public short length;
54
55 /**
56 * Constructor to initialize parameters.
57 *
58 * @param prefixLen length of IP Prefix
59 * @param ipPrefix IP Prefix
60 * @param length length of value field
61 */
62 public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) {
63 this.ipPrefix = ipPrefix;
64 this.prefixLen = prefixLen;
65 this.length = length;
66 }
67
68 /**
69 * Returns IP Prefix.
70 *
71 * @return IP Prefix
72 */
73 public IpPrefix getPrefixValue() {
74 IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen);
75 return prefix;
76 }
77
78 /**
79 * Returns IP Prefix length.
80 *
81 * @return IP Prefix length
82 */
83 public byte getPrefixLen() {
84 return this.prefixLen;
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(ipPrefix, prefixLen);
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97
98 if (obj instanceof IPReachabilityInformationTlv) {
99 IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj;
100 return Objects.equals(prefixLen, other.prefixLen) && Objects.equals(ipPrefix, other.ipPrefix);
101 }
102 return false;
103 }
104
105 @Override
106 public int write(ChannelBuffer cb) {
107 int iLenStartIndex = cb.writerIndex();
108 cb.writeShort(TYPE);
109 cb.writeShort(length);
110 cb.writeByte(prefixLen);
111 cb.writeBytes(ipPrefix);
112 return cb.writerIndex() - iLenStartIndex;
113 }
114
115 /**
116 * Reads the channel buffer and returns object of IPReachabilityInformationTlv.
117 *
118 * @param cb ChannelBuffer
119 * @param length of value field
120 * @return object of IPReachabilityInformationTlv
121 */
122 public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) {
123 byte preficLen = cb.readByte();
124 byte[] prefix;
125 if (preficLen == 0) {
126 prefix = new byte[] {0};
127 } else {
128 int len = preficLen / ONE_BYTE_LEN;
129 int reminder = preficLen % ONE_BYTE_LEN;
130 if (reminder > 0) {
131 len = len + 1;
132 }
133 prefix = new byte[len];
134 cb.readBytes(prefix, 0, len);
135 }
136 return IPReachabilityInformationTlv.of(preficLen, prefix, length);
137 }
138
139 public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) {
140 return new IPReachabilityInformationTlv(preficLen, prefix, length);
141 }
142 @Override
143 public short getType() {
144 return TYPE;
145 }
146
147 @Override
148 public String toString() {
149 return MoreObjects.toStringHelper(getClass())
150 .add("Type", TYPE)
151 .add("Length", length)
152 .add("Prefixlength", getPrefixLen())
153 .add("Prefixvalue", getPrefixValue())
154 .toString();
155 }
156}