How to use the napalm.base.base.NetworkDriver function in napalm

To help you get started, we’ve selected a few napalm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github napalm-automation / napalm / test / base / test_napalm_test_framework.py View on Github external
def setUp(self):
        class FakeThing(NetworkDriver):
            def __init__(self):
                pass

        self.device = FakeThing()
github napalm-automation / napalm / test / base / validate / test_validate.py View on Github external
assert expected_report == actual_report, yaml.safe_dump(actual_report)

    def test_immutable_validation_source(self):
        """Test validation_source is not modified."""
        mocked_data = os.path.join(BASEPATH, "mocked_data", "strict_pass_skip")

        device = FakeDriver(mocked_data)
        source = _read_yaml(os.path.join(mocked_data, "validate.yml"))
        witness = _read_yaml(os.path.join(mocked_data, "validate.yml"))
        device.compliance_report(validation_source=source)

        assert source == witness, yaml.safe_dump(source)


class FakeDriver(NetworkDriver):
    """This is a fake NetworkDriver."""

    def __init__(self, path):
        self.path = path

    def __getattribute__(self, name):
        def load_json(filename):
            def func(**kwargs):
                with open(filename, "r") as f:
                    return json.loads(f.read())

            return func

        if name.startswith("get_") or name in C.ACTION_TYPE_METHODS:
            filename = os.path.join(self.path, "{}.json".format(name))
            return load_json(filename)
github napalm-automation / napalm / test / base / test_get_network_driver.py View on Github external
def test_get_network_driver(self, driver):
        """Check that we can get the desired driver and is instance of NetworkDriver."""
        self.assertTrue(issubclass(get_network_driver(driver), NetworkDriver))
github napalm-automation / napalm / napalm / junos / junos.py View on Github external
from napalm.junos import constants as C
from napalm.base.exceptions import ConnectionException
from napalm.base.exceptions import MergeConfigException
from napalm.base.exceptions import CommandErrorException
from napalm.base.exceptions import ReplaceConfigException
from napalm.base.exceptions import CommandTimeoutException
from napalm.base.exceptions import LockError
from napalm.base.exceptions import UnlockError

# import local modules
from napalm.junos.utils import junos_views

log = logging.getLogger(__file__)


class JunOSDriver(NetworkDriver):
    """JunOSDriver class - inherits NetworkDriver from napalm.base."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """
        Initialise JunOS driver.

        Optional args:
            * config_lock (True/False): lock configuration DB after the connection is established.
            * lock_disable (True/False): force configuration lock to be disabled (for external lock
                management).
            * port (int): custom port
            * key_file (string): SSH key file path
            * keepalive (int): Keepalive interval
            * ignore_warning (boolean): not generate warning exceptions
        """
        self.hostname = hostname
github napalm-automation / napalm / napalm / eos / eos.py View on Github external
from napalm.base.exceptions import (
    ConnectionException,
    MergeConfigException,
    ReplaceConfigException,
    SessionLockedException,
    CommandErrorException,
)
from napalm.eos.constants import LLDP_CAPAB_TRANFORM_TABLE
import napalm.base.constants as c

# local modules
# here add local imports
# e.g. import napalm.eos.helpers etc.


class EOSDriver(NetworkDriver):
    """Napalm driver for Arista EOS."""

    SUPPORTED_OC_MODELS = []

    HEREDOC_COMMANDS = [
        ("banner login", 1),
        ("banner motd", 1),
        ("comment", 1),
        ("protocol https certificate", 2),
    ]

    _RE_BGP_INFO = re.compile(
        r"BGP neighbor is (?P.*?), remote AS (?P.*?), .*"
    )  # noqa
    _RE_BGP_RID_INFO = re.compile(
        r".*BGP version 4, remote router ID (?P.*?), VRF (?P.*?)$"
github napalm-automation / napalm / napalm / vyos / vyos.py View on Github external
import vyattaconfparser

from netmiko import __version__ as netmiko_version
from netmiko import ConnectHandler
from netmiko import SCPConn

# NAPALM base
import napalm.base.constants as C
from napalm.base.utils import py23_compat
from napalm.base.base import NetworkDriver
from napalm.base.exceptions import ConnectionException, \
                                   MergeConfigException, ReplaceConfigException


class VyOSDriver(NetworkDriver):

    _MINUTE_SECONDS = 60
    _HOUR_SECONDS = 60 * _MINUTE_SECONDS
    _DAY_SECONDS = 24 * _HOUR_SECONDS
    _WEEK_SECONDS = 7 * _DAY_SECONDS
    _YEAR_SECONDS = 365 * _DAY_SECONDS
    _DEST_FILENAME = "/var/tmp/candidate_running.conf"
    _BACKUP_FILENAME = "/var/tmp/backup_running.conf"
    _BOOT_FILENAME = "/config/config.boot"

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        self.hostname = hostname
        self.username = username
        self.password = password
        self.timeout = timeout
        self.device = None
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
AFI_COMMAND_MAP = {
    "IPv4 Unicast": "ipv4 unicast",
    "IPv6 Unicast": "ipv6 unicast",
    "VPNv4 Unicast": "vpnv4 all",
    "VPNv6 Unicast": "vpnv6 unicast all",
    "IPv4 Multicast": "ipv4 multicast",
    "IPv6 Multicast": "ipv6 multicast",
    "L2VPN E-VPN": "l2vpn evpn",
    "MVPNv4 Unicast": "ipv4 mvpn all",
    "MVPNv6 Unicast": "ipv6 mvpn all",
    "VPNv4 Flowspec": "ipv4 flowspec",
    "VPNv6 Flowspec": "ipv6 flowspec",
}


class IOSDriver(NetworkDriver):
    """NAPALM Cisco IOS Handler."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """NAPALM Cisco IOS Handler."""
        if optional_args is None:
            optional_args = {}
        self.hostname = hostname
        self.username = username
        self.password = password
        self.timeout = timeout

        self.transport = optional_args.get("transport", "ssh")

        # Retrieve file names
        self.candidate_cfg = optional_args.get("candidate_cfg", "candidate_config.txt")
        self.merge_cfg = optional_args.get("merge_cfg", "merge_config.txt")
github napalm-automation / napalm / napalm / base / mock.py View on Github external
class MockDevice(object):
    def __init__(self, parent, profile):
        self.parent = parent
        self.profile = profile

    def run_commands(self, commands):
        """Mock for EOS"""
        return list(self.parent.cli(commands).values())[0]

    def show(self, command):
        """Mock for nxos"""
        return self.run_commands([command])


class MockDriver(NetworkDriver):
    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """
        Supported optional_args:
            * path(str) - path to where the mocked files are located
            * profile(list) - List of profiles to assign
        """
        self.hostname = hostname
        self.username = username
        self.password = password
        self.path = optional_args.get("path", "")
        self.profile = optional_args.get("profile", [])
        self.fail_on_open = optional_args.get("fail_on_open", False)

        self.opened = False
        self.calls = {}
        self.device = MockDevice(self, self.profile)
github napalm-automation-community / napalm-ce / napalm_ce / ce.py View on Github external
from napalm.base.exceptions import (
    ConnectionException,
    MergeConfigException,
    ReplaceConfigException,
    CommandErrorException,
    CommitError,
)

# Easier to store these as constants
HOUR_SECONDS = 3600
DAY_SECONDS = 24 * HOUR_SECONDS
WEEK_SECONDS = 7 * DAY_SECONDS
YEAR_SECONDS = 365 * DAY_SECONDS


class CEDriver(NetworkDriver):
    """Napalm driver for HUAWEI CloudEngine."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        """Constructor."""
        self.device = None
        self.hostname = hostname
        self.username = username
        self.password = password
        self.timeout = timeout

        # Get optional arguments
        if optional_args is None:
            optional_args = {}

        # Netmiko possible arguments
        netmiko_argument_map = {