sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 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 IS reachability TLV. |
| 27 | */ |
| 28 | public class IsReachabilityTlv extends TlvHeader { |
| 29 | |
| 30 | private int reserved; |
| 31 | private List<MetricsOfReachability> metricsOfReachabilities = new ArrayList<>(); |
| 32 | |
| 33 | /** |
| 34 | * Creates an instance of IS reachability TLV. |
| 35 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 36 | * @param tlvHeader TLV header |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 37 | */ |
| 38 | public IsReachabilityTlv(TlvHeader tlvHeader) { |
| 39 | this.setTlvType(tlvHeader.tlvType()); |
| 40 | this.setTlvLength(tlvHeader.tlvLength()); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the reserved value of IS reachability TLV. |
| 45 | * |
| 46 | * @return reserved |
| 47 | */ |
| 48 | public int reserved() { |
| 49 | return reserved; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Sets the reserved value for IS reachability TLV. |
| 54 | * |
| 55 | * @param reserved reserved |
| 56 | */ |
| 57 | public void setReserved(int reserved) { |
| 58 | this.reserved = reserved; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Adds the metric of reachability to IS reachability TLV.. |
| 63 | * |
| 64 | * @param metricsOfReachability metric of reachability |
| 65 | */ |
| 66 | public void addMeticsOfReachability(MetricsOfReachability metricsOfReachability) { |
| 67 | this.metricsOfReachabilities.add(metricsOfReachability); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void readFrom(ChannelBuffer channelBuffer) { |
| 72 | this.setReserved(channelBuffer.readByte()); |
| 73 | while (channelBuffer.readableBytes() > 0) { |
| 74 | MetricsOfReachability metricsOfReachability = new MetricsOfReachability(); |
| 75 | metricsOfReachability.readFrom(channelBuffer); |
| 76 | this.metricsOfReachabilities.add(metricsOfReachability); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public byte[] asBytes() { |
| 82 | byte[] bytes = null; |
| 83 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 84 | byte[] tlvBody = tlvBodyAsBytes(); |
| 85 | tlvHeader[1] = (byte) tlvBody.length; |
| 86 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 87 | return bytes; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns TLV body of IS reachability TLV. |
| 92 | * |
| 93 | * @return byteArray TLV body of area address TLV |
| 94 | */ |
| 95 | private byte[] tlvBodyAsBytes() { |
| 96 | List<Byte> bytes = new ArrayList<>(); |
| 97 | bytes.add((byte) this.reserved()); |
| 98 | for (MetricsOfReachability metricsOfReachability : this.metricsOfReachabilities) { |
| 99 | bytes.addAll(Bytes.asList(metricsOfReachability.asBytes())); |
| 100 | } |
| 101 | return Bytes.toArray(bytes); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(getClass()) |
| 107 | .omitNullValues() |
| 108 | .add("metricsOfReachabilities", metricsOfReachabilities) |
| 109 | .toString(); |
| 110 | } |
| 111 | } |