blob: 44ad9e0f86de6773ddd04280873660d96fb55a37 [file] [log] [blame]
mohamed rahile04626f2016-04-05 20:42:53 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
mohamed rahile04626f2016-04-05 20:42:53 +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.util;
17
mohamed rahile04626f2016-04-05 20:42:53 +053018import com.google.common.primitives.Bytes;
sunishvka1dfc3e2016-04-16 12:24:47 +053019import org.onlab.packet.Ip4Address;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053020import org.onlab.packet.MacAddress;
21import org.onosproject.isis.controller.IsisInterface;
22import org.onosproject.isis.controller.IsisInterfaceState;
23import org.onosproject.isis.controller.IsisNeighbor;
24import org.onosproject.isis.controller.IsisPduType;
25import org.onosproject.isis.io.isispacket.IsisHeader;
26import org.onosproject.isis.io.isispacket.pdu.L1L2HelloPdu;
27import org.onosproject.isis.io.isispacket.pdu.P2PHelloPdu;
28import org.onosproject.isis.io.isispacket.tlv.AdjacencyStateTlv;
29import org.onosproject.isis.io.isispacket.tlv.AreaAddressTlv;
30import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv;
31import org.onosproject.isis.io.isispacket.tlv.IsisNeighborTlv;
mohamed rahile04626f2016-04-05 20:42:53 +053032import org.onosproject.isis.io.isispacket.tlv.PaddingTlv;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053033import org.onosproject.isis.io.isispacket.tlv.ProtocolSupportedTlv;
mohamed rahile04626f2016-04-05 20:42:53 +053034import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
sunishvka1dfc3e2016-04-16 12:24:47 +053035import org.onosproject.isis.io.isispacket.tlv.TlvType;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
mohamed rahile04626f2016-04-05 20:42:53 +053038
39import javax.xml.bind.DatatypeConverter;
40import java.util.ArrayList;
41import java.util.List;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053042import java.util.Set;
mohamed rahile04626f2016-04-05 20:42:53 +053043import java.util.StringTokenizer;
44
45/**
sunishvka1dfc3e2016-04-16 12:24:47 +053046 * Representation of ISIS utils.
mohamed rahile04626f2016-04-05 20:42:53 +053047 */
48public final class IsisUtil {
sunishvka1dfc3e2016-04-16 12:24:47 +053049 public static final int ETHER_HEADER_LEN = 17;
mohamed rahile04626f2016-04-05 20:42:53 +053050 public static final int ID_SIX_BYTES = 6;
51 public static final int ID_PLUS_ONE_BYTE = 7;
52 public static final int ID_PLUS_TWO_BYTE = 8;
53 public static final int THREE_BYTES = 3;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053054 public static final int TWO_BYTES = 2;
mohamed rahile04626f2016-04-05 20:42:53 +053055 public static final int SIX_BYTES = 6;
sunishvka1dfc3e2016-04-16 12:24:47 +053056 public static final int EIGHT_BYTES = 8;
mohamed rahile04626f2016-04-05 20:42:53 +053057 public static final int FOUR_BYTES = 4;
58 public static final int PADDING_FIXED_LENGTH = 255;
sunishvka1dfc3e2016-04-16 12:24:47 +053059 public static final int TLVHEADERLENGTH = 2;
60 public static final int INITIAL_BANDWIDTH = 12500000;
61 private static final Logger log = LoggerFactory.getLogger(IsisUtil.class);
mohamed rahile04626f2016-04-05 20:42:53 +053062
63 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053064 * Creates an instance.
mohamed rahile04626f2016-04-05 20:42:53 +053065 */
66 private IsisUtil() {
67
68 }
69
70 /**
sunishvka1dfc3e2016-04-16 12:24:47 +053071 * Checks given IPs are in same network or not.
72 *
73 * @param ip1 IP address
74 * @param ip2 IP address
75 * @param mask network mask
76 * @return true if both are in same network else false
77 */
78 public static boolean sameNetwork(Ip4Address ip1, Ip4Address ip2, byte[] mask) {
79 try {
80 byte[] a1 = ip1.toOctets();
81 byte[] a2 = ip2.toOctets();
82 for (int i = 0; i < a1.length; i++) {
83 if ((a1[i] & mask[i]) != (a2[i] & mask[i])) {
84 return false;
85 }
86 }
87 } catch (Exception e) {
88 log.debug("Exception::IsisUtil::sameNetwork:: {}", e.getMessage());
89 }
90 return true;
91 }
92
93 /**
mohamed rahile04626f2016-04-05 20:42:53 +053094 * Parse byte array to string system ID.
95 *
96 * @param bytes system ID
sunishvka1dfc3e2016-04-16 12:24:47 +053097 * @return systemId system ID
mohamed rahile04626f2016-04-05 20:42:53 +053098 */
99 public static String systemId(byte[] bytes) {
100 String systemId = "";
101 for (Byte byt : bytes) {
102 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
103 if (hexa.length() % 2 != 0) {
104 hexa = "0" + hexa;
105 }
106 systemId = systemId + hexa;
107 if (systemId.length() == 4 || systemId.length() == 9) {
108 systemId = systemId + ".";
109 }
110 }
111 return systemId;
112 }
113
114 /**
115 * Parse byte array to LAN ID.
116 *
117 * @param bytes LAN ID
sunishvka1dfc3e2016-04-16 12:24:47 +0530118 * @return systemIdPlus system ID
mohamed rahile04626f2016-04-05 20:42:53 +0530119 */
120 public static String systemIdPlus(byte[] bytes) {
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530121 int count = 1;
mohamed rahile04626f2016-04-05 20:42:53 +0530122 String systemId = "";
123 for (Byte byt : bytes) {
124 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
125 if (hexa.length() % 2 != 0) {
126 hexa = "0" + hexa;
127 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530128 if (count == 7 && bytes.length == 8) {
129 systemId = systemId + hexa + "-";
130 } else {
131 systemId = systemId + hexa;
132 }
mohamed rahile04626f2016-04-05 20:42:53 +0530133 if (systemId.length() == 4 || systemId.length() == 9
134 || systemId.length() == 14) {
135 systemId = systemId + ".";
136 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530137 count++;
mohamed rahile04626f2016-04-05 20:42:53 +0530138 }
139 return systemId;
140 }
141
142 /**
143 * Parse byte array to area address.
144 *
145 * @param bytes area address
sunishvka1dfc3e2016-04-16 12:24:47 +0530146 * @return areaAddres area address
mohamed rahile04626f2016-04-05 20:42:53 +0530147 */
148 public static String areaAddres(byte[] bytes) {
sunishvka1dfc3e2016-04-16 12:24:47 +0530149 String areaAddres = "";
mohamed rahile04626f2016-04-05 20:42:53 +0530150 for (Byte byt : bytes) {
151 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
152 if (hexa.length() % 2 != 0) {
153 hexa = "0" + hexa;
154 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530155 areaAddres = areaAddres + hexa;
mohamed rahile04626f2016-04-05 20:42:53 +0530156 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530157 return areaAddres;
mohamed rahile04626f2016-04-05 20:42:53 +0530158 }
159
160 /**
161 * Parse area address to bytes.
162 *
163 * @param address area address
164 * @return areaAddress area address
165 */
sunishvka1dfc3e2016-04-16 12:24:47 +0530166 public static List<Byte> areaAddressToBytes(String address) {
167 List<Byte> idList = new ArrayList<>();
168 for (int i = 0; i < address.length(); i = i + 2) {
169 Character c1 = address.charAt(i);
170 Character c2 = address.charAt(i + 1);
171 String str = c1.toString() + c2.toString();
172 idList.add((byte) Integer.parseInt(str, 16));
mohamed rahile04626f2016-04-05 20:42:53 +0530173 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530174 return idList;
mohamed rahile04626f2016-04-05 20:42:53 +0530175 }
176
177 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530178 * Returns PDU headaer length.
179 *
180 * @param pduType PDU type
181 * @return headerLength header length
182 */
183 public static int getPduHeaderLength(int pduType) {
184 int headerLength = 0;
185 switch (IsisPduType.get(pduType)) {
186 case L1HELLOPDU:
187 case L2HELLOPDU:
188 case L1LSPDU:
189 case L2LSPDU:
190 headerLength = IsisConstants.HELLOHEADERLENGTH;
191 break;
192 case P2PHELLOPDU:
193 headerLength = IsisConstants.P2PHELLOHEADERLENGTH;
194 break;
195 case L1PSNP:
196 case L2PSNP:
197 headerLength = IsisConstants.PSNPDUHEADERLENGTH;
198 break;
199 case L1CSNP:
200 case L2CSNP:
201 headerLength = IsisConstants.CSNPDUHEADERLENGTH;
202 break;
203 default:
204 break;
205 }
206 return headerLength;
207 }
208
209 /**
sunishvka1dfc3e2016-04-16 12:24:47 +0530210 * Adds the PDU length in packet.
mohamed rahile04626f2016-04-05 20:42:53 +0530211 *
sunishvka1dfc3e2016-04-16 12:24:47 +0530212 * @param isisPacket ISIS packet
213 * @param lengthBytePos1 length byte position
214 * @param lengthBytePos2 length byte position
215 * @param reservedBytePos reserved byte position
216 * @return byte array with PDU length
mohamed rahile04626f2016-04-05 20:42:53 +0530217 */
sunishvka1dfc3e2016-04-16 12:24:47 +0530218 public static byte[] addLengthAndMarkItInReserved(byte[] isisPacket, int lengthBytePos1,
219 int lengthBytePos2, int reservedBytePos) {
220 //Set the length of the packet
221 //Get the total length of the packet
222 int length = isisPacket.length;
223 //Convert the lenth to two bytes as the length field is 2 bytes
224 byte[] lenthInTwoBytes = IsisUtil.convertToTwoBytes(length);
225 //isis header 3rd and 4th position represents length
226 isisPacket[lengthBytePos1] = lenthInTwoBytes[0]; //assign 1st byte in lengthBytePos1
227 isisPacket[lengthBytePos2] = lenthInTwoBytes[1]; //assign 2st byte in lengthBytePos2
228 isisPacket[reservedBytePos] = (byte) lengthBytePos1;
229 return isisPacket;
230 }
231
232 /**
233 * Adds the checksum in packet.
234 *
235 * @param isisPacket ISIS packet
236 * @param checksumBytePos1 checksum byte position
237 * @param checksumBytePos2 checksum byte position
238 * @return byte array with PDU length
239 */
240 public static byte[] addChecksum(byte[] isisPacket, int checksumBytePos1, int checksumBytePos2) {
241 //Set the checksum for the packet
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530242 //Convert the length to two bytes as the length field is 2 bytes
sunishvka1dfc3e2016-04-16 12:24:47 +0530243 byte[] checksumInTwoBytes = new ChecksumCalculator().calculateLspChecksum(
244 isisPacket, checksumBytePos1, checksumBytePos2);
245 //isis header 3rd and 4th position represents length
246 isisPacket[checksumBytePos1] = checksumInTwoBytes[0];
247 isisPacket[checksumBytePos2] = checksumInTwoBytes[1];
248 return isisPacket;
249 }
250
251 /**
252 * Adds frame a packet of 1498 of size.
253 *
254 * @param isisPacket ISIS packet
255 * @param interfaceIndex interface index
256 * @return byte array with 1498 is the length
257 */
258 public static byte[] framePacket(byte[] isisPacket, int interfaceIndex) {
259 //Set the length of the packet
260 //Get the total length of the packet
261 int length = isisPacket.length;
262 //PDU_LENGTH + 1 byte for interface index
263 if (length < IsisConstants.PDU_LENGTH + 1) {
264 byte[] bytes = new byte[IsisConstants.PDU_LENGTH + 1];
265 System.arraycopy(isisPacket, 0, bytes, 0, length);
266 bytes[IsisConstants.PDU_LENGTH] = (byte) interfaceIndex;
267 return bytes;
mohamed rahile04626f2016-04-05 20:42:53 +0530268 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530269 return isisPacket;
mohamed rahile04626f2016-04-05 20:42:53 +0530270 }
271
272 /**
273 * Parse source and LAN ID.
274 *
275 * @param id source and LAN ID
sunishvka1dfc3e2016-04-16 12:24:47 +0530276 * @return source and LAN ID
mohamed rahile04626f2016-04-05 20:42:53 +0530277 */
278 public static List<Byte> sourceAndLanIdToBytes(String id) {
sunishvka1dfc3e2016-04-16 12:24:47 +0530279 List<Byte> idList = new ArrayList<>();
280 StringTokenizer tokenizer = new StringTokenizer(id, "." + "-");
mohamed rahile04626f2016-04-05 20:42:53 +0530281 while (tokenizer.hasMoreElements()) {
282 int i = 0;
283 String str = tokenizer.nextToken();
sunishvka1dfc3e2016-04-16 12:24:47 +0530284 idList.add((byte) Integer.parseInt(str.substring(0, i + 2), 16));
mohamed rahile04626f2016-04-05 20:42:53 +0530285 if (str.length() > 2) {
sunishvka1dfc3e2016-04-16 12:24:47 +0530286 idList.add((byte) Integer.parseInt(str.substring(i + 2, str.length()), 16));
mohamed rahile04626f2016-04-05 20:42:53 +0530287 }
mohamed rahile04626f2016-04-05 20:42:53 +0530288 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530289 return idList;
mohamed rahile04626f2016-04-05 20:42:53 +0530290 }
291
292 /**
293 * Parse padding for PDU based on current length.
294 *
295 * @param currentLength current length
296 * @return byteArray padding array
297 */
sunishvka1dfc3e2016-04-16 12:24:47 +0530298 public static byte[] getPaddingTlvs(int currentLength) {
mohamed rahile04626f2016-04-05 20:42:53 +0530299 List<Byte> bytes = new ArrayList<>();
sunishvka1dfc3e2016-04-16 12:24:47 +0530300 while (IsisConstants.PDU_LENGTH > currentLength) {
301 int length = IsisConstants.PDU_LENGTH - currentLength;
mohamed rahile04626f2016-04-05 20:42:53 +0530302 TlvHeader tlvHeader = new TlvHeader();
sunishvka1dfc3e2016-04-16 12:24:47 +0530303 tlvHeader.setTlvType(TlvType.PADDING.value());
mohamed rahile04626f2016-04-05 20:42:53 +0530304 if (length >= PADDING_FIXED_LENGTH) {
305 tlvHeader.setTlvLength(PADDING_FIXED_LENGTH);
306 } else {
sunishvka1dfc3e2016-04-16 12:24:47 +0530307 tlvHeader.setTlvLength(IsisConstants.PDU_LENGTH - (currentLength + TLVHEADERLENGTH));
mohamed rahile04626f2016-04-05 20:42:53 +0530308 }
309 PaddingTlv tlv = new PaddingTlv(tlvHeader);
310 bytes.addAll(Bytes.asList(tlv.asBytes()));
sunishvka1dfc3e2016-04-16 12:24:47 +0530311 currentLength = currentLength + tlv.tlvLength() + TLVHEADERLENGTH;
mohamed rahile04626f2016-04-05 20:42:53 +0530312 }
313 byte[] byteArray = new byte[bytes.size()];
314 int i = 0;
315 for (byte byt : bytes) {
316 byteArray[i++] = byt;
317 }
318 return byteArray;
mohamed rahile04626f2016-04-05 20:42:53 +0530319 }
320
321 /**
322 * Converts an integer to two bytes.
323 *
324 * @param numberToConvert number to convert
325 * @return numInBytes given number as bytes
326 */
327 public static byte[] convertToTwoBytes(int numberToConvert) {
328
329 byte[] numInBytes = new byte[2];
330 String s1 = Integer.toHexString(numberToConvert);
331 if (s1.length() % 2 != 0) {
332 s1 = "0" + s1;
333 }
334 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
335 if (hexas.length == 1) {
336 numInBytes[0] = 0;
337 numInBytes[1] = hexas[0];
338 } else {
339 numInBytes[0] = hexas[0];
340 numInBytes[1] = hexas[1];
341 }
342 return numInBytes;
343 }
344
345 /**
346 * Converts a number to four bytes.
347 *
348 * @param numberToConvert number to convert
349 * @return numInBytes given number as bytes
350 */
351 public static byte[] convertToFourBytes(int numberToConvert) {
mohamed rahile04626f2016-04-05 20:42:53 +0530352 byte[] numInBytes = new byte[4];
353 String s1 = Integer.toHexString(numberToConvert);
354 if (s1.length() % 2 != 0) {
355 s1 = "0" + s1;
356 }
357 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
358 if (hexas.length == 1) {
359 numInBytes[0] = 0;
360 numInBytes[1] = 0;
361 numInBytes[2] = 0;
362 numInBytes[3] = hexas[0];
363 } else if (hexas.length == 2) {
364 numInBytes[0] = 0;
365 numInBytes[1] = 0;
366 numInBytes[2] = hexas[0];
367 numInBytes[3] = hexas[1];
368 } else if (hexas.length == 3) {
369 numInBytes[0] = 0;
370 numInBytes[1] = hexas[0];
371 numInBytes[2] = hexas[1];
372 numInBytes[3] = hexas[2];
373 } else {
374 numInBytes[0] = hexas[0];
375 numInBytes[1] = hexas[1];
376 numInBytes[2] = hexas[2];
377 numInBytes[3] = hexas[3];
378 }
379 return numInBytes;
380 }
381
sunishvka1dfc3e2016-04-16 12:24:47 +0530382 /**
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530383 * Returns the P2P hello PDU.
384 *
385 * @param isisInterface ISIS interface instance
386 * @param paddingEnabled padding enabled or not
387 * @return hello PDU
388 */
389 public static byte[] getP2pHelloPdu(IsisInterface isisInterface, boolean paddingEnabled) {
390 IsisHeader isisHeader = new IsisHeader();
391 isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR);
392 isisHeader.setPduHeaderLength((byte) IsisConstants.P2PHELLOHEADERLENGTH);
393 isisHeader.setVersion((byte) IsisConstants.ISISVERSION);
394 isisHeader.setIdLength((byte) IsisConstants.IDLENGTH);
395 isisHeader.setIsisPduType(IsisPduType.P2PHELLOPDU.value());
396 isisHeader.setVersion2((byte) IsisConstants.ISISVERSION);
397 //isisHeader.setReserved((byte) IsisConstants.RESERVED);
398 isisHeader.setReserved((byte) IsisConstants.PDULENGTHPOSITION);
399 isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS);
400 P2PHelloPdu p2pHelloPdu = new P2PHelloPdu(isisHeader);
401 p2pHelloPdu.setCircuitType((byte) isisInterface.reservedPacketCircuitType());
402 p2pHelloPdu.setSourceId(isisInterface.systemId());
403 p2pHelloPdu.setHoldingTime(isisInterface.holdingTime());
404 p2pHelloPdu.setPduLength(IsisConstants.PDU_LENGTH);
405 p2pHelloPdu.setLocalCircuitId((byte) IsisConstants.LOCALCIRCUITIDFORP2P);
406
407 TlvHeader tlvHeader = new TlvHeader();
408 tlvHeader.setTlvType(TlvType.AREAADDRESS.value());
409 tlvHeader.setTlvLength(0);
410 AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
411 areaAddressTlv.addAddress(isisInterface.areaAddress());
412 p2pHelloPdu.addTlv(areaAddressTlv);
413
414 tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value());
415 tlvHeader.setTlvLength(0);
416 ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader);
417 protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED);
418 p2pHelloPdu.addTlv(protocolSupportedTlv);
419
420 tlvHeader.setTlvType(TlvType.ADJACENCYSTATE.value());
421 tlvHeader.setTlvLength(0);
422 AdjacencyStateTlv adjacencyStateTlv = new AdjacencyStateTlv(tlvHeader);
423 adjacencyStateTlv.setAdjacencyType((byte) IsisInterfaceState.DOWN.value());
424 adjacencyStateTlv.setLocalCircuitId(Integer.parseInt(isisInterface.circuitId()));
425 Set<MacAddress> neighbors = isisInterface.neighbors();
426 if (neighbors.size() > 0) {
427 IsisNeighbor neighbor = isisInterface.lookup(neighbors.iterator().next());
428 adjacencyStateTlv.setAdjacencyType((byte) neighbor.interfaceState().value());
429 adjacencyStateTlv.setNeighborSystemId(neighbor.neighborSystemId());
430 adjacencyStateTlv.setNeighborLocalCircuitId(neighbor.localExtendedCircuitId());
431 }
432 p2pHelloPdu.addTlv(adjacencyStateTlv);
433
434 tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value());
435 tlvHeader.setTlvLength(0);
436 IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader);
437 ipInterfaceAddressTlv.addInterfaceAddres(isisInterface.interfaceIpAddress());
438 p2pHelloPdu.addTlv(ipInterfaceAddressTlv);
439
440 byte[] beforePadding = p2pHelloPdu.asBytes();
441 byte[] helloMessage;
442 if (paddingEnabled) {
443 byte[] paddingTlvs = getPaddingTlvs(beforePadding.length);
444 helloMessage = Bytes.concat(beforePadding, paddingTlvs);
445 } else {
446 helloMessage = beforePadding;
447 }
448 return helloMessage;
449 }
450
451 /**
452 * Returns the L1 hello PDU.
453 *
454 * @param isisInterface ISIS interface instance
455 * @param paddingEnabled padding enabled or not
456 * @return helloMessage hello PDU
457 */
458 public static byte[] getL1HelloPdu(IsisInterface isisInterface, boolean paddingEnabled) {
459 return getL1OrL2HelloPdu(isisInterface, IsisPduType.L1HELLOPDU, paddingEnabled);
460 }
461
462 /**
463 * Returns the L2 hello PDU.
464 *
465 * @param isisInterface ISIS interface instance
466 * @param paddingEnabled padding enabled or not
467 * @return helloMessage hello PDU
468 */
469 public static byte[] getL2HelloPdu(IsisInterface isisInterface, boolean paddingEnabled) {
470 return getL1OrL2HelloPdu(isisInterface, IsisPduType.L2HELLOPDU, paddingEnabled);
471 }
472
473 /**
474 * Returns the hello PDU.
475 *
476 * @param isisInterface ISIS interface instance
477 * @param paddingEnabled padding enabled or not
478 * @return helloMessage hello PDU
479 */
480 private static byte[] getL1OrL2HelloPdu(IsisInterface isisInterface, IsisPduType isisPduType,
481 boolean paddingEnabled) {
482 String lanId = "";
483 IsisHeader isisHeader = new IsisHeader();
484 isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR);
485 isisHeader.setPduHeaderLength((byte) IsisConstants.HELLOHEADERLENGTH);
486 isisHeader.setVersion((byte) IsisConstants.ISISVERSION);
487 isisHeader.setIdLength((byte) IsisConstants.IDLENGTH);
488 if (isisPduType == IsisPduType.L1HELLOPDU) {
489 isisHeader.setIsisPduType(IsisPduType.L1HELLOPDU.value());
490 lanId = isisInterface.l1LanId();
491 } else if (isisPduType == IsisPduType.L2HELLOPDU) {
492 isisHeader.setIsisPduType(IsisPduType.L2HELLOPDU.value());
493 lanId = isisInterface.l2LanId();
494 }
495 isisHeader.setVersion2((byte) IsisConstants.ISISVERSION);
496 isisHeader.setReserved((byte) IsisConstants.PDULENGTHPOSITION);
497 isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS);
498 L1L2HelloPdu l1L2HelloPdu = new L1L2HelloPdu(isisHeader);
499 l1L2HelloPdu.setCircuitType((byte) isisInterface.reservedPacketCircuitType());
500 l1L2HelloPdu.setSourceId(isisInterface.systemId());
501 l1L2HelloPdu.setHoldingTime(isisInterface.holdingTime());
502 l1L2HelloPdu.setPduLength(IsisConstants.PDU_LENGTH);
503 l1L2HelloPdu.setPriority((byte) isisInterface.priority());
504 l1L2HelloPdu.setLanId(lanId);
505 TlvHeader tlvHeader = new TlvHeader();
506 tlvHeader.setTlvType(TlvType.AREAADDRESS.value());
507 tlvHeader.setTlvLength(0);
508 AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
509 areaAddressTlv.addAddress(isisInterface.areaAddress());
510 l1L2HelloPdu.addTlv(areaAddressTlv);
511 Set<MacAddress> neighbors = isisInterface.neighbors();
512 if (neighbors.size() > 0) {
513 tlvHeader.setTlvType(TlvType.ISNEIGHBORS.value());
514 tlvHeader.setTlvLength(0);
515 IsisNeighborTlv isisNeighborTlv = new IsisNeighborTlv(tlvHeader);
516 for (MacAddress neighbor : neighbors) {
517 isisNeighborTlv.addNeighbor(neighbor);
518 }
519 l1L2HelloPdu.addTlv(isisNeighborTlv);
520 }
521 tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value());
522 tlvHeader.setTlvLength(0);
523 ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader);
524 protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED);
525 l1L2HelloPdu.addTlv(protocolSupportedTlv);
526
527 tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value());
528 tlvHeader.setTlvLength(0);
529 IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader);
530 ipInterfaceAddressTlv.addInterfaceAddres(isisInterface.interfaceIpAddress());
531 l1L2HelloPdu.addTlv(ipInterfaceAddressTlv);
532
533 byte[] beforePadding = l1L2HelloPdu.asBytes();
534 byte[] helloMessage;
535 if (paddingEnabled) {
536 byte[] paddingTlvs = getPaddingTlvs(beforePadding.length);
537 helloMessage = Bytes.concat(beforePadding, paddingTlvs);
538 } else {
539 helloMessage = beforePadding;
540 }
541 return helloMessage;
542 }
543
544 /**
sunishvka1dfc3e2016-04-16 12:24:47 +0530545 * Converts a byte to integer variable.
546 *
547 * @param bytesToConvert bytes to convert
548 * @return integer representation of bytes
549 */
550 public static int byteToInteger(byte[] bytesToConvert) {
551 final StringBuilder builder = new StringBuilder();
552 for (byte eachByte : bytesToConvert) {
553 builder.append(String.format("%02x", eachByte));
554 }
555 int number = Integer.parseInt(builder.toString(), 16);
556 return number;
mohamed rahile04626f2016-04-05 20:42:53 +0530557 }
sunishvka1dfc3e2016-04-16 12:24:47 +0530558
559 /**
560 * Converts a byte to long variable.
561 *
562 * @param bytesToConvert bytes to convert
563 * @return long representation of bytes
564 */
565 public static long byteToLong(byte[] bytesToConvert) {
566 final StringBuilder builder = new StringBuilder();
567 for (byte eachByte : bytesToConvert) {
568 builder.append(String.format("%02x", eachByte));
569 }
570 long number = Long.parseLong(builder.toString(), 16);
571 return number;
572 }
573
574 /**
575 * Converts a number to four bytes.
576 *
577 * @param numberToConvert number to convert
578 * @return numInBytes given number as bytes
579 */
580 public static byte[] convertToFourBytes(long numberToConvert) {
581 byte[] numInBytes = new byte[4];
582 String s1 = Long.toHexString(numberToConvert);
583 if (s1.length() % 2 != 0) {
584 s1 = "0" + s1;
585 }
586 if (s1.length() == 16) {
587 s1 = s1.substring(8, s1.length());
588 }
589 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
590 if (hexas.length == 1) {
591 numInBytes[0] = 0;
592 numInBytes[1] = 0;
593 numInBytes[2] = 0;
594 numInBytes[3] = hexas[0];
595 } else if (hexas.length == 2) {
596 numInBytes[0] = 0;
597 numInBytes[1] = 0;
598 numInBytes[2] = hexas[0];
599 numInBytes[3] = hexas[1];
600 } else if (hexas.length == 3) {
601 numInBytes[0] = 0;
602 numInBytes[1] = hexas[0];
603 numInBytes[2] = hexas[1];
604 numInBytes[3] = hexas[2];
605 } else {
606 numInBytes[0] = hexas[0];
607 numInBytes[1] = hexas[1];
608 numInBytes[2] = hexas[2];
609 numInBytes[3] = hexas[3];
610 }
611 return numInBytes;
612 }
613
614 /**
615 * Converts a number to eight bit binary.
616 *
617 * @param binaryString string to binary
618 * @return numInBytes given number as bytes
619 */
620 public static String toEightBitBinary(String binaryString) {
621 String eightBit = binaryString;
622 if (eightBit.length() % 8 != 0) {
623 int numOfZero = 8 - eightBit.length();
624 while (numOfZero > 0) {
625 eightBit = "0" + eightBit;
626 numOfZero--;
627 }
628 }
629 return eightBit;
630 }
631
632 /**
633 * Converts a number to four bit binary.
634 *
635 * @param binaryString string to binary
636 * @return numInBytes given number as bytes
637 */
638 public static String toFourBitBinary(String binaryString) {
639 String fourBit = binaryString;
640 if (fourBit.length() % 4 != 0) {
641 int numOfZero = 4 - fourBit.length();
642 while (numOfZero > 0) {
643 fourBit = "0" + fourBit;
644 numOfZero--;
645 }
646 }
647 return fourBit;
648 }
649
650 /**
651 * Converts a number to three bytes.
652 *
653 * @param numberToConvert number to convert
654 * @return given number as bytes
655 */
656 public static byte[] convertToThreeBytes(int numberToConvert) {
657 byte[] numInBytes = new byte[4];
658 String s1 = Integer.toHexString(numberToConvert);
659 if (s1.length() % 2 != 0) {
660 s1 = "0" + s1;
661 }
662 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
663 if (hexas.length == 1) {
664 numInBytes[0] = 0;
665 numInBytes[1] = 0;
666 numInBytes[2] = hexas[0];
667 } else if (hexas.length == 2) {
668 numInBytes[0] = 0;
669 numInBytes[1] = hexas[0];
670 numInBytes[2] = hexas[1];
671 } else {
672 numInBytes[0] = hexas[0];
673 numInBytes[1] = hexas[1];
674 numInBytes[2] = hexas[2];
675 }
676 return numInBytes;
677 }
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530678
679 /**
680 * Converts the bytes of prefix to string type value.
681 *
682 * @param bytes array of prefix
683 * @return string value of prefix
684 */
685 public static String prefixConversion(byte[] bytes) {
686 String prefix = "";
687 for (int i = 0; i < bytes.length; i++) {
688 if (i < (bytes.length - 1)) {
689 prefix = prefix + bytes[i] + ".";
690 } else {
691 prefix = prefix + bytes[i];
692 }
693 }
694 return prefix;
695 }
696}