blob: e1cccb49a8f271b5c1b130a81b7c69b79ea14546 [file] [log] [blame]
sunishvka1dfc3e2016-04-16 12:24:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunishvka1dfc3e2016-04-16 12:24:47 +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 */
16package 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;
sunish vkc3824e82016-05-11 19:38:24 +053021import org.onosproject.isis.io.util.IsisUtil;
sunishvka1dfc3e2016-04-16 12:24:47 +053022
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Representation of LSP entries TLV.
28 */
29public class LspEntriesTlv extends TlvHeader implements IsisTlv {
30 private List<LspEntry> lspEntryList = new ArrayList<>();
31
32 /**
33 * Creates an instance of LSP entries TLV.
34 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053035 * @param tlvHeader TLV header
sunishvka1dfc3e2016-04-16 12:24:47 +053036 */
37 public LspEntriesTlv(TlvHeader tlvHeader) {
38 this.setTlvType(tlvHeader.tlvType());
39 this.setTlvLength(tlvHeader.tlvLength());
40 }
41
42 /**
43 * Returns the LSP entry of LSP entries TLV.
44 *
45 * @return LSP entries
46 */
47 public List<LspEntry> lspEntry() {
48 return lspEntryList;
49 }
50
51 /**
52 * Adds the the LSP entry to LSP entries TLV.
53 *
54 * @param lspEntry LSP entry
55 */
56 public void addLspEntry(LspEntry lspEntry) {
57 this.lspEntryList.add(lspEntry);
58 }
59
60 @Override
61 public void readFrom(ChannelBuffer channelBuffer) {
sunish vkc3824e82016-05-11 19:38:24 +053062 while (channelBuffer.readableBytes() >= (IsisUtil.EIGHT_BYTES * 2)) {
sunishvka1dfc3e2016-04-16 12:24:47 +053063 LspEntry lspEntry = new LspEntry();
sunish vkc3824e82016-05-11 19:38:24 +053064 lspEntry.readFrom(channelBuffer.readBytes(IsisUtil.EIGHT_BYTES * 2));
sunishvka1dfc3e2016-04-16 12:24:47 +053065 lspEntryList.add(lspEntry);
66 }
67 }
68
69 @Override
70 public byte[] asBytes() {
71 byte[] bytes = null;
72 byte[] tlvHeader = tlvHeaderAsByteArray();
73 byte[] tlvBody = tlvBodyAsBytes();
74 tlvHeader[1] = (byte) tlvBody.length;
75 bytes = Bytes.concat(tlvHeader, tlvBody);
76 return bytes;
77 }
78
79 /**
80 * Returns TLV body of LSP entries TLV.
81 *
82 * @return byteArray TLV body of LSP entries TLV
83 */
84 private byte[] tlvBodyAsBytes() {
85 List<Byte> bytes = new ArrayList<>();
86 for (LspEntry lspEntry : lspEntryList) {
87 bytes.addAll(Bytes.asList(lspEntry.lspEntryAsBytes()));
88 }
89 return Bytes.toArray(bytes);
90 }
91
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(getClass())
95 .omitNullValues()
96 .add("lspEntryList", lspEntryList)
97 .toString();
98 }
99}