Factory classes: Session, Device, and User

Session Factories

There are three classes for creating Session objects:

  • NetconfSessionFactory

  • RestconfSessionFactory

  • DeviceSessionFactory

They are defined in api-session.hpp and use the base class SessionFactory.

NetconfSessionFactory

class NetconfSessionFactory:
    virtual public SessionFactory,
    public _CommonAddin<NetconfSessionFactory>,
    public _KeyfilesAddin<NetconfSessionFactory>,
    public _SslFilesAddin<NetconfSessionFactory>
{
    public:
    // 'username' is the username for the account. 'target' is the ASCII IP
    // address or DNS hostname of the target machine. 'port' is the port number
    // to use, if not default. Will throw std::invalid_argument if 'username' or
    // 'target' are NULL.
    NetconfSessionFactory(const xstring &username, const xstring &target,
        uint16_t port = 0);

    // This variant uses a User object, which may specify several options.
    NetconfSessionFactory(const User &user, const xstring &target, uint16_t port
        = 0);

    NetconfSessionFactory& tcpTransport();
    NetconfSessionFactory& tcpNcxTransport();
    NetconfSessionFactory& sshTransport();
    NetconfSessionFactory& tlsTransport();

    private:
    NetconfSessionFactory(pvt_t&, const xstring &target, uint16_t port);
};

RestconfSessionFactory

class RestconfSessionFactory:
    virtual public SessionFactory,
    public _CommonAddin<RestconfSessionFactory>,
    public _EncodingAddin<RestconfSessionFactory>,
    public _KeyfilesAddin<RestconfSessionFactory>,
    public _SslFilesAddin<RestconfSessionFactory>
{
    public:
    // 'username' is the username for the account. 'target' is the ASCII IP
    // address or DNS hostname of the target machine. 'port' is the port number
    // to use, if not default. Will throw std::invalid_argument if 'username' or
    // 'target' are NULL.
    RestconfSessionFactory(const xstring &username, const xstring &target,
        uint16_t port = 0);

    RestconfSessionFactory(const User &user, const xstring &target, uint16_t
        port = 0);

    RestconfSessionFactory& tcpTransport();
    RestconfSessionFactory& tlsTransport();

    private:
    RestconfSessionFactory(pvt_t&, const xstring &target, uint16_t port);
};

DeviceSessionFactory

// Use this when you have a Device that you want to use for configuration. It
// will automatically determine the device type and the default transport to
// use. You can alter the transport, but attempting to select one that doesn't
// match the Device type will fail with an exception.
class DeviceSessionFactory:
    virtual public SessionFactory,
    public _CommonAddin<DeviceSessionFactory>,
    public _EncodingAddin<DeviceSessionFactory>,
    public _KeyfilesAddin<DeviceSessionFactory>,
    public _SslFilesAddin<DeviceSessionFactory>
{
    public: //
    DeviceSessionFactory(const xstring &username, const Device &device);
    DeviceSessionFactory(const User &user, const Device &device,
                         bool use_defserver = false);

    // Only usable with a Netconf or Restconf device.
    DeviceSessionFactory& tcpTransport();

    // Only usable with a Netconf device.
    DeviceSessionFactory& tcpNcxTransport();

    // Only usable with a Netconf device.
    DeviceSessionFactory& sshTransport();

    // Only usable with a Restconf device.
    DeviceSessionFactory& tlsTransport();

    private:
    DeviceSessionFactory(pvt_t&, const Device &device);

    Device::Type::type_t mType;
};

Device Factories

There are two classes for creating Device objects:

  • NetconfDeviceFactory

  • RestconfDeviceFactory

They are defined in api-device.hpp and use the base class DeviceFactory.

NetconfDeviceFactory

class NetconfDeviceFactory:
    public DeviceFactory
{
    public: //
    NetconfDeviceFactory(const xstring &name,
                         const xstring &address,
                         uint16_t port = 0,
                         const xstring &transport = "ssh");

};

RestconfDeviceFactory

class RestconfDeviceFactory:
    public DeviceFactory
{
    public: //
    RestconfDeviceFactory(const xstring &name,
                          const xstring &address,
                          uint16_t port = 0);
};

User Factory

Listed below is the User Factory which is defined in api-users.hpp,

// Creating a user could require a number of parameters, many of which have
// default values. The UserFactory class provides an easy-to-use way to specify
// only the parameters that you need to.
//
// All parameters, except the 'id' and 'username' parameters to the constructor,
// are optional.
class UserFactory {
    public: //
    // Creates a UserFactory object. 'id' is the name this User will be known
    // by; 'username' is the username for logging into a server.
    UserFactory(const xstring &id, const xstring &username);

    // The password. Omit this (or use nullptr for it) if no password will be
    // used, or if you'll specify the password separately.
    UserFactory& password(const xstring &password);

    // The filespecs for the public key/private key files.
    UserFactory& publicPrivateKeyFiles(const filesystem::path &publicFilespec,
        const filesystem::path &privateFilespec);

    // The filespecs for the SSL client certificate and its keyfile.
    UserFactory& sslClientCertificate(const filesystem::path &filespec);
    UserFactory& sslClientCertificateKeyFile(const filesystem::path &filespec);

    // The filespec for the SSL trust store. Required (?) if using SSL.
    UserFactory& sslTrustStore(const filesystem::path &filespec);

    // Allow fallback to tcp if ssl failed. Defaults to true.
    UserFactory& fallbackOkay(bool setTo);

    // After filling in the required parameters, you can either call this
    // function or pass the class to the User constructor.
    User create() const;

    // Writes a text-format version of the object's settings, for debugging
    // purposes.
    std::string str() const;

    ////////////////////////////////////////////////////////////////////////////
    private: //
    xstring mName;
    xstring mUsername;

    xstring mPassword;

    filesystem::path mPublicKey, mPrivateKey;

    bool mSslFallbackOk;
    filesystem::path mSslCert, mSslKey, mSslTrustStore;

    friend class User;
};