From ff9365b4764d15e9dac18ece4b1ae9744c301c23 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 16 Mar 2015 14:15:18 +0100 Subject: [PATCH] Renaming drivers to napalm for the sake of clarity when importing --- drivers/__init__.py | 0 drivers/base.py | 106 ---------------------------------- drivers/eos.py | 137 -------------------------------------------- drivers/iosxr.py | 48 ---------------- drivers/junos.py | 44 -------------- 5 files changed, 335 deletions(-) delete mode 100644 drivers/__init__.py delete mode 100644 drivers/base.py delete mode 100644 drivers/eos.py delete mode 100644 drivers/iosxr.py delete mode 100644 drivers/junos.py diff --git a/drivers/__init__.py b/drivers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/drivers/base.py b/drivers/base.py deleted file mode 100644 index c09c10b..0000000 --- a/drivers/base.py +++ /dev/null @@ -1,106 +0,0 @@ -# TODO check common API -# TODO install pyEOS - - -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 load_candidate_config(self, filename=None, config=None): - """ - Populates the attribute candidate_config with the desired configuration. You can populate it from a file or - from a string. If you send both a filename and a string containing the configuration, the file takes precedence. - - :param filename: Path to the file containing the desired configuration. By default is None. - :param config: String containing the desired configuration. - """ - raise NotImplementedError - - def compare_config(self): - """ - - :return: A string showing the difference between the running_config and the candidate_config. The running_config is - loaded automatically just before doing the comparison so there is no neeed for you to do it. - """ - raise NotImplementedError - - def commit_config(self): - """ - Applies the configuration loaded with the method load_candidate_config on the device. Note that the current - configuration of the device is replaced with the new configuration. - """ - raise NotImplementedError - - def discard_config(self): - """ - Discards the configuration loaded into the candidate. - """ - raise NotImplementedError - - def rollback(self): - """ - If changes have been made, revert changes to the original state. - """ - 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_interface(self, name): - """ - Retrieves information from the available interfaces on the device. - :param name: (string) Interface name of the interface you want to get. - :return: (list of Interface) List of Interface 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 diff --git a/drivers/eos.py b/drivers/eos.py deleted file mode 100644 index 8df19a5..0000000 --- a/drivers/eos.py +++ /dev/null @@ -1,137 +0,0 @@ -# Copyright 2014 Spotify AB. All rights reserved. -# -# The contents of this file are licensed under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with the -# License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations under -# the License. - -from pyEOS import EOS - -from base import NetworkDriver -from objects.facts import Facts -from objects.bgp import BGPInstance, BGPNeighbor -from objects.interface import Interface, InterfaceStatus -from objects.lldp import LLDPNeighbor - -def _process_interface(interface): - name = interface['name'] - - if interface['interfaceStatus'] == u'notconnect': - status = InterfaceStatus.OPER_DOWN - elif interface['interfaceStatus'] == u'connected': - status = InterfaceStatus.UP - elif interface['interfaceStatus'] == u'disabled': - status = InterfaceStatus.ADMIN_DOWN - else: - raise Exception('Unknown interface status: %s, %s' % (name, interface['interfaceStatus'])) - - return Interface( - name, - interface['bandwidth'], - status - ) - - -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 load_candidate_config(self, filename=None, config=None): - self.device.load_candidate_config(filename=filename, config=config) - - def compare_config(self): - return self.device.compare_config() - - def commit_config(self): - self.device.replace_config() - - def discard_config(self): - pass - - def rollback(self): - self.device.rollback() - - 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 - - def get_interface(self, name): - interfaces = self.device.show_interfaces() - interface = interfaces['interfaces'][name] - return _process_interface(interface) - - def get_interfaces(self): - interfaces = list() - - for interface in self.device.show_interfaces()['interfaces'].values(): - interfaces.append(_process_interface(interface)) - - return interfaces - - def get_lldp_neighbors(self): - neighbors = list() - - for n in self.device.show_lldp_neighbors()['lldpNeighbors']: - neighbors.append( - LLDPNeighbor( - n['neighborDevice'], - n['port'], - n['neighborPort'] - ) - ) - - return neighbors \ No newline at end of file diff --git a/drivers/iosxr.py b/drivers/iosxr.py deleted file mode 100644 index 343deb6..0000000 --- a/drivers/iosxr.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2014 Spotify AB. All rights reserved. -# -# The contents of this file are licensed under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with the -# License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations under -# the License. - -from pyIOSXR import IOSXR - -from base import NetworkDriver - - -class IOSXRDriver(NetworkDriver): - - def __init__(self, hostname, username, password): - self.hostname = hostname - self.username = username - self.password = password - self.device = IOSXR(hostname, username, password) - - def open(self): - self.device.open() - - def close(self): - self.device.close() - - def load_candidate_config(self, filename=None, config=None): - self.device.load_candidate_config(filename=filename, config=config) - - def compare_config(self): - return self.device.compare_config() - - def commit_config(self): - self.device.commit_config() - - def discard_config(self): - self.device.discard_config() - - def rollback(self): - self.device.rollback() - diff --git a/drivers/junos.py b/drivers/junos.py deleted file mode 100644 index 0caa473..0000000 --- a/drivers/junos.py +++ /dev/null @@ -1,44 +0,0 @@ -from base import NetworkDriver - -from jnpr.junos import Device -from jnpr.junos.utils.config import Config - - -class JunOSDriver(NetworkDriver): - - def __init__(self, hostname, user, password): - self.hostname = hostname - self.user = user - self.password = password - self.device = Device(hostname, user=user, password=password) - - def open(self): - self.device.open() - self.device.bind(cu=Config) - self.device.cu.lock() - - def close(self): - self.device.cu.unlock() - self.device.close() - - def load_candidate_config(self, filename=None, config=None): - if filename is None: - configuration = config - else: - with open(filename) as f: - configuration = f.read() - - self.device.cu.load(configuration, format='text', overwrite=True) - - def compare_config(self): - return self.device.cu.diff() - - def commit_config(self): - self.device.cu.commit() - - def discard_config(self): - self.device.cu.rollback(rb_id=0) - - def rollback(self): - self.device.cu.rollback(rb_id=1) - self.commit_config()