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 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 55 | * Returns the prefix of IP external reachability TLV. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 56 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 57 | * @return prefix |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 58 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 59 | public String prefix() { |
| 60 | return prefix; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 64 | * Sets 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 | * @param prefix 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 void setPrefix(String prefix) { |
| 69 | this.prefix = prefix; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns if down true else false of IP external reachability TLV. |
| 74 | * |
| 75 | * @return if down true else false |
| 76 | */ |
| 77 | public boolean isDown() { |
| 78 | return down; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets if down true else false of IP external reachability TLV. |
| 83 | * |
| 84 | * @param upOrDown if down true else false |
| 85 | */ |
| 86 | public void setDown(boolean upOrDown) { |
| 87 | this.down = upOrDown; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns true if sub TLV present else false of IP external reachability TLV. |
| 92 | * |
| 93 | * @return true if present else false |
| 94 | */ |
| 95 | public boolean isSubTlvPresence() { |
| 96 | return subTlvPresence; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Sets true if sub TLV present else false of IP external reachability TLV. |
| 101 | * |
| 102 | * @param subTlvPresence true if present else false |
| 103 | */ |
| 104 | public void setSubTlvPresence(boolean subTlvPresence) { |
| 105 | this.subTlvPresence = subTlvPresence; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets the prefix length of IP external reachability TLV. |
| 110 | * |
| 111 | * @return prefix length |
| 112 | */ |
| 113 | public int prefixLength() { |
| 114 | return prefixLength; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the prefix length of IP external reachability TLV. |
| 119 | * |
| 120 | * @param prefixLength the prefix length of IP external reachability TLV |
| 121 | */ |
| 122 | public void setPrefixLength(int prefixLength) { |
| 123 | this.prefixLength = prefixLength; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adds the traffic engineering sub TLV to IP external reachability TLV. |
| 128 | * |
| 129 | * @param trafEnginSubTlv traffic engineering sub TLV |
| 130 | */ |
| 131 | public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) { |
| 132 | this.trafEnginSubTlv.add(trafEnginSubTlv); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the sub TLV length of IP external reachability TLV. |
| 137 | * |
| 138 | * @return sub TLV length |
| 139 | */ |
| 140 | public byte subTlvLength() { |
| 141 | return subTlvLength; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Sets the sub TLV length for IP external reachability TLV. |
| 146 | * |
| 147 | * @param subTlvLength sub TLV length |
| 148 | */ |
| 149 | public void setSubTlvLength(byte subTlvLength) { |
| 150 | this.subTlvLength = subTlvLength; |
| 151 | } |
| 152 | |
| 153 | /** |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 154 | * Returns metric of IP external reachability TLV. |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 155 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 156 | * @return metric |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 157 | */ |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 158 | public int metric() { |
| 159 | return metric; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets default metric for IP external reachability TLV. |
| 164 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 165 | * @param metric default 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 void setMetric(int metric) { |
| 168 | this.metric = metric; |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void readFrom(ChannelBuffer channelBuffer) { |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 173 | this.setMetric(channelBuffer.readInt()); |
| 174 | int controlInfo = channelBuffer.readByte(); |
| 175 | byte[] tempByteArray = null; |
| 176 | |
| 177 | String string = IsisUtil.toEightBitBinary(Integer.toBinaryString(controlInfo)); |
| 178 | if (string.charAt(0) == '0') { |
| 179 | this.setDown(false); |
| 180 | } |
| 181 | if (string.charAt(1) == '1') { |
| 182 | this.setSubTlvPresence(true); |
| 183 | } |
| 184 | this.setPrefixLength(Integer.parseInt(string.substring(2, string.length()), 2)); |
| 185 | if (this.prefixLength >= 0 && this.prefixLength <= 8) { |
| 186 | channelBuffer.readByte(); |
| 187 | } else if (this.prefixLength >= 8 && this.prefixLength <= 16) { |
| 188 | tempByteArray = new byte[IsisUtil.TWO_BYTES]; |
| 189 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.TWO_BYTES); |
| 190 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 191 | } else if (this.prefixLength >= 17 && this.prefixLength <= 24) { |
| 192 | tempByteArray = new byte[IsisUtil.THREE_BYTES]; |
| 193 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.THREE_BYTES); |
| 194 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 195 | } else if (this.prefixLength >= 24 && this.prefixLength <= 32) { |
| 196 | tempByteArray = new byte[IsisUtil.FOUR_BYTES]; |
| 197 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES); |
| 198 | this.setPrefix(IsisUtil.prefixConversion(tempByteArray)); |
| 199 | } |
| 200 | if (this.isSubTlvPresence()) { |
| 201 | this.setSubTlvLength(channelBuffer.readByte()); |
| 202 | while (channelBuffer.readableBytes() > 0) { |
| 203 | TlvHeader tlvHeader = new TlvHeader(); |
| 204 | tlvHeader.setTlvType(channelBuffer.readByte()); |
| 205 | tlvHeader.setTlvLength(channelBuffer.readByte()); |
| 206 | SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType()); |
| 207 | if (tlvValue != null) { |
| 208 | this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader, |
| 209 | channelBuffer.readBytes(tlvHeader.tlvLength()))); |
| 210 | } else { |
| 211 | channelBuffer.readBytes(tlvHeader.tlvLength()); |
| 212 | } |
| 213 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 217 | |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 218 | @Override |
| 219 | public byte[] asBytes() { |
| 220 | byte[] bytes = null; |
| 221 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 222 | byte[] tlvBody = tlvBodyAsBytes(); |
| 223 | //systemID + pseudo number+length of subtlv=11l |
| 224 | tlvBody[10] = (byte) (tlvBody.length - 11); |
| 225 | tlvHeader[1] = (byte) tlvBody.length; |
| 226 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 227 | return bytes; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns TLV body of IP external reachability TLV. |
| 232 | * |
| 233 | * @return byteArray TLV body of IP external reachability TLV. |
| 234 | */ |
| 235 | private byte[] tlvBodyAsBytes() { |
| 236 | List<Byte> bodyLst = new ArrayList<>(); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 237 | bodyLst.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.metric()))); |
| 238 | String controlInfo = ""; |
| 239 | if (this.isDown()) { |
| 240 | controlInfo = controlInfo + "1"; |
| 241 | } else { |
| 242 | controlInfo = controlInfo + "0"; |
| 243 | } |
| 244 | if (this.isSubTlvPresence()) { |
| 245 | controlInfo = controlInfo + "1"; |
| 246 | } else { |
| 247 | controlInfo = controlInfo + "0"; |
| 248 | } |
| 249 | String prefixlength = IsisUtil.toEightBitBinary(Integer.toBinaryString(this.prefixLength())); |
| 250 | controlInfo = controlInfo + prefixlength.substring(2, prefixlength.length()); |
| 251 | bodyLst.add(Byte.parseByte(controlInfo, 2)); |
| 252 | if (this.isSubTlvPresence()) { |
| 253 | bodyLst.add(this.subTlvLength()); |
| 254 | for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) { |
| 255 | bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv)); |
| 256 | } |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 257 | } |
| 258 | return Bytes.toArray(bodyLst); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public String toString() { |
| 263 | return MoreObjects.toStringHelper(getClass()) |
| 264 | .omitNullValues() |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 265 | .add("down", down) |
| 266 | .add("subTlvPresence", subTlvPresence) |
| 267 | .add("prefixLength", prefixLength) |
| 268 | .add("metric", metric) |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 269 | .add("subTlvLength", subTlvLength) |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 270 | .add("prefix", prefix) |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 271 | .add("trafEnginSubTlv", trafEnginSubTlv) |
| 272 | .toString(); |
| 273 | } |
| 274 | } |