1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/execution/construct.py

145 lines
4.6 KiB
Python
Raw Normal View History

"""Construct SSH command/API parameters from validated query data.
2019-07-07 23:28:23 -07:00
Accepts filtered & validated input from execute.py, constructs SSH
command for Netmiko library or API call parameters for supported
hyperglass API modules.
"""
2020-02-03 02:35:11 -07:00
# Standard Library
import re
2020-04-12 02:17:16 -07:00
import json as _json
2020-02-03 02:35:11 -07:00
import operator
2019-06-10 12:22:38 -07:00
2020-02-03 02:35:11 -07:00
# Project
from hyperglass.util import log
2020-04-12 02:17:16 -07:00
from hyperglass.constants import (
TRANSPORT_REST,
TARGET_FORMAT_SPACE,
TARGET_JUNIPER_ASPATH,
)
2020-02-03 02:35:11 -07:00
from hyperglass.configuration import commands
class Construct:
"""Construct SSH commands/REST API parameters from validated query data."""
2020-01-31 02:06:27 -10:00
def __init__(self, device, query_data):
2019-12-31 13:30:55 -07:00
"""Initialize command construction.
Arguments:
device {object} -- Device object
query_data {object} -- Validated query object
"""
2019-09-25 22:40:02 -07:00
log.debug(
2020-01-31 02:06:27 -10:00
"Constructing {q} query for '{t}'",
q=query_data.query_type,
t=str(query_data.query_target),
)
2020-01-31 02:06:27 -10:00
self.device = device
self.query_data = query_data
2020-03-24 18:36:04 -07:00
self.target = self.query_data.query_target
2019-09-09 00:18:01 -07:00
2020-01-31 02:06:27 -10:00
# Set transport method based on NOS type
self.transport = "scrape"
if self.device.nos in TRANSPORT_REST:
self.transport = "rest"
2019-12-31 13:30:55 -07:00
2020-01-31 02:06:27 -10:00
# Remove slashes from target for required platforms
if self.device.nos in TARGET_FORMAT_SPACE:
2020-03-24 18:36:04 -07:00
self.target = re.sub(r"\/", r" ", str(self.query_data.query_target))
2019-09-09 00:18:01 -07:00
2020-01-31 02:06:27 -10:00
# Set AFIs for based on query type
if self.query_data.query_type in ("bgp_route", "ping", "traceroute"):
2020-03-24 18:36:04 -07:00
# For IP queries, AFIs are enabled (not null/None) VRF -> AFI definitions
# where the IP version matches the IP version of the target.
2020-01-31 02:06:27 -10:00
self.afis = [
v
for v in (
self.query_data.query_vrf.ipv4,
self.query_data.query_vrf.ipv6,
2019-09-30 07:51:17 -07:00
)
2020-01-31 02:06:27 -10:00
if v is not None and self.query_data.query_target.version == v.version
]
elif self.query_data.query_type in ("bgp_aspath", "bgp_community"):
2020-03-24 18:36:04 -07:00
# For AS Path/Community queries, AFIs are just enabled VRF -> AFI
# definitions, no IP version checking is performed (since there is no IP).
2020-01-31 02:06:27 -10:00
self.afis = [
v
for v in (
self.query_data.query_vrf.ipv4,
self.query_data.query_vrf.ipv6,
2019-09-30 07:51:17 -07:00
)
2020-01-31 02:06:27 -10:00
if v is not None
]
2019-09-09 00:18:01 -07:00
2020-04-12 02:17:16 -07:00
# For devices that follow Juniper's AS_PATH regex standards,
# filter out Cisco-style special characters.
if self.device.nos in TARGET_JUNIPER_ASPATH:
query = str(self.query_data.query_target)
asns = re.findall(r"\d+", query)
if bool(re.match(r"^\_", query)):
# Replace `_65000` with `.* 65000`
asns.insert(0, r".*")
if bool(re.match(r".*(\_)$", query)):
# Replace `65000_` with `65000 .*`
asns.append(r".*")
self.target = " ".join(asns)
2020-01-31 02:06:27 -10:00
def json(self, afi):
"""Return JSON version of validated query for REST devices.
2020-01-31 02:06:27 -10:00
Arguments:
afi {object} -- AFI object
2019-12-31 13:30:55 -07:00
Returns:
2020-01-31 02:06:27 -10:00
{str} -- JSON query string
"""
2020-01-31 02:06:27 -10:00
log.debug("Building JSON query for {q}", q=repr(self.query_data))
2020-04-12 02:17:16 -07:00
return _json.dumps(
2020-01-31 02:06:27 -10:00
{
"query_type": self.query_data.query_type,
"vrf": self.query_data.query_vrf.name,
"afi": afi.protocol,
"source": str(afi.source_address),
2020-03-24 18:36:04 -07:00
"target": str(self.target),
2020-01-31 02:06:27 -10:00
}
)
2019-09-09 00:18:01 -07:00
2020-01-31 02:06:27 -10:00
def scrape(self, afi):
"""Return formatted command for 'Scrape' endpoints (SSH).
2020-01-31 02:06:27 -10:00
Arguments:
afi {object} -- AFI object
2019-12-31 13:30:55 -07:00
Returns:
2020-01-31 02:06:27 -10:00
{str} -- Command string
"""
2020-01-31 02:06:27 -10:00
command = operator.attrgetter(
f"{self.device.nos}.{afi.protocol}.{self.query_data.query_type}"
)(commands)
return command.format(
2020-03-24 18:36:04 -07:00
target=self.target,
2020-01-31 02:06:27 -10:00
source=str(afi.source_address),
vrf=self.query_data.query_vrf.name,
)
2019-09-09 00:18:01 -07:00
2020-01-31 02:06:27 -10:00
def queries(self):
"""Return queries for each enabled AFI.
2019-12-31 13:30:55 -07:00
Returns:
2020-01-31 02:06:27 -10:00
{list} -- List of queries to run
"""
query = []
2020-01-31 02:06:27 -10:00
for afi in self.afis:
if self.transport == "rest":
2020-01-31 02:06:27 -10:00
query.append(self.json(afi=afi))
else:
query.append(self.scrape(afi=afi))
2019-09-09 00:18:01 -07:00
2019-09-25 22:40:02 -07:00
log.debug(f"Constructed query: {query}")
return query