From b65494c2c8e9053aa3cd2d7fb5799c04ec2c49f1 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 5 Mar 2015 16:33:48 +0100 Subject: [PATCH] asd --- drivers/base.py | 46 +++++++--- drivers/eos.py | 77 +++++++++++++++- objects/interface.py | 22 ++--- objects/lldp.py | 8 +- test/unit/config.py | 20 +++++ test/unit/config01.txt | 197 +++++++++++++++++++++++++++++++++++++++++ test/unit/config02.txt | 164 ++++++++++++++++++++++++++++++++++ 7 files changed, 509 insertions(+), 25 deletions(-) create mode 100644 test/unit/config.py create mode 100644 test/unit/config01.txt create mode 100644 test/unit/config02.txt diff --git a/drivers/base.py b/drivers/base.py index c514907..0e651a6 100644 --- a/drivers/base.py +++ b/drivers/base.py @@ -1,3 +1,7 @@ +# TODO check common API +# TODO install pyEOS + + class NetworkDriver: def __init__(self, hostname, user, password): """ @@ -27,22 +31,34 @@ class NetworkDriver: """ raise NotImplementedError - def compare_configuration(self, candidate): + def load_candidate_config(self, filename=None, config=None): """ - 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. + 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 replace_configuration(self, candidate): + def compare_config(self): """ - 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. + + :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 replace_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 rollback(self): + """ + If changes have been made, revert changes to the original state. """ raise NotImplementedError @@ -60,6 +76,14 @@ class NetworkDriver: """ 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. diff --git a/drivers/eos.py b/drivers/eos.py index 9d4618c..53a64b3 100644 --- a/drivers/eos.py +++ b/drivers/eos.py @@ -1,8 +1,43 @@ +# 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): @@ -12,13 +47,24 @@ class EOSDriver(NetworkDriver): 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 replace_config(self): + self.device.replace_config() + + def rollback(self): + self.device.rollback() + def get_facts(self): hostname = self.device.show_hostname() sv = self.device.show_version() @@ -58,4 +104,31 @@ class EOSDriver(NetworkDriver): bgp_neighbors = list_peers, ) bgp_table.append(bgp_instance) - return bgp_table \ No newline at end of file + 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/objects/interface.py b/objects/interface.py index a8268cf..40b2f61 100644 --- a/objects/interface.py +++ b/objects/interface.py @@ -1,21 +1,21 @@ -class Status: - UP = 0 - OPER_DOWN = 1 - ADMIN_DOWN = 2 - +class InterfaceStatus: + UP = 1 + OPER_DOWN = -1 + ADMIN_DOWN = -2 class Interface: - def __init__(self, name, speed, interface_type, status): + def __init__(self, name, speed, 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 \ No newline at end of file + self.name = name + self.speed = speed + self.status = status + + def __str__(self): + return self.name \ No newline at end of file diff --git a/objects/lldp.py b/objects/lldp.py index ea2d33d..7854608 100644 --- a/objects/lldp.py +++ b/objects/lldp.py @@ -10,4 +10,10 @@ class LLDPNeighbor: """ self.hostname = hostname self.local_port = local_port - self.remote_port = remote_port \ No newline at end of file + self.remote_port = remote_port + + def __str__(self): + return self.hostname + + def __repr__(self): + return 'LLDPNeighbor: %s' % self.__str__() \ No newline at end of file diff --git a/test/unit/config.py b/test/unit/config.py new file mode 100644 index 0000000..7fb2a56 --- /dev/null +++ b/test/unit/config.py @@ -0,0 +1,20 @@ +# 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. + +hostname = '10.48.71.3' +username = 'admin' +password = 'sp0tify' +use_ssl = True +config_file_1 = 'config01.txt' +config_file_2 = 'config02.txt' diff --git a/test/unit/config01.txt b/test/unit/config01.txt new file mode 100644 index 0000000..c0bd136 --- /dev/null +++ b/test/unit/config01.txt @@ -0,0 +1,197 @@ +! Command: show running-config +! device: config01 (DCS-7150S-64-CL, EOS-4.14.5F) +! +! boot system flash:/EOS-4.14.5F.swi +! +transceiver qsfp default-mode 4x10G +! +queue-monitor length update-interval 5000000 +! +hostname config01 +! +spanning-tree mode mstp +! +aaa authorization exec default local +! +no aaa root +! +username admin privilege 15 role network-admin secret 5 $1$7uXjRZfX$pOFGCCKivNwqIDYFIYbze0 +! +vrf definition mgmtVRF + rd 65000:65000 +! +vrf definition public + rd 1:1 +! +vrf definition service + rd 1:2 +! +interface Ethernet1 + description "Description for eth1" +! +interface Ethernet2 + description "Description for eth2" + no switchport + ip address 10.0.0.1/31 +! +interface Ethernet3 + description "Description for eth3" +! +interface Ethernet4 +! +interface Ethernet5 +! +interface Ethernet6 +! +interface Ethernet7 +! +interface Ethernet8 + description "Description for eth8" +! +interface Ethernet9 +! +interface Ethernet10 +! +interface Ethernet11 +! +interface Ethernet12 +! +interface Ethernet13 +! +interface Ethernet14 +! +interface Ethernet15 +! +interface Ethernet16 +! +interface Ethernet17 +! +interface Ethernet18 +! +interface Ethernet19 +! +interface Ethernet20 +! +interface Ethernet21 +! +interface Ethernet22 +! +interface Ethernet23 +! +interface Ethernet24 +! +interface Ethernet25 +! +interface Ethernet26 +! +interface Ethernet27 +! +interface Ethernet28 +! +interface Ethernet29 +! +interface Ethernet30 +! +interface Ethernet31 +! +interface Ethernet32 +! +interface Ethernet33 +! +interface Ethernet34 +! +interface Ethernet35 +! +interface Ethernet36 +! +interface Ethernet37 +! +interface Ethernet38 +! +interface Ethernet39 +! +interface Ethernet40 +! +interface Ethernet41 +! +interface Ethernet42 +! +interface Ethernet43 +! +interface Ethernet44 +! +interface Ethernet45 +! +interface Ethernet46 +! +interface Ethernet47 +! +interface Ethernet48 +! +interface Ethernet49/1 +! +interface Ethernet49/2 +! +interface Ethernet49/3 +! +interface Ethernet49/4 +! +interface Ethernet50/1 +! +interface Ethernet50/2 +! +interface Ethernet50/3 +! +interface Ethernet50/4 +! +interface Ethernet51/1 +! +interface Ethernet51/2 +! +interface Ethernet51/3 +! +interface Ethernet51/4 +! +interface Ethernet52/1 +! +interface Ethernet52/2 +! +interface Ethernet52/3 +! +interface Ethernet52/4 +! +interface Loopback2 + vrf forwarding public + ip address 2.2.2.2/32 +! +interface Loopback3 + vrf forwarding service + ip address 3.3.3.3/32 +! +interface Management1 + ip address 10.48.71.3/22 +! +ip route 0.0.0.0/0 10.48.68.1 +ip route vrf mgmtVRF 0.0.0.0/0 10.48.68.1 +! +no ip routing +ip routing vrf public +ip routing vrf service +no ip routing vrf mgmtVRF +! +router bgp 12345 + vrf public + local-as 2 + neighbor 2.2.2.2 remote-as 123 + neighbor 2.2.2.2 maximum-routes 12000 + ! + vrf service + local-as 3 + neighbor 3.3.3.3 remote-as 321 + neighbor 3.3.3.3 maximum-routes 12000 +! +management api http-commands + no shutdown +! +! +end diff --git a/test/unit/config02.txt b/test/unit/config02.txt new file mode 100644 index 0000000..0713fda --- /dev/null +++ b/test/unit/config02.txt @@ -0,0 +1,164 @@ +transceiver qsfp default-mode 4x10G + +queue-monitor length update-interval 5000000 + +hostname config02 + +spanning-tree mode mstp + +aaa authorization exec default local + +no aaa root + +username admin privilege 15 role network-admin secret 5 $1$7uXjRZfX$pOFGCCKivNwqIDYFIYbze0 + +vrf definition mgmtVRF + rd 65000:65000 + +interface Ethernet1 + description "whatever" + +interface Ethernet2 + description "whatever" + no switchport + ip address 10.0.0.1/31 + +interface Ethernet3 + description "whatever" + +interface Ethernet4 + +interface Ethernet5 + +interface Ethernet6 + +interface Ethernet7 + +interface Ethernet8 + +interface Ethernet9 + +interface Ethernet10 + +interface Ethernet11 + +interface Ethernet12 + +interface Ethernet13 + +interface Ethernet14 + +interface Ethernet15 + +interface Ethernet16 + +interface Ethernet17 + +interface Ethernet18 + +interface Ethernet19 + +interface Ethernet20 + +interface Ethernet21 + +interface Ethernet22 + +interface Ethernet23 + +interface Ethernet24 + +interface Ethernet25 + +interface Ethernet26 + +interface Ethernet27 + +interface Ethernet28 + +interface Ethernet29 + +interface Ethernet30 + +interface Ethernet31 + +interface Ethernet32 + +interface Ethernet33 + +interface Ethernet34 + +interface Ethernet35 + +interface Ethernet36 + +interface Ethernet37 + +interface Ethernet38 + +interface Ethernet39 + +interface Ethernet40 + +interface Ethernet41 + +interface Ethernet42 + +interface Ethernet43 + +interface Ethernet44 + +interface Ethernet45 + +interface Ethernet46 + +interface Ethernet47 + +interface Ethernet48 + +interface Ethernet49/1 + +interface Ethernet49/2 + +interface Ethernet49/3 + +interface Ethernet49/4 + +interface Ethernet50/1 + +interface Ethernet50/2 + +interface Ethernet50/3 + +interface Ethernet50/4 + +interface Ethernet51/1 + +interface Ethernet51/2 + +interface Ethernet51/3 + +interface Ethernet51/4 + +interface Ethernet52/1 + +interface Ethernet52/2 + +interface Ethernet52/3 + +interface Ethernet52/4 + +interface Management1 + ip address 10.48.71.3/22 + +ip route 0.0.0.0/0 10.48.68.1 +ip route vrf mgmtVRF 0.0.0.0/0 10.48.68.1 + +no ip routing +no ip routing vrf mgmtVRF + +management api http-commands + no shutdown + + +end