blob: 08a98fb0507d43689cbe33f68ec96d38ea4d8bd7 [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;
21import org.onosproject.isis.io.util.IsisUtil;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Representation of adjacency state TLV of P2P neighbor.
28 */
29public 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 *
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053039 * @param tlvHeader TLV header
sunishvka1dfc3e2016-04-16 12:24:47 +053040 */
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());
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530121 this.setLocalCircuitId(channelBuffer.readInt());
sunishvka1dfc3e2016-04-16 12:24:47 +0530122 if (channelBuffer.readableBytes() > 0) {
sunishvka1dfc3e2016-04-16 12:24:47 +0530123 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)));
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530149 if (this.neighborSystemId != null) {
sunishvka1dfc3e2016-04-16 12:24:47 +0530150 bytes.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborSystemId));
151 bytes.addAll(Bytes.asList(IsisUtil.convertToFourBytes(this.neighborLocalCircuitId)));
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530152 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530153 return Bytes.toArray(bytes);
154 }
155
156 @Override
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .omitNullValues()
160 .add("adjacencyType", adjacencyType)
161 .add("localCircuitId", localCircuitId)
162 .add("neighborSystemId", neighborSystemId)
163 .add("neighborLocalCircuitId", neighborLocalCircuitId)
164 .toString();
165 }
166}