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; |
| 24 | import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv; |
| 25 | import org.onosproject.isis.io.util.IsisUtil; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Representation of IP external reachability TLV. |
| 32 | */ |
| 33 | public class IpExternalReachabilityTlv extends TlvHeader implements IsisTlv { |
| 34 | |
| 35 | private String sysIdAndPseudoNumber; |
| 36 | private int defaultMetric; |
| 37 | private byte subTlvLength; |
| 38 | private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>(); |
| 39 | |
| 40 | /** |
| 41 | * Sets TLV type and TLV length for IP external reachability TLV. |
| 42 | * |
| 43 | * @param tlvHeader tlvHeader |
| 44 | */ |
| 45 | public IpExternalReachabilityTlv(TlvHeader tlvHeader) { |
| 46 | this.setTlvType(tlvHeader.tlvType()); |
| 47 | this.setTlvLength(tlvHeader.tlvLength()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Gets the system ID and pseudo number of IP external reachability TLV. |
| 52 | * |
| 53 | * @return sysIdAndPseudoNumber system ID and pseudo number |
| 54 | */ |
| 55 | public String sysIdAndPseudoNumber() { |
| 56 | return sysIdAndPseudoNumber; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets the system ID and pseudo number for IP external reachability TLV. |
| 61 | * |
| 62 | * @param sysIdAndPseudoNumber system ID and pseudo number |
| 63 | */ |
| 64 | public void setSysIdAndPseudoNumber(String sysIdAndPseudoNumber) { |
| 65 | this.sysIdAndPseudoNumber = sysIdAndPseudoNumber; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Adds the traffic engineering sub TLV to IP external reachability TLV. |
| 70 | * |
| 71 | * @param trafEnginSubTlv traffic engineering sub TLV |
| 72 | */ |
| 73 | public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) { |
| 74 | this.trafEnginSubTlv.add(trafEnginSubTlv); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the sub TLV length of IP external reachability TLV. |
| 79 | * |
| 80 | * @return sub TLV length |
| 81 | */ |
| 82 | public byte subTlvLength() { |
| 83 | return subTlvLength; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Sets the sub TLV length for IP external reachability TLV. |
| 88 | * |
| 89 | * @param subTlvLength sub TLV length |
| 90 | */ |
| 91 | public void setSubTlvLength(byte subTlvLength) { |
| 92 | this.subTlvLength = subTlvLength; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets default metric of IP external reachability TLV. |
| 97 | * |
| 98 | * @return default metric |
| 99 | */ |
| 100 | public int defaultMetric() { |
| 101 | return defaultMetric; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets default metric for IP external reachability TLV. |
| 106 | * |
| 107 | * @param defaultMetric default metric |
| 108 | */ |
| 109 | public void setDefaultMetric(int defaultMetric) { |
| 110 | this.defaultMetric = defaultMetric; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void readFrom(ChannelBuffer channelBuffer) { |
| 115 | byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE]; |
| 116 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE); |
| 117 | this.setSysIdAndPseudoNumber(IsisUtil.systemIdPlus(tempByteArray)); |
| 118 | this.setDefaultMetric(channelBuffer.readUnsignedMedium()); |
| 119 | this.setSubTlvLength((byte) channelBuffer.readByte()); |
| 120 | while (channelBuffer.readableBytes() > 0) { |
| 121 | TlvHeader tlvHeader = new TlvHeader(); |
| 122 | tlvHeader.setTlvType(channelBuffer.readByte()); |
| 123 | tlvHeader.setTlvLength(channelBuffer.readByte()); |
| 124 | this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader, |
| 125 | channelBuffer.readBytes(tlvHeader.tlvLength()))); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public byte[] asBytes() { |
| 131 | byte[] bytes = null; |
| 132 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 133 | byte[] tlvBody = tlvBodyAsBytes(); |
| 134 | //systemID + pseudo number+length of subtlv=11l |
| 135 | tlvBody[10] = (byte) (tlvBody.length - 11); |
| 136 | tlvHeader[1] = (byte) tlvBody.length; |
| 137 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 138 | return bytes; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Gets TLV body of IP external reachability TLV. |
| 143 | * |
| 144 | * @return byteArray TLV body of IP external reachability TLV. |
| 145 | */ |
| 146 | public byte[] tlvBodyAsBytes() { |
| 147 | List<Byte> bodyLst = new ArrayList<>(); |
| 148 | bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.sysIdAndPseudoNumber())); |
| 149 | bodyLst.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.defaultMetric()))); |
| 150 | bodyLst.add(this.subTlvLength()); |
| 151 | for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) { |
| 152 | bodyLst.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv)); |
| 153 | } |
| 154 | return Bytes.toArray(bodyLst); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public String toString() { |
| 159 | return MoreObjects.toStringHelper(getClass()) |
| 160 | .omitNullValues() |
| 161 | .add("sysIdAndPseudoNumber", sysIdAndPseudoNumber) |
| 162 | .add("defaultMetric", defaultMetric) |
| 163 | .add("subTlvLength", subTlvLength) |
| 164 | .toString(); |
| 165 | } |
| 166 | } |