Skip to content

Commit

Permalink
Map IS-IS System IDs to their corresponding hostnames
Browse files Browse the repository at this point in the history
Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Jan 8, 2025
1 parent 19da113 commit 8c1f3b2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/internal_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT
//

use std::collections::BTreeMap;
use std::fmt::Write;
use std::process::{Child, Command, Stdio};

Expand Down Expand Up @@ -757,6 +758,7 @@ pub(crate) fn cmd_show_isis_adjacency(
session: &mut Session,
mut args: ParsedArgs,
) -> Result<bool, String> {
let hostnames = isis_hostnames(session)?;
YangTableBuilder::new(session, DataType::All)
.xpath(XPATH_PROTOCOL)
.filter_list_key("type", Some(PROTOCOL_ISIS))
Expand All @@ -765,7 +767,13 @@ pub(crate) fn cmd_show_isis_adjacency(
.filter_list_key("name", get_opt_arg(&mut args, "name"))
.column_leaf("Interface", "name")
.xpath(XPATH_ISIS_ADJACENCY)
.column_leaf("System ID", "neighbor-sysid")
.column_from_fn(
"System ID",
Box::new(move |dnode| {
let system_id = dnode.child_value("neighbor-sysid");
hostnames.get(&system_id).cloned().unwrap_or(system_id)
}),
)
.column_leaf("SNPA", "neighbor-snpa")
.column_leaf("Level", "usage")
.column_leaf("State", "state")
Expand All @@ -780,14 +788,25 @@ pub(crate) fn cmd_show_isis_database(
session: &mut Session,
_args: ParsedArgs,
) -> Result<bool, String> {
let hostnames = isis_hostnames(session)?;
YangTableBuilder::new(session, DataType::All)
.xpath(XPATH_PROTOCOL)
.filter_list_key("type", Some(PROTOCOL_ISIS))
.column_leaf("Instance", "name")
.xpath(XPATH_ISIS_DATABASE)
.column_leaf("Level", "level")
.xpath(XPATH_ISIS_LSP)
.column_leaf("LSP ID", "lsp-id")
.column_from_fn(
"LSP ID",
Box::new(move |dnode| {
let mut lsp_id = dnode.child_value("lsp-id");
let system_id = &lsp_id[..14];
if let Some(hostname) = hostnames.get(system_id) {
lsp_id.replace_range(..14, hostname);
}
lsp_id
}),
)
.column_leaf_hex32("Sequence", "sequence")
.column_leaf_hex16("Checksum", "checksum")
.column_leaf("Lifetime", "remaining-lifetime")
Expand All @@ -796,6 +815,34 @@ pub(crate) fn cmd_show_isis_database(
Ok(false)
}

fn isis_hostnames(
session: &mut Session,
) -> Result<BTreeMap<String, String>, String> {
let xpath = format!(
"{}[type='{}'][name='{}']/ietf-isis:isis/hostnames",
XPATH_PROTOCOL, PROTOCOL_ISIS, "main"
);

// Fetch hostname mappings.
let data = fetch_data(session, DataType::State, &xpath)?;

// Collect hostname mappings into a binary tree.
let hostnames = data
.find_path(&xpath)
.unwrap()
.find_xpath("hostname")
.unwrap()
.filter_map(|dnode| {
Some((
dnode.child_opt_value("system-id")?,
dnode.child_opt_value("hostname")?,
))
})
.collect();

Ok(hostnames)
}

// ===== OSPF "show" commands =====

const PROTOCOL_OSPFV2: &str = "ietf-ospf:ospfv2";
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// SPDX-License-Identifier: MIT
//

#![feature(let_chains)]

mod client;
mod error;
mod internal_commands;
Expand Down

0 comments on commit 8c1f3b2

Please sign in to comment.