Skip to content

Commit

Permalink
lib.yang.list: integrate state, ptree support
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed Nov 4, 2022
1 parent adb40ef commit e8c9725
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/lib/ptree/ptree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local lib = require("core.lib")
local shm = require("core.shm")
local timer = require("core.timer")
local worker = require("core.worker")
local cltable = require("lib.cltable")
local cpuset = require("lib.cpuset")
local rrd = require("lib.rrd")
local scheduling = require("lib.scheduling")
Expand Down
22 changes: 9 additions & 13 deletions src/lib/ptree/support.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local path_mod = require("lib.yang.path")
local path_data = require("lib.yang.path_data")
local yang = require("lib.yang.yang")
local data = require("lib.yang.data")
local cltable = require("lib.cltable")

function compute_parent_paths(path)
local function sorted_keys(t)
Expand Down Expand Up @@ -43,19 +42,16 @@ local function add_child_objects(accum, grammar, config)
table.insert(accum, config)
return visit(grammar, config)
end
function visitor.table(grammar, config)
function visitor.list(grammar, config)
if grammar.value_ctype then
-- List entries are raw data and do not contain children with
-- distinct identities.
return
end
local child_grammar = {type="struct", members=grammar.values,
ctype=grammar.value_ctype}
if not grammar.key_ctype or grammar.native_key then
for k, v in pairs(config) do visit_child(child_grammar, v) end
elseif grammar.key_ctype and grammar.value_ctype then
-- Ctables are raw data, and raw data doesn't contain children
-- with distinct identity.
return
elseif grammar.key_ctype then
for k, v in cltable.pairs(config) do visit_child(child_grammar, v) end
else
error("unreachable")
for _, entry in ipairs(config) do
visit_child(child_grammar, entry)
end
end
function visitor.array(grammar, config)
Expand Down Expand Up @@ -88,7 +84,7 @@ local function compute_objects_maybe_updated_in_place (schema, config,
if subgrammar.type == 'scalar' then return objs end
table.insert(objs, getter(config))
-- Members of raw data can't be updated in place either.
if subgrammar.type == 'table' then
if subgrammar.type == 'list' then
if subgrammar.key_ctype and subgrammar.value_ctype then return objs end
elseif subgrammar.type == 'struct' then
if subgrammar.ctype then return objs end
Expand Down
29 changes: 11 additions & 18 deletions src/lib/yang/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ state_reader_from_schema_by_name = util.memoize(state_reader_from_schema_by_name

function selftest ()
print("selftest: lib.yang.state")
local ffi = require("ffi")
local simple_router_schema_src = [[module snabb-simple-router {
namespace snabb:simple-router;
prefix simple-router;
Expand Down Expand Up @@ -152,26 +153,18 @@ function selftest ()
uses "detailed-counters";
}
}]]
local function table_length(tbl)
local rtn = 0
for k,v in pairs(tbl) do rtn = rtn + 1 end
return rtn
end
local function in_array(needle, haystack)
for _, i in pairs(haystack) do if needle == i then return true end end
return false
end

local simple_router_schema = yang.load_schema(simple_router_schema_src,
"state-test")
local reader = state_reader_from_schema(simple_router_schema)
local state = reader({})
assert(0 == state.state.total_packets)
assert(0 == state.state.dropped_packets)
assert(0 == state.detailed_state.dropped_wrong_route)
assert(0 == state.detailed_state.dropped_not_permitted)
-- Would like to assert "state.routes == nil" but state is actually
-- a cdata object, and trying to access the non-existent routes
-- property throws an error.
local state = reader({
['total-packets'] = ffi.new("struct counter", {c=1}),
['dropped-packets'] = ffi.new("struct counter", {c=2}),
['dropped-wrong-route'] = ffi.new("struct counter", {c=3}),
['dropped-not-permitted'] = ffi.new("struct counter", {c=4})
})
assert(1 == state.state.total_packets)
assert(2 == state.state.dropped_packets)
assert(3 == state.detailed_state.dropped_wrong_route)
assert(4 == state.detailed_state.dropped_not_permitted)
print('selftest: ok')
end

0 comments on commit e8c9725

Please sign in to comment.