Initial commit

This commit is contained in:
David Barroso committed 2014-12-19 15:17:27 +01:00
1 parent 8472e7d97c
commit d3df425784
10 files changed
+235

No files matched your search

+1
View File
@@ -52,3 +52,4 @@ coverage.xml
# Sphinx documentation
docs/_build/
.idea
View File
Whitespace-only changes.
+76
View File
@@ -0,0 +1,76 @@
class NetworkDriver:
def __init__(self, hostname, user, password):
"""
This is the base class you have to inherit from when writing your own Network Driver to manage any device. You
will, in addition, have to override all the methods specified on this class. Make sure you follow the guidelines
for every method and that you return the correct data.
:param hostname: (str) IP or FQDN of the device you want to connect to.
:param user: (str) Username you want to use
:param password: (str) Password
:return:
"""
raise NotImplementedError
def open(self):
"""
Opens a connection to the device.
:return: None
"""
raise NotImplementedError
def close(self):
"""
Closes the connection to the device.
:return: None
"""
raise NotImplementedError
def compare_configuration(self, candidate):
"""
Compares the running configuration with a candidate configuration.
:param candidate: (str) Configuration you want to compare with.
:return: A string showing the difference between the running configuration and the candidate configuration.
"""
raise NotImplementedError
def replace_configuration(self, candidate):
"""
Replaces the running configuration with the candidate configuration.
:param candidate: (str) Configuration you want to have on the device.
:return: None
:raises SyntacticError: If configuration syntax was incorrect.
:raises SemanticError: If the device reported semantic errors on the configuration.
:raises UnknownConfigurationError: If none of the above.
"""
raise NotImplementedError
def get_facts(self):
"""
Retrieves facts from the device.
:return: (Facts) Facts from the device.
"""
raise NotImplementedError
def get_bgp_neighbors(self):
"""
Retrieves BGP neighbors information from the device.
:return: (list of BGPNeighbor) List of BGPNeighbor from the device.
"""
raise NotImplementedError
def get_interfaces(self):
"""
Retrieves information from the available interfaces on the device.
:return: (list of Interface) List of Interface from the device.
"""
raise NotImplementedError
def get_lldp_neighbors(self):
"""
Retrieves LLDP information from the device.
:return: (list of LLDPNeighbor) List of LLDPNeighbor.
"""
raise NotImplementedError
+61
View File
@@ -0,0 +1,61 @@
from pyEOS import EOS
from base import NetworkDriver
from objects.facts import Facts
from objects.bgp import BGPInstance, BGPNeighbor
class EOSDriver(NetworkDriver):
def __init__(self, hostname, user, password):
self.hostname = hostname
self.user = user
self.password = password
self.device = EOS(hostname, user, password, use_ssl=True)
def open(self):
self.device.open()
def close(self):
self.device.close()
def get_facts(self):
hostname = self.device.show_hostname()
sv = self.device.show_version()
return Facts(
vendor = 'Arista',
hostname = hostname['hostname'],
fqdn = hostname['fqdn'],
hardware_model = sv['modelName'],
serial_number = sv['serialNumber'],
os_version = sv['version'],
interfaces = self.device.show_interfaces()['interfaces'].keys(),
)
def get_bgp_neighbors(self):
output = self.device.show_ip_bgp_summary_vrf_all()
bgp_table = list()
for vrf, bgp in output['vrfs'].iteritems():
list_peers = list()
for peer, values in bgp['peers'].iteritems():
p = BGPNeighbor(
ip = peer,
remote_as = int(values['asn']),
state = values['peerState'],
time = int(values['upDownTime']),
prefixes_accepted = int(values['prefixAccepted']),
)
list_peers.append(p)
bgp_instance = BGPInstance(
vrf = vrf,
asn = int(bgp['asn']),
router_id = bgp['routerId'],
bgp_neighbors = list_peers,
)
bgp_table.append(bgp_instance)
return bgp_table
View File
Whitespace-only changes.
+39
View File
@@ -0,0 +1,39 @@
class BGPInstance:
def __init__(self, vrf, asn, router_id, bgp_neighbors):
"""
:param vrf: (str) VRF to which the BGP instance belongs to
:param asn: (int) The ASN of the BGP instance
:param router_id: (str) The router ID
:param bgp_neighbors: (list of BGPNeighbor) A list of bgp peers
:return:
"""
self.vrf = vrf
self.asn = asn
self.router_id = router_id
self.bgp_neighbors = bgp_neighbors
def __str__(self):
return 'BGP Instance: vrf=%s, asn=%s, router_id=%s' % (self.vrf, self.asn, self.router_id)
class BGPNeighbor:
def __init__(self, ip, remote_as, state, time, prefixes_accepted):
"""
:param ip: (str) IP of the BGP neighbor
:param remote_as: (str) Remote AS of the BGP neighbor
:param state: (str) State of the BGP neighbor relationship. Make sure you use the standard values as: Idle,
Connect, Active, OpenSent, OpenConfirm, Established
:param time: (int) For how long the device has been on the up/down state
:param prefixes_accepted: (int) Number of prefixes accepted
:return:
"""
self.ip = ip
self.remote_as = remote_as
self.state = state
self.time = time
self.prefixes_accepted = prefixes_accepted
def __str__(self):
return "BGP Neighbor: ip=%s, asn=%s, state=%s" % (self.ip, self.remote_as, self.state)
+8
View File
@@ -0,0 +1,8 @@
class SyntacticError(Exception):
pass
class SemanticError(Exception):
pass
class UnknownConfigurationError(Exception):
pass
+16
View File
@@ -0,0 +1,16 @@
class Facts:
def __init__(self, vendor, hostname, fqdn, hardware_model, serial_number, os_version, interfaces):
"""
:param vendor: (str) Vendor of the device.
:param hardware_model: (str) Specific hardware model of the device
:param serial_number: (str) Serial number of the device
:param os_version: (str) OS Version running the device
:param interfaces: (list of str) List of interface names available on the device
"""
self.vendor = vendor
self.hostname = hostname
self.fqdn = fqdn
self.hardware_model = hardware_model
self.serial_number = serial_number
self.os_version = os_version
self.interfaces = interfaces
+21
View File
@@ -0,0 +1,21 @@
class Status:
UP = 0
OPER_DOWN = 1
ADMIN_DOWN = 2
class Interface:
def __init__(self, name, speed, interface_type, status):
"""
Represents an interface on a switch.
:param name: (str) Name of the interface
:param speed: (int) Speed of the interface in Gbps.
:param interface_type: (str) Type of the interface, i.e.
:param status: (Status) Status of an interface. Make sure you use the values on the class Status.
:return:
"""
self.name
self.speed
self.type
self.interface_type
+13
View File
@@ -0,0 +1,13 @@
class LLDPNeighbor:
def __init__(self, hostname, local_port, remote_port):
"""
Represents and LLDP neighbor.
:param hostname: (str) hostname of the LLDP Neighbor
:param local_port: (str) Local port where the LLDP Neighbor is connected to.
:param remote_port: (str) Remote port where the LLDP Neighbor is connected to.
:return:
"""
self.hostname = hostname
self.local_port = local_port
self.remote_port = remote_port