Skip to content

Commit

Permalink
Reindent lib.yang.state
Browse files Browse the repository at this point in the history
  • Loading branch information
wingo committed Aug 3, 2017
1 parent 6f34faf commit 3930a04
Showing 1 changed file with 84 additions and 84 deletions.
168 changes: 84 additions & 84 deletions src/lib/yang/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,102 +11,102 @@ local counter = require("core.counter")
local counter_directory = "/apps"

local function flatten(val)
local rtn = {}
for k, v in pairs(val) do
if type(v) == "table" then
v = flatten(v)
for k1, v1 in pairs(v) do rtn[k1] = v1 end
else
rtn[k] = v
end
end
return rtn
local rtn = {}
for k, v in pairs(val) do
if type(v) == "table" then
v = flatten(v)
for k1, v1 in pairs(v) do rtn[k1] = v1 end
else
rtn[k] = v
end
end
return rtn
end

function find_counters(pid)
local path = shm.root.."/"..pid..counter_directory
local apps = {}
for _, c in pairs(lib.files_in_directory(path)) do
local counters = {}
local counterdir = "/"..pid..counter_directory.."/"..c
for k,v in pairs(shm.open_frame(counterdir)) do
if type(v) == "cdata" then
counters[k] = v.c
end
end
apps[c] = counters
end
return apps
local path = shm.root.."/"..pid..counter_directory
local apps = {}
for _, c in pairs(lib.files_in_directory(path)) do
local counters = {}
local counterdir = "/"..pid..counter_directory.."/"..c
for k,v in pairs(shm.open_frame(counterdir)) do
if type(v) == "cdata" then
counters[k] = v.c
end
end
apps[c] = counters
end
return apps
end

function collect_state_leaves(schema)
-- Iterate over schema looking fo state leaves at a specific path into the
-- schema. This should return a dictionary of leaf to lua path.
local function collection(scm, path)
local function newpath(oldpath)
return lib.deepcopy(oldpath)
end
if path == nil then path = {} end
table.insert(path, scm.id)

if scm.kind == "container" then
-- Iterate over the body and recursively call self on all children.
local rtn = {}
for _, child in pairs(scm.body) do
local leaves = collection(child, newpath(path))
table.insert(rtn, leaves)
end
return rtn
elseif scm.kind == "leaf" then
if scm.config == false then
local rtn = {}
rtn[path] = scm.id
return rtn
end
elseif scm.kind == "module" then
-- Iterate over schema looking fo state leaves at a specific path into the
-- schema. This should return a dictionary of leaf to lua path.
local function collection(scm, path)
local function newpath(oldpath)
return lib.deepcopy(oldpath)
end
if path == nil then path = {} end
table.insert(path, scm.id)

if scm.kind == "container" then
-- Iterate over the body and recursively call self on all children.
local rtn = {}
for _, child in pairs(scm.body) do
local leaves = collection(child, newpath(path))
table.insert(rtn, leaves)
end
return rtn
elseif scm.kind == "leaf" then
if scm.config == false then
local rtn = {}
for _, v in pairs(scm.body) do
-- We deliberately don't want to include the module in the path.
table.insert(rtn, collection(v, {}))
end
rtn[path] = scm.id
return rtn
end
return {}
end

local leaves = collection(schema)
if leaves == nil then return {} end
leaves = flatten(leaves)
return function () return leaves end
end
elseif scm.kind == "module" then
local rtn = {}
for _, v in pairs(scm.body) do
-- We deliberately don't want to include the module in the path.
table.insert(rtn, collection(v, {}))
end
return rtn
end
return {}
end

local leaves = collection(schema)
if leaves == nil then return {} end
leaves = flatten(leaves)
return function () return leaves end
end

local function set_data_value(data, path, value)
local head = yang_data.normalize_id(table.remove(path, 1))
if #path == 0 then
data[head] = value
return
end
if data[head] == nil then data[head] = {} end
set_data_value(data[head], path, value)
local head = yang_data.normalize_id(table.remove(path, 1))
if #path == 0 then
data[head] = value
return
end
if data[head] == nil then data[head] = {} end
set_data_value(data[head], path, value)
end

function show_state(scm, pid, raw_path)
local schema = yang.load_schema_by_name(scm)
local grammar = yang_data.data_grammar_from_schema(schema)
local counters = find_counters(pid)
local path = xpath.convert_path(grammar, raw_path)

-- Lookup the specific schema element that's being addressed by the path
local leaves = collect_state_leaves(schema)()
local data = {}
for leaf_path, leaf in pairs(leaves) do
for _, counter in pairs(counters) do
if counter[leaf] then
set_data_value(data, leaf_path, counter[leaf])
end
end
end
return data
local schema = yang.load_schema_by_name(scm)
local grammar = yang_data.data_grammar_from_schema(schema)
local counters = find_counters(pid)
local path = xpath.convert_path(grammar, raw_path)

-- Lookup the specific schema element that's being addressed by the path
local leaves = collect_state_leaves(schema)()
local data = {}
for leaf_path, leaf in pairs(leaves) do
for _, counter in pairs(counters) do
if counter[leaf] then
set_data_value(data, leaf_path, counter[leaf])
end
end
end
return data
end

function selftest ()
Expand Down Expand Up @@ -165,11 +165,11 @@ function selftest ()
end
local function in_array(needle, haystack)
for _, i in pairs(haystack) do if needle == i then return true end end
return false
return false
end

local simple_router_schema = yang.load_schema(simple_router_schema_src,
"state-test")
"state-test")
local leaves = collect_state_leaves(simple_router_schema)()

-- Check the correct number of leaves have been found
Expand Down

0 comments on commit 3930a04

Please sign in to comment.