blob: 2d29a7c9396e6eeeb876325bfaba978b231d1dd9 [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Dhruv Dhodye64b93e2016-04-20 19:26:55 +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.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 partial sequence number PDU.
35 */
36public class Psnp extends IsisHeader {
37 /*
38 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 | Intradomain Routing Protocol Discriminator |
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Length Indicator |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | Version/Protocol ID Extension |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | ID Length |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 | R | R | R | PDU Type |
48 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 | Version |
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 | Reserved |
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 | Maximum area address |
54 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 | PDU Length |
56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 | Source ID |
58 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 | Start LSP ID |
60 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 | End LSP ID |
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 | Variable Lengths Fields |
64 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65
66 Hello Message Format
67 REFERENCE : ISO/IECĀ 10589
68 */
69 private int pduLength;
70 private String sourceId;
71 private List<IsisTlv> variableLengths = new ArrayList<>();
72
73 /**
74 * Creates the instance for this class.
75 *
76 * @param isisHeader ISIS header
77 */
78 public Psnp(IsisHeader isisHeader) {
79 populateHeader(isisHeader);
80 }
81
nicklesh adlakha90bfa6a2016-04-28 20:38:33 +053082 /**
83 * Adds the TLV to list.
84 *
85 * @param isisTlv ISIS TLV instance
86 */
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053087 public void addTlv(IsisTlv isisTlv) {
88 variableLengths.add(isisTlv);
89 }
90
91 /**
nicklesh adlakha90bfa6a2016-04-28 20:38:33 +053092 * Returns the list of all tlvs.
93 *
94 * @return variableLengths list of tlvs
95 */
96 public List<IsisTlv> getAllTlv() {
97 return variableLengths;
98 }
99
100 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530101 * Returns the source ID of csnp.
102 *
103 * @return sourceId source ID
104 */
105 public String sourceId() {
106 return sourceId;
107 }
108
109 /**
110 * Sets the source ID for csnp.
111 *
112 * @param sourceId source ID
113 */
114 public void setSourceId(String sourceId) {
115 this.sourceId = sourceId;
116 }
117
118 /**
119 * Returns the packet data unit length of link state packet.
120 * Entire length of this PDU, in octets
121 *
122 * @return pduLength packte date unit length
123 */
124 public int pduLength() {
125 return pduLength;
126 }
127
128 /**
129 * Sets the packet data unit length for link state packet.
130 * Entire Length of this PDU, in octets
131 *
132 * @param pduLength packte data length
133 */
134 public void setPduLength(int pduLength) {
135 this.pduLength = pduLength;
136 }
137
138 @Override
139 public void readFrom(ChannelBuffer channelBuffer) {
140 this.setPduLength(channelBuffer.readUnsignedShort());
141 //source id + 2 value
142 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
143 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
144 this.setSourceId(IsisUtil.systemIdPlus(tempByteArray));
145 //tlv here
146 while (channelBuffer.readableBytes() > 0) {
147 TlvHeader tlvHeader = new TlvHeader();
148 tlvHeader.setTlvType(channelBuffer.readUnsignedByte());
149 tlvHeader.setTlvLength(channelBuffer.readUnsignedByte());
150 TlvType tlvValue = TlvType.get(tlvHeader.tlvType());
151 if (tlvValue != null) {
152 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
nicklesh adlakha90bfa6a2016-04-28 20:38:33 +0530153 if (tlv != null) {
154 this.variableLengths.add(tlv);
155 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530156 } else {
157 channelBuffer.readBytes(tlvHeader.tlvLength());
158 }
159 }
160 }
161
162
163 @Override
164 public byte[] asBytes() {
165 byte[] psnpMessage = null;
166 byte[] isisPduHeader = isisPduHeader();
167 byte[] psnpBody = partialSequenceNumberPduBody();
168 psnpMessage = Bytes.concat(isisPduHeader, psnpBody);
169 return psnpMessage;
170 }
171
172 /**
173 * Builds the ISIS PDU header.
174 *
175 * @return headerList ISIS PDU header
176 */
177 public byte[] isisPduHeader() {
178 List<Byte> headerList = new ArrayList<>();
179 headerList.add(this.irpDiscriminator());
180 headerList.add((byte) IsisUtil.getPduHeaderLength(this.pduType()));
181 headerList.add(this.version());
182 headerList.add(this.idLength());
183 headerList.add((byte) this.pduType());
184 headerList.add(this.version2());
185 headerList.add(this.reserved());
186 headerList.add(this.maximumAreaAddresses());
187 return Bytes.toArray(headerList);
188 }
189
190 /**
191 * Builds the partial sequence number PDU body.
192 *
193 * @return bodyList partial sequence number PDU body
194 */
195 public byte[] partialSequenceNumberPduBody() {
196 List<Byte> bodyList = new ArrayList<>();
197 bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength())));
198 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId()));
199 for (IsisTlv isisTlv : variableLengths) {
200 bodyList.addAll(TlvsToBytes.tlvToBytes(isisTlv));
201 }
202 return Bytes.toArray(bodyList);
203 }
204
205 @Override
206 public String toString() {
207 return MoreObjects.toStringHelper(getClass())
208 .omitNullValues()
209 .add("pduLength", pduLength)
210 .add("sourceId", sourceId)
211 .toString();
212 }
213
214 @Override
215 public boolean equals(Object o) {
216 if (this == o) {
217 return true;
218 }
219 if (o == null || getClass() != o.getClass()) {
220 return false;
221 }
222 Psnp that = (Psnp) o;
223 return Objects.equal(pduLength, that.pduLength);
224 }
225
226 @Override
227 public int hashCode() {
228 return Objects.hashCode(sourceId, pduLength);
229 }
230}