blob: b2eb6c92c00df6267a4c3416efb68f6863496b37 [file] [log] [blame]
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05301/*
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +05302* 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*/
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053016package org.onosproject.isis.io.isispacket.tlv;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053021import org.onosproject.isis.io.util.IsisUtil;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Representation of IS extended reachability TLV.
28 */
29public class IsExtendedReachability extends TlvHeader implements IsisTlv {
30
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053031 private List<NeighborForExtendedIs> neighbors = new ArrayList<>();
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053032
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 /**
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053044 * Adds the neighbor for extended IS instance to IS extended reachability TLV.
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053045 *
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053046 * @param neighbor neighbor for extended IS instance
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053047 */
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053048 public void addNeighbor(NeighborForExtendedIs neighbor) {
49 this.neighbors.add(neighbor);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053050 }
51
52 @Override
53 public void readFrom(ChannelBuffer channelBuffer) {
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053054 while (channelBuffer.readableBytes() >= IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES) {
55 NeighborForExtendedIs extendedIs = new NeighborForExtendedIs();
56 extendedIs.readFrom(channelBuffer);
57 this.addNeighbor(extendedIs);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053058 }
59 }
60
61 @Override
62 public byte[] asBytes() {
63 byte[] bytes = null;
64 byte[] tlvHeader = tlvHeaderAsByteArray();
65 byte[] tlvBody = tlvBodyAsBytes();
66 tlvHeader[1] = (byte) tlvBody.length;
67 bytes = Bytes.concat(tlvHeader, tlvBody);
68 return bytes;
69 }
70
71 /**
72 * Returns TLV body of IS extended reachability TLV.
73 *
74 * @return byteArray TLV body of IS extended reachability TLV.
75 */
76 private byte[] tlvBodyAsBytes() {
77 List<Byte> byteList = new ArrayList<>();
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053078 for (NeighborForExtendedIs neighbor : this.neighbors) {
79 byteList.addAll(Bytes.asList(neighbor.neighborBodyAsbytes()));
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053080 }
81 return Bytes.toArray(byteList);
82 }
83
84 @Override
85 public String toString() {
86 return MoreObjects.toStringHelper(getClass())
87 .omitNullValues()
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053088 .add("neighbors", neighbors)
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053089 .toString();
90 }
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053091}