tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +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 | */ |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 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; |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 21 | import org.onosproject.isis.io.util.IsisUtil; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Representation of IS extended reachability TLV. |
| 28 | */ |
| 29 | public class IsExtendedReachability extends TlvHeader implements IsisTlv { |
| 30 | |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 31 | private List<NeighborForExtendedIs> neighbors = new ArrayList<>(); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Creates an instance of IP external reachability TLV. |
| 35 | * |
| 36 | * @param tlvHeader TLV header |
| 37 | */ |
| 38 | public IsExtendedReachability(TlvHeader tlvHeader) { |
| 39 | this.setTlvType(tlvHeader.tlvType()); |
| 40 | this.setTlvLength(tlvHeader.tlvLength()); |
| 41 | } |
| 42 | |
| 43 | /** |
sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame] | 44 | * Returns neighbor list. |
| 45 | * |
| 46 | * @return neighbor list |
| 47 | */ |
| 48 | public List<NeighborForExtendedIs> neighbours() { |
| 49 | return neighbors; |
| 50 | } |
| 51 | |
| 52 | /** |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 53 | * Adds the neighbor for extended IS instance to IS extended reachability TLV. |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 54 | * |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 55 | * @param neighbor neighbor for extended IS instance |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 56 | */ |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 57 | public void addNeighbor(NeighborForExtendedIs neighbor) { |
| 58 | this.neighbors.add(neighbor); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 59 | } |
| 60 | |
sunish vk | c3824e8 | 2016-05-11 19:38:24 +0530 | [diff] [blame] | 61 | |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 62 | @Override |
| 63 | public void readFrom(ChannelBuffer channelBuffer) { |
sunish vk | c3824e8 | 2016-05-11 19:38:24 +0530 | [diff] [blame] | 64 | while (channelBuffer.readableBytes() >= (IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES)) { |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 65 | NeighborForExtendedIs extendedIs = new NeighborForExtendedIs(); |
| 66 | extendedIs.readFrom(channelBuffer); |
| 67 | this.addNeighbor(extendedIs); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public byte[] asBytes() { |
| 73 | byte[] bytes = null; |
| 74 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 75 | byte[] tlvBody = tlvBodyAsBytes(); |
| 76 | tlvHeader[1] = (byte) tlvBody.length; |
| 77 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 78 | return bytes; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns TLV body of IS extended reachability TLV. |
| 83 | * |
| 84 | * @return byteArray TLV body of IS extended reachability TLV. |
| 85 | */ |
| 86 | private byte[] tlvBodyAsBytes() { |
| 87 | List<Byte> byteList = new ArrayList<>(); |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 88 | for (NeighborForExtendedIs neighbor : this.neighbors) { |
| 89 | byteList.addAll(Bytes.asList(neighbor.neighborBodyAsbytes())); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 90 | } |
| 91 | return Bytes.toArray(byteList); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return MoreObjects.toStringHelper(getClass()) |
| 97 | .omitNullValues() |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 98 | .add("neighbors", neighbors) |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame] | 99 | .toString(); |
| 100 | } |
PRASHANTH SHIVANANJAPPA | 491b8af | 2016-04-27 19:23:24 +0530 | [diff] [blame] | 101 | } |