Devnet
YANG
Restconf
# RESTCONF Overview
 
RESTCONF is a protocol widely supported across various Cisco platforms, operating as an HTTP-based protocol. Similar to NETCONF, RESTCONF leverages YANG data models to define its programmatic interface structure. It supports standard HTTP request methods such as GET, PUT, PATCH, POST, and DELETE.
 
Both NETCONF and RESTCONF utilize the same YANG data models to represent network device configurations. This ensures consistency in data retrieval between the two protocols. RESTCONF offers flexibility by supporting both JSON and XML encodings.
 
While NETCONF is connection-oriented, allowing multiple actions within a single conversation, RESTCONF follows the principles of REST APIs, enabling only one action at a time.
 
## Understand a RESTCONF API
 
Key characteristics of RESTCONF:
 
- Subset of NETCONF functionality
- Exposes YANG models via a REST API (URL)
- Utilizes HTTP(S) as transport
- Supports XML or JSON encoding
- Developed to leverage HTTP tools and libraries
- Utilizes common HTTP verbs in REST APIs
 
Once the fundamentals of REST and NETCONF are grasped individually, understanding RESTCONF becomes more intuitive. RESTCONF operations are directly derived from YANG models, simplifying the process of accessing resources and performing actions.
 
## RESTCONF Protocol Operations
 
| Operation | Description                                      |
|-----------|--------------------------------------------------|
| GET       | Retrieves data from a resource (config/operational)|
| POST      | Creates a configuration data resource            |
| PUT       | Creates or replaces a configuration data resource|
| PATCH     | Merges configuration data with target resource   |
| DELETE    | Deletes a configuration data resource            |
 
RESTCONF operations align with REST principles, employing HTTP verbs effectively. Notably, the PUT operation can replace entire sections of configuration, akin to declarative network configuration.
 
## Comparison: RESTCONF vs. NETCONF
 
| RESTCONF Operation | NETCONF Operation                    |
|--------------------|--------------------------------------|
| GET                | `<get-config>`, `<get>`              |
| POST               | `<edit-config>` (operation="create") |
| PUT                | `<edit-config>` (operation="create/replace") |
| PATCH              | `<edit-config>` (operation="merge")   |
| DELETE             | `<edit-config>` (operation="delete")  |
 
RESTCONF operations map directly to their corresponding counterparts in NETCONF. While RESTCONF provides a functional subset of NETCONF capabilities, NETCONF offers more granular operations for configuration changes.
 
## Use RESTCONF in Python
 
Configuration and state data in RESTCONF can be retrieved using the GET method. Python's `requests` module facilitates RESTCONF API calls, enabling data retrieval and manipulation. Below is an example demonstrating the retrieval of interface configurations from a Cisco device:
 
```python
import requests
import json
 
# Define headers and authentication
HEADERS = {"Accept": "application/yang-data+json"}
AUTH = ("cisco", "cisco")
 
# Make a GET call to retrieve interface configurations
response = requests.get('https://csr1kv1/restconf/data/Cisco-IOS-XE-native:native/interface', auth=AUTH, headers=HEADERS, verify=False)
print(response.text)

The example showcases how to interact with a RESTCONF API using Python, retrieving configuration data in JSON format. Similarly, XML data can be retrieved by adjusting the Accept header accordingly.

Navigate a RESTCONF API Response

RESTCONF responses are organized hierarchically, allowing for targeted data retrieval. By leveraging the hierarchical structure, specific data can be accessed through API requests. For instance, to retrieve the interface description for GigabitEthernet4, the following URL can be used:

https://csr1kv1/restconf/data/Cisco-IOS-XE-native:native/interface=GigabitEthernet/4/description

The response from this request would provide the desired interface description.

Use RESTCONF PUTs for Replace Operations

RESTCONF's PUT method enables declarative configuration changes, allowing for efficient management of network device configurations. By specifying the target configuration state, only relevant changes are applied. However, it's important to note that with the PUT operation, existing configurations might be overwritten if not included in the declaration.

Here's an example of using the PUT method to configure OSPF networks:

PUT https://csr1kv1/restconf/data/Cisco-IOS-XE-native:native/router/ospf/10
Body:
{
  "Cisco-IOS-XE-ospf:ospf": [
    {
      "id": 10,
      "network": [
        {
          "ip": "10.0.10.1",
          "mask": "0.0.0.0",
          "area": 0
        },
        {
          "ip": "10.0.20.1",
          "mask": "0.0.0.0",
          "area": 0
        }
      ]
    }
  ]
}

This PUT request replaces the existing OSPF configuration with the specified networks.

Conclusion

RESTCONF offers a modern approach to network configuration and management, leveraging HTTP-based communication and YANG data models. With its support for JSON and XML encodings, RESTCONF provides flexibility in data exchange and manipulation. Python libraries such as requests facilitate RESTCONF API interactions, enabling seamless integration into automation workflows.

By understanding the principles of REST and the specifics of RESTCONF operations, network administrators can efficiently retrieve, modify, and manage device configurations, ensuring consistency and reliability in network operations.



This conclusion summarizes the key points discussed regarding RESTCONF, emphasizing its modern approach to network configuration and the importance of understanding REST principles for effective utilization.