blob: 33980451656a7c63836aa7e78ce0c8973ae94c2d [file] [log] [blame]
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002* Copyright 2016-present Open Networking Foundation
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +05303*
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 /**
sunish vk7bdf4d42016-06-24 12:29:43 +053044 * Returns neighbor list.
45 *
46 * @return neighbor list
47 */
48 public List<NeighborForExtendedIs> neighbours() {
49 return neighbors;
50 }
51
52 /**
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053053 * Adds the neighbor for extended IS instance to IS extended reachability TLV.
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053054 *
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053055 * @param neighbor neighbor for extended IS instance
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053056 */
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053057 public void addNeighbor(NeighborForExtendedIs neighbor) {
58 this.neighbors.add(neighbor);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053059 }
60
sunish vkc3824e82016-05-11 19:38:24 +053061
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053062 @Override
63 public void readFrom(ChannelBuffer channelBuffer) {
sunish vkc3824e82016-05-11 19:38:24 +053064 while (channelBuffer.readableBytes() >= (IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES)) {
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053065 NeighborForExtendedIs extendedIs = new NeighborForExtendedIs();
66 extendedIs.readFrom(channelBuffer);
67 this.addNeighbor(extendedIs);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053068 }
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 SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053088 for (NeighborForExtendedIs neighbor : this.neighbors) {
89 byteList.addAll(Bytes.asList(neighbor.neighborBodyAsbytes()));
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053090 }
91 return Bytes.toArray(byteList);
92 }
93
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .omitNullValues()
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +053098 .add("neighbors", neighbors)
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053099 .toString();
100 }
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +0530101}