blob: e929f8b4bf0e28fb4e835aa15e6c64bdc50efc49 [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
18
19import com.google.common.base.MoreObjects;
20import com.google.common.primitives.Bytes;
21import org.onosproject.isis.io.isispacket.tlv.PaddingTlv;
22import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
23
24import javax.xml.bind.DatatypeConverter;
25import java.util.ArrayList;
26import java.util.List;
27import java.util.StringTokenizer;
28
29/**
30 * Represents ISIS utils.
31 */
32public final class IsisUtil {
33 public static final int AREAADDRESS = 1;
34 public static final int ISREACHABILITY = 2;
35 public static final int ISNEIGHBORS = 6;
36 public static final int PADDING = 8;
37 public static final int LSPENTRY = 9;
38 public static final int AUTHENTICATION = 10;
39 public static final int CHECKSUM = 12;
40 public static final int EXTENDEDISREACHABILITY = 22;
41 public static final int ISALIAS = 24;
42 public static final int IPINTERNALREACHABILITY = 128;
43 public static final int PROTOCOLSUPPORTED = 129;
44 public static final int IPEXTERNALREACHABILITY = 130;
45 public static final int IDRPINFORMATION = 131;
46 public static final int IPINTERFACEADDRESS = 132;
47 public static final int L1HELLOPDU = 1;
48 public static final int L2HELLOPDU = 2;
49 public static final int P2PHELLOPDU = 3;
50 public static final int L1LSPDU = 18;
51 public static final int L2LSPDU = 20;
52 public static final int L1CSNP = 24;
53 public static final int L2CSNP = 25;
54 public static final int L1PSNP = 26;
55 public static final int L2PSNP = 27;
56 public static final int L1L2_LS_PDUHEADERLENGTH = 27;
57 public static final int P2PPDUHEADERLENGTH = 20;
58 public static final int PSNPPDUHEADERLENGTH = 17;
59 public static final char ETHER_FRAME_LEN = 1514;
60 public static final int ID_SIX_BYTES = 6;
61 public static final int ID_PLUS_ONE_BYTE = 7;
62 public static final int ID_PLUS_TWO_BYTE = 8;
63 public static final int THREE_BYTES = 3;
64 public static final int SIX_BYTES = 6;
65 public static final int FOUR_BYTES = 4;
66 public static final int PADDING_FIXED_LENGTH = 255;
67
68 /**
69 * Creates an instance of this class.
70 */
71 private IsisUtil() {
72
73 }
74
75 /**
76 * Parse byte array to string system ID.
77 *
78 * @param bytes system ID
79 * @return systemId system ID.
80 */
81 public static String systemId(byte[] bytes) {
82 String systemId = "";
83 for (Byte byt : bytes) {
84 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
85 if (hexa.length() % 2 != 0) {
86 hexa = "0" + hexa;
87 }
88 systemId = systemId + hexa;
89 if (systemId.length() == 4 || systemId.length() == 9) {
90 systemId = systemId + ".";
91 }
92 }
93 return systemId;
94 }
95
96 /**
97 * Parse byte array to LAN ID.
98 *
99 * @param bytes LAN ID
100 * @return systemId system ID.
101 */
102 public static String systemIdPlus(byte[] bytes) {
103 String systemId = "";
104 for (Byte byt : bytes) {
105 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
106 if (hexa.length() % 2 != 0) {
107 hexa = "0" + hexa;
108 }
109 systemId = systemId + hexa;
110 if (systemId.length() == 4 || systemId.length() == 9
111 || systemId.length() == 14) {
112 systemId = systemId + ".";
113 }
114 }
115 return systemId;
116 }
117
118 /**
119 * Parse byte array to area address.
120 *
121 * @param bytes area address
122 * @return areaAddress area address
123 */
124 public static String areaAddres(byte[] bytes) {
125 int count = 0;
126 String areaAddress = "";
127 for (Byte byt : bytes) {
128 String hexa = Integer.toHexString(Byte.toUnsignedInt(byt));
129 if (hexa.length() % 2 != 0) {
130 hexa = "0" + hexa;
131 }
132 if (count == 0) {
133 hexa = hexa + ".";
134 }
135 areaAddress = areaAddress + hexa;
136 count++;
137 }
138 return areaAddress;
139 }
140
141 /**
142 * Parse area address to bytes.
143 *
144 * @param address area address
145 * @return areaAddress area address
146 */
147 public static List<Byte> areaAddresToBytes(String address) {
148 List<Byte> idLst = new ArrayList();
149 StringTokenizer tokenizer = new StringTokenizer(address, ".");
150 int count = 0;
151 while (tokenizer.hasMoreElements()) {
152 String str = tokenizer.nextToken();
153 if (str.length() % 2 != 0) {
154 str = "0" + str;
155 }
156 if (count > 0) {
157
158 for (int i = 0; i < str.length(); i = i + 2) {
159 idLst.add((byte) Integer.parseInt(str.substring(i, i + 2), 16));
160 }
161 } else {
162 idLst.add((byte) Integer.parseInt(str, 16));
163 }
164 count++;
165 }
166 return idLst;
167 }
168
169 /**
170 * Gets PDU header length.
171 *
172 * @param pduType PDU type
173 * @return headerLength header length
174 */
175 public static int getPduHeaderLength(int pduType) {
176 int headerLength = 0;
177 switch (pduType) {
178 case L1HELLOPDU:
179 case L2HELLOPDU:
180 case L1LSPDU:
181 case L2LSPDU:
182 headerLength = L1L2_LS_PDUHEADERLENGTH;
183 break;
184 case P2PHELLOPDU:
185 headerLength = P2PPDUHEADERLENGTH;
186 break;
187 case L1PSNP:
188 case L2PSNP:
189 headerLength = PSNPPDUHEADERLENGTH;
190 break;
191 default:
192 break;
193 }
194 return headerLength;
195 }
196
197 /**
198 * Parse source and LAN ID.
199 *
200 * @param id source and LAN ID
201 * @return sourceAndLanIdToBytes source and LAN ID
202 */
203 public static List<Byte> sourceAndLanIdToBytes(String id) {
204 List<Byte> idLst = new ArrayList();
205
206 StringTokenizer tokenizer = new StringTokenizer(id, ".");
207 while (tokenizer.hasMoreElements()) {
208 int i = 0;
209 String str = tokenizer.nextToken();
210 idLst.add((byte) Integer.parseInt(str.substring(0, i + 2), 16));
211 if (str.length() > 2) {
212 idLst.add((byte) Integer.parseInt(str.substring(i + 2, str.length()), 16));
213 }
214
215 }
216 return idLst;
217 }
218
219 /**
220 * Parse padding for PDU based on current length.
221 *
222 * @param currentLength current length
223 * @return byteArray padding array
224 */
225 public static byte[] paddingForPdu(int currentLength) {
226 List<Byte> bytes = new ArrayList<>();
227 while (ETHER_FRAME_LEN > currentLength) {
228 int length = ETHER_FRAME_LEN - currentLength;
229 TlvHeader tlvHeader = new TlvHeader();
230 tlvHeader.setTlvType(PADDING);
231 if (length >= PADDING_FIXED_LENGTH) {
232 tlvHeader.setTlvLength(PADDING_FIXED_LENGTH);
233 } else {
234 tlvHeader.setTlvLength(ETHER_FRAME_LEN - currentLength);
235 }
236 PaddingTlv tlv = new PaddingTlv(tlvHeader);
237 bytes.addAll(Bytes.asList(tlv.asBytes()));
238 currentLength = currentLength + tlv.tlvLength();
239 }
240 byte[] byteArray = new byte[bytes.size()];
241 int i = 0;
242 for (byte byt : bytes) {
243 byteArray[i++] = byt;
244 }
245 return byteArray;
246
247 }
248
249 /**
250 * Converts an integer to two bytes.
251 *
252 * @param numberToConvert number to convert
253 * @return numInBytes given number as bytes
254 */
255 public static byte[] convertToTwoBytes(int numberToConvert) {
256
257 byte[] numInBytes = new byte[2];
258 String s1 = Integer.toHexString(numberToConvert);
259 if (s1.length() % 2 != 0) {
260 s1 = "0" + s1;
261 }
262 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
263 if (hexas.length == 1) {
264 numInBytes[0] = 0;
265 numInBytes[1] = hexas[0];
266 } else {
267 numInBytes[0] = hexas[0];
268 numInBytes[1] = hexas[1];
269 }
270 return numInBytes;
271 }
272
273 /**
274 * Converts a number to four bytes.
275 *
276 * @param numberToConvert number to convert
277 * @return numInBytes given number as bytes
278 */
279 public static byte[] convertToFourBytes(int numberToConvert) {
280
281 byte[] numInBytes = new byte[4];
282 String s1 = Integer.toHexString(numberToConvert);
283 if (s1.length() % 2 != 0) {
284 s1 = "0" + s1;
285 }
286 byte[] hexas = DatatypeConverter.parseHexBinary(s1);
287 if (hexas.length == 1) {
288 numInBytes[0] = 0;
289 numInBytes[1] = 0;
290 numInBytes[2] = 0;
291 numInBytes[3] = hexas[0];
292 } else if (hexas.length == 2) {
293 numInBytes[0] = 0;
294 numInBytes[1] = 0;
295 numInBytes[2] = hexas[0];
296 numInBytes[3] = hexas[1];
297 } else if (hexas.length == 3) {
298 numInBytes[0] = 0;
299 numInBytes[1] = hexas[0];
300 numInBytes[2] = hexas[1];
301 numInBytes[3] = hexas[2];
302 } else {
303 numInBytes[0] = hexas[0];
304 numInBytes[1] = hexas[1];
305 numInBytes[2] = hexas[2];
306 numInBytes[3] = hexas[3];
307 }
308 return numInBytes;
309 }
310
311 @Override
312 public String toString() {
313 return MoreObjects.toStringHelper(getClass())
314 .omitNullValues()
315 .toString();
316 }
317}