Tutorial 10: Network Automation, NETCONF, RESTCONF, and APIs

COMP347 Unit 8 – Network Management and Network Operations

Table of Contents

Learning Objectives

After completing this extended tutorial, you should be able to:

Overview

Network automation is the use of software, scripts, and APIs to perform network configuration, monitoring, and management tasks with minimal human intervention. It is a critical enabler for modern, scalable, and agile network operations. This tutorial provides a comprehensive, in‑depth exploration of network automation, focusing on the key protocols and interfaces: NETCONF, RESTCONF, and REST APIs. We begin with the fundamentals: drivers for automation (scale, speed, consistency), the distinction between imperative (scripting) and declarative (intent‑based) approaches, and the importance of idempotency.

We then dive into NETCONF (RFC 6241), a network configuration protocol that uses XML and YANG for structured data, supporting transactional changes, candidate configurations, and rollback. We cover its operations (get, get‑config, edit‑config, copy‑config, delete‑config, lock, unlock, commit, etc.) and illustrate with examples. RESTCONF (RFC 8040) is presented as a RESTful alternative to NETCONF, using HTTP/JSON and YANG, making it easier for web‑friendly applications. We also discuss vendor‑specific REST APIs (e.g., Cisco, Juniper) that offer similar capabilities. The role of YANG models is emphasized throughout, as they provide the data structure for both protocols.

We explore automation tools (Ansible, Terraform, Python scripts), and how to integrate them with CI/CD pipelines. Security considerations (authentication, authorization, encryption) are addressed. Case studies illustrate successful automation implementations, from cloud provider networks to enterprise campus environments. The quiz, exercises, and homework are designed to build both theoretical and practical skills.

Technical and Theoretical Content

1. Introduction to Network Automation

Network automation is the practice of using software to perform networking tasks that were traditionally done manually via CLI. Key drivers:

Automation can be applied to provisioning, configuration, compliance, troubleshooting, and reporting.

2. Automation Principles and Best Practices

3. NETCONF Protocol

NETCONF (Network Configuration Protocol) is defined in RFC 6241. It uses an SSH‑based transport (or sometimes TLS) and XML encoding. It provides:

Example of an edit‑config RPC:

<rpc message-id="101">
  <edit-config>
    <target>
      <running/>
    </target>
    <config>
      <interfaces xmlns="urn:example:interfaces">
        <interface>
          <name>eth0</name>
          <enabled>true</enabled>
        </interface>
      </interfaces>
    </config>
  </edit-config>
</rpc>
        

NETCONF also supports notifications (event‑driven messages) and is widely implemented in modern devices.

4. RESTCONF Protocol

RESTCONF (RFC 8040) is a RESTful protocol that uses HTTP methods (GET, POST, PUT, PATCH, DELETE) to interact with YANG‑defined data. It uses JSON or XML encoding, typically over HTTPS. It provides:

Example: To get the system hostname:

GET /restconf/data/system/hostname
        

Response (JSON):

{
  "system:hostname": "router1"
}
        

5. YANG and Data Models in Automation

YANG (RFC 7950) is the data modeling language used by both NETCONF and RESTCONF. It defines the structure of configuration and state data. In automation, YANG models:

Automation tools like Ansible can use YANG models (via `yang` modules) to validate configurations before applying.

6. REST APIs for Network Devices

Many vendors provide proprietary REST APIs (e.g., Cisco DNA Center, Juniper Mist, Arista CloudVision). These APIs often use JSON over HTTPS and offer a higher‑level abstraction than NETCONF/RESTCONF, often catering to specific use cases (e.g., intent‑based networking). They are important for automation because:

When selecting an API, consider: coverage, stability, rate limits, and authentication mechanisms (OAuth, API keys).

7. Automation Tools and Frameworks

Choice depends on team skills, device types, and integration needs.

8. DevOps and CI/CD for Networking

Applying DevOps principles to networking involves:

This reduces lead time for changes and improves reliability.

9. Securing Automation APIs

10. Case Studies

Quiz (46 Questions)

All answers are hidden; click Show Answer to reveal.

Question 1:

Define network automation and list its main drivers.

Show Answer
Network automation uses software to manage network devices. Drivers: scale, speed, consistency, agility, cost reduction.
Question 2:

Explain the principle of idempotency in automation.

Show Answer
A change is idempotent if applying it multiple times yields the same final state as applying it once.
Question 3:

What is the difference between declarative and imperative automation?

Show Answer
Declarative specifies the desired state; imperative specifies the steps to get there.
Question 4:

Which protocol is defined in RFC 6241?

Show Answer
NETCONF.
Question 5:

What transport protocol does NETCONF typically use?

Show Answer
SSH (TCP port 830).
Question 6:

Name three NETCONF operations.

Show Answer
get, edit-config, commit, lock, unlock, copy-config, etc.
Question 7:

What is a candidate configuration in NETCONF?

Show Answer
A working copy that can be edited and validated before being committed to the running configuration.
Question 8:

What is the primary advantage of NETCONF over CLI scripting?

Show Answer
Structured data, transactional changes, candidate config, and standardized error handling.
Question 9:

Which protocol is defined in RFC 8040?

Show Answer
RESTCONF.
Question 10:

What transport protocol does RESTCONF use?

Show Answer
HTTP/HTTPS.
Question 11:

Name the HTTP methods used by RESTCONF and their purposes.

Show Answer
GET (retrieve), POST (create), PUT (replace), PATCH (partial update), DELETE.
Question 12:

What data encoding formats does RESTCONF support?

Show Answer
XML and JSON.
Question 13:

How does RESTCONF handle the concept of a candidate configuration?

Show Answer
RESTCONF does not have a built‑in candidate; changes are applied directly. However, some implementations provide a candidate datastore via specific URLs or query parameters.
Question 14:

What is YANG and why is it important for automation?

Show Answer
YANG is a data modeling language for configuration and state data. It provides schema for validation and enables programmatic access.
Question 15:

What is the role of OpenConfig in network automation?

Show Answer
OpenConfig provides vendor‑neutral YANG models, enabling multi‑vendor interoperability and simplified automation.
Question 16:

List three automation tools mentioned in the tutorial.

Show Answer
Ansible, Terraform, Python with ncclient/requests, Jenkins.
Question 17:

What is the benefit of storing automation code in version control (Git)?

Show Answer
Audit trail, collaboration, rollback, and integration with CI/CD.
Question 18:

Explain the concept of Infrastructure as Code (IaC) in networking.

Show Answer
Managing network configurations and resources using code (declarative definitions) stored in version control, enabling automation and reproducibility.
Question 19:

What is a CI/CD pipeline in the context of network automation?

Show Answer
A set of automated steps that validate, test, and deploy network changes when code is committed, enabling rapid and reliable delivery.
Question 20:

How can you secure an automation API?

Show Answer
Use TLS, strong authentication (OAuth, certificates), RBAC, audit logging, and secure credential storage.
Question 21:

What is the difference between NETCONF and RESTCONF in terms of state management?

Show Answer
NETCONF supports candidate and running datastores; RESTCONF typically uses direct datastore access, though some implementations provide candidate capabilities.
Question 22:

What is the purpose of the `validate` operation in NETCONF?

Show Answer
To verify that a candidate configuration is syntactically and semantically correct before committing.
Question 23:

How does RESTCONF handle YANG lists and containers in URLs?

Show Answer
Using path segments: e.g., `/restconf/data/interfaces/interface=eth0` for a list entry.
Question 24:

What is the role of a YANG module in NETCONF/RESTCONF?

Show Answer
It defines the structure, types, and constraints of the data, which both client and server use to communicate.
Question 25:

What is the purpose of the `lock` operation in NETCONF?

Show Answer
To prevent other sessions from modifying the configuration during a critical operation.
Question 26:

Can RESTCONF use XML as well as JSON?

Show Answer
Yes, both are supported.
Question 27:

What is the default TCP port for NETCONF over SSH?

Show Answer
830.
Question 28:

What is the purpose of an Ansible playbook?

Show Answer
A YAML file that defines a set of tasks to be executed on managed hosts (including network devices).
Question 29:

How can you test automation changes without affecting production?

Show Answer
Use a staging environment, dry‑run mode, or lab devices to validate changes.
Question 30:

What is the advantage of using Python with `ncclient` over Ansible for NETCONF?

Show Answer
Provides more flexibility for complex logic, custom error handling, and integration with other Python libraries.
Question 31:

Explain the concept of "state" vs. "configuration" in YANG models.

Show Answer
Configuration nodes are writable (desired state). State nodes are read‑only (operational data).
Question 32:

How does RESTCONF represent a YANG leaf‑list?

Show Answer
As a JSON array of values.
Question 33:

What is the purpose of a "commit" operation in NETCONF?

Show Answer
To apply the candidate configuration to the running configuration atomically.
Question 34:

What is the role of a "notification" in NETCONF?

Show Answer
To asynchronously send event‑driven messages (e.g., configuration changes, alarms) to the manager.
Question 35:

How can you authenticate to a RESTCONF server?

Show Answer
Using HTTP Basic Auth, client certificates, or token‑based (e.g., OAuth2) over HTTPS.
Question 36:

What is the main advantage of using declarative tools like Terraform over script‑based automation?

Show Answer
Declarative tools focus on the desired state; they automatically handle dependencies and reduce drift.
Question 37:

What is a "data store" in the context of NETCONF?

Show Answer
A repository of configuration data (e.g., running, candidate, startup).
Question 38:

How does RESTCONF support the retrieval of operational state data?

Show Answer
By accessing state containers (often marked as `config false` in YANG) via the same REST API, e.g., `/restconf/data/interfaces/state`.
Question 39:

What are the benefits of using a network automation platform like Cisco NSO?

Show Answer
Provides service orchestration, multi‑vendor support, transactional commit, and rollback capabilities.
Question 40:

Explain the term "drift" in the context of automation.

Show Answer
Deviation of the actual configuration from the desired state defined in code, often due to manual changes or external modifications.
Question 41:

What is the purpose of a "dry‑run" in automation?

Show Answer
To simulate the changes without applying them, allowing validation and review.
Question 42:

How can you use Git branches in network automation?

Show Answer
To separate development, testing, and production environments, enabling staged rollouts.
Question 43:

What is the role of a "webhook" in automation?

Show Answer
A callback triggered by an event (e.g., Git push) to initiate automation workflows.
Question 44:

Why is monitoring important after an automated change?

Show Answer
To verify that the change had the intended effect and to detect any negative impact on performance or availability.
Question 45:

What is the difference between `edit-config` and `copy-config` in NETCONF?

Show Answer
`edit-config` applies incremental changes; `copy-config` replaces the entire target datastore with the source.
Question 46:

How can you ensure that automation scripts are idempotent when using NETCONF?

Show Answer
By using `edit-config` with the `operation` attribute (`create`, `replace`, `merge`, `delete`) to make changes only if needed, and avoiding absolute replacements.

Exercises (24 Applied Problems)

Sample solutions are hidden – click to reveal.

Exercise 1:

Write a NETCONF RPC (XML) to retrieve the running configuration of a device.

Show Sample Solution
<rpc message-id="101">
  <get-config>
    <source>
      <running/>
    </source>
  </get-config>
</rpc>
            
Exercise 2:

Write a RESTCONF GET request to retrieve the interface list (using JSON). Specify the URL and expected response.

Show Sample Solution

URL: GET /restconf/data/interfaces
Response: JSON object containing a list of interfaces with their attributes.

Exercise 3:

Explain the steps to perform a configuration change using NETCONF with a candidate datastore.

Show Sample Solution
1. Lock the candidate. 2. Edit the candidate (edit-config with target candidate). 3. Validate (validate operation). 4. Commit. 5. Unlock.
Exercise 4:

Write a Python snippet using `ncclient` to connect to a NETCONF device and retrieve the hostname.

Show Sample Solution
from ncclient import manager
with manager.connect(host='10.0.0.1', port=830, username='user', password='pass', hostkey_verify=False) as m:
    result = m.get_config('running', filter=('subtree', ''))
    print(result)
            
Exercise 5:

Explain the difference between using `edit-config` with `operation="replace"` vs. `operation="merge"`.

Show Sample Solution
`replace` replaces the entire target subtree with the provided config; `merge` updates only the nodes that are provided, leaving others unchanged.
Exercise 6:

Write an Ansible playbook to enable an interface on a Cisco router using the `ios_config` module.

Show Sample Solution
- name: Enable interface
  hosts: routers
  tasks:
    - name: Set interface up
      ios_config:
        lines:
          - no shutdown
        parents: interface GigabitEthernet0/1
            
Exercise 7:

How can you use Terraform to manage a network device via RESTCONF? (Conceptual)

Show Sample Solution
Use a Terraform provider that supports RESTCONF (e.g., the `restconf` provider) and define resources that map to YANG data models.
Exercise 8:

Explain the security measures you would implement for a RESTCONF API exposed on the internet.

Show Sample Solution
Use TLS with strong ciphers, enforce strong authentication (client certificates or OAuth), implement rate limiting, use RBAC, and log all access.
Exercise 9:

What is the purpose of a `lock` in NETCONF and when would you use it?

Show Sample Solution
To prevent concurrent modifications. Use during complex multi‑step changes to ensure consistency.
Exercise 10:

Write a NETCONF RPC to delete a specific interface configuration.

Show Sample Solution
<rpc>
  <edit-config>
    <target><candidate/></target>
    <config>
      <interfaces xmlns="urn:example:interfaces">
        <interface operation="delete">
          <name>eth0</name>
        </interface>
      </interfaces>
    </config>
  </edit-config>
</rpc>
            
Exercise 11:

How can you test a NETCONF RPC without affecting the device?

Show Sample Solution
Use the `validate` operation on a candidate configuration, or use a simulation tool (e.g., netconf‑sim) in a lab.
Exercise 12:

Describe the steps to integrate network automation with a CI/CD pipeline using GitLab CI.

Show Sample Solution
1. Store automation code in GitLab. 2. Create `.gitlab-ci.yml` with jobs: lint, validate, dry‑run, deploy (staging), deploy (prod). 3. Use environment variables for credentials. 4. Schedule or trigger on merge.
Exercise 13:

Explain the concept of "configuration drift" and how automation can prevent it.

Show Sample Solution
Drift is deviation from desired state. Automation can periodically reapply the intended configuration (re‑convergence) and alert on manual changes.
Exercise 14:

Write a Python script using `requests` to GET the system hostname from a RESTCONF server.

Show Sample Solution
import requests
url = "https://device/restconf/data/system/hostname"
headers = {"Accept": "application/yang-data+json"}
auth = ("user", "pass")
response = requests.get(url, headers=headers, auth=auth, verify=False)
print(response.json())
            
Exercise 15:

What is the role of YANG in NETCONF? How would you define a new data model?

Show Sample Solution
YANG defines the structure of data. Define a module with containers, lists, leaves, and compile it using tools like `pyang`.
Exercise 16:

Explain the difference between HTTP PATCH and PUT in RESTCONF.

Show Sample Solution
PUT replaces the entire resource; PATCH performs a partial update (only the fields provided).
Exercise 17:

How would you handle authentication in an automated script that uses both CLI and API methods?

Show Sample Solution
Store credentials in a vault (e.g., HashiCorp Vault) and retrieve them dynamically. Use environment variables or secrets management in CI/CD.
Exercise 18:

Design a high‑level automation architecture for a global enterprise with 1000 devices. Include components for configuration deployment, compliance, and monitoring.

Show Sample Solution
Git repository for code; CI/CD pipeline (Jenkins); Ansible for deployment; Prometheus for metrics; Grafana for dashboards; and a compliance scanning tool (e.g., Chef InSpec).
Exercise 19:

What is the purpose of the `capabilities` exchange in NETCONF?

Show Sample Solution
To negotiate the set of features and YANG models supported by both the client and server during session establishment.
Exercise 20:

Write an Ansible task to retrieve and print the running config of a device using the `ios_command` module.

Show Sample Solution
- name: Show running config
  ios_command:
    commands: show running-config
  register: result
- debug: var=result.stdout_lines
            
Exercise 21:

Explain how you can use YANG models in Ansible to validate configs before deployment.

Show Sample Solution
Use the `yang` module (or custom filters) to validate a configuration snippet against the YANG schema before pushing.
Exercise 22:

What are the advantages of using RESTCONF over NETCONF for cloud‑native applications?

Show Sample Solution
RESTCONF uses standard HTTP/JSON, which is more natural for cloud developers, and is easier to integrate with web frameworks.
Exercise 23:

Write a NETCONF RPC to perform a `commit` operation.

Show Sample Solution
<rpc message-id="102">
  <commit/>
</rpc>
            
Exercise 24:

How can you implement a rollback in NETCONF if a commit fails?

Show Sample Solution
Before committing, have a backup copy of the running config (via `copy-config`). If commit fails, use `copy-config` to restore the backup.

Homework (18 In‑Depth Assignments)

Sample answers are hidden; use them to guide your study.

Homework 1:

Write a detailed comparison of NETCONF and RESTCONF, including their architectures, transport, data encoding, operations, and use cases. Provide examples of when to use each.

Show Sample Answer

NETCONF is session‑based, uses SSH, XML, and supports candidate/commit. RESTCONF is HTTP‑based, uses JSON/XML, and is simpler for web applications. Use NETCONF for complex, transactional changes; RESTCONF for lightweight automation.

Homework 2:

Design an automation solution for provisioning a new branch office network (router, switch, firewall) using Ansible and RESTCONF. Include a network topology, variable definitions, and playbook structure.

Show Sample Solution

Topology: Edge router, core switch, firewall. Use group vars for site‑specific data. Playbook: tasks to configure each device using appropriate modules (ios_config, etc.). Use YANG models where possible.

Homework 3:

Research and explain the YANG data models used by OpenConfig for BGP. Show how you would use NETCONF to retrieve BGP neighbors.

Show Sample Solution

OpenConfig BGP model: `/bgp/neighbors/neighbor`. Use `get` with filter to retrieve the subtree.

Homework 4:

Implement a Python script using `ncclient` to modify the SNMP community string on a device using a candidate configuration, validate, and commit.

Show Sample Solution

Script outline: connect, lock candidate, edit‑config (replace community), validate, commit, unlock. Handle exceptions and rollback.

Homework 5:

Explain the role of `git` in network automation and describe a typical workflow for implementing a change (from code to production).

Show Sample Solution

Workflow: developer creates branch, edits code, opens pull request, peer review, merge to main, CI/CD pipeline triggers automated testing and deployment to staging, then to production after approval.

Homework 6:

Analyze the security implications of exposing a RESTCONF API to a management network. What measures would you implement to secure it?

Show Sample Solution

Use TLS, client certificates, RBAC, IP whitelisting, audit logging, and rate limiting. Also, use strong authentication tokens and rotate them.

Homework 7:

Write a detailed guide on how to use Terraform to manage a network device (e.g., a router) with RESTCONF, including provider configuration and resource definitions.

Show Sample Solution

Use the `restconf` provider. Define resources with paths and payloads matching YANG. Example: `resource "restconf_data" "hostname" { path = "/system/hostname" content = "router1" }`

Homework 8:

Describe the process of implementing a CI/CD pipeline for network configurations using GitLab CI, including stages for linting, validation, dry‑run, and deployment.

Show Sample Solution

.gitlab-ci.yml defines jobs: lint (using yang‑tools), validate (syntax), dry‑run (against lab), deploy‑staging (apply to staging), deploy‑prod (manual job with approval).

Homework 9:

Explain how you can use `pyang` to validate a YANG module before using it in automation.

Show Sample Solution
`pyang` is a tool to validate YANG syntax, check for errors, and generate documentation. It can be integrated into CI pipelines.
Homework 10:

Compare and contrast Ansible and Terraform for network automation. In what scenarios would you choose one over the other?

Show Sample Solution
Ansible is procedural, uses push model, and is better for configuration management on existing devices. Terraform is declarative, uses providers, and is better for provisioning infrastructure (cloud and network).
Homework 11:

Design a monitoring feedback loop that automatically triggers a rollback if a configuration change causes a drop in service health.

Show Sample Solution
Use telemetry to monitor key SLIs. If SLI falls below threshold within a time window after change, invoke a rollback script (e.g., restore backup via NETCONF).
Homework 12:

Write a research paper on the evolution from CLI to NETCONF/RESTCONF and the impact on network engineering roles.

Show Sample Solution
Paper should discuss historical context, drivers for automation, skill shift from CLI to programming, and the rise of network automation engineers.
Homework 13:

Explain the concept of "service abstraction" in network automation and how it can be implemented using YANG models and APIs.

Show Sample Solution
Service abstraction hides device‑level details; users define high‑level services (e.g., VPN). YANG models for services map to device configs, and APIs orchestrate the deployment.
Homework 14:

Design a configuration compliance check using RESTCONF to verify that all interfaces have a description set.

Show Sample Solution
Query RESTCONF for all interfaces; parse JSON; check if `description` leaf is present; report missing ones.
Homework 15:

Discuss the challenges of automating network devices from multiple vendors and how YANG and OpenConfig can address them.

Show Sample Solution
Challenges: different CLIs, MIBs, APIs. OpenConfig provides common YANG models; vendors implement them, enabling a single automation framework.
Homework 16:

Write a case study on a company that successfully implemented network automation, including the tools used, the challenges faced, and the outcomes.

Show Sample Solution
Example: a financial firm adopted Ansible and Git to manage 500 devices; reduced change time by 80%, reduced outage due to config errors.
Homework 17:

Explain the role of NETCONF notifications in an automation system. Provide an example of a notification that could trigger an automated response.

Show Sample Solution
Notifications are event‑driven. Example: interface down notification triggers automated rerouting and ticket creation.
Homework 18:

Analyze the impact of network automation on network security: both the benefits (e.g., consistent policies, faster patching) and the risks (e.g., misconfigured automation, API exposure).

Show Sample Solution
Benefits: reduced human error, faster security updates, consistent policy enforcement. Risks: automation code vulnerabilities, API exposure, credential leaks. Mitigations: secure development, regular audits, and robust access controls.

Summary

This extended tutorial has provided a comprehensive exploration of network automation, focusing on NETCONF, RESTCONF, and REST APIs. We covered the drivers and principles of automation (idempotency, declarative vs. imperative), and dove deep into the NETCONF and RESTCONF protocols, their operations, and their use of YANG data models. We also examined vendor‑specific REST APIs, automation tools (Ansible, Terraform, Python), and the integration of automation into CI/CD pipelines. Security considerations and best practices were discussed, along with case studies illustrating real‑world implementations.

Network automation is a critical skill for modern network professionals, enabling scale, speed, and consistency. The quiz, exercises, and homework assignments are designed to build both theoretical knowledge and practical experience. In the next tutorial, we will explore SDN Management and Programmable Networks, extending automation concepts to software‑defined architectures.

COMP347 Unit 8 – Extended Tutorial 10 • TrustOpen University • Last updated: August 2026