Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 92 #98

Merged
merged 3 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jdiff/extract_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
jmespath_refkey_parser,
associate_key_of_my_value,
keys_values_zipper,
multi_reference_keys,
)


Expand Down Expand Up @@ -43,6 +44,12 @@ def extract_data_from_json(data: Union[Mapping, List], path: str = "*", exclude:
# return if path is not specified
return data

# Multi ref_key
if len(re.findall(r"\$.*?\$", path)) > 1:
clean_path = path.replace("$", "")
values = jmespath.search(f"{clean_path}{' | []' * (path.count('*') - 1)}", data)
return keys_values_zipper(multi_reference_keys(path, data), associate_key_of_my_value(clean_path, values))

values = jmespath.search(jmespath_value_parser(path), data)

if values is None:
Expand Down
45 changes: 42 additions & 3 deletions jdiff/utils/jmespath_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import re
from typing import Mapping, List, Union

import jmespath


def jmespath_value_parser(path: str):
"""
Expand All @@ -31,15 +33,15 @@ def jmespath_value_parser(path: str):
path_suffix = path.split(".")[-1]

if regex_match_ref_key:
reference_key = regex_match_ref_key.group()
if regex_ref_key.search(path_suffix):
# [$peerAddress$,prefixesReceived] --> [prefixesReceived]
reference_key = regex_match_ref_key.group()
return path.replace(reference_key, "")

# result[0].$vrfs$.default... --> result[0].vrfs.default....
regex_normalized_value = re.search(r"\$.*\$", regex_match_ref_key.group())
regex_normalized_value = re.search(r"\$.*\$", reference_key)
if regex_normalized_value:
normalized_value = regex_match_ref_key.group().split("$")[1]
normalized_value = reference_key.split("$")[1]
return path.replace(regex_normalized_value.group(), normalized_value)
return path

Expand Down Expand Up @@ -120,3 +122,40 @@ def keys_values_zipper(list_of_reference_keys: List, wanted_value_with_key: List
final_result.append({my_key: wanted_value_with_key[my_index]})

return final_result


def multi_reference_keys(jmspath, data):
"""Build a list of concatenated reference keys.

Args:
jmspath: "$*$.peers.$*$.*.ipv4.[accepted_prefixes]"
data: tests/mock/napalm_get_bgp_neighbors/multi_vrf.json

Returns:
["global.10.1.0.0", "global.10.2.0.0", "global.10.64.207.255", "global.7.7.7.7", "vpn.10.1.0.0", "vpn.10.2.0.0"]
"""
ref_key_regex = re.compile(r"\$.*?\$")
mapping = []
split_path = jmspath.split(".")

ref_key_index = -1 # -1 as the starting value, so it will match split path list indexes
for index, element in enumerate(split_path):
if ref_key_regex.search(element):
ref_key_index += 1
key_path = (
".".join(split_path[:index]).replace("$", "") or "@"
) # @ is for top keys, as they are stripped with "*"
flat_path = f"{key_path}{' | []' * key_path.count('*')}" # | [] to flatten the data, nesting level is eq to "*" count
sub_data = jmespath.search(flat_path, data) # extract sub-data with up to the ref key
if isinstance(sub_data, dict):
keys = list(sub_data.keys())
elif isinstance(sub_data, list):
keys = []
for parent, children in zip(
mapping[ref_key_index - 1], sub_data
): # refer to previous keys as they are already present in mapping
keys.extend(f"{parent}.{child}" for child in children.keys()) # concatenate keys
else:
raise ValueError("Ref key anchor must return either a dict or a list.")
mapping.append(keys)
return mapping[-1] # return last element as it has all previous ref_keys concatenated.
138 changes: 138 additions & 0 deletions tests/mock/napalm_get_bgp_neighbors/multi_vrf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"global": {
"peers": {
"10.1.0.0": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": true,
"is_up": true,
"local_as": 4268360780,
"remote_as": 67890,
"remote_id": "0.0.0.0",
"uptime": 1783
},
"10.2.0.0": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": true,
"is_up": false,
"local_as": 4268360780,
"remote_as": 67890,
"remote_id": "0.0.0.0",
"uptime": 1782
},
"10.64.207.255": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": false,
"is_up": true,
"local_as": 4268360780,
"remote_as": 12345,
"remote_id": "0.0.0.0",
"uptime": 1782
},
"7.7.7.7": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": true,
"is_up": true,
"local_as": 4268360780,
"remote_as": 67890,
"remote_id": "0.0.0.0",
"uptime": 0
}
},
"router_id": "1.1.0.1"
},
"vpn": {
"peers": {
"10.1.0.0": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": true,
"is_up": true,
"local_as": 4268360780,
"remote_as": 67890,
"remote_id": "0.0.0.0",
"uptime": 1783
},
"10.2.0.0": {
"address_family": {
"ipv4": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
},
"ipv6": {
"accepted_prefixes": 1000,
"received_prefixes": 1000,
"sent_prefixes": 1000
}
},
"description": "",
"is_enabled": true,
"is_up": false,
"local_as": 4268360780,
"remote_as": 67890,
"remote_id": "0.0.0.0",
"uptime": 1782
}
},
"router_id": "10.1.0.1"
}
}
83 changes: 76 additions & 7 deletions tests/test_get_value.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,85 @@
"""Test GitHub issues."""
"""Test extract_data_from_json."""
import pytest
from jdiff import extract_data_from_json
from .utility import load_json_file, ASSERT_FAIL_MESSAGE


my_data = [{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}]
test_cases_extract_data_none = [
"global[*]",
'global.peers."1.1.1.1"',
]


@pytest.mark.parametrize("data", my_data)
def test_jmspath_return_none(data):
"""Handle exception when JMSPath retunr None."""
my_jmspath = "global[*]"
@pytest.mark.parametrize("jmspath", test_cases_extract_data_none)
def test_jmspath_return_none(jmspath):
"""Handle exception when JMSPath returns None."""
data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}
with pytest.raises(TypeError) as error:
extract_data_from_json(data=data, path=my_jmspath)() # pylint: disable=E0110
extract_data_from_json(data=data, path=jmspath)

assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__()


test_cases_extract_data_no_ref_key = [
("global.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000], [1000], [1000]]),
("vpn.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000]]),
(
"*.peers.*.*.*.[accepted_prefixes]",
[[1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000]],
),
]


test_cases_extract_data_with_ref_key = [
(
"global.peers.$*$.*.ipv6.[accepted_prefixes]",
[
{"10.1.0.0": {"accepted_prefixes": 1000}},
{"10.2.0.0": {"accepted_prefixes": 1000}},
{"10.64.207.255": {"accepted_prefixes": 1000}},
{"7.7.7.7": {"accepted_prefixes": 1000}},
],
),
(
"vpn.peers.$*$.*.ipv6.[accepted_prefixes]",
[{"10.1.0.0": {"accepted_prefixes": 1000}}, {"10.2.0.0": {"accepted_prefixes": 1000}}],
),
(
"$*$.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
[
{"global.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.64.207.255": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.7.7.7.7": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
],
),
(
"$*$.peers.$*$.*.ipv6.[received_prefixes,sent_prefixes]",
[
{"global.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.64.207.255": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.7.7.7.7": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
],
),
pytest.param(
"$*$.peers.$*$.address_family.$*$.[accepted_prefixes]",
"",
marks=pytest.mark.xfail(reason="Jmespath issue - path returns empty list."),
),
]


@pytest.mark.parametrize(
"jmspath, expected_value", test_cases_extract_data_no_ref_key + test_cases_extract_data_with_ref_key
)
def test_extract_data_from_json(jmspath, expected_value):
"""Test JMSPath return value."""
data = load_json_file("napalm_get_bgp_neighbors", "multi_vrf.json")
value = extract_data_from_json(data=data, path=jmspath)

assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)
41 changes: 40 additions & 1 deletion tests/test_jmespath_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
jmespath_refkey_parser,
keys_values_zipper,
associate_key_of_my_value,
multi_reference_keys,
)
from .utility import ASSERT_FAIL_MESSAGE
from .utility import load_json_file, ASSERT_FAIL_MESSAGE


value_parser_case_1 = (
Expand Down Expand Up @@ -112,3 +113,41 @@ def test_keys_zipper(ref_keys, wanted_values, expected_output):
def test_keys_association(path, wanted_values, expected_output):
output = associate_key_of_my_value(path, wanted_values)
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)


multi_ref_key_case_1 = (
"$*$.peers.$*$.*.ipv4.[accepted_prefixes]",
["global.10.1.0.0", "global.10.2.0.0", "global.10.64.207.255", "global.7.7.7.7", "vpn.10.1.0.0", "vpn.10.2.0.0"],
)


multi_ref_key_case_2 = (
"$*$.peers.$*$.address_family.$*$.[accepted_prefixes]",
[
"global.10.1.0.0.ipv4",
"global.10.1.0.0.ipv6",
"global.10.2.0.0.ipv4",
"global.10.2.0.0.ipv6",
"global.10.64.207.255.ipv4",
"global.10.64.207.255.ipv6",
"global.7.7.7.7.ipv4",
"global.7.7.7.7.ipv6",
"vpn.10.1.0.0.ipv4",
"vpn.10.1.0.0.ipv6",
"vpn.10.2.0.0.ipv4",
"vpn.10.2.0.0.ipv6",
],
)


multi_ref_key_test_cases = [
multi_ref_key_case_1,
multi_ref_key_case_2,
]


@pytest.mark.parametrize("path, expected_output", multi_ref_key_test_cases)
def test_multi_ref_key(path, expected_output):
data = load_json_file("napalm_get_bgp_neighbors", "multi_vrf.json")
output = multi_reference_keys(path, data)
assert expected_output == output, ASSERT_FAIL_MESSAGE.format(output=output, expected_output=expected_output)