diff --git a/src/lib/ptree/ptree.lua b/src/lib/ptree/ptree.lua index f1add44a15..c5cf1bffe2 100644 --- a/src/lib/ptree/ptree.lua +++ b/src/lib/ptree/ptree.lua @@ -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") diff --git a/src/lib/ptree/support.lua b/src/lib/ptree/support.lua index aa14fc3f62..5488deec49 100644 --- a/src/lib/ptree/support.lua +++ b/src/lib/ptree/support.lua @@ -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) @@ -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) @@ -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 diff --git a/src/lib/yang/state.lua b/src/lib/yang/state.lua index 45096a5336..7846881537 100644 --- a/src/lib/yang/state.lua +++ b/src/lib/yang/state.lua @@ -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; @@ -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