Software TSN Switch
A software switch to connect VMs and Containers to a TSN network
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
common::NetconfSession Class Reference

Manages a NETCONF client session and basic datastore operations for a network device. More...

#include <NetconfSession.h>

Collaboration diagram for common::NetconfSession:
Collaboration graph

Public Member Functions

 NetconfSession ()
 
 ~NetconfSession ()
 
bool connect (const std::string &ip, int port, const std::string &user, const std::string &password)
 Establishes a NETCONF session with the specified device via SSH.
 
void disconnect ()
 Disconnects the NETCONF session.
 
bool isConnected () const
 Checks if the NETCONF session is currently connected.
 
struct lyd_node * getData (const std::string &xpath="")
 Retrieves data from the NETCONF server using the specified XPath filter.
 
bool editData (const std::string &configXml)
 Edits the configuration data on the NETCONF server into the candidate datastore.
 
bool commit ()
 Commits the current configuration from the candidate datastore into the running datastore.
 
bool subscribe (const std::string &xpath, std::function< void(struct lyd_node *)> callback)
 Subscribes to notifications matching the specified XPath filter and registers a callback.
 
void startNotificationListener ()
 Starts a background thread to listen for incoming notifications and dispatch them to registered callbacks.
 
void stopNotificationListener ()
 Stops the background notification listener thread and cleans up resources.
 

Private Attributes

struct nc_session * session_ = nullptr
 
std::map< std::string, std::function< void(struct lyd_node *)> > subscriptions_
 
std::thread listenerThread_
 
std::atomic< bool > listening_ {false}
 

Detailed Description

Manages a NETCONF client session and basic datastore operations for a network device.

This class wraps the underlying NETCONF session lifecycle, including SSH-based connection setup, session teardown, and connectivity checks. It also provides helper methods for reading operational data and applying configuration changes via candidate datastore edit/commit workflows.

Constructor & Destructor Documentation

◆ NetconfSession()

common::NetconfSession::NetconfSession ( )

◆ ~NetconfSession()

common::NetconfSession::~NetconfSession ( )

Member Function Documentation

◆ commit()

bool common::NetconfSession::commit ( )

Commits the current configuration from the candidate datastore into the running datastore.

Returns
true if the commit operation is successful, false otherwise.

◆ connect()

bool common::NetconfSession::connect ( const std::string &  ip,
int  port,
const std::string &  user,
const std::string &  password 
)

Establishes a NETCONF session with the specified device via SSH.

Parameters
ipThe IP address of the device e.g., "192.168.1.1".
portThe Netconf port of the device (standard is 830).
userThe username for authentication.
passwordThe password for authentication.
Returns
true if the connection is successful, false otherwise.

◆ disconnect()

void common::NetconfSession::disconnect ( )

Disconnects the NETCONF session.

◆ editData()

bool common::NetconfSession::editData ( const std::string &  configXml)

Edits the configuration data on the NETCONF server into the candidate datastore.

Parameters
configXmlThe configuration data in XML format.
Returns
true if the edit operation is successful, false otherwise.

◆ getData()

struct lyd_node * common::NetconfSession::getData ( const std::string &  xpath = "")

Retrieves data from the NETCONF server using the specified XPath filter.

Parameters
xpathThe XPath filter to apply (default is empty, which retrieves all data).
Returns
The retrieved data as a lyd_node pointer.

◆ isConnected()

bool common::NetconfSession::isConnected ( ) const

Checks if the NETCONF session is currently connected.

Returns
true if connected, false otherwise.

◆ startNotificationListener()

void common::NetconfSession::startNotificationListener ( )

Starts a background thread to listen for incoming notifications and dispatch them to registered callbacks.

◆ stopNotificationListener()

void common::NetconfSession::stopNotificationListener ( )

Stops the background notification listener thread and cleans up resources.

◆ subscribe()

bool common::NetconfSession::subscribe ( const std::string &  xpath,
std::function< void(struct lyd_node *)>  callback 
)

Subscribes to notifications matching the specified XPath filter and registers a callback.

Parameters
xpathThe XPath filter for notifications to subscribe to.
callbackThe callback function to invoke when a matching notification is received.
Returns
true if the subscription is successful, false otherwise.

Member Data Documentation

◆ listenerThread_

std::thread common::NetconfSession::listenerThread_
private

◆ listening_

std::atomic<bool> common::NetconfSession::listening_ {false}
private

◆ session_

struct nc_session* common::NetconfSession::session_ = nullptr
private

◆ subscriptions_

std::map<std::string, std::function<void(struct lyd_node*)> > common::NetconfSession::subscriptions_
private

The documentation for this class was generated from the following files: