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