blob: 7e75dc7390d997b208c54619095e6eb48169c9a3 [file] [log] [blame]
karthik1977bc5ea1e2023-01-02 19:25:14 +05301/*
2 * Copyright 2023-present Open Networking Foundation
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.netflow;
17
18import java.net.InetAddress;
19import java.net.UnknownHostException;
20import java.nio.ByteBuffer;
21import java.util.function.BiFunction;
22
23
24/**
25 * Netflow utility.
26 */
27public final class NetflowUtils {
28 private NetflowUtils() {
29 }
30
31 public static final BiFunction<ByteBuffer, Integer, Object> VAR_BYTE = (bb, len) -> {
32 return bb.get();
33 };
34
35 public static final BiFunction<ByteBuffer, Integer, Object> VAR_SHORT = (bb, len) -> {
36 return bb.getShort();
37 };
38
39 public static final BiFunction<ByteBuffer, Integer, Object> VAR_INT = (bb, len) -> {
40 return bb.getInt();
41 };
42 public static final BiFunction<ByteBuffer, Integer, Object> VAR_FLOAT = (bb, len) -> {
43 return bb.getFloat();
44 };
45 public static final BiFunction<ByteBuffer, Integer, Object> VAR_LONG = (bb, len) -> {
46 return bb.getLong();
47 };
48
49 public static final BiFunction<ByteBuffer, Integer, Object> NULL = (bb, len) -> {
50 byte[] address = new byte[len];
51 bb.get(address);
52 return null;
53 };
54
55 public static final BiFunction<ByteBuffer, Integer, Object> VAR_INT_LONG = (bb, len) -> {
56 if (len == 4) {
57 return bb.getInt();
58 }
59 return bb.getLong();
60 };
61
62 public static final BiFunction<ByteBuffer, Integer, Object> VAR_SHORT_INT = (bb, len) -> {
63 if (len == 2) {
64 return bb.getShort();
65 }
66 return bb.getInt();
67 };
68
69 public static final BiFunction<ByteBuffer, Integer, Object> VAR_IP_ADDRESS = (bb, len) -> {
70 return toInetAddress(bb, len);
71 };
72
73 public static final BiFunction<ByteBuffer, Integer, Object> VAR_MAC = (bb, len) -> {
74 byte[] mac = new byte[len];
75 bb.get(mac);
76 //return MacAddress.valueOf(mac);;
77 return null;
78 };
79
80 public static InetAddress toInetAddress(ByteBuffer bb, int length) {
81 byte[] address = new byte[length];
82 bb.get(address);
83
84 InetAddress ipAddress = null;
85 try {
86 ipAddress = InetAddress.getByAddress(address);
87 } catch (UnknownHostException e) {
88 throw new IllegalArgumentException("Invalid host buffer");
89 }
90
91 return ipAddress;
92 }
93
94}