unicon.bases.routers package

Submodules

unicon.bases.routers.connection module

Module:

unicon.bases.routers.connection

Authors:

ATS TEAM (ats-dev@cisco.com, CSG( STEP) - India)

Description:

Defines the Connection Base classes for SingleRp and DualRP router chasis types. It defines the work flow of unicon connection and services initialization

Classes defined in this module serves as the Base classes for implementing various platform

class unicon.bases.routers.connection.BaseDualRpConnection(*args, os=None, os_flavor=None, platform=None, model=None, chassis_type=None, **kwargs)

Bases: unicon.bases.routers.connection.BaseMultiRpConnection

Base class for Dual rp routers/Switches

This does the following

  1. Spawns a connection to each device

  2. Creates State Machine instance for each device

  3. Provides method to connect, initialize the devices

    and perform services

  4. Designates role of each device

Class which are inheriting this class for implementing any particular platform has to define the following class attributes

  1. os = OS for which the implementation intended

  2. platform = Platform platform of the implementation

  3. model = Model for which this implementation supports

  4. chassis_type = Hardware chassis type single_rp, dual_rp

    or stack

  5. state_machine_class = State machine to be used

  6. subcommand_list = List of subcommand supported

Example:

class IosConnection(BaseDualRpConnection):
    os = 'ios'
    platform = None
    chassis_type = 'dual_rp'
    state_machine_class = IosDualRpStateMachine
    connection_provider_class = IosDualRpConnectionProvider
    subcommand_list = ServiceList
    settings = IosConnectionSettings()

Initializes the connection class It does the below initialization

  1. Instantiates the Connection provider

  2. Creates Settings

  3. Attaches the service list

  4. Creates the device Context

DEFAULT_ALIASES = ['a', 'b']
create_subconnections(*args, aliases=[], **kwargs)

Create and return connections contained on a multi-RP connection.

Child connections use the parent’s state machine class.

Returns

subconnections – Subconnection objects

Return type

list

swap_roles()

Swaps the role of the subconnections This typically occurs when the switchover service is invoked.

class unicon.bases.routers.connection.BaseMultiRpConnection(*args, os=None, os_flavor=None, platform=None, model=None, chassis_type=None, **kwargs)

Bases: unicon.bases.connection.Connection

Base class for Multi rp routers/Switches

This does the following

  1. Spawns a connection to each device

  2. Creates State Machine instance for each device

  3. Provides method to connect, initialize the devices

    and perform services

  4. Designates role of each device

Any class inheriting from this class must define the following class attributes:

  1. os = OS for which the implementation intended

  2. platform = Platform platform of the implementation

  3. model = Model for which this implementation supports

  4. chassis_type = Hardware chassis type single_rp, dual_rp

    or stack

  5. state_machine_class = State machine to be used

  6. subcommand_list = List of subcommands supported

Example:

class IosConnection(BaseDualRpConnection):
    os = 'ios'
    platform = None
    chassis_type = 'dual_rp'
    state_machine_class = IosDualRpStateMachine
    connection_provider_class = IosDualRpConnectionProvider
    subcommand_list = ServiceList
    settings = IosConnectionSettings()

Initializes the connection class It does the below initialization

  1. Instantiates the Connection provider

  2. Creates Settings

  3. Attaches the service list

  4. Creates the device Context

property active

Returns the active subconnection

chassis_type = None
connect(*args, **kwargs)

provides mechanism to connect to the device by duck typing connection provider’s connect method

property connected
create_subconnections(*args, aliases=[], **kwargs)

Get list of contained connections on a multi-RP connection.

disconnect()

provides mechanism to disconnect from the device by duck typing connection provider’s disconnect method

property handles_designated

Returns if the the role of each device handle is designated

property hostname
property is_connected

Returns the connection status of the device

property is_ha

Returns True if this is a HA handle

os = None
platform = None
settings = None
setup_connection()

Spawn subconnections and wait to each RP

property standby

Returns the standby subconnection

property subconnections
class unicon.bases.routers.connection.BaseQuadRpConnection(*args, os=None, os_flavor=None, platform=None, model=None, chassis_type=None, **kwargs)

Bases: unicon.bases.routers.connection.BaseMultiRpConnection

Base class for Quad rp routers/Switches

This does the following

  1. Spawns a connection to each rp

  2. Creates State Machine instance for each device

  3. Provides method to connect, initialize the devices

    and perform services

  4. Designates role of each device

Class which are inheriting this class for implementing any particular platform has to define the following class attributes

  1. os = OS for which the implementation intended

  2. platform = Platform platform of the implementation

  3. model = Model for which this implementation supports

  4. chassis_type = Hardware chassis type Quad

  5. state_machine_class = State machine to be used

  6. subcommand_list = List of subcommand supported

Example:

class IosxeStackConnection(BaseStackRpConnection):
    os = 'iosxe'
    platform = None
    chassis_type = 'quad'
    state_machine_class = IosxeQuadRpStateMachine
    connection_provider_class = IosxeQuadRpConnectionProvider
    subcommand_list = ServiceList
    settings = IosxeQuadConnectionSettings()

Initializes the connection class It does the below initialization

  1. Instantiates the Connection provider

  2. Creates Settings

  3. Attaches the service list

  4. Creates the device Context

DEFAULT_ALIASES = ['a', 'b', 'c', 'd']
property active_ics

Returns the active switch in chassis standby subconnection

create_subconnections(*args, aliases=[], **kwargs)

Create and return connections contained on a stack connection.

Child connections use the parent’s state machine class.

Returns

subconnections – Subconnection objects

Return type

list

property standby_ics

Returns the standby switch in chassis standby subconnection

class unicon.bases.routers.connection.BaseSingleRpConnection(*args, os=None, os_flavor=None, platform=None, model=None, chassis_type=None, **kwargs)

Bases: unicon.bases.connection.Connection

Base class for single rp routers

This does the following

  1. Spawns a connection to the device

  2. Creates a State Machine instance for this device

  3. Provides method to connect, initialize the devices

    and perform services

Class which are inheriting this class for implementing any particular platform has to define the following class attributes

  1. os = OS for which the implementation intended

  2. platform = Platform platform of the implementation

  3. model = Model for which this implementation supports

  4. chassis_type = Hardware chassis type single_rp, dual_rp

    or stack

  5. state_machine_class = State machine to be used

  6. subcommand_list = List of subcommand supported

Example:

class IosConnection(BaseSingleRpConnection):
    os = 'ios'
    platform = None
    chassis_type = 'single_rp'
    state_machine_class = IosSingleRpStateMachine
    connection_provider_class = IosSingleRpConnectionProvider
    subcommand_list = ServiceList
    settings = IosConnectionSettings()

Initializes SingleRP connection

chassis_type = None
property connection_type

Returns if the is a console or VTY connection

disconnect()

provides mechanism to disconnect from the device by duck typing connection provider’s disconnect method

property hostname

Returns this connection’s hostname

property is_ha

Attribute to check if this is a HA connection

os = None
platform = None
setup_connection()

Creates a Session and spawns a connection

class unicon.bases.routers.connection.BaseStackRpConnection(*args, os=None, os_flavor=None, platform=None, model=None, chassis_type=None, **kwargs)

Bases: unicon.bases.routers.connection.BaseMultiRpConnection

Base class for Stack rp routers/Switches

This does the following

  1. Spawns a connection to each rp

  2. Creates State Machine instance for each device

  3. Provides method to connect, initialize the devices

    and perform services

  4. Designates role of each device

Class which are inheriting this class for implementing any particular platform has to define the following class attributes

  1. os = OS for which the implementation intended

  2. platform = Platform platform of the implementation

  3. model = Model for which this implementation supports

  4. chassis_type = Hardware chassis type stack

  5. state_machine_class = State machine to be used

  6. subcommand_list = List of subcommand supported

Example:

class IosxeStackConnection(BaseStackRpConnection):
    os = 'iosxe'
    platform = None
    chassis_type = 'stack'
    state_machine_class = IosxeStackRpStateMachine
    connection_provider_class = IosxeStackRpConnectionProvider
    subcommand_list = ServiceList
    settings = IosxeConnectionSettings()

Initializes the connection class It does the below initialization

  1. Instantiates the Connection provider

  2. Creates Settings

  3. Attaches the service list

  4. Creates the device Context

DEFAULT_ALIASES = 'peer_{}'
create_subconnections(*args, aliases=[], **kwargs)

Create and return connections contained on a stack connection.

Child connections use the parent’s state machine class.

Returns

subconnections – Subconnection objects

Return type

list

unicon.bases.routers.connection_provider module

Module:

unicon.bases.routers.connection_provider

Author:

ATS Team (ats-dev@cisco.com)

Description:

The connection class for any platform depends on connection provider class for providing two essential methods, namely, connect and disconnect. As the name suggests their role is to let the application connect and disconnect to the device.

Though they may contain any number of methods but connect and disconnect are their only public APIs.

This module contains connection providers classes for all types of chassis, e.g Single RP, Dual RP etc. And it may expand in future based on the needs of coming platforms. The implementations are provided here are targeted for generic platform which means that it can/should be used as starting point for writing connection providers for other platforms, though this is just a recommendation.

Dependencies:

connection providers are passed the whole connection object. By the time it is instantiated, it is also imperative that, our connection class contains the following objects:

* spawn objects are created.
* state machine objects are created.

It is also expected that all the other vital attributes are present like settings, logs etc.

Lifecycle:

Connection Providers can only be instantiated after the following steps are completed:

* Creation of all the spawn classes.
* Creation of all the state machines.
Mutations:

The caller of connect and disconnect method is Connection. Connection Provider classes are passed the parent object (Connection) so that some mutation in the parent can be performed. Typical parent attributes which are touched upon by connection providers:

handles_designated
Exceptions:

All the classes in this module should raise ConnectionError, in case exceptions are caught.

Example

Examples are provided in class documentation.

class unicon.bases.routers.connection_provider.BaseCommonRpConnectionProvider(connection)

Bases: object

Connection provider with common methods for single and dual RP

Initializes the connection provider.

execute_init_commands(standby=False)

Executes the initialization commands on the device. On standy don’t execute configure commands.

get_connection_dialog()

Creates and returns Dialog, for processing device connection

By default connection provided uses state machine’s dialog for any connection processing. If any additional dialog required has to be updated here

learn_tokens(con, device)
set_init_commands()
class unicon.bases.routers.connection_provider.BaseDualRpConnectionProvider(connection)

Bases: unicon.bases.routers.connection_provider.BaseMultiRpConnectionProvider

Initializes the connection provider.

designate_handles()

Identifies the Role of each handle and designates if it is active or standby

class unicon.bases.routers.connection_provider.BaseMultiRpConnectionProvider(connection)

Bases: unicon.bases.routers.connection_provider.BaseCommonRpConnectionProvider

Connection Provider for Dual RP The connection class for any platform depends on connection provider class for providing two essential methods, namely, connect and disconnect. As the name suggests their role is to let the application connect and disconnect to the device.

Initializes the connection provider.

assign_ha_mode()

Gets the HA mode for this chassis

connect(*args, **kwargs)

Connects, initializes and designates handle

designate_handles()

Identifies the Role of each handle and designates if it is active or standby

disconnect()

Disconnect the connection to both RP’s

establish_connection()

Reads the device state and brings both RP to the right state

init_active()

Performs active RP initialization

init_connection()

Bring device to intialised state

init_standby()

Performs Standby RP initialization

learn_hostname(subconnection)

Update the learned hostname if learn is enabled.

learn_tokens(subconnection)
unlock_standby()

Unlocks standby handle

class unicon.bases.routers.connection_provider.BaseQuadRpConnectionProvider(connection)

Bases: unicon.bases.routers.connection_provider.BaseMultiRpConnectionProvider

Connection Provider for Quad device The connection class for any platform depends on connection provider class for providing two essential methods, namely, connect and disconnect. As the name suggests their role is to let the application connect and disconnect to the device.

Initializes the connection provider.

designate_handles()

Identifies the Role of each handle and designates if it is active, standby, active_ics or standby_ics

establish_connection()

Reads the device state and brings both RP to the right state

is_interactive(conn)

Check whether a connection is interactive

class unicon.bases.routers.connection_provider.BaseSingleRpConnectionProvider(connection)

Bases: unicon.bases.routers.connection_provider.BaseCommonRpConnectionProvider

Connection provider for single RP

The connection class for any platform depends on connection provider class for providing two essential methods, namely, connect and disconnect. As the name suggests their role is to let the application connect to and disconnect from the device.

Initializes the connection provider.

connect(*args, **kwargs)

Performs the device connection and initialization

disconnect()

Disconnect from the device

establish_connection()

Reads the device state and brings it to the right state

Note: Passive hostname learning is enabled by default and will give a warning if the device hostname does not match the learned hostname. The learned hostname is only used if user specifies learn_hostname=True. A timeout may occur if the prompt pattern uses the hostname, the timeout error includes the hostname and a hint to check the hostname if a mismatch was detected.

init_connection()

Bring device to intialised state

init_handle()

bring device handle to enable state

learn_hostname()

Update the learned hostname if learn is enabled.

learn_tokens()
class unicon.bases.routers.connection_provider.BaseStackRpConnectionProvider(connection)

Bases: unicon.bases.routers.connection_provider.BaseMultiRpConnectionProvider

Connection Provider for Stack The connection class for any platform depends on connection provider class for providing two essential methods, namely, connect and disconnect. As the name suggests their role is to let the application connect and disconnect to the device.

Initializes the connection provider.

connect(*args, **kwargs)

Connects, initializes and designates handle

designate_handles()

Identifies the Role of each handle and designates if it is active or standby

get_redundancy_details(subcon)

Get the redundancy information of the device

unicon.bases.routers.services module

Module:

unicon.bases.routers.services

Authors:

ATS TEAM (ats-dev@cisco.com, CSG( STEP) - India)

Description:

Contains service base classes related to routers

class unicon.bases.routers.services.BaseService(connection, context, **kwargs)

Bases: object

Base class for all router services.

It include 4 execution steps, which are .

  • pre_service which generally check for the pre-requisite for particular service. One of them will be if connection is established, before try to execute a service on the connection.

  • call_service which includes the actual service implementation in it. and set the value for self.result, which will be used to process the final result of service.

  • post_service includes reverting the device to earlier state. One of them will be bringing the device to end state of that service after service execution.

  • get_service_result will verify the self.result (response of each service) with service error_pattern. If self.result doesn’t match any of the above pattern, service result will be considered pass or it raises SubCommandFailure exception.

Example ::

Implement service which issue given command and device and get the return response from device.

from unicon.bases.routers.services import BaseService

class RunCommand(BaseService):

    def __init__(self,  connection,  context, **kwargs):
        self.connection = connection
        self.context = context
        self.error_pattern = ["my command error"]
        self.start_state = 'enable'
        self.end_state = 'enable'
        self.result = None
        self.__dict__.update(kwargs)

    def pre_service(self, *args, **kwargs):
        # Check if connection is established
        if self.connection.is_connected:
            return
        elif self.connection.reconnect:
            self.connection.connect()
        else:
            raise ConnectionError("Connection is not established to device")

        # Bring the device to required state to issue a command.

        self.connection.state_machine.go_to(self.start_state,
                                            self.connection.spawn,
                                            context=self.connection.context)


    def post_service(self, *args, **kwargs):
        # Bring the device back to end state.
        self.connection.state_machine.go_to(self.end_state,
                                            self.connection.spawn,
                                            context=self.connection.context)

    def call_service(self, command, *args, **kwargs):
        # Command to issue on device is `command`
        con = self.connection
        con.log.debug("+++ run_command +++ ")
        con.spawn.sendline(command)
        # self.result attribute will be used at result validation.
        self.result = con.spawn.expect(.*#?)

    def get_service_result(self):
        pass
Parameters
  • connection – Device connection object

  • context – connection context

add_error_patterns(pattern_list=[], mode='extend')

Allow user to modify error_pattern.

add_pattern(pattern_type, pattern_list=[], mode='extend')

Procedure to add/modify/remove error patterns.

call_service(*args, **kwargs)

Actual services implementation code

get_handle(target=None)
get_service_result()

Common result procedure for all Services

get_sm(target=None)
get_spawn(target=None)
log_service_call(message=None)
classmethod platform_check(*args, **kwargs)

Verifies whether the service is applicable for a given platform

Returns

If the service is applicable for a given platform False: If service is not applicable for the given platform

Return type

True

post_service(*args, **kwargs)

Common post_service procedure for all Services

pre_service(*args, **kwargs)

Common pre_service procedure for all Services

service_dialog(handle=None, service_dialog=None, matched_retries=None, matched_retry_sleep=None)

Form dialog for required service state

trim_device_output(result)
verify_error_pattern(error_pat, result)

Procedure to which matches the error pattern.

Module contents