Support dataplane subnet mask & latency histogram
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / helper.lua
1 --
2 -- Copyright (c) 2020 Intel Corporation
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 --
16
17 function convertIPToHex(ip)
18   local address_chunks = {}
19   if type(ip) ~= "string" then
20     print ("IP ADDRESS ERROR: ", ip)
21     return "IP ADDRESS ERROR"
22   end
23
24   local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)(\/%d+)$")}
25   if #chunks == 5 then
26     for i,v in ipairs(chunks) do
27       if i < 5 then
28         if tonumber(v) > 255 then
29           print ("IPV4 ADDRESS ERROR: ", ip)
30           return "IPV4 ADDRESS ERROR"
31         end
32         address_chunks[#address_chunks + 1] = string.format ("%02x", v)
33       end
34     end
35     result = table.concat(address_chunks, " ")
36     print ("Hex IPV4: ", result)
37     return result
38   end
39
40   local chunks = {ip:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
41   if #chunks == 4 then
42     for i,v in ipairs(chunks) do
43       if tonumber(v) > 255 then
44         print ("IPV4 ADDRESS ERROR: ", ip)
45         return "IPV4 ADDRESS ERROR"
46       end
47       address_chunks[#address_chunks + 1] = string.format ("%02x", v)
48     end
49     result = table.concat(address_chunks, " ")
50     print ("Hex IPV4: ", result)
51     return result
52   end
53
54   delimiter = ":"
55   for match in (ip..delimiter):gmatch("(.-)"..delimiter) do
56     if match ~= "" then
57       number = tonumber(match, 16)
58       if number <= 65535 then
59         table.insert(address_chunks, string.format("%02x %02x",number/256,number % 256))
60       end
61     else
62       table.insert(address_chunks, "")
63     end
64   end
65   for i, chunk in ipairs(address_chunks) do
66     if chunk =="" then
67       table.remove(address_chunks, i)
68       for j = 1,(8-#address_chunks) do
69         table.insert(address_chunks, i, "00 00")
70       end
71       break
72     end
73   end
74   result = table.concat(address_chunks, " ")
75   print ("Hex IPV6: ", result)
76   return result
77 end