unicon.statemachine package

Submodules

unicon.statemachine.statemachine module

Module:

unicon.statemachine.statemachine

Authors:

Vivek Jha (vivejha), CSG STEP - India Mohamed Nowfal(mohamoha), CSG STEP - India

Description:

This module defines State, Path and StateMachine classes.

Each device state shall be defined using the ‘State’ class and link between two device state shall be defined using Path

State Machines consumes both State and Path instances, and provides API’s for computing the path between two state and als performing state change on the device

class unicon.statemachine.statemachine.Path(from_state, to_state, command=None, dialog=None)

Bases: object

This class defines a path between two device states

For changing a device state to any state, this path has to be defined

Path takes the following inputs
  1. From state (starting state of the path)

  2. TO state (destination state of this path)

  3. Command Which takes the device to the destination state

  4. Dialog instance ( List of patterns/prompts which will

    be encountered during this path)

Example

path1 = Path(from_state='enable',
             to_state='config',
             command='config terminal'
             dialog=None)

path1 = Path(from_state='disable',
             to_state='enable',
             command='en'
             dialog=Dialog([r'password:', action=send_password, None, True, True]))

Initializes path attributes and also validates them

validate_update()

Validates the path attributes

class unicon.statemachine.statemachine.State(name, pattern)

Bases: object

Defines a device state

Example

enable_state =  State(name='enable', pattern=r'^.*Router|%N#')

Initializes the state

add_state_pattern(pattern_list)

Appends state patterns to the existing list

Example

state.add_state_pattern('.*new pattern')
property pattern

Return State pattern

Joins all the patterns for this state and returns, after doing appropriate hostname substitution.

restore_state_pattern()

Restore state pattern back to default one.

Example

state.restore_state_pattern()
substitute_hostname(pattern)

Perform hostname substitution in state patterns

Replaces all %N with the hostname, if hostname is provided

substitute_learn_hostname(pattern)

Prepare pattern for hostname learning. Substitute successive occurances of HOSTNAME_SUBST_PAT with named sub-matches hostname0, hostname1, … These sub-matches are processed in connection._get_learned_hostname.

static validate(name, pattern)

Validate the state parameters

class unicon.statemachine.statemachine.StateMachine(hostname=None, learn_hostname=False)

Bases: object

StateMachine for connection

StateMachine class has the detail of all supported device state for any particular platform and the connection between each of this state, and also provide mechanism to change the device state

This class performs the following

  1. Collects and Validates supported device state

  2. Collects path between any two defined state

  3. Computes the transition path from one state to any end state

  4. Keeps in track of the current device state

  5. constructs the Dialog for changing device state on runtime

  6. Enables hostname substitution

  7. Provides methods to change a device state

Each state created outside statemachine has to be add to it

Initializes the state machine

add_default_statements(statements)

Adds default Statements

This statement will be appended to all state transitions

Example

stmt1 =  Statement(pattern=r'.*[confirm]',
                   action=lambda spawn:spawn.sendline()
                   loop_continue=True)
stmt2 =  Statement(pattern=r'.*[yes/no]',
                   action=lambda spawn:spawn.sendline('yes')
                   loop_continue=True)

# Adding default statements
sm.add_default_statements(stm1)
sm.add_default_statements(stm2)

# Adding a list of statements
sm.add_default_statements([stm1, stmt2])
add_path(path)

Add path to the list of available paths

Example

# Create a path instance first
path1 = Path('enable', 'config',
             command='config terminal',
             dialog=None)

# Now add it to SM
sm.add_path(path1)
add_state(state)

Appends a given state to the state machine

Example

state = State(state_name='name', pattern=None')
sm.add_state(state)
create()

Create the statemachine

create_graph()

Creates a graph out of the all states available in SM

create_path(from_state, to_state, command=None, dialog=None)

Creates a Path and add it to the state machine

Example

sm.create_path('enable', 'config',
             command='config terminal',
             dialog=None)
create_state(name, pattern)

Creates a state and add it to State Machines

Example

sm.create_state(state_name='name', pattern=None')
property current_state

Current device state property getter

detect_state(spawn, context={})

Detect the device state by matching the prompt patterns from the device states to the last matched pattern. If a prompt could not be matched, will use sendline() to try and get the prompt and detect the new state. This will detect the ‘last known’ state or try to detect the current state.

The detection on last match depends on the service implementation matching to known prompts. If the service did not match to a known prompt before using detect_state, a sendline() will be executed to detect the prompt.

dotgraph()

Creates a graphviz/dot graph out of the all states available in SM. You can use graphviz/dot to create a PNG statemachine image using “dot -T png -o sm.png sm.dot”

find_all_paths(from_state, to_state)

Return all possible paths from any given state to any state

Example

sm.find_all_paths('enable', 'disable')
get_path(from_state, to_state)

Get the path object for the given from and to state

This API is used for getting paths between directly linked states

Usage:

sm.get_path(<from_state>, <to_state>)

Example::
sm.get_path('enable', 'disable')
get_shortest_path(from_state, to_state)

Finds the shortest path from the list of possible paths

Example

sm.get_shortest_path('rommon', 'enable')
get_state(state_name)

Returns the state object for the given the name

Example

sm.get_state('config')
go_to(to_state, spawn, context={}, dialog=None, timeout=None, hop_wise=False, prompt_recovery=False)

Performs device state change

Parameters
  • to_state – Destination state, or a list of state or a keyword ‘any’

  • spawn – Device handle where the state change has to be performed

  • dialog – Additional dialogs which has to be appended to the SM path

  • prompt_recovery – Enable/Disable prompt_recovery feature

  • timeout – timeout for this goto operation

  • hop_wise – If enabled each path is processed separately Default: False

Example

# Changing to a particular state from the current state
# Here sm is the state machine instance
sm.go_to(to_state='enable',
        spawn=dev.spawn,
        context=dev.context)

# Changing state to anyone state from the given list of states
sm.go_to(to_state=['enable', 'disable'],
        spawn=dev.spawn,
        context=dev.context)

# Bring device to 'any' valid available state in state machine
# ( Usually used for reading the device state)

sm.go_to(to_state='any',
        spawn=dev.spawn,
        context=dev.context)
property hostname
property learn_hostname
property learn_pattern
remove_path(from_state, to_state)

Removes a particular path from SM

Usage:

sm.remove_path(<from_state>, <to_state>)

Example

sm.remove_path('enable', 'disable')
remove_state(state)

Removes a state from SM, given the string name

Example

sm.remove_state('disable')
update_cur_state(state)

Updates the current device state

unicon.statemachine.statetransition module

Module:

unicon.statemachine.statetransition

Authors:

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

Description:

This module defines State Transition classes This define three types of state transitions

StateTransition:

Creates a single large dialog after computing the path for all the hops involved in the state change, which will be consumed by dialog processor

HopWiseStateTransition:

Creates separate dialog for each Hop involved in the state change, which will be iterated over to reach the destination

AnyStateTransition:

This takes a list of destination states or ‘any’ as input Returns if any of the destination is reached, if ‘any’ is the input then takes the device to any of state machine supported state

class unicon.statemachine.statetransition.AnyStateTransition(state_machine, to_state, spawn, dialog, timeout, context, prompt_recovery=False)

Bases: unicon.statemachine.statetransition.StateTransition

State transition for any state

Creates a transition object with all the expected state input provided or all the states supported by SM

It takes a list of state as input, in this case it tries brings the device to any of this state

Also it supports a special key ‘any’, in this case it will try to bring the device to any of the available state in state machine

Example1:
# Creates a transition object
st = AnyStateTransition(state_machine=sm,
                     to_state=['enable', 'disable'],
                     spawn=dev_handle.spawn,
                     dialog=None,
                     context=dev_handle.context)

# Perform the state change
st.do_transition()
Example1:
# Creates a transition object,
# This usually used to identify the device state
# mostly usefull in case of intial connection and reload
st = AnyStateTransition(state_machine=sm,
                     to_state='any',
                     spawn=dev_handle.spawn,
                     dialog=None,
                     context=dev_handle.context)

# Perform the state change
st.do_transition()

Initialize

Parameters
  • state_machine – State Machine of the device

  • to_state – Expected end state

  • spawn – Device handle

  • dialog – Additional Dialogs for this transition

  • context – Device context

create_statement_for_intermediate_state(path)

Creates a Statement for intermediate State

with action as goto next state and loop continue enabled

create_transition_dialogs()

Creates a single large Dialog with the list of transition path provided Also adds the state pattern as a statement to the dialog

do_transitions()

Performs the state transition

class unicon.statemachine.statetransition.HopWiseStateTransition(state_machine, to_state, spawn, dialog, timeout, context, prompt_recovery=False)

Bases: unicon.statemachine.statetransition.StateTransition

Create state transition

Creates a transition object based on the current state of device and the ‘to_state’ and also performs the state change

It creates a list, with the detail of the command and dialog required for this transition, the whole transition may involve multiple intermediate state, each intermediate state transition is processed with separate Dialog

Example

# Creates a transition object
st = HopWiseStateTransition(state_machine=sm,
                            to_state='enable',
                            spawn=dev_handle.spawn,
                            dialog=None,
                            context=dev_handle.context)
# Perform the state change
st.do_transition()

Initialize

Parameters
  • state_machine – State Machine of the device

  • to_state – Expected end state

  • spawn – Device handle

  • dialog – Additional Dialogs for this transition

  • context – Device context

create_transition_dialogs()

Create dialog

Creates a individual Dialog for each path Add add a statement with the states pattern as the exit point

class unicon.statemachine.statetransition.StateTransition(state_machine, to_state, spawn, dialog, timeout, context, prompt_recovery=False)

Bases: object

State transition class

Creates a transition object based on the current state of device and the ‘to_state’ and also performs the state change

This class identifies the commands to perform the state change and also the dialog required, it creates a single large Dialog for whole the transition, which may involve multiple intermediate state change transition on the way to the destination state

Example

# Creates a transition object
st = StateTransition(state_machine=sm,
                     to_state='enable',
                     spawn=dev_handle.spawn
                     dialog=None
                     context=dev_handle.context)

# Perform the state change
st.do_transition()

Initialize

Parameters
  • state_machine – State Machine of the device

  • to_state – Expected end state

  • spawn – Device handle

  • dialog – Additional Dialogs for this transition

  • prompt_recovery – Enable/Disable prompt recovery feature

  • timeout – Overall timeout for this transition

  • context – Device context

create_statement_for_expected_state(index, path)

Create a statement for end state

create_statement_for_intermediate_state(index, path)

Creates a Statement for intermediate State

with action as goto next state and loop continue enabled

create_statement_for_state_pattern(index, path)

Create a statement from path.to_state.pattern

create_transition_dialogs()

Creates a single large Dialog

With list of transition path provided creates a dialog Also add statement for the expected states to the dialog

do_transitions()

Perform the state transition

static goto_next_state(transition, spawn, command, state)

Execute command to take to next state

static update_cur_state(transition, state)

Updates the current state

Module contents