blob: d97537e8ae6c870b230947296f467e7076090b45 [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
Priyanka B02040732015-11-29 11:30:29 +053018import java.nio.ByteBuffer;
Satish Kd7836052015-11-21 21:46:32 +053019import java.util.Arrays;
Priyanka Bb2988fa2015-10-09 12:45:36 +053020import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.bgpio.util.Validation;
Priyanka Bb2988fa2015-10-09 12:45:36 +053025
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides IP Reachability InformationTlv Tlv which contains IP Prefix.
30 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031public class IPReachabilityInformationTlv implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053032
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
Priyanka Bb2988fa2015-10-09 12:45:36 +053047 public static final short TYPE = 265;
48 public static final int ONE_BYTE_LEN = 8;
Priyanka B02040732015-11-29 11:30:29 +053049
Priyanka Bb2988fa2015-10-09 12:45:36 +053050 private byte prefixLen;
51 private byte[] ipPrefix;
52 public short length;
53
54 /**
55 * Constructor to initialize parameters.
56 *
57 * @param prefixLen length of IP Prefix
58 * @param ipPrefix IP Prefix
59 * @param length length of value field
60 */
61 public IPReachabilityInformationTlv(byte prefixLen, byte[] ipPrefix, short length) {
62 this.ipPrefix = ipPrefix;
63 this.prefixLen = prefixLen;
64 this.length = length;
65 }
66
67 /**
68 * Returns IP Prefix.
69 *
70 * @return IP Prefix
71 */
72 public IpPrefix getPrefixValue() {
73 IpPrefix prefix = Validation.bytesToPrefix(ipPrefix, prefixLen);
74 return prefix;
75 }
76
77 /**
78 * Returns IP Prefix length.
79 *
80 * @return IP Prefix length
81 */
82 public byte getPrefixLen() {
83 return this.prefixLen;
84 }
85
86 @Override
87 public int hashCode() {
Satish Kd7836052015-11-21 21:46:32 +053088 return Objects.hash(Arrays.hashCode(ipPrefix), prefixLen);
Priyanka Bb2988fa2015-10-09 12:45:36 +053089 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
96
97 if (obj instanceof IPReachabilityInformationTlv) {
98 IPReachabilityInformationTlv other = (IPReachabilityInformationTlv) obj;
Satish Kd7836052015-11-21 21:46:32 +053099 return Objects.equals(prefixLen, other.prefixLen) && Arrays.equals(ipPrefix, other.ipPrefix);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530100 }
101 return false;
102 }
103
104 @Override
105 public int write(ChannelBuffer cb) {
106 int iLenStartIndex = cb.writerIndex();
107 cb.writeShort(TYPE);
108 cb.writeShort(length);
109 cb.writeByte(prefixLen);
110 cb.writeBytes(ipPrefix);
111 return cb.writerIndex() - iLenStartIndex;
112 }
113
114 /**
115 * Reads the channel buffer and returns object of IPReachabilityInformationTlv.
116 *
117 * @param cb ChannelBuffer
118 * @param length of value field
119 * @return object of IPReachabilityInformationTlv
120 */
121 public static IPReachabilityInformationTlv read(ChannelBuffer cb, short length) {
122 byte preficLen = cb.readByte();
123 byte[] prefix;
124 if (preficLen == 0) {
125 prefix = new byte[] {0};
126 } else {
127 int len = preficLen / ONE_BYTE_LEN;
128 int reminder = preficLen % ONE_BYTE_LEN;
129 if (reminder > 0) {
130 len = len + 1;
131 }
132 prefix = new byte[len];
133 cb.readBytes(prefix, 0, len);
134 }
135 return IPReachabilityInformationTlv.of(preficLen, prefix, length);
136 }
137
138 public static IPReachabilityInformationTlv of(final byte preficLen, final byte[] prefix, final short length) {
139 return new IPReachabilityInformationTlv(preficLen, prefix, length);
140 }
141 @Override
142 public short getType() {
143 return TYPE;
144 }
145
146 @Override
Priyanka B02040732015-11-29 11:30:29 +0530147 public int compareTo(Object o) {
148 if (this.equals(o)) {
149 return 0;
150 }
151 ByteBuffer value1 = ByteBuffer.wrap(this.ipPrefix);
152 ByteBuffer value2 = ByteBuffer.wrap(((IPReachabilityInformationTlv) o).ipPrefix);
153 return value1.compareTo(value2);
154 }
155
156 @Override
Priyanka Bb2988fa2015-10-09 12:45:36 +0530157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .add("Type", TYPE)
160 .add("Length", length)
161 .add("Prefixlength", getPrefixLen())
162 .add("Prefixvalue", getPrefixValue())
163 .toString();
164 }
165}