Skip to content

Instantly share code, notes, and snippets.

@dgjustice
Created December 12, 2022 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgjustice/78678d99f006bfc5c1abfc724e6a3bdf to your computer and use it in GitHub Desktop.
Save dgjustice/78678d99f006bfc5c1abfc724e6a3bdf to your computer and use it in GitHub Desktop.
Messing with YANG
use std::sync::Arc;
use std::fs::File;
use yang2::context::{Context, ContextFlags};
use yang2::data::{
Data, DataFormat, DataParserFlags, DataTree,
DataValidationFlags,
};
static SEARCH_DIR: &str = "/path/to/openconfig/public/";
fn main() -> std::io::Result<()> {
// Initialize context.
let mut ctx = Context::new(ContextFlags::NO_YANGLIBRARY)
.expect("Failed to create context");
ctx.set_searchdir(SEARCH_DIR)
.expect("Failed to set YANG search directory");
// Load YANG modules.
for module_name in &["openconfig-lldp"] {
ctx.load_module(module_name, None, &[])
.expect("Failed to load module");
}
let ctx = Arc::new(ctx);
let dtree = DataTree::parse_file(
&ctx,
File::open("/path/to/nxos-lldp.xml")?,
DataFormat::XML,
DataParserFlags::empty(),
DataValidationFlags::empty(),
)
.expect("Failed to parse data tree");
// Iterate over all nodes of the data tree.
println!("Iterating over all data nodes...");
for dnode in dtree.traverse() {
println!(" {}: {:?}", dnode.path(), dnode.value());
}
println!("Iterating over subpath only...");
for dnode in dtree
.find_xpath("/openconfig-lldp:lldp/interfaces")
.expect("Failed to find interfaces")
{
println!(" {}: {:?}", dnode.path(), dnode.value());
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment