0% found this document useful (0 votes)
234 views141 pages

Network Programmability Foundation PDF

Network programmability allows software to configure and manage network infrastructure through centralization of policies, automation of tasks, and separation of the control and data planes. Software-defined networking (SDN) implements this separation through a controller that acts as a centralized software interface. The SDN architecture uses a three-layer model consisting of applications, a controller, and infrastructure devices. Popular automation tools like Ansible and Puppet allow network tasks to be automated through playbooks, inventory files, and configuration resources. Version control systems like Git and shared code repositories on GitHub help track changes to network configurations and code over time in distributed environments.

Uploaded by

kongarajaykumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views141 pages

Network Programmability Foundation PDF

Network programmability allows software to configure and manage network infrastructure through centralization of policies, automation of tasks, and separation of the control and data planes. Software-defined networking (SDN) implements this separation through a controller that acts as a centralized software interface. The SDN architecture uses a three-layer model consisting of applications, a controller, and infrastructure devices. Popular automation tools like Ansible and Puppet allow network tasks to be automated through playbooks, inventory files, and configuration resources. Version control systems like Git and shared code repositories on GitHub help track changes to network configurations and code over time in distributed environments.

Uploaded by

kongarajaykumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

Network Programmability

Foundation
Introduction to Network Programmability

ine.com
+ Network
Programmability
Module Overview
overview
+ Network Automation
+ SDN
Network Programmability Overview

+ The ability to use software to configure & manage network infrastructure


+ Main drivers
+ Centralized Policy & Configuration
+ E.g. a Controller (SDN)
+ Consistency & Predictability
+ Removes the "human factor"
+ Optimization & Security
+ Application visibility
+ Threat detection/mitigation
+ Automation
Network Automation

+ Network Programmability allows for Automation


+ Performing a certain task without human intervention
+ Orchestration refers to automating/dealing with a lot of things at once
+ Automation tools commonly used today include Ansible & Puppet

+ Automation Benefits
+ Improved configuration/deployment times
+ Deterministic outcomes
+ Data collection - reporting & troubleshooting
+ Cost reduction
Software Defined Networking (SDN)

+ Originally defined as a separation of the Control & Data Planes


+ The Control Plane is pulled out from the network to a separate unit -
Controller
+ Single centralized software "interface" to the network
+ Simplifies programmability, integration & management
+ SDN implies programmability but is not the same thing

+ Controller Examples
+ Open DayLight
+ Cisco Application Policy Infrastructure Controller (APIC)
+ APIC-EM (APIC Enterprise Module)
SDN Architecture

+ SDN Architecture is logically represented using a three-layer model


+ Application
+ Control
+ "Ties" Application to Infrastructure (and vice versa) via special interfaces
+ Infrastructure

+ Controller Interfaces
+ Northbound (NBI)
+ Applications <-> Controller
+ Southbound (SBI)
+ Controller <-> Network
SDN Architecture

+ Controller Interfaces are typically implemented as APIs


+ Application Programming Interface (API) allows software (machine) to
communicate with other software (machine)

+ NBI Standards
+ Representational State Transfer (REST) API

+ SBI Standards
+ OpenFlow
+ NetConf
+ SNMP or CLI (Telnet, SSH)
Network Programmability
Foundation
Automation Tools

ine.com
+ Ansible overview
Module Overview
+ Ansible files
+ Puppet overview
Ansible Overview

+ Open-source* agentless automation software


+ The Ansible software is only needed on the Control Machine
+ Linux/Unix with Python 2 (2.7+) or 3 (3.5+) installed
+ Managed Nodes are accessed through SSH by default
+ All node SSH keys should be part of /etc/ssh/known_hosts
+ The key checking process can be disabled in ansible.cfg

+ Ansible Operations
+ Network devices are managed in the Local Mode
+ Python code gets executed locally on the Control unit
+ Resulting CLI commands are sent over SSH
Ansible Files

+ Ansible requires Inventory & Playbook files to start automation

+ Inventory File
+ Describes all Managed Nodes (INI or YAML format)
+ Location: /etc/ansible/hosts
+ Example
10.2.3.4

[routers]
r1.ine.com
r2.ine.com
Ansible Files

+ Playbook File
+ Defines automation instructions in YAML format
+ Hosts-to-Task mappings
+ Tasks define Modules to run and their parameters
+ Check out documentation at docs.ansible.com (Module Index)

+ Playbook Execution
+ ansible-playbook -i inventory_fname playbook_name.yml

+ Ad-hoc commands can be issued without a Playbook


+ Only useful for very small tasks
Playbook Example

---
- name: description
hosts: routers
connection: local

tasks:
- name: TASK1 - SHOW RUN
ios_command:
commands:
- show run
provider:
username: cisco
password: cisco
host: "{{ inventory_hostname }}"
Puppet Overview

+ Client-server automation software


+ The Puppet server (Master) must be a Linux station
+ Managed Nodes (Slaves) can be much more
+ Require a Puppet Agent
+ Authenticated using SSL certificates

+ Main Puppet Components


+ Resources & Manifests
+ Written in custom Ruby-based Domain Specific Language (DSL)
Puppet Overview

+ Resources
+ System components Puppet can manage
+ General Syntax
<type> { 'title':
attribute_n => value1,
}
+ Example
service { 'ssh':
ensure => running,
}

+ Manifests
+ Files (.pp) with Resources & other Puppet code
Network Programmability
Foundation
Version Control with Git

ine.com
+ Version Control & Git
Module Overview overview
+ Working with Git CLI
+ Git Branching
Version Control Overview

+ A process of tracking files & changes to those over time


+ Also known as Source/Revision Control
+ Advantages
+ Change tracking
+ Accountability
+ Simplified collaboration

+ Common Applications
+ Software development
+ Configuration management
+ Documentation maintenance
Git Overview

+ A popular Version Control System


+ Open-source & free for download
+ https://git-scm.com
+ Client-server Architecture
+ Easy installation
+ https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Working with Git CLI

+ Create a Git Repository


+ Go to the Git-designated folder and issue git init
+ The .git subdirectory is where all project files will be stored

+ Configure user information


+ Edit the .gitconfig file

+ Add files
+ Copy files to the Git folder
+ The tracking process starts after using git add
+ Confirm with git status
Working with Git CLI

+ Commit the change to create a 'snapshot'


+ git commit
+ Verify with git log

+ File modifications require a similar procedure


+ Once the files are modified, sync them via git add & commit with git commit
+ Using git status helps a lot
+ To check the differences between the versions of the same file, use git diff
+ Input varies from commit hashes to file names
+ git diff 0023cdd..fcd6199 filename
Git Branching

+ A process of making "copies" of the main Repository


+ Allows to work on the project without affecting the Repo (Master Branch)
+ New features
+ Testing
+ Collaboration

+ Git Branch is just a reference to an existing snapshot (commit)


+ Branch workspace is separated from the Master Branch
+ The branch changes can be added to the Repo (Merging) or deleted
Working with Git Branches

+ Create a new branch


+ git branch bname
+ To start using a branch, issue git checkout [bname | master]
+ Using git checkout -d automatically creates a branch and make it "active"

+ Merge two branches, if needed (optional)


+ Issue git merge bname from the "main" branch (e.g. Master)
+ Optionally delete the branch after merge
+ git branch -d bname
Network Programmability
Foundation
Distributed Git & GitHub

ine.com
Module Overview
+ Git & multiple systems
+ Using GitHub
Git & Multiple Systems

+ Git was designed to support distributed environments in two ways


+ Peer-to-peer
+ Centralized (Shared)
+ Private or Public

+ Supported Protocols
+ Native Git (TCP 9418)
+ git://fqdn/path_to_repo/repository
+ SSH
+ ssh://[user@]fqdn/path_to_repo/repository
+ HTTP[S]
+ http[s]://fqdn/path_to_repo/repository
Working with a Shared Repository

+ Create a Shared Repository


+ git init --bare rname

+ Download & initialize the repository locally


+ git clone rname_url local_directory
+ A "backlink" to the repository is created automatically

+ Synchronize files
+ Use git pull to download changes
+ Local changes can be uploaded via git push
Using GitHub

+ GitHub is a public Git-based Shared Repository service


+ Commonly used for open-source projects

+ Working with GitHub


+ Create an account on https://github.com
+ Download & sync with remote system
+ git clone https://github.com/user/name.git
+ Use regular Git syntax to manage the files & updates
+ git add, git commit, git pull, git push, etc.
Network Programmability
Foundation
Introduction to Python

ine.com
+ Python overview
Module Overview + Writing Python code
+ Running a Python file
+ Documentation
Python Overview

+ Open source interpreted programming language created in late 1980’s


+ Popular for many reasons
+ Easy to use & read
+ Portable
+ Extendable
+ Object-Oriented
+ Works in virtual environments
+ Single Python installation may not meet requirements of all applications
+ Virtual environments with self-contained directories resolve the conflict
+ E.g. "venv" or "virtualenv" packages

+ Generally deemed as a natural choice for Network Programmability


Python Overview

+ Version 3.x fixes problems found in version 2.x (EOL in 2020)


+ https://wiki.python.org/moin/Python2orPython3

+ The Python code (CPython) can be downloaded from www.python.org


+ Some OSes come with Python pre-installed
Writing Python Code

+ Python Shell (Interactive Interpreter)


+ Opened through the python command (or pythonx for version x)
+ Great for testing, but instructions are “lost” once executed

+ Python File
+ Regular text file with the ".py" extension
+ Code editor does matter
+ Source Code Editors help with syntax, formatting, highlighting, etc.
+ IDE (Integrated Development Environment)
+ Contains a Source Code Editor and much more
+ Default IDLE (Integrated Development and Learning Environment) may work
Running a Python File

+ Your OS must known the right application to run the Python file
+ Select it explicitly
+ E.g. python test1.py or /usr/bin/python test1.py
+ Embed the application information into the beginning of the file
+ Known as "Shebang" or "Magic Line"
+ #!/usr/bin/python
+ #!/usr/bin/env python
+ Examine $PATH to find the python app
Python Documentation

+ Python Documentation can be found at https://docs.python.org


+ Tutorial
+ Library Reference
+ Global Module Index
+ General Index
+ Search
Network Programmability
Foundation
Basic Python Constructs

ine.com
+ Variables
+ Basic functions
Module Overview
+ Data Types
+ Comments
+ Indentation
Variables

+ User-defined containers for data values


+ Must start with a letter or the underscore character
+ Cannot be a Python-reserved word (e.g. print, loop)
+ Name should be human-readable & meaningful
+ E.g. "interface_speed" and not "axuqz2"
+ The '=' operator is used to assign variable a value
+ E.g. "interface_speed = 100000"

+ Variable is technically acting as a reference (pointer) to the memory


+ Check with id()
Data Types

+ Used to distinguish between the different types of values a Python


program may use
+ Common built-in Data Types
+ String (str)
+ Boolean (bool)
+ List (list)
+ Tuple (tuple)
+ Dictionary (dict)

+ Mutable Data Type can be modified in the original memory location


+ As opposed to Immutable Data Types
Basic Functions

+ Existing Python code meant for a purpose


+ fname(optional_arguments)

+ Common functions
+ print() - displays input/arguments (e.g. a variable, string) on the screen
+ Referring to the variable itself is another way of printing it
+ id() - returns the memory address of a variable
+ type() - tells the Data Type
+ dir() - shows valid object’s attributes (e.g. methods)
+ If no arguments are provided, displays all elements in the namespace
+ help() - explains the method’s syntax; also check https://docs.python.org
Indentation

+ Indentation (leading whitespaces) in Python is critical


+ Defines a code block
+ A group of statements to be treated as one statement
if counter > 0 and counter < 5:
print (counter, ‘ is greater than 0’)
counter += 1
print (‘Back to block 1’)
+ Makes the code easier to read
+ Not important within the lines (e.g. 'if a>0' is the same as 'if a >0')

+ Indentation type (tabs vs spaces) must be consistent


Comments

+ Including meaningful comments in the code is deemed as a best practice


+ A single line comment starts with a hash (#)
# This is a one line comment
+ Multiline comments technically don’t exist in Python
+ Using a triple single/double quote (''')/(""") makes a string that is ignored
- not recommended
'''
Multiline quasicomment1
Multiline quasicomment2
Multiline quasicomment3
'''
Network Programmability
Foundation
Data Types - Numbers

ine.com
+ Python Integers
Module Overview
+ Python Floats
+ Useful functions
Python Numbers

+ Integer
+ A whole number (positive or negative)
+ 1, 2, 5, 121, -17, etc.
+ Immutable, represented as int
+ Binary & Hex numbers are treated as Integers
+ To represent a binary/hex number precede it with '0b'/'0x'

+ Float
+ A fractional number written in decimal (.) notation (positive or negative )
+ 1.99, 24.5, -17.182, etc.
+ Immutable, represented as float
Mathematical Operations

+ Python supports regular mathematical operations through operators


+ Addition (+)
+ Subtraction (-)
+ Multiplication (*)
+ Division (/)
+ Exponentiation (**)
+ Modulus (%)
+ Yields the remainder from the division

+ Parenthesis can be used to change the default order of operations


+ E.g. -2**2 results in -4 vs (-2)**2 results in 4
Useful Functions

+ int()
+ Returns an integer representation of a string or number
+ The default number base 10 can be changed with a second argument
+ int (number/string, base)
+ E.g. int('1110', 2) or int ('af', 16)

+ bin()
+ Converts an integer to a binary string

+ hex()
+ Converts an integer to a hexadecimal string
Network Programmability
Foundation
Data Types - Boolean

ine.com
Module Overview
+ Python Booleans
Python Booleans

+ Boolean is a two-value subtype of Integer


+ True/False (case sensitive)

+ Booleans are often returned/used by certain functions or checks (e.g.


Comparisons)
+ Pseudo-code: If (x==y) do z

+ Boolean Operations
+ and
+ or
+ not
Network Programmability
Foundation
Data Types - String

ine.com
Module Overview + Python Strings
+ Useful methods
Python Strings

+ Represent textual data (a sequence of characters)


+ Defined within single (') or double ("") quotes
+ E.g. interface = 'Gig0/1' or interface = "Gig0/1"
+ Triple quotes (''' or """) allow to write strings that span multiple lines
+ Quotation within the text itself can be maintained with backslash (\)
+ Also works for special code sequences, such as tab (\t) or new line (\n)
+ Data can be converted to String using the str() function
Python Strings

+ String Operations
+ Arithmetic Operators
+ Concatenation (+)
+ Multiplication (*)
+ Indexing
+ Use string[x] to retrieve character positioned at x
+ Slicing
+ Use string[x:y] to retrieve characters from x to y-1
Useful Methods

+ upper() & lower()


+ Useful for string comparison

+ strip()
+ Removes leading and trailing whitespaces (default)
+ A custom set of characters to be removed can be defined

+ find(string, substr)
+ Returns the lowest index in the string where substr is found
Useful Methods

+ split('sep')
+ Returns a List of the words in the string delimited by sep

+ splitlines()
+ Returns a List of the lines in the string, breaking at line boundaries

+ format()
+ Takes a string to format and an arbitrary set of "replacement fields" ({ })
+ Documentation
+ https://docs.python.org/3/library/string.html#string-formatting
+ https://www.python.org/dev/peps/pep-3101/
Network Programmability
Foundation
Data Types - List & Tuple

ine.com
+ Python List & Tuple
Module Overview
overview
+ Useful methods
Python Lists

+ An ordered sequence of objects (elements) of any type


+ List elements are enclosed by brackets ([ ]) and separated with commas
+ E.g. duplex=['half', 'full', 'auto'] or mix=['str', 4]
+ The len() function returns the total number of list objects/elements

+ Accessing List Elements


+ Indexing
+ Use list[x] to get object positioned at x
+ Slicing
+ Use list[a:b:c] to get a slice of list from a to b-1 with step c
+ At least argument is needed, e.g. list[2:]
Python Tuples

+ Similar to List, but immutable


+ Ensures data integrity
+ Tuple elements are enclosed by parenthesis '( )' and separated with commas
+ E.g. fixed_BW = (1000, 10000, 100000)

+ Tuple Elements can be accessed using Indexing & Slicing, like with Lists
Useful List Methods

+ append(object)
+ Adds object to the end of the list

+ insert(index, object)
+ Inserts object to the list at position index

+ pop(index)
+ Removes object positioned at index from the list and returns it

+ sort()
+ Reorders list elements (from "lower" to "higher")
Useful List & Tuple Methods

+ index(object)
+ Returns the index of object

+ count(object)
+ Returns the number of object occurrences in the list or tuple
Network Programmability
Foundation
Data Types - Dictionary

ine.com
Module Overview + Dictionary overview
+ Useful methods
Python Dictionaries

+ A unordered collection of key-value pairs, known as Items


+ Dictionary items (key: value) are enclosed by curly brackets ({ }) and
separated with commas
+ E.g. device1={'hostname': 'R1', 'os': 12.1}
+ Values can be of any type, but Keys must be "hashable"
+ Any immutable data type will work, such as String, Integer or Tuple
+ The dict() function is an alternative way of creating Dictionaries

+ Accessing the Dictionary


+ To access value mapped to key key from dictionary dict use dict[key]
+ Since Dictionary is mutable, values can be modified
Useful Methods

+ get()
+ Same as dict[key], but does not raise an error if key does not exists

+ keys(), values(), item()


+ Returns a List of keys/values/items of the dictionary

+ pop(key)
+ Removes the item indexed with key and returns its value

+ update(dict2)
+ Adds the content of dict2 to the dictionary
Network Programmability
Foundation
Conditionals

ine.com
Module Overview + Conditional statements
+ Supported operators
Conditional Statements

+ Special structures used to control the program's flow


+ The if statement tests a condition using Boolean logic
+ True (or anything else than numerical 0)
+ False (o numerical 0)
+ The statement(s) after if is/are only executed if the test result equals True
+ If the result is False, the statement(s) is/are ignored

+ Syntax
if condition:
statement
+ Important: a colon ':' follows condition & indentation precedes statement(s)
Conditional Statements

+ Alternative conditions may be specified to "extend" the if statement


+ The elif clause is evaluated only if the preceding if/elif expression is False
+ The else clause is evaluated only if all preceding expressions are False

if condition:
statement(s)
elif altcondition1:
statement(s)
elif altcondition2:
statement(s)

else:
statement(s)
Conditional Statements

+ The elif/else clauses must be always properly aligned to the if statement


+ Conditionals can be nested in one another
+ Indentation!
Supported Operators

+ The if/elif conditions are often built using multiple elements tied together
with Operators
+ Comparison
+ Equal (==), not equal (!=)
+ Less than (<), greater than (>)
+ Less than or equal (<=), greater than or equal (>=)
+ Boolean
+ and, or, not
+ Membership
+ in, not in
Example
Network Programmability
Foundation
Loops

ine.com
+ Loops overview
+ The while loop
Module Overview
+ The for loop
+ Controlling the
Iterations
Loops Overview

+ Python loops provide a way to implement Iteration


+ Repetitive execution of the same block of code

+ Loop Types
+ while
+ for
The while Loop

+ Syntax
while condition:
statement(s)

+ Executed until the condition turns to False


+ Good for an indefinite type of Iteration
+ Unknown number of "cycles"
The for Loop

+ Syntax
for variable(s) in iterable:
statement(s)

+ Iterable is an object that can be used in iteration


+ A sequence/collection of elements, e.g. List, Tuple, String or File
+ The iter() function tells if an object is iterable or not

+ Executed from the beginning to the end of the iterable object


+ Represents a definite type of Iteration
+ The number of cycles is finite and determined by the object
Controlling the Iterations

+ Loops can be controlled with two special instructions


+ break
+ Immediately terminates the loop
+ Program proceeds to the first after-loop statement
+ continue
+ Immediately resumes the loop
+ New iteration
Network Programmability
Foundation
Functions

ine.com
+ Functions overview
+ Parameters &
Module Overview
Arguments
+ Function output
+ The pass statement
Functions Overview

+ Frequently used code written for a specific purpose


+ Built-in functions include print(), dir(), int(), etc.

+ Basic Syntax
def fname():
'''docstring''' # Optional
code

+ For a function to work, it must be invoked


fname()
Parameters & Arguments

+ Function input (parameters) is optional, but commonly used


+ Parameters can be of any Data Type and number
+ It is even possible to initialize a parameter with a default value
+ To denote an unknown number of parameters, precede one with *
+ Makes the param iterable
+ All formal arguments must be provided during the invocation

+ Full Syntax
def fname(p_1, p_n='default', *params):
'''docstring''' # Optional
code # Code can refer to the parameters
Function Output

+ Functions do something but don't return any data by default


+ Use return to change this behavior
+ Example
def givenumber():
print('This function does something here')
return 2
x = givenumber()
print(x)
The pass Statement

+ Python syntax does not allow to define empty blocks of code


+ Conditionals, loops, functions, etc.
+ Use pass as a placeholder for future code
Network Programmability
Foundation
File Access

ine.com
+ Accessing a File
Module Overview
+ File Operations
+ Closing a File
Accessing a File

+ Before a file can be read or modified, it must be opened


+ open ('filepath', 'mode') # returns a File Object
+ filepath refers to the file path & its name
+ mode determines the level of access to the file
+ r (read-only, default)
+ w (writing, overwrites the file)
+ a (writing, appending to the end of the file)
+ t (open in text/string format, default)
+ b (open in binary/bytes format)
+ + (used with r/w/a for reading & writing)
+ r+ writes to the beginning of the file
+ w+ writes to the beginning of the file & creates a file if it does not exist
+ a+ writes to the end of the file & creates a file if it does not exist
File Operations

+ All file operations are relative to the current stream position ('pointer')
+ tell() returns the current position within the file
+ seek(offset, whence) allows to change the current position
+ whence means relative to the start (0), current position (1) or end (2)

+ Reading
+ Since file object is iterable, the for loop can read it
for line in fileobj:
print (line, end = ' ')
+ File Reading Methods
+ read(), readline(), readlines()
File Operations

+ Writing & Appending


+ File Writing Methods
+ write(string)
+ writelines(sequence)
+ sequence must be iterable (typically a List)

+ For the written data to be read, it may need to be sent to the OS


+ Close the file or use flush()
Closing a File

+ Closing a file flushes the buffer & prevents resource leaks


+ close()

+ Consider with() instead of open()


+ Automatically closes the file
+ Allows for exception handling
+ Syntax
with open ('filepath', 'mode') as fileobject
# code for fileobject
Network Programmability
Foundation
Classes

ine.com
Module Overview + Classes Overview
+ Attribute Types
Classes Overview

+ Classes allow for Object Oriented Programming (OOP)


+ Programming technique attempting to model the real world
+ Things, processes, interactions, etc.

+ A Class is just a template for something you want to model


+ Objects ("Instances") are individual representations/entities of a Class
+ Class definition is made of Attributes & Methods
class cname:
'''docstring''' # optional class documentation
attr_n = value
def method_n():
method_code
Attribute Types

+ Attributes can belong to the Class or Object


+ Class Attributes
+ Specific to all objects/instances ('global')
class cname:
attr_n = value
+ Instance
+ Specific to the object ('local')
+ Defined via __init__() function run automatically as the object is created
class cname:
def __init__(self, atr_1, atr_n) # self refers to the object itself
self.atr_1 = atr_1
self.atr_n = atr_n
Sample Code

class c1:
classatr1 = 'Same for all objects'
def __init__(self, objattr1):
self.objattr1 = objattr1
self.objattr2 = 'object-specific2 default'

obj1 = c1('object-specific1')
Network Programmability
Foundation
Modules & Packages

ine.com
+ Modules & Packages
Module Overview Overview
+ Installing & Accessing
Modules & Packages
Modules Overview

+ A Module refers to an existing .py file


+ Useful code (functions, variables, classes, etc.) one may want to use in his/her
program without duplicating it
+ Not meant to be executed

+ Python comes with a collection of modules ("Standard Library")


+ Examples
+ re (regular expressions)
+ sys (system parameters)
+ os (OS access, file operations)
+ json (JSON data formatting)
Packages Overview

+ A Package refers to a collection of modules


+ Used to organize modules of one type at one place
+ Sometimes referred to as a Library
+ This term can also refer to more than Package
Modules & Packages - Installation

+ Python distribution may already include the entire Standard Library


+ Depends on the OS
+ Additional (or not included) packages must be installed

+ Python Package Index (PyPI)


+ Open source repository of Python software
+ https://pypi.org
+ Projects/packages can be easily installed using the pip tool
+ Basic syntax: pip install requests
+ https://packaging.python.org/tutorials/installing-packages/
Modules & Packages - Access

+ In order to use a Module/Package, it must be first loaded via import


+ import mod_name [as alias]
+ The module code can be accessed after a dot
+ mod_name.item or alias.item
+ from mod_name import item
+ Using from mod_name import * is not recommended
+ The dotted notation is not needed, refer to item directly
+ Loading Packages is similar
+ import package[.subpackage]
+ import package[.subpackage].mod_name
+ from package[.subpackage].mod_name import item
Network Programmability
Foundation
Introduction to APIs

ine.com
+ API overview
Module Overview
+ REST API
+ NETCONF
API Overview

+ Application Programming Interface (API) is a "language" two systems


must understand to communicate
+ Using APIs eliminates the need to parse raw data
+ Standard CLIs were built for humans, not machines and/or software
+ Time-consuming data extraction

+ Common Network APIs


+ REST (RESTful)
+ NETCONF
REST API

+ An API based on Representational State Transfer (REST) Architecture


+ Systems conforming to the REST Architecture are said to be RESTful
+ Client-server
+ Stateless
+ Uniform Interface
+ Uniquely identified Resources & clear data format

+ RESTful clients & servers typically communicate over HTTP


+ Resources are decoupled from their representation
+ JSON or XML
+ RESTCONF is an example of a REST API available on IOS-XE
HTTP Requests & Responses

+ Request Methods
+ GET (retrieve a resource)
+ POST (create a resource)
+ PUT (update/replace a resource)
+ PATCH (modify a resource - partial update)
+ DELETE (delete a resource)

+ Response Codes
+ Success - 2xx
+ Error - 4xx (client), 5xx (server)
NETCONF (Network Configuration Protocol)

+ Network management protocol defined in RFC 6241


+ Operates on structured data to install, manipulate & delete configurations

+ NETCONF Layers
+ Transport
+ SSHv2 (TLS & SOAP are supported, but not common)
+ Messages
+ Provides Remote Procedure Call (RPC) -based communication
+ Operations
+ Content
+ XML-formatted data
NETCONF Messages

+ RPC Request <rpc>


<rpc message-id="id_x">
<!-- Remaining XML-formatted request code -->
</rpc>

+ RPC Response <rpc-reply>


+ Request & Response message-id must match
+ The content is sent within the <data> </data> tags
<rpc-reply message-id="id_x">
<data>
<!-- XML-formatted content -->
</data>
</rpc-reply>
NETCONF Operations

+ Determine the action performed on a remote system


<rpc message-id="id_x">
<operation>
<!-- XML-formatted request -->
<operation>
</rpc>

+ Common Operations
+ <get>
+ Retrieves running configuration or its part
+ <edit-config>
+ Makes a config change
+ <close-session>
+ Graceful session termination
Network Programmability
Foundation
Data Formats - XML

ine.com
+ Data Formats
Module Overview
+ XML overview
+ XML syntax
Data Formats

+ Define syntax for storing & exchanging data


+ Required to "agree" on the meaning of the sent/received data
+ Important Formats
+ JSON
+ XML
+ YAML
XML Overview

+ eXtensible Markup Language was designed to store & transport data


+ Human & machine -readable
+ Open standard documented at https://www.w3.org/XML/

+ XML files are made of Tags & Elements


+ Tag determines the beginning & end of data
+ Start tag name is enclosed in <>, e.g. <name>
+ XML's extensibility allows to define arbitrary tags
+ Tag ends in </>, e.g. </name>
+ Element refers to tags & data
+ E.g. <name>some data</name> or <name /> (empty element)
+ Tag/element indentation is ignored
Basic Syntax

+ XML file can optionally start with a Declaration


+ Prepares an XML processor to parse the document
+ If used, must appear in the first line of the document
+ Example: <?xml version = "1.0" encoding = "UTF-8"?>

+ The main document's element is known as Root


+ There can be only one Root element
+ Other elements can be nested within the Root & themselves
<device>
<vendor>Cisco</vendor>
<model>2911</model>
</device>
Attributes & Comments

+ XML elements can be associated with one or more Attribute(s)


+ Attributes use the name="value" format & follow the start tag
<device code="IOS XR 7.0.1">
+ Another way of presenting data
+ Good for identifying elements, e.g. <device id="1">

+ Adding comments
+ XML comments start with "<!--" ad end with "-->"
<!-- this is a comment -->
Namespaces

+ XML uses namespaces to prevent naming conflicts


+ Defined as an attribute in the xmlns:[prefix]="uri" format
+ In the start tag of conflicting elements
<prefix1:device xmlns:prefix1="uri1"> … </prefix1:device>
<prefix2:device xmlns:prefix2="uri2"> … </prefix2:device>
+ Inside the start of Root element
<root xmlns:prefix1="uri1" xmlns:prefix2="uri2">
+ Prefix & URI are arbitrary
+ URI typically points to the namespace documentation
Namespaces

+ Prefixes require child elements to be prefixed to belong to the namespace


<a:device xmlns:a="http://www.example.com/namespace-a">
<a:platform>2911</a:platform>
</a:device>

+ Default Namespace simplifies things


<device xmlns="http://www.example.com/namespace-a">
<platform>2911</platform>
</device>

+ More on XML
+ https://www.w3schools.com/xml/default.asp
Network Programmability
Foundation
Data Formats - YAML

ine.com
Module Overview + YAML overview
+ YAML conventions
YAML Overview

+ YAML Ain't Markup Language


+ Well-suited for configuration files (.yaml or .yml extension)
+ Human-readable, but more complex to generate & parse
+ Comments
+ Documentation
+ https://yaml.org/
+ Look for current version, e.g. YAML 1.2
+ Terminology
+ Mappings -> Dictionaries
+ Sequences -> Lists
+ Scalars -> Strings, Numbers
YAML Conventions

+ YAML document starts with three hyphens (---)


+ Comments start with a hash and can be placed in a separate line or after data
+ Indentation controls the interpretation of the data (e.g. nesting)

+ Lists
+ List members are denoted by a single hyphen
+ Inline format: comma-separated members within square brackets
+ Mixing different data types is allowed

--- ---
# comment - R1 # comment
- R1 - 2120
- ASA1 - ['R2', 'R3', 'ASA1']
YAML Conventions

+ Dictionaries
+ Values follow keys with a colon (no hyphen)
+ Inline format: comma-separated key:value pairs enclosed in curly braces
+ Multi data type support

--- ---
router: r1 # inline format
frwl: {router:r1, frwl:['ASA1', 'ASA2'], 3:cisco}
- ASA1
- ASA2
3: cisco
YAML Conventions

+ Blocks
+ Multi-line code can be written as literal '|' or folded '>' blocks
+ Literal block preserves new lines, folded block replaces them with spaces
example: |
This is a multi-line string
that spans across 2 lines.

+ Strings
+ Quotes denote a string & "cancel" meaning of special characters
platform: "2120"
cancel: "this:that"
Network Programmability
Foundation
Data Formats - JSON

ine.com
Module Overview + JSON overview
+ JSON syntax
JSON Overview

+ JavaScript Object Notation (JSON) is an open-standard data format


+ Human & machine -readable
+ Uses two main structures
+ Objects (an unordered collection of name/value pairs, aka Dictionary)
+ Names/keys must be Strings & values can be Strings, Numbers, Objects,
Lists, Boolean or Null
+ Lists
+ List elements can be of the same type as Object names
+ String values must be double-quoted
JSON Syntax

+ Dictionary (Object)
+ Curly braces enclose name:key comma-separated pairs
{"R1":"2911", "R2":"2951", "count"=2}

+ List
+ Square brackets hold comma-separated elements
["R1", "R2", "R3"]

+ Multi-line notation is common


{
"R1":"2911",
"R2":"2951"
}
JSON Syntax

+ Dictionary & List


+ Data types can be nested & combined
{
"device1": ["IOS", "2911"],
"other_devices": [
{
"platform":"2911",
"code":"IOS"
},
{
"platform":"2951",
"code":"IOS"
}
]
}
Using JSON in Python

import json

with open("file.json") as f:
data = f.read()

jsonDict = json.loads(data)
for k, v in jsonDict.items():
print( "The key {} uses a {} value.".format(str(k), str(type(v))) )
Network Programmability
Foundation
API Toolset

ine.com
Module Overview + Documentation
+ Tools & Libraries
API Documentation

+ Critical for understanding the syntax of API calls & responses

+ ASA REST API


+ Requires an installed & working REST API plugin
+ rest-api image, rest-api agent
+ https://www.cisco.com/c/en/us/td/docs/security/asa/api/qsg-asa-api.html
+ API documentation can be found at https://ASA_IP/doc
REST API Tools

+ Client URL (cURL)


+ CLI-based tool for working with URLs
+ HTTP, FTP, SFTP, TFTP & more
+ Often used to simulate HTTP client requests
+ E.g. curl -u user:pw -k https://asav_IP/api/interfaces/physical

+ Postman
+ GUI-based HTTP frontend
+ Available as a native application or Chrome's extension (deprecated)
+ https://www.getpostman.com/downloads
Python Libraries

+ Python Libraries for Network Programmers


+ Requests
+ "Industry-standard" recommended for HTTP interactions
+ REST API
+ ncclient
+ NETCONF client for Python
+ Netmiko
+ Paramiko-based SSH library for CLI-based interactions
+ Non-programmable API, but still useful (e.g. unsupported features)

+ Make sure to install missing libraries before trying to use them


Network Programmability
Foundation
Python Automation with REST API

ine.com
+ Python Requests
Module Overview
Library
+ Example
Python Requests Library

+ Simplifies generation of REST API calls


+ Install with PIP (pip install requests)
+ Make it accessible via import requests
+ Adding JSON module (import json) is needed to encode & decode JSON
objects (string <-> dictionary)
+ Documented at https://2.python-requests.org/en/master/
Python Requests Library

+ General Syntax
+ requests.method(params)
+ Returns the Response object that may be acted upon
+ E.g. response = requests.get('https://ASA_IP')

+ Useful Parameters
+ Authentication
+ Basic HTTP, OAuth, None, Custom
+ Importing the HTTPBasicAuth function is optional
+ Headers may be added to specify the formatting
+ headers = { 'Accept': 'application/json', 'Content-Type':
'application/json' }
Python Requests Library

+ Response Attributes
+ status_code
+ text

+ JSON Methods
+ dumps()
+ Dictionary -> String
+ loads()
+ String -> Dictionary
EXPERTS AT MAKING YOU AN EXPERT

You might also like