Skip to content

Commit

Permalink
Merge pull request #997 from vnitinv/nested-fields-sax-xpath
Browse files Browse the repository at this point in the history
handle sax parser input for nested fields
  • Loading branch information
Nitin Kr authored Mar 5, 2020
2 parents 74821f7 + 91f7ad4 commit 10f3811
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
24 changes: 15 additions & 9 deletions lib/jnpr/junos/factory/optable.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,23 @@ def generate_sax_parser_input(obj):
# fields:
# input-bytes: traffic-statistics/input-bytes
# output-bytes: traffic-statistics/output-bytes
existing_elem = parser_ingest.xpath(tags[0])
if existing_elem:
obj = existing_elem[0]
for tag in tags[1:]:
obj.append(E(tag))
# or
# fields:
# prefix-count: bgp-option-information/prefix-limit/prefix-count
# prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy
local_obj = parser_ingest
for tag in tags[:-1]:
existing_elem = local_obj.xpath(tag)
if existing_elem:
local_obj = existing_elem[0]
else:
continue
else:
continue
local_obj.append(E(tags[-1]))
else:
obj = E(tags[0])
for tag in tags[1:]:
obj.append(E(tag))
obj = E(tags[-1])
for tag in tags[:-1][::-1]:
obj = E(tag, obj)
map_multilayer_fields[tags[0]] = obj
parser_ingest.insert(i + 1, obj)
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/factory/test_optable.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ def bad(key):

self.assertRaises(ValueError, bad, 'bunk')

def test_generate_sax_parser_fields_with_many_slash(self):
yaml_data = """
---
bgpNeighborTable:
rpc: get-bgp-neighbor-information
item: bgp-peer
key: peer-address
view: bgpNeighborView
bgpNeighborView:
fields:
prefix-count: bgp-option-information/prefix-limit/prefix-count
prefix-dummy: bgp-option-information/prefix-limit/prefix-dummy
"""
globals().update(FactoryLoader().load(yaml.load(yaml_data,
Loader=yaml.Loader)))
tbl = bgpNeighborTable(self.dev)
data = generate_sax_parser_input(tbl)
self.assertEqual(data.tag, 'bgp-peer')
self.assertEqual(len(etree.tostring(data)), len(
b'<bgp-peer><peer-address/><bgp-option-information><prefix-limit>'
b'<prefix-count/><prefix-dummy/></prefix-limit>'
b'</bgp-option-information></bgp-peer>'))

def test_generate_sax_parser_item_with_many_slash(self):
yaml_data = """
---
Expand Down
1 change: 0 additions & 1 deletion tests/unit/factory/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def test_table__contains__(self, mock_execute):
def test_table_items(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.ppt.get('ge-0/0/0')
print (self.ppt.items())
self.assertEqual(len(self.ppt.items()[1][1]), 8)

def test_table_get_return_none(self):
Expand Down
1 change: 0 additions & 1 deletion tests/unit/facts/test_get_software_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def test_sw_info_dual(self, mock_execute):
@patch('jnpr.junos.Device.execute')
def test_sw_info_dual_other_re_off(self, mock_execute):
mock_execute.side_effect = self._mock_manager_dual_other_re_off
print (self.dev.facts)
self.assertEqual(self.dev.facts['junos_info']['re1']['text'],
'18.3I20180716_1639')
self.assertEqual(self.dev.facts['hostname'], 'R1_re01')
Expand Down

0 comments on commit 10f3811

Please sign in to comment.