d38bb80862989fcdfe55dae5bd2c91b085be3c81
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / helper.lua
1 --
2 -- Copyright (c) 2010-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+)$")}
25   if #chunks == 4 then
26     for i,v in ipairs(chunks) do
27       if tonumber(v) > 255 then
28         print ("IPV4 ADDRESS ERROR: ", ip)
29         return "IPV4 ADDRESS ERROR"
30       end
31       address_chunks[#address_chunks + 1] = string.format ("%02x", v)
32     end
33     result = table.concat(address_chunks, " ")
34     print ("Hex IPV4: ", result)
35     return result
36   end
37
38   delimiter = ":"
39   for match in (ip..delimiter):gmatch("(.-)"..delimiter) do
40     if match ~= "" then
41       number = tonumber(match, 16)
42       if number <= 65535 then
43         table.insert(address_chunks, string.format("%02x %02x",number/256,number % 256))
44       end
45     else
46       table.insert(address_chunks, "")
47     end
48   end
49   for i, chunk in ipairs(address_chunks) do
50     if chunk =="" then
51       table.remove(address_chunks, i)
52       for j = 1,(8-#address_chunks) do
53         table.insert(address_chunks, i, "00 00")
54       end
55       break
56     end
57   end
58   result = table.concat(address_chunks, " ")
59   print ("Hex IPV6: ", result)
60   return result
61 end