sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 21 | import org.onosproject.isis.io.util.IsisUtil; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Representation of LSP entry. |
| 28 | */ |
| 29 | public class LspEntry { |
| 30 | |
| 31 | private int lspSequenceNumber; |
| 32 | private int lspChecksum; |
| 33 | private int remainingTime; |
| 34 | private String lspId; |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Returns LSP sequence number of LSP entry. |
| 39 | * |
| 40 | * @return LSP sequence number |
| 41 | */ |
| 42 | public int lspSequenceNumber() { |
| 43 | return lspSequenceNumber; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Sets LSP sequenceNumber for LSP entry. |
| 48 | * |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 49 | * @param lspSequenceNumber lspSequenceNumber |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 50 | */ |
| 51 | public void setLspSequenceNumber(int lspSequenceNumber) { |
| 52 | this.lspSequenceNumber = lspSequenceNumber; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns LSP checksum of LSP entry. |
| 57 | * |
| 58 | * @return LSP checksum |
| 59 | */ |
| 60 | public int lspChecksum() { |
| 61 | return lspChecksum; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sets LSP checksum for LSP entry. |
| 66 | * |
| 67 | * @param lspChecksum LSP checksum |
| 68 | */ |
| 69 | public void setLspChecksum(int lspChecksum) { |
| 70 | this.lspChecksum = lspChecksum; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns remaining time of LSP entry. |
| 75 | * |
| 76 | * @return remaining time |
| 77 | */ |
| 78 | public int remainingTime() { |
| 79 | return remainingTime; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets remaining time for LSP entry. |
| 84 | * |
| 85 | * @param remainingTime remaining time |
| 86 | */ |
| 87 | public void setRemainingTime(int remainingTime) { |
| 88 | this.remainingTime = remainingTime; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns LSP ID of LSP entry. |
| 93 | * |
| 94 | * @return LSP ID |
| 95 | */ |
| 96 | public String lspId() { |
| 97 | return lspId; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets LSP ID for LSp entry. |
| 102 | * |
| 103 | * @param lspId LSP ID |
| 104 | */ |
| 105 | public void setLspId(String lspId) { |
| 106 | this.lspId = lspId; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sets the LSP entry values for LSP entry from byte buffer. |
| 111 | * |
| 112 | * @param channelBuffer channel Buffer instance |
| 113 | */ |
| 114 | public void readFrom(ChannelBuffer channelBuffer) { |
| 115 | this.setRemainingTime(channelBuffer.readUnsignedShort()); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 116 | byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_TWO_BYTE]; |
| 117 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_TWO_BYTE); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 118 | this.setLspId(IsisUtil.systemIdPlus(tempByteArray)); |
| 119 | this.setLspSequenceNumber(channelBuffer.readInt()); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 120 | this.setLspChecksum(channelBuffer.readUnsignedShort()); |
sunishvk | a1dfc3e | 2016-04-16 12:24:47 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns LSP entry values as bytes of LSP entry. |
| 125 | * |
| 126 | * @return byteArray LSP entry values as bytes of LSP entry |
| 127 | */ |
| 128 | public byte[] lspEntryAsBytes() { |
| 129 | List<Byte> bytes = new ArrayList<>(); |
| 130 | bytes.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.remainingTime()))); |
| 131 | bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.lspId())); |
| 132 | bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.lspSequenceNumber()))); |
| 133 | bytes.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.lspChecksum()))); |
| 134 | return Bytes.toArray(bytes); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return MoreObjects.toStringHelper(getClass()) |
| 140 | .omitNullValues() |
| 141 | .add("lspSequenceNumber", lspSequenceNumber) |
| 142 | .add("lspChecksum", lspChecksum) |
| 143 | .add("remainingTime", remainingTime) |
| 144 | .add("lspId", lspId) |
| 145 | .toString(); |
| 146 | } |
| 147 | } |