sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.isis.io.isispacket.tlv; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.primitives.Bytes; |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder; |
| 23 | import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes; |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 24 | import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 25 | import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv; |
| 26 | import org.onosproject.isis.io.util.IsisUtil; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Representation of IP extended reachability TLV. |
| 33 | */ |
| 34 | public class IpExtendedReachabilityTlv extends TlvHeader implements IsisTlv { |
| 35 | |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 36 | private boolean down; |
| 37 | private boolean subTlvPresence; |
| 38 | private int prefixLength; |
| 39 | private int metric; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 40 | private byte subTlvLength; |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 41 | private String prefix; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 42 | private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>(); |
| 43 | |
| 44 | /** |
| 45 | * Creates an instance of IP external reachability TLV. |
| 46 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 47 | * @param tlvHeader TLV header |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 48 | */ |
| 49 | public IpExtendedReachabilityTlv(TlvHeader tlvHeader) { |
| 50 | this.setTlvType(tlvHeader.tlvType()); |
| 51 | this.setTlvLength(tlvHeader.tlvLength()); |
| 52 | } |
| 53 | |
| 54 | /** |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame^] | 55 | * Returns list of traffic engineering sub tlvs. |
| 56 | * |
| 57 | * @return trafEnginSubTlv |
| 58 | */ |
| 59 | public List<TrafficEngineeringSubTlv> teTlvs() { |
| 60 | return this.trafEnginSubTlv; |
| 61 | } |
| 62 | |
| 63 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 64 | * Returns the prefix of IP external reachability TLV. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 65 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 66 | * @return prefix |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 67 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 68 | public String prefix() { |
| 69 | return prefix; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 73 | * Sets the prefix of IP external reachability TLV. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 74 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 75 | * @param prefix prefix |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 76 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 77 | public void setPrefix(String prefix) { |
| 78 | this.prefix = prefix; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns if down true else false of IP external reachability TLV. |
| 83 | * |
| 84 | * @return if down true else false |
| 85 | */ |
| 86 | public boolean isDown() { |
| 87 | return down; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets if down true else false of IP external reachability TLV. |
| 92 | * |
| 93 | * @param upOrDown if down true else false |
| 94 | */ |
| 95 | public void setDown(boolean upOrDown) { |
| 96 | this.down = upOrDown; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns true if sub TLV present else false of IP external reachability TLV. |
| 101 | * |
| 102 | * @return true if present else false |
| 103 | */ |
| 104 | public boolean isSubTlvPresence() { |
| 105 | return subTlvPresence; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets true if sub TLV present else false of IP external reachability TLV. |
| 110 | * |
| 111 | * @param subTlvPresence true if present else false |
| 112 | */ |
| 113 | public void setSubTlvPresence(boolean subTlvPresence) { |
| 114 | this.subTlvPresence = subTlvPresence; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Sets the prefix length of IP external reachability TLV. |
| 119 | * |
| 120 | * @return prefix length |
| 121 | */ |
| 122 | public int prefixLength() { |
| 123 | return prefixLength; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the prefix length of IP external reachability TLV. |
| 128 | * |
| 129 | * @param prefixLength the prefix length of IP external reachability TLV |
| 130 | */ |
| 131 | public void setPrefixLength(int prefixLength) { |
| 132 | this.prefixLength = prefixLength; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds the traffic engineering sub TLV to IP external reachability TLV. |
| 137 | * |
| 138 | * @param trafEnginSubTlv traffic engineering sub TLV |
| 139 | */ |
| 140 | public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) { |
| 141 | this.trafEnginSubTlv.add(trafEnginSubTlv); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the sub TLV length of IP external reachability TLV. |
| 146 | * |
| 147 | * @return sub TLV length |
| 148 | */ |
| 149 | public byte subTlvLength() { |
| 150 | return subTlvLength; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the sub TLV length for IP external reachability TLV. |
| 155 | * |
| 156 | * @param subTlvLength sub TLV length |
| 157 | */ |
| 158 | public void setSubTlvLength(byte subTlvLength) { |
| 159 | this.subTlvLength = subTlvLength; |
| 160 | } |
| 161 | |
| 162 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 163 | * Returns metric of IP external reachability TLV. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 164 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 165 | * @return metric |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 166 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 167 | public int metric() { |
| 168 | return metric; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Sets default metric for IP external reachability TLV. |
| 173 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 174 | * @param metric default metric |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 175 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 176 | public void setMetric(int metric) { |
| 177 | this.metric = metric; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void readFrom(ChannelBuffer channelBuffer) { |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 182 | this.setMetric(channelBuffer.readInt()); |
| 183 | int controlInfo = channelBuffer.readByte(); |
| 184 | byte[] tempByteArray = null; |
| 185 | |
| 186 | String string = IsisUtil.toEightBitBinary(Integer.toBinaryString(controlInfo)); |
| 187 | if (string.charAt(0) == '0') { |
| 188 | this.setDown(false); |
| 189 | } |
| 190 | if (string.charAt(1) == '1') { |
| 191 | this.setSubTlvPresence(true); |
| 192 | } |
| 193 | this.setPrefixLength(Integer.parseInt(string.substring(2, string.length()), 2)); |
| 194 | if (this.prefixLength >= 0 && this.prefixLength <= 8) { |
| 195 | channelBuffer.readByte(); |
| 196 | } else if (this.prefixLength >= 8 && this.prefixLength <= 16) { |
| 197 | tempByteArray = new byte[IsisUtil.TWO_BYTES]; |
| 198 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.TWO_BYTES); |
| 199 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 200 | } else if (this.prefixLength >= 17 && this.prefixLength <= 24) { |
| 201 | tempByteArray = new byte[IsisUtil.THREE_BYTES]; |
| 202 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.THREE_BYTES); |
| 203 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 204 | } else if (this.prefixLength >= 24 && this.prefixLength <= 32) { |
| 205 | tempByteArray = new byte[IsisUtil.FOUR_BYTES]; |
| 206 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES); |
| 207 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 208 | } |
| 209 | if (this.isSubTlvPresence()) { |
| 210 | this.setSubTlvLength(channelBuffer.readByte()); |
| 211 | while (channelBuffer.readableBytes() > 0) { |
| 212 | TlvHeader tlvHeader = new TlvHeader(); |
| 213 | tlvHeader.setTlvType(channelBuffer.readByte()); |
| 214 | tlvHeader.setTlvLength(channelBuffer.readByte()); |
| 215 | SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType()); |
| 216 | if (tlvValue != null) { |
sunish vk | c3824e8 | 2016-05-11 19:38:24 +0530 | [diff] [blame] | 217 | TrafficEngineeringSubTlv subTlv = |
| 218 | SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength())); |
| 219 | if (subTlv != null) { |
| 220 | this.addSubTlv(subTlv); |
| 221 | } |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 222 | } else { |
| 223 | channelBuffer.readBytes(tlvHeader.tlvLength()); |
| 224 | } |
| 225 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 229 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 230 | @Override |
| 231 | public byte[] asBytes() { |
| 232 | byte[] bytes = null; |
| 233 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 234 | byte[] tlvBody = tlvBodyAsBytes(); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 235 | tlvHeader[1] = (byte) tlvBody.length; |
| 236 | bytes = Bytes.concat(tlvHeader, tlvBody); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 237 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 238 | return bytes; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Returns TLV body of IP external reachability TLV. |
| 243 | * |
| 244 | * @return byteArray TLV body of IP external reachability TLV. |
| 245 | */ |
| 246 | private byte[] tlvBodyAsBytes() { |
| 247 | List<Byte> bodyLst = new ArrayList<>(); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 248 | bodyLst.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.metric()))); |
| 249 | String controlInfo = ""; |
| 250 | if (this.isDown()) { |
| 251 | controlInfo = controlInfo + "1"; |
| 252 | } else { |
| 253 | controlInfo = controlInfo + "0"; |
| 254 | } |
| 255 | if (this.isSubTlvPresence()) { |
| 256 | controlInfo = controlInfo + "1"; |
| 257 | } else { |
| 258 | controlInfo = controlInfo + "0"; |
| 259 | } |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 260 | String prefixLength = IsisUtil.toEightBitBinary(Integer.toBinaryString(this.prefixLength())); |
| 261 | controlInfo = controlInfo + prefixLength.substring(2, prefixLength.length()); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 262 | bodyLst.add(Byte.parseByte(controlInfo, 2)); |
| 263 | if (this.isSubTlvPresence()) { |
| 264 | bodyLst.add(this.subTlvLength()); |
| 265 | for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) { |
| 266 | bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv)); |
| 267 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 268 | } |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 269 | bodyLst.addAll(Bytes.asList(IsisUtil.prefixToBytes(this.prefix()))); |
| 270 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 271 | return Bytes.toArray(bodyLst); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public String toString() { |
| 276 | return MoreObjects.toStringHelper(getClass()) |
| 277 | .omitNullValues() |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 278 | .add("down", down) |
| 279 | .add("subTlvPresence", subTlvPresence) |
| 280 | .add("prefixLength", prefixLength) |
| 281 | .add("metric", metric) |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 282 | .add("subTlvLength", subTlvLength) |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 283 | .add("prefix", prefix) |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 284 | .add("trafEnginSubTlv", trafEnginSubTlv) |
| 285 | .toString(); |
| 286 | } |
| 287 | } |