blob: 6ed6a4cf87136bdd8c23f904565fe0435582394b [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 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
82 public void addTlv(IsisTlv isisTlv) {
83 variableLengths.add(isisTlv);
84 }
85
86 /**
87 * Returns the source ID of csnp.
88 *
89 * @return sourceId source ID
90 */
91 public String sourceId() {
92 return sourceId;
93 }
94
95 /**
96 * Sets the source ID for csnp.
97 *
98 * @param sourceId source ID
99 */
100 public void setSourceId(String sourceId) {
101 this.sourceId = sourceId;
102 }
103
104 /**
105 * Returns the packet data unit length of link state packet.
106 * Entire length of this PDU, in octets
107 *
108 * @return pduLength packte date unit length
109 */
110 public int pduLength() {
111 return pduLength;
112 }
113
114 /**
115 * Sets the packet data unit length for link state packet.
116 * Entire Length of this PDU, in octets
117 *
118 * @param pduLength packte data length
119 */
120 public void setPduLength(int pduLength) {
121 this.pduLength = pduLength;
122 }
123
124 @Override
125 public void readFrom(ChannelBuffer channelBuffer) {
126 this.setPduLength(channelBuffer.readUnsignedShort());
127 //source id + 2 value
128 byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
129 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
130 this.setSourceId(IsisUtil.systemIdPlus(tempByteArray));
131 //tlv here
132 while (channelBuffer.readableBytes() > 0) {
133 TlvHeader tlvHeader = new TlvHeader();
134 tlvHeader.setTlvType(channelBuffer.readUnsignedByte());
135 tlvHeader.setTlvLength(channelBuffer.readUnsignedByte());
136 TlvType tlvValue = TlvType.get(tlvHeader.tlvType());
137 if (tlvValue != null) {
138 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
139 this.variableLengths.add(tlv);
140 } else {
141 channelBuffer.readBytes(tlvHeader.tlvLength());
142 }
143 }
144 }
145
146
147 @Override
148 public byte[] asBytes() {
149 byte[] psnpMessage = null;
150 byte[] isisPduHeader = isisPduHeader();
151 byte[] psnpBody = partialSequenceNumberPduBody();
152 psnpMessage = Bytes.concat(isisPduHeader, psnpBody);
153 return psnpMessage;
154 }
155
156 /**
157 * Builds the ISIS PDU header.
158 *
159 * @return headerList ISIS PDU header
160 */
161 public byte[] isisPduHeader() {
162 List<Byte> headerList = new ArrayList<>();
163 headerList.add(this.irpDiscriminator());
164 headerList.add((byte) IsisUtil.getPduHeaderLength(this.pduType()));
165 headerList.add(this.version());
166 headerList.add(this.idLength());
167 headerList.add((byte) this.pduType());
168 headerList.add(this.version2());
169 headerList.add(this.reserved());
170 headerList.add(this.maximumAreaAddresses());
171 return Bytes.toArray(headerList);
172 }
173
174 /**
175 * Builds the partial sequence number PDU body.
176 *
177 * @return bodyList partial sequence number PDU body
178 */
179 public byte[] partialSequenceNumberPduBody() {
180 List<Byte> bodyList = new ArrayList<>();
181 bodyList.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength())));
182 bodyList.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId()));
183 for (IsisTlv isisTlv : variableLengths) {
184 bodyList.addAll(TlvsToBytes.tlvToBytes(isisTlv));
185 }
186 return Bytes.toArray(bodyList);
187 }
188
189 @Override
190 public String toString() {
191 return MoreObjects.toStringHelper(getClass())
192 .omitNullValues()
193 .add("pduLength", pduLength)
194 .add("sourceId", sourceId)
195 .toString();
196 }
197
198 @Override
199 public boolean equals(Object o) {
200 if (this == o) {
201 return true;
202 }
203 if (o == null || getClass() != o.getClass()) {
204 return false;
205 }
206 Psnp that = (Psnp) o;
207 return Objects.equal(pduLength, that.pduLength);
208 }
209
210 @Override
211 public int hashCode() {
212 return Objects.hashCode(sourceId, pduLength);
213 }
214}