Skip to content

Commit

Permalink
Merge pull request #9 from jillesca/isis_fix
Browse files Browse the repository at this point in the history
Fix: isis metrics when only one neighbor is present. Enhance: Error handling.
  • Loading branch information
jillesca authored Feb 2, 2024
2 parents 4fec979 + 907a854 commit f6d0bd4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 29 deletions.
31 changes: 15 additions & 16 deletions ncpeek/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Optional, Union
from dataclasses import dataclass
from ncpeek.utils.text_utils import (
Expand Down Expand Up @@ -73,22 +74,19 @@ def _run(self) -> str:

def _process_device(self, device: dict) -> dict:
"""Processes a single device operation and parsing its reply."""
try:
device = NetconfDevice(**device)
rpc = NetconfSession(
device=device,
netconf_filter=self._netconf_filter,
operation=self._operation,
)
data_dict = convert_xml_to_dict(xml_string=rpc.reply())
parser = get_parser(netconf_filter=self._filter_id)
parsed_data = parser.parse(
data_to_parse=data_dict,
device=device,
netconf_filter_id=self._filter_id,
)
except Exception as err:
parsed_data = [{"error": f"{err=}"}]
device = NetconfDevice(**device)
rpc = NetconfSession(
device=device,
netconf_filter=self._netconf_filter,
operation=self._operation,
)
data_dict = convert_xml_to_dict(xml_string=rpc.reply())
parser = get_parser(netconf_filter=self._filter_id)
parsed_data = parser.parse(
data_to_parse=data_dict,
device=device,
netconf_filter_id=self._filter_id,
)
return parsed_data


Expand All @@ -99,6 +97,7 @@ def cli() -> None:
print(client.execute_cli())
except Exception as err:
print(f"Error found: {err=}")
sys.exit(2)


if __name__ == "__main__":
Expand Down
22 changes: 14 additions & 8 deletions ncpeek/parsers/cisco_ios_xe_isis_oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ def _group_data_by_system_id(self, data: Dict) -> Dict:
Dict: The grouped data.
"""
grouped_data = defaultdict(list)
for neighbor in data["data"]["isis-oper-data"]["isis-instance"][
neighbors = data["data"]["isis-oper-data"]["isis-instance"][
"isis-neighbor"
]:
]

if isinstance(neighbors, dict):
neighbors = [neighbors]
for neighbor in neighbors:
grouped_data[neighbor["system-id"]].append(
self._create_neighbor_dict(neighbor)
)
Expand Down Expand Up @@ -114,12 +118,14 @@ def _prepare_neighbor_data(self, grouped_data: Dict) -> List[Dict]:
{
"system-id": system_id,
"local_interfaces_status": interfaces,
"neighbor_status": 1
if any(
interface["isis_status"] == "isis-adj-up"
for interface in interfaces
)
else 0,
"neighbor_status": (
1
if any(
interface["isis_status"] == "isis-adj-up"
for interface in interfaces
)
else 0
),
}
for system_id, interfaces in grouped_data.items()
]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ncpeek"
version = "0.1.3"
version = "0.1.4"
description = "ncpeek (short for netconf peek) is a netconf client that retrieves data using the ncclient library."
authors = ["Jesus Illescas <[email protected]>"]
readme = ["README.md"]
Expand Down
45 changes: 41 additions & 4 deletions tests/test_parsers/test_cisco_ios_xe_isis_oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
username="username",
)
parser = ISISStatsIOSXEParser(device=device)
filter_id = "filter1"

data_to_parse = {
FILTER_ID = "filter1"

DATA_TO_PARSE = {
"data": {
"isis-oper-data": {
"isis-instance": {
Expand Down Expand Up @@ -48,7 +49,7 @@


def test_parse():
parsed_data = parser.parse(data_to_parse, device, filter_id)
parsed_data = parser.parse(DATA_TO_PARSE, device, FILTER_ID)
expected_data = [
{
"device": "device-1",
Expand Down Expand Up @@ -96,7 +97,7 @@ def test_parse():


def test_group_data_by_system_id():
grouped_data = parser._group_data_by_system_id(data_to_parse)
grouped_data = parser._group_data_by_system_id(DATA_TO_PARSE)
expected_data = {
"00:00:00:00:00:0a": [
{
Expand Down Expand Up @@ -126,3 +127,39 @@ def test_group_data_by_system_id():
}

assert grouped_data == expected_data


SINGLE_ISIS_NEIGHBOR = {
"data": {
"isis-oper-data": {
"isis-instance": {
"isis-neighbor": {
"holdtime": "22",
"if-name": "GigabitEthernet3",
"ipv4-address": "10.4.4.1",
"level": "isis-level-1",
"state": "isis-adj-up",
"system-id": "00:00:00:00:00:0b",
},
},
},
}
}


def test_group_data_by_system_id_single_neighbor():
grouped_data = parser._group_data_by_system_id(SINGLE_ISIS_NEIGHBOR)

expected_data = {
"00:00:00:00:00:0b": [
{
"interface_name": "GigabitEthernet3",
"level": "isis-level-1",
"local_ipv4_address": "10.4.4.1",
"isis_status": "isis-adj-up",
"holdtime": "22",
}
]
}

assert grouped_data == expected_data

0 comments on commit f6d0bd4

Please sign in to comment.