Professional Documents
Culture Documents
Network Programmability Foundation PDF
Network Programmability Foundation PDF
Foundation
Introduction to Network Programmability
ine.com
+ Network
Programmability
Module Overview
overview
+ Network Automation
+ SDN
Network Programmability Overview
+ Automation Benefits
+ Improved configuration/deployment times
+ Deterministic outcomes
+ Data collection - reporting & troubleshooting
+ Cost reduction
Software Defined Networking (SDN)
+ Controller Examples
+ Open DayLight
+ Cisco Application Policy Infrastructure Controller (APIC)
+ APIC-EM (APIC Enterprise Module)
SDN Architecture
+ Controller Interfaces
+ Northbound (NBI)
+ Applications <-> Controller
+ Southbound (SBI)
+ Controller <-> Network
SDN Architecture
+ 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
+ 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
+ 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
---
- 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
+ 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
+ Common Applications
+ Software development
+ Configuration management
+ Documentation maintenance
Git Overview
+ Add files
+ Copy files to the Git folder
+ The tracking process starts after using git add
+ Confirm with git status
Working with Git CLI
ine.com
Module Overview
+ Git & multiple systems
+ Using GitHub
Git & Multiple Systems
+ 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
+ Synchronize files
+ Use git pull to download changes
+ Local changes can be uploaded via git push
Using GitHub
ine.com
+ Python overview
Module Overview + Writing Python code
+ Running a Python file
+ Documentation
Python Overview
+ 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
ine.com
+ Variables
+ Basic functions
Module Overview
+ Data Types
+ Comments
+ Indentation
Variables
+ 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
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
+ 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 Operations
+ and
+ or
+ not
Network Programmability
Foundation
Data Types - String
ine.com
Module Overview + Python Strings
+ Useful methods
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
+ 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
+ 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
+ get()
+ Same as dict[key], but does not raise an error if key does not exists
+ 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
+ Syntax
if condition:
statement
+ Important: a colon ':' follows condition & indentation precedes statement(s)
Conditional Statements
if condition:
statement(s)
elif altcondition1:
statement(s)
elif altcondition2:
statement(s)
…
else:
statement(s)
Conditional Statements
+ 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
+ Loop Types
+ while
+ for
The while Loop
+ Syntax
while condition:
statement(s)
+ Syntax
for variable(s) in iterable:
statement(s)
ine.com
+ Functions overview
+ Parameters &
Module Overview
Arguments
+ Function output
+ The pass statement
Functions Overview
+ Basic Syntax
def fname():
'''docstring''' # Optional
code
+ Full Syntax
def fname(p_1, p_n='default', *params):
'''docstring''' # Optional
code # Code can refer to the parameters
Function Output
ine.com
+ Accessing a File
Module Overview
+ File Operations
+ Closing a File
Accessing a File
+ 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
ine.com
Module Overview + Classes Overview
+ Attribute Types
Classes Overview
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
ine.com
+ API overview
Module Overview
+ REST API
+ NETCONF
API Overview
+ 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)
+ 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
+ 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
+ Adding comments
+ XML comments start with "<!--" ad end with "-->"
<!-- this is a comment -->
Namespaces
+ 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
+ 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
+ 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"]
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
+ Postman
+ GUI-based HTTP frontend
+ Available as a native application or Chrome's extension (deprecated)
+ https://www.getpostman.com/downloads
Python Libraries
ine.com
+ Python Requests
Module Overview
Library
+ Example
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