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 adjacency state TLV of P2P neighbor. |
| 28 | */ |
| 29 | public class AdjacencyStateTlv extends TlvHeader implements IsisTlv { |
| 30 | |
| 31 | private byte adjacencyType; |
| 32 | private int localCircuitId; |
| 33 | private String neighborSystemId; |
| 34 | private int neighborLocalCircuitId; |
| 35 | |
| 36 | /** |
| 37 | * Creates an instance of adjacency state TLV.. |
| 38 | * |
| 39 | * @param tlvHeader tlvHeader |
| 40 | */ |
| 41 | public AdjacencyStateTlv(TlvHeader tlvHeader) { |
| 42 | this.setTlvType(tlvHeader.tlvType()); |
| 43 | this.setTlvLength(tlvHeader.tlvLength()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns local circuit ID for adjacency state TLV. |
| 48 | * |
| 49 | * @return local circuit ID |
| 50 | */ |
| 51 | public int localCircuitId() { |
| 52 | return localCircuitId; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets local circuit ID for adjacency state TLV. |
| 57 | * |
| 58 | * @param localCircuitId local circuit Id |
| 59 | */ |
| 60 | public void setLocalCircuitId(int localCircuitId) { |
| 61 | this.localCircuitId = localCircuitId; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns neighbor system ID for adjacency state TLV. |
| 66 | * |
| 67 | * @return neighbor system ID |
| 68 | */ |
| 69 | public String neighborSystemId() { |
| 70 | return neighborSystemId; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sets neighbor system ID for adjacency state TLV. |
| 75 | * |
| 76 | * @param neighborSystemId neighbor system ID |
| 77 | */ |
| 78 | public void setNeighborSystemId(String neighborSystemId) { |
| 79 | this.neighborSystemId = neighborSystemId; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns neighbor local circuit ID for adjacency state TLV. |
| 84 | * |
| 85 | * @return neighbor local circuit ID |
| 86 | */ |
| 87 | public int neighborLocalCircuitId() { |
| 88 | return neighborLocalCircuitId; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets neighbor local circuit ID for adjacency state TLV. |
| 93 | * |
| 94 | * @param neighborLocalCircuitId neighbor local circuit ID |
| 95 | */ |
| 96 | public void setNeighborLocalCircuitId(int neighborLocalCircuitId) { |
| 97 | this.neighborLocalCircuitId = neighborLocalCircuitId; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns adjacency type of adjacency state TLV. |
| 102 | * |
| 103 | * @return adjacency type |
| 104 | */ |
| 105 | public byte adjacencyType() { |
| 106 | return adjacencyType; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sets adjacency type for adjacency state TLV. |
| 111 | * |
| 112 | * @param adjacencyType adjacency type |
| 113 | */ |
| 114 | public void setAdjacencyType(byte adjacencyType) { |
| 115 | this.adjacencyType = adjacencyType; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void readFrom(ChannelBuffer channelBuffer) { |
| 120 | this.setAdjacencyType(channelBuffer.readByte()); |
| 121 | if (channelBuffer.readableBytes() > 0) { |
| 122 | this.setLocalCircuitId(channelBuffer.readInt()); |
| 123 | byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES]; |
| 124 | channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES); |
| 125 | this.setNeighborSystemId(IsisUtil.systemId(tempByteArray)); |
| 126 | this.setNeighborLocalCircuitId(channelBuffer.readInt()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public byte[] asBytes() { |
| 132 | byte[] bytes = null; |
| 133 | byte[] tlvHeader = tlvHeaderAsByteArray(); |
| 134 | byte[] tlvBody = tlvBodyAsBytes(); |
| 135 | tlvHeader[1] = (byte) tlvBody.length; |
| 136 | bytes = Bytes.concat(tlvHeader, tlvBody); |
| 137 | return bytes; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns adjacency type TLV body as byte array. |
| 142 | * |
| 143 | * @return byteArray TLV body of area address TLV |
| 144 | */ |
| 145 | private byte[] tlvBodyAsBytes() { |
| 146 | List<Byte> bytes = new ArrayList<>(); |
| 147 | bytes.add(this.adjacencyType); |
| 148 | bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.localCircuitId))); |
| 149 | bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborSystemId)); |
| 150 | bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.neighborLocalCircuitId))); |
| 151 | return Bytes.toArray(bytes); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public String toString() { |
| 156 | return MoreObjects.toStringHelper(getClass()) |
| 157 | .omitNullValues() |
| 158 | .add("adjacencyType", adjacencyType) |
| 159 | .add("localCircuitId", localCircuitId) |
| 160 | .add("neighborSystemId", neighborSystemId) |
| 161 | .add("neighborLocalCircuitId", neighborLocalCircuitId) |
| 162 | .toString(); |
| 163 | } |
| 164 | } |