blob: 53a82a80482a56868b2c6c109e01d163bad34621 [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 L1L2 hello PDU.
35 */
36public class L1L2HelloPdu extends HelloPdu {
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 | Circuit Type |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 | Source ID |
59 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 | Holding Time |
61 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 | PDU Length |
63 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 | PDU Length |
65 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 | R | Priority |
67 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 | LAN ID |
69 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 | Variable Lengths Fields |
71 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72
73 Hello Message Format
74 REFERENCE : ISO/IECĀ 10589
75 */
76
77 private byte priority;
78 private String lanId;
79
80 /**
81 * Parametrized constructor.
82 *
83 * @param isisHeader ISIs header
84 */
85 public L1L2HelloPdu(IsisHeader isisHeader) {
86 populateHeader(isisHeader);
87 }
88
89 /**
90 * Returns the LAN ID.
91 *
92 * @return LAN ID
93 */
94
95 public String lanId() {
96 return lanId;
97 }
98
99 /**
100 * Sets the LAN ID.
101 *
102 * @param lanId LAN ID
103 */
104 public void setLanId(String lanId) {
105 this.lanId = lanId;
106 }
107
108 /**
109 * Returns the priority.
110 *
111 * @return priority
112 */
113 public byte priority() {
114 return priority;
115 }
116
117 /**
118 * Sets priority.
119 *
120 * @param priority priority
121 */
122 public void setPriority(byte priority) {
123 this.priority = priority;
124 }
125
126 @Override
127 public void readFrom(ChannelBuffer channelBuffer) {
128 this.setCircuitType(channelBuffer.readByte());
129 //sorce id
130 byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES];
131 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES);
132 this.setSourceId(IsisUtil.systemId(tempByteArray));
133 this.setHoldingTime(channelBuffer.readUnsignedShort());
134 this.setPduLength(channelBuffer.readUnsignedShort());
135 this.setPriority(channelBuffer.readByte());
136 //landid id + 1 value
137 tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
138 channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
139 this.setLanId(IsisUtil.systemIdPlus(tempByteArray));
140 //tlv here
141 while (channelBuffer.readableBytes() > 0) {
142 TlvHeader tlvHeader = new TlvHeader();
143 tlvHeader.setTlvType(channelBuffer.readUnsignedByte());
144 tlvHeader.setTlvLength(channelBuffer.readUnsignedByte());
145 TlvType tlvType = TlvType.get(tlvHeader.tlvType());
146 if (tlvType != null) {
147 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
sunish vkc3824e82016-05-11 19:38:24 +0530148 if (tlv != null) {
149 this.variableLengths.add(tlv);
150 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530151 } else {
152 channelBuffer.readBytes(tlvHeader.tlvLength());
153 }
154 }
155 }
156
157 @Override
158 public byte[] asBytes() {
159 byte[] helloMessage = null;
160 byte[] helloHeader = l1l2IsisPduHeader();
161 byte[] helloBody = l1l2HelloPduBody();
162 helloMessage = Bytes.concat(helloHeader, helloBody);
163 return helloMessage;
164 }
165
166 /**
167 * Parse the ISIS L1L2 PDU header.
168 *
169 * @return ISIS L1L2 PDU header
170 */
171 public byte[] l1l2IsisPduHeader() {
172 List<Byte> headerLst = new ArrayList<>();
173 headerLst.add(this.irpDiscriminator());
174 headerLst.add((byte) IsisUtil.getPduHeaderLength(this.pduType()));
175 headerLst.add(this.version());
176 headerLst.add(this.idLength());
177 headerLst.add((byte) this.pduType());
178 headerLst.add(this.version2());
179 headerLst.add(this.reserved());
180 headerLst.add(this.maximumAreaAddresses());
181 return Bytes.toArray(headerLst);
182 }
183
184 /**
185 * Parse the ISIS L1L2 PDU body.
186 *
187 * @return ISIS L1L2 PDU body
188 */
189 public byte[] l1l2HelloPduBody() {
190 List<Byte> bodyLst = new ArrayList<>();
191
192 bodyLst.add(this.circuitType());
193 bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.sourceId()));
194 bodyLst.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.holdingTime())));
195 bodyLst.addAll(Bytes.asList(IsisUtil.convertToTwoBytes(this.pduLength())));
196 bodyLst.add(this.priority);
197 bodyLst.addAll(IsisUtil.sourceAndLanIdToBytes(this.lanId()));
198 for (IsisTlv isisTlv : variableLengths) {
199 bodyLst.addAll(TlvsToBytes.tlvToBytes(isisTlv));
200 }
201 return Bytes.toArray(bodyLst);
202 }
203
204 @Override
205 public String toString() {
206 return MoreObjects.toStringHelper(getClass())
207 .omitNullValues()
208 .add("priority", priority)
209 .add("lanId", lanId)
210 .toString();
211 }
212
213 @Override
214 public boolean equals(Object o) {
215 if (this == o) {
216 return true;
217 }
218 if (o == null || getClass() != o.getClass()) {
219 return false;
220 }
221 L1L2HelloPdu that = (L1L2HelloPdu) o;
222 return Objects.equal(priority, that.priority) &&
223 Objects.equal(lanId, that.lanId);
224 }
225
226 @Override
227 public int hashCode() {
228 return Objects.hashCode(priority, lanId);
229 }
230}