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 | package org.onosproject.isis.io.isispacket.tlv; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | /** |
| 26 | * Representation of IP internal reachability TLV. |
| 27 | */ |
| 28 | public class IpInternalReachabilityTlv extends TlvHeader implements IsisTlv { |
| 29 | private List<MetricOfInternalReachability> metricOfInternalReachability = new ArrayList<>(); |
| 30 | |
| 31 | /** |
| 32 | * Creates an instance of IP internal reachability TLV. |
| 33 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame^] | 34 | * @param tlvHeader TLV header |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 35 | */ |
| 36 | public IpInternalReachabilityTlv(TlvHeader tlvHeader) { |
| 37 | this.setTlvType(tlvHeader.tlvType()); |
| 38 | this.setTlvLength(tlvHeader.tlvLength()); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Adds the metric of internal reachability to internal reachability TLV. |
| 43 | * |
| 44 | * @param metricValue metric of internal reachability |
| 45 | */ |
| 46 | public void addInternalReachabilityMetric(MetricOfInternalReachability metricValue) { |
| 47 | this.metricOfInternalReachability.add(metricValue); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void readFrom(ChannelBuffer channelBuffer) { |
| 52 | while (channelBuffer.readableBytes() > 0) { |
| 53 | MetricOfInternalReachability metricOfInternalReachability = new MetricOfInternalReachability(); |
| 54 | metricOfInternalReachability.readFrom(channelBuffer); |
| 55 | this.metricOfInternalReachability.add(metricOfInternalReachability); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public byte[] asBytes() { |
| 61 | byte[] bytes = null; |
| 62 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 63 | byte[] tlvBody = tlvBodyAsBytes(); |
| 64 | tlvHeader[1] = (byte) tlvBody.length; |
| 65 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 66 | return bytes; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns TLV body of internal reachability TLV. |
| 71 | * |
| 72 | * @return byteArray TLV body of area address TLV |
| 73 | */ |
| 74 | private byte[] tlvBodyAsBytes() { |
| 75 | List<Byte> bytes = new ArrayList<>(); |
| 76 | for (MetricOfInternalReachability metricOfInternalReachability : |
| 77 | this.metricOfInternalReachability) { |
| 78 | bytes.addAll(Bytes.asList(metricOfInternalReachability.asBytes())); |
| 79 | } |
| 80 | return Bytes.toArray(bytes); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return MoreObjects.toStringHelper(getClass()) |
| 86 | .omitNullValues() |
| 87 | .add("metricOfInternalReachability", metricOfInternalReachability) |
| 88 | .toString(); |
| 89 | } |
| 90 | } |