Plugs: https://learninglabs.cisco.com Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad ++, sublime text etc.) Postman REST Client -- http://www.getpostman.com/ Python Go to command prompt and type python Install instructions -- http://learnpythonthehardway.org/book/ex0.html Python Requests library -- http://docs.python-requests.org
More Plugs: learninglabs.cisco.com Check these out
What We Know
Easier Network Element Management REST / JSON is the answer of the day Customers Demand Easier Network Element Management
The Evolution of Device Interaction Bootstrap Configure Extend Plug n Play PoAP Smart Install Auto Install CLI Controllers Openstack Puppet/Chef REST JSON RPC NETCONF CLI I2RS onePK OpenFlow Onbox Python EEM Evolving First – categorize the different APIs/protocols/frameworks into buckets. Some cross buckets, some enable technologies in other buckets, but in general, they are all optimized for particular kinds of tasks. Bootstrap – day one configuration Configure – into the capabilities of the network device Extending what you can do with the device. Dynamic programming, also sometimes used to do configuration Generally speaking optimized for extension Look from bottom up. CLI is tool of choice for bootstrap and configure. Middle bucket is where we want to concentrate on. By far the most things people want is a configurator, and to handle configuration tasks. Solve immediate pain points Traditional
REST Follows a Familiar Model Web Browsing REST API Twitter: IDs of last five followers {"ids":[303776224, 19449911, 607032789, 86544242, 2506725913, 17631389], "next_cursor":0, "next_cursor_str":"0", "previous_cursor":0, "previous_cursor_str":"0"} HTTP GET HTTP GET HTML JSON/XML Here is an example of a rest call to twitter This is a real return value Describes how data should be displayed to please human viewer Describes data in a format applications can understand
REST is An Architectural Style (Not a protocol) REST= REpresentational State Transfer Proposed by Roy Fielding in 2000 Developed by W3C in parallel with HTTP 1.1 Simple CRUD using HTTP Stateless client-server model Uses URIs to identify resources of interest Rest its an architectural style, it is not a protocol it is a suggestion on how to behave works over crud ( Create, Read, Update, Delete)
There Are LOTS of RESTful APIs Thousands of programs use rest apis
Why Does This Matter for Networking? Easy to use Why Does This Matter for Networking? Human Readable Software Friendly Large Developer Base Client Libraries in Many Languages Lot of people that can program rest Easy
REST: It’s Not Just for Web Services Products and applications can consume restful apis Even at cisco Vmware ucs ucs-director, open daylight
REST: Coming Soon to a Device Near You Supported on CSR1kV since XE 3.10 ASR1K in XE 3.14 Primarily for Config DNS, NTP, Interface, Routing, ACL, NAT Some Stats Interface, CPU, Memory Runs in a service container Uses onePK Python APIs under the hood Seeing experimentation on bringing rest apis to you Csr1kv Kind of limited now but being built out
Sample: Grab Hosts from APIC-EM Application Policy Infrastructure Controller (APIC) Enterprise Module (EM) 3rd Party App Request Response 3rd Party App GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec} List of Hosts returned in JSON Cisco APIC-EM Request Response
Anatomy of a REST Request Method GET, POST, PUT, DELETE URL Example: http://{APIC-EMController}/api/v0/host/1/3 Authentication Basic HTTP, OAuth, none Custom Headers HTTP Headers Example: Content-Type: application/json Request Body JSON or XML containing data needed to complete request JSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. http://www.json.org/
And what is in the Response? HTTP Status Codes http://www.w3.org/Protocols/HTTP/HTRESP.html 200 OK 201 Created 500 Internal Error Headers Body JSON XML
REST in Action: How can I try it? HTTP clients can help you quickly test web services Postman - http://www.getpostman.com/ Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/ Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GET SOAPUI Many IDEs have consoles for testing REST Services built in
Cost
REST Demo – Using Postman
REST Demo – Using Postman
REST Demo – Using Postman Get Hosts Method: GET URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn} Get Devices URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn} Get Policies URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn} Get Applications URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}
REST DEMO – Using the POST or PUT Method To send data to a REST service and either create or update data, you will need to use POST or PUT. Create Policy Example Method: POST URL: http://APIC-EMController/api/v0/policy Custom Headers: Content-Type: application/json Request Body: JSON that specifies details of new policy What if the Content-Type header is missing? What if there is a mistake in the JSON Request Body? Handy tool for validating JSON -- http://jsonlint.com/
REST Demo – Using POST or PUT Method Show what happens if you forget the Content-Type Header
First REST call from Python Source code file: apic-em1.py #import requests library import requests #specify URL url = 'http://Your-API-EM-Controller/api/v0/host’ #Call REST API response = requests.get(url) #Print Response print response.text More info on requests library - http://docs.python-requests.org/en/latest/index.html
Python Examples Download Sample Code https://github.com/CiscoDevNet/coding-skills-sample-code Coding 102 apic-em1.py – simple example to get list of hosts apic-em-helloworld.py – “hello world” type example to show list of devices learning-lab-basics.py – Retrieves device list and pretty prints JSON learning-lab-basics-step2.py – Retrieves network device list and parses JSON to display networkDeviceId values learning-lab-basics-step3.py – Retrieves and lists all devices, hosts, policies and configured applications learning-lab-create-policy.py – Shows how to create a new policy using the POST Method
AD-HOC REST (REST like) Interface Leverage existing automation technology Brownfield
Bonus JSON-RPC
Comparison: REST/JSON-RPC Similar: Both Send/Receive JSON over HTTP JSON-RPC (N9K NX-API) REST (CSR 1000v) Both send and receive json over http Sometimes they seem the same Differences are not that important
Comparison: REST/JSON-RPC Different: Resources (URIs) REST: Many Resources https://172.6.1.118/api/v1… …/global/banner …/global/hostname …/global/reload …/interfaces/… …/routing-svc/… …/nat-svc/… …/acl/… … JSON-RPC: Few Resources https://10.10.10.8/ins Differences between them Rest operates on the idea of these different resources acls/interfaces… Resources for each thing you want to do Json has much fewer resources CSR1kV N9K
Different: Methods GET: Retrieve/List PUT: Replace REST: Standard HTTP Methods GET: Retrieve/List PUT: Replace POST: Create New Entry DELETE: Delete JSON-RPC: POST + body method Rest only uses standard http methods Json rpc uses post. And puts information in the body of what you should do
JSON-RPC Details A very simple remote procedure call protocol encoded in JSON, sent over HTTP http://www.jsonrpc.org/specification JSON RPC Request Properties method – (string) name of the method to be invoked. params – (array) objects to be passed as parameters to the defined method. Id – (any type) used to match the response with request JSON RPC Response Properties result - data returned by the invoked method. error - specified Error code if there was an error invoking the method, otherwise null. id - id of the corresponding request.
NXAPI Switch# conf t Switch(config)# feature nxapi CLI Interaction with device over HTTP / HTTPS Input/Output encoded in JSON or XML (key for programmability) [ { "jsonrpc": "2.0", "method": "cli", "params": { "cmd": "show clock", "version": 1 }, "id": 1 } ] Show clock NXAPI Web Server (NGINX) Switch# conf t Switch(config)# feature nxapi Switch(config)# exit HTTP / HTTPS { "jsonrpc": "2.0", "result": { "body": { "simple_time": "15:00:37.762 PST Mon Aug 18 2014\n" } }, "id": 1
NXAPI – Web Sandbox Point browser to IP Address of Network Element Enter CLI Commands See formatted input and output
NXAPI – Python Generation Click on the Python button, and the tool will generate python Interaction code for you.