blob: 80dc5b1e0ba269cdcfcc010fb08c0c0ec878a0bc [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +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.pdu;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import com.google.common.primitives.Bytes;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.isis.io.isispacket.IsisHeader;
23import org.onosproject.isis.io.isispacket.tlv.IsisTlv;
24import org.onosproject.isis.io.isispacket.tlv.TlvFinder;
25import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
26import org.onosproject.isis.io.isispacket.tlv.TlvType;
27import org.onosproject.isis.io.isispacket.tlv.TlvsToBytes;
28import org.onosproject.isis.io.util.IsisUtil;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Representation of complete sequence number PDU.
35 */
36public class Csnp extends IsisHeader {
37
38 /*
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 | Intra-domain Routing Protocol Discriminator |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Length Indicator |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 | Version/Protocol ID Extension |
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | ID Length |
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 | R | R | R | PDU Type |
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 | Version |
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | Reserved |
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 | Maximum area address |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | PDU Length |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 | Source ID |
59 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 | Start LSP ID |
61 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 | End LSP ID |
63 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 | Variable Lengths Fields |
65 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66
67 CSNP Message Format
68 REFERENCE : ISO/IECĀ 10589
69 */
70 private int pduLength;
71 private String sourceId;
72 private String startLspId;
73 private String endLspId;
74 private List<IsisTlv> variableLengths = new ArrayList<>();
75
76 /**
77 * Creates the instance for this class.
78 *
79 * @param isisHeader ISIS header
80 */
81 public Csnp(IsisHeader isisHeader) {
82 populateHeader(isisHeader);
83 }
84
85 /**
86 * Returns the list of all tlvs.
87 *
88 * @return variableLengths list of tlvs
89 */
90 public List<IsisTlv> getAllTlv() {
91 return variableLengths;
92 }
93
94 /**
95 * Returns the source ID of csnp.
96 *
97 * @return sourceId source ID
98 */
99 public String sourceId() {
100 return sourceId;
101 }
102
103 /**
104 * Sets the source ID for csnp.
105 *
106 * @param sourceId source ID
107 */
108 public void setSourceId(String sourceId) {
109 this.sourceId = sourceId;
110 }
111
112 /**
113 * Returns the initial link state packet ID of csnp.
114 *
115 * @return startLspId start link state packet ID
116 */
117 public String startLspId() {
118 return startLspId;
119 }
120
121 /**
122 * Sets the initial link state packet ID for csnp.
123 *
124 * @param startLspId start link state packet ID
125 */
126 public void setStartLspId(String startLspId) {
127 this.startLspId = startLspId;
128 }
129
130 /**
131 * Returns the end link state packet ID of csnp.
132 *
133 * @return endLspId end link state packet ID of csnp.
134 */
135 public String endLspId() {
136 return endLspId;
137 }
138
139 /**
140 * Sets the end link state packet ID for csnp.
141 *
142 * @param endLspId end link state packet ID of csnp.
143 */
144 public void setEndLspId(String endLspId) {
145 this.endLspId = endLspId;
146 }
147
148 /**
149 * Returns the packet data unit length of link state packet.
150 * Entire length of this PDU, in octets
151 *
152 * @return pduLength packet date unit length
153 */
154 public int pduLength() {
155 return pduLength;
156 }
157
158 /**
159 * Sets the packet data unit length for link state packet.
160 * Entire Length of this PDU, in octets
161 *
162 * @param pduLength packet data length
163 */
164 public void setPduLength(int pduLength) {
165 this.pduLength = pduLength;
166 }
167
168 @Override
169 public void readFrom(ChannelBuffer channelBuffer) {
170 this.setPduLength(channelBuffer.readUnsignedShort());
171 //source id + 1 value
172 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
173 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
174 this.setSourceId(IsisUtil.systemIdPlus(tempByteArray));
175 //start lsp id + 2 value
176 tempByteArray = new byte[IsisUtil.ID_PLUS_TWO_BYTE];
177 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_TWO_BYTE);
178 this.setStartLspId(IsisUtil.systemIdPlus(tempByteArray));
179 //end lsp id + 2 value
180 tempByteArray = new byte[IsisUtil.ID_PLUS_TWO_BYTE];
181 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_TWO_BYTE);
182 this.setEndLspId(IsisUtil.systemIdPlus(tempByteArray));
183 //tlv here
184 while (channelBuffer.readableBytes() > 0) {
185 TlvHeader tlvHeader = new TlvHeader();
186 tlvHeader.setTlvType(channelBuffer.readUnsignedByte());
187 tlvHeader.setTlvLength(channelBuffer.readUnsignedByte());
188 TlvType tlvValue = TlvType.get(tlvHeader.tlvType());
189 if (tlvValue != null) {
190 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
191 this.variableLengths.add(tlv);
192 } else {
193 channelBuffer.readBytes(tlvHeader.tlvLength());
194 }
195 }
196 }
197
198 @Override
199 public byte[] asBytes() {
200 byte[] csnpMessage = null;
201 byte[] isisPduHeader = isisPduHeader();
202 byte[] csnpBody = completeSequenceNumberPduBody();
203 csnpMessage = Bytes.concat(isisPduHeader, csnpBody);
204 return csnpMessage;
205 }
206
207 /**
208 * Builds ISIS PDU header for complete sequence numbers PDU.
209 *
210 * @return isisPduHeader ISIS PDU header
211 */
212 public byte[] isisPduHeader() {
213 List<Byte> headerList = new ArrayList<>();
214 headerList.add(this.irpDiscriminator());
215 headerList.add((byte) IsisUtil.getPduHeaderLength(this.pduType()));
216 headerList.add(this.version());
217 headerList.add(this.idLength());
218 headerList.add((byte) this.pduType());
219 headerList.add(this.version2());
220 headerList.add(this.reserved());
221 headerList.add(this.maximumAreaAddresses());
222 return Bytes.toArray(headerList);
223 }
224
225 /**
226 * Builds complete sequence numbers PDU body.
227 *
228 * @return bodyList complete sequence numbers PDU body
229 */
230 public byte[] completeSequenceNumberPduBody() {
231 List<Byte> bodyList = new ArrayList<>();
232 bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength())));
233 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId()));
234 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.startLspId()));
235 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.endLspId()));
236 for (IsisTlv isisTlv : variableLengths) {
237 bodyList.addAll(TlvsToBytes.tlvToBytes(isisTlv));
238 }
239 return Bytes.toArray(bodyList);
240 }
241
242 @Override
243 public String toString() {
244 return MoreObjects.toStringHelper(getClass())
245 .omitNullValues()
246 .add("pduLength", pduLength)
247 .add("sourceId", sourceId)
248 .add("startLspId", startLspId)
249 .add("endLspId", endLspId)
250 .toString();
251 }
252
253 @Override
254 public boolean equals(Object o) {
255 if (this == o) {
256 return true;
257 }
258 if (o == null || getClass() != o.getClass()) {
259 return false;
260 }
261 Csnp that = (Csnp) o;
262 return Objects.equal(pduLength, that.pduLength) &&
263 Objects.equal(sourceId, that.sourceId) &&
264 Objects.equal(startLspId, that.startLspId) &&
265 Objects.equal(endLspId, that.endLspId);
266 }
267
268 @Override
269 public int hashCode() {
270 return Objects.hashCode(pduLength, sourceId, startLspId, endLspId);
271 }
272}