Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_mpls_ldp_session_count(device):
""" Get mpls ldp seesion count
Args:
device(`str`): Device str
Returns:
int: session count
Raises:
None
"""
log.info("Getting LDP neighbor count")
ldp_session_count = 0
try:
output_ldp = device.parse("show mpls ldp neighbor")
except SchemaEmptyParserError as e:
return ldp_session_count
for vrf in output_ldp["vrf"]:
ldp_session_count += len(output_ldp["vrf"][vrf]["peers"].keys())
log.info("LDP neighbor count is {}".format(ldp_session_count))
return ldp_session_count
None
Raises:
SyntaxError, KeyError, AssertionError
example:
>>> check_checkpoint_status(device=device, expect='delete',
name='bgp-001',abstract=abstract)
'''
assert expect in ['create', 'delete']
try:
ret = abstract.parser.show_archive.ShowArchive(device=device)
ret = ret.parse()
except SchemaEmptyParserError as e:
ret = ''
except Exception as e:
raise SyntaxError("Cannot parse command 'show archive'") from e
if expect == 'create':
if ret and 'most_recent_file' in ret['archive'] and \
name in ret['archive']['most_recent_file']:
self.ckname = ret['archive']['most_recent_file']
log.info('{n} is successfully created'.format(n=name))
else:
raise KeyError(
'{n} is failed to create - no recent archive is found'.format(n=name))
else:
if (not ret) or ('most_recent_file' not in ret['checkpoint']):
log.info('{n} is successfully deleted'.format(n=name))
else:
Returns
True
False
"""
if not parsed_output:
try:
parsed_output = device.parse(
"show mpls ldp igp sync interface {intf}".format(
intf=interface
)
)
except SchemaEmptyParserError:
raise SchemaEmptyParserError(
"Fail to parse 'show mpls ldp igp sync "
"interface {intf}' command".format(intf=interface)
)
vrf = vrf if vrf else "default"
try:
igp_synchronization_enabled = (
parsed_output["vrf"]
.get(vrf, {})
.get("interface", {})
.get(interface, {})
.get("ldp", {})
.get("igp_synchronization_enabled", False)
)
"Current session state is {cur_state}. "
"Expecting {exp_state}".format(
cur_state=session_state,
exp_state=expected_session_state,
)
)
output = get_ip_bgp_neighbors(
device=device,
address_family=address_family,
vrf=vrf_name,
neighbor_address=neighbor_address,
all_neighbors=all_neighbors,
)
except SchemaEmptyParserError as e:
return False, None
timeout.sleep()
log.error(
"Session state for neighbor {neighbor} is {session_state}. "
"Expecting {exp_state}".format(
neighbor=neighbor_address,
session_state=session_state,
exp_state=expected_session_state,
)
)
return False, new_output
# kwargs is used to pass args that aren't "None" to traceroute()
kwargs = {k: v for k, v in locals().items() if v}
kwargs.pop('device')
if vrf:
kwargs.pop('vrf')
kwargs['command'] = 'traceroute vrf {}'.format(vrf)
try:
output = device.traceroute(**kwargs)
except SubCommandFailure as e:
log.info("Could not find any traceroute information")
return False
parser_obj = Traceroute(device=device)
try:
parsed_ouput = parser_obj.parse(output=output)
except SchemaEmptyParserError as e:
log.info(
"Could find any traceroute information for prefix {address}".format(
address=addr
)
)
return None
return parsed_ouput
Raises:
SyntaxError, KeyError, AssertionError
example:
>>> check_checkpoint_status(device=device, expect='delete',
name='bgp-001',abstract=abstract)
'''
assert expect in ['create', 'delete']
try:
ret = abstract.parser.show_checkpoint.ShowCheckpointSummary(
device=device)
ret = ret.parse()
except SchemaEmptyParserError as e:
ret = ''
except Exception as e:
raise SyntaxError("Cannot parse command 'show checkpoint "
"summary'") from e
if expect == 'create':
if ret and name in ret['checkpoint']:
log.info('{n} is successfully created'.format(n=name))
else:
raise KeyError('{n} is failed to create'.format(n=name))
else:
if (not ret) or (name not in ret['checkpoint']):
log.info('{n} is successfully deleted'.format(n=name))
else:
raise KeyError('{n} is failed to delete'.format(n=name))
Raises:
None
"""
out = None
try:
if not output_dict:
try:
if interface:
cmd = 'show interfaces {interface}'.format(interface=interface)
else:
cmd = 'show interfaces'
if extensive:
cmd = '{cmd} extensive'.format(cmd=cmd)
out = device.parse(cmd)
except SchemaEmptyParserError:
return None
else:
out = output_dict
except SchemaEmptyParserError:
return None
result = True
# Get first interface inorder to compare output-bps with other interfaces
physical_intf_check = out.q.contains(
'{interface}|.*output-bps.*'.format(
interface=logical_interface),
regex=True)
# To handle list within list
def get_protocols_bgp_process(device, vrf=None):
""" Returns bgp process id from show protocols
Args:
device ('obj'): device to run on
Returns:
bgp process id
Raises:
None
"""
log.info("Getting bgp process id on {}".format(device.hostname))
try:
out = device.parse("show ip protocols")
except SchemaEmptyParserError:
return None
vrf = vrf if vrf else "default"
if (
out
and "protocols" in out
and "bgp" in out["protocols"]
and "instance" in out["protocols"]["bgp"]
and vrf in out["protocols"]["bgp"]["instance"]
):
return out["protocols"]["bgp"]["instance"][vrf].get("bgp_id", None)
Raises:
SchemaEmptyParserError
"""
commands = ["show ip route vrf {} summary", "show ip route summary"]
if vrf:
cmd = commands[0].format(vrf)
else:
cmd = commands[1]
try:
output = device.parse(cmd)
except SchemaEmptyParserError:
raise SchemaEmptyParserError(
"Command '{}' has " "not returned any results".format(cmd)
)
if not vrf:
vrf = "default"
count = output.get("vrf", {}).get(vrf, {}).\
get("total_route_source", {}).get("subnets")
return count