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