Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netconf service #341

Merged
3 commits merged into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions sdk/cpp/core/docsgen/api/services/netconf_service.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
NetconfService
==============

.. cpp:namespace:: ydk

.. cpp:enum-class:: Datastore

:param candidate: Enum representing candidate datastore
:param running: Enum representing running datastore
:param startup: Enum representing startup datastore
:param url: Enum representing url datastore
:param config: Enum representing config datastore

.. cpp:class:: NetconfService : public Service

Netconf Service class for supporting encoding and decoding C++ model API objects of type :cpp:class:`Entity<Entity>`

.. cpp:function:: NetconfService()

Constructs an instance of NetconfService

.. cpp:function:: bool cancel_commit(NetconfServiceProvider & provider, std::string persist_id = "")

Cancels an ongoing confirmed commit. If the `persist_id` parameter is not given, the operation MUST be issued on the same session that issued the confirmed commit.

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param persist_id: Cancels a persistent confirmed commit.
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool close_session(NetconfServiceProvider & provider)

Request graceful termination of a NETCONF session

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool commit(NetconfServiceProvider & provider, std::string confirmed = "", std::string confirm_timeout = "", std::string persist = "", std::string persist_id = "")

Instructs the device to implement the configuration data contained in the candidate configuration

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param confirmed: An optional argument
:param confirm_timeout: An optional argument
:param persist: An optional argument
:param persist_id: An optional argument
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool copy_config(NetconfServiceProvider & provider, DataStore target, DataStore source)

Create or replace an entire configuration datastore with the contents of another complete configuration datastore. If the target datastore exists, it is overwritten. Otherwise, a new one is created, if allowed.

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration being used as the destination of type :cpp:enum-class:`Datastore`
:param source: The configuration being used as the source of type :cpp:enum-class:`Datastore`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool copy_config(NetconfServiceProvider & provider, DataStore target, Entity& source)

Create or replace an entire configuration datastore with the contents of another complete configuration datastore. If the target datastore exists, it is overwritten. Otherwise, a new one is created, if allowed.

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration being used as the destination of type :cpp:enum-class:`Datastore`
:param source: The configuration being used as the source of type :cpp:class:`Entity<ydk::Entity>`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool delete_config(NetconfServiceProvider & provider, DataStore target, std::string url = "")

Delete a configuration datastore. The RUNNING configuration datastore cannot be deleted.

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration of type :cpp:enum-class:`Datastore` to be deleted
:param url: Required only when target is set to :cpp:enum-class:`Datastore`.:cpp:enum:`url`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool discard_changes(NetconfServiceProvider & provider)

Used to revert the candidate configuration to the current running configuration

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool edit_config(NetconfServiceProvider & provider, DataStore target, Entity& config, std::string default_operation = "", std::string test_option = "", std::string error_option = "")

Loads all or part of a specified configuration to the specified target configuration datastore. Allows the new configuration to be expressed using a local file, a remote file, or inline. If the target configuration datastore does not exist, it will be created.

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration being edited of type :cpp:enum-class:`Datastore`
:param config: An instance of :cpp:class:`Entity<ydk::Entity>` that is a hierarchy configuration of data as defined by one of the device's data models
:param default_operation: Selects the default operation (merge, replace, or none). The default value for this parameter is "merge".
:param test_option: Optionally set to "test-then-set", "set", or "test-only" if the device advertises the :validate:1.1 capability
:param error_option: Optionally set to "stop-on-error", "continue-on-error", or "rollback-on-error"
:return: The result of the operation as bool.
:raises:

.. cpp:function:: std::unique_ptr<Entity> get_config(NetconfServiceProvider & provider, DataStore source, Entity& filter);

Retrieve all or part of a specified configuration datastore

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param source: The configuration being queried of type :cpp:enum-class:`Datastore`
:return: The requested data as :cpp:class:`Entity<ydk::Entity>`
:raises:

.. cpp:function:: std::unique_ptr<Entity> get(NetconfServiceProvider & provider, Entity& filter)

Retrieve running configuration and device state information

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param filter: An instance of :cpp:class:`Entity<ydk::Entity>` that specifies the portion of the system configuration and state data to retrieve
:return: The requested data as :cpp:class:`Entity<ydk::Entity>`
:raises:

.. cpp:function:: bool kill_session(NetconfServiceProvider & provider, int session_id)

Force the termination of a NETCONF session

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param session_id: An instance of int that is the session identifier of the NETCONF session to be terminated
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool lock(NetconfServiceProvider & provider, DataStore target)

Allows the client to lock the entire configuration datastore system of a device

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration of type :cpp:enum-class:`Datastore` to lock
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool unlock(NetconfServiceProvider & provider, DataStore target)

Used to release a configuration lock, previously obtained with the LOCK operation

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param target: The configuration of type DATASTORE to unlock
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool validate(NetconfServiceProvider & provider, DataStore source)

Checks a complete configuration for syntactical and semantic errors before applying the configuration to the device

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param source: An instance of :cpp:enum-class:`Datastore`
:return: The result of the operation as bool
:raises:

.. cpp:function:: bool validate(NetconfServiceProvider & provider, Entity& source)

Checks a complete configuration for syntactical and semantic errors before applying the configuration to the device

:param provider: An instance of :cpp:class:`NetconfServiceProvider<ydk::NetconfServiceProvider>`
:param source: An instance of :cpp:class:`Entity<ydk::Entity>`
:return: The result of the operation as bool
:raises:
72 changes: 36 additions & 36 deletions sdk/cpp/core/src/executor_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,56 @@ string get_netconf_payload(path::DataNode* input, string data_value, string data

ExecutorService::ExecutorService()
{
BOOST_LOG_TRIVIAL(debug) << "ydk.services.NetconfService";
BOOST_LOG_TRIVIAL(debug) << "ydk.services.NetconfService";
}

bool ExecutorService::execute_rpc(NetconfServiceProvider & provider, Entity & entity)
{
// Get the operation
auto operation = entity.get_entity_path(entity.parent).path;
// // Get the operation
// auto operation = entity.get_entity_path(entity.parent).path;

// Set the data tag
string data_tag = "entity";
// // Set the data tag
// string data_tag = "entity";

// Set the config flag
bool set_config_flag = false;
// // Set the config flag
// bool set_config_flag = false;

// Get the root schema node
path::RootSchemaNode* root_schema = provider.get_root_schema();
unique_ptr<ydk::path::Rpc> ydk_rpc { root_schema->rpc(operation) };
// to do: implement way to walk through children to get what to create
ydk_rpc->input()->create("source/candidate"); // -- hard coding
// Create netconf payload
path::CodecService codec_service{};
std::string netconf_payload{"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"};
netconf_payload+=codec_service.encode(ydk_rpc->input(), path::CodecService::Format::XML, false);
netconf_payload+="</rpc>";
// // Get the root schema node
// path::RootSchemaNode* root_schema = provider.get_root_schema();
// unique_ptr<ydk::path::Rpc> ydk_rpc { root_schema->rpc(operation) };
// // to do: implement way to walk through children to get what to create
// ydk_rpc->input()->create("source/candidate"); // -- hard coding
// // Create netconf payload
// path::CodecService codec_service{};
// std::string netconf_payload{"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"};
// netconf_payload+=codec_service.encode(ydk_rpc->input(), path::CodecService::Format::XML, false);
// netconf_payload+="</rpc>";

// Execute payload
std::string reply = provider.execute_payload(netconf_payload);
// // Execute payload -- removed... rewrite this code!!!
// std::string reply = provider.execute_payload(netconf_payload);

// Scan reply for OK
return reply.find("<ok/>") != string::npos;
// // Scan reply for OK
// return reply.find("<ok/>") != string::npos;
}

string get_netconf_payload(path::DataNode* input, string data_value, string data_tag)
{
path::CodecService codec_service{};
auto config_node = input->create(data_tag, data_value);
if(!config_node)
{
BOOST_LOG_TRIVIAL(debug) << "Failed to create data tree";
throw YCPPIllegalStateError{"Failed to create data tree"};
}
// path::CodecService codec_service{};
// auto config_node = input->create(data_tag, data_value);
// if(!config_node)
// {
// BOOST_LOG_TRIVIAL(debug) << "Failed to create data tree";
// throw YCPPIllegalStateError{"Failed to create data tree"};
// }

std::string payload{"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"};
payload+=codec_service.encode(input, path::CodecService::Format::XML, false);
payload+="</rpc>";
BOOST_LOG_TRIVIAL(debug) <<"=============Generating payload=============";
BOOST_LOG_TRIVIAL(debug) <<payload;
BOOST_LOG_TRIVIAL(debug) <<"=========================="<<endl;
return payload;
// std::string payload{"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"};
// payload+=codec_service.encode(input, path::CodecService::Format::XML, false);
// payload+="</rpc>";
// BOOST_LOG_TRIVIAL(debug) <<"=============Generating payload=============";
// BOOST_LOG_TRIVIAL(debug) <<payload;
// BOOST_LOG_TRIVIAL(debug) <<"=========================="<<endl;
// return payload;
}

}
50 changes: 36 additions & 14 deletions sdk/cpp/core/src/netconf_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include <iostream>
#include <sstream>
#include <memory>
#include <boost/log/trivial.hpp>
#include <libyang/libyang.h>

#include "types.hpp"
#include "netconf_client.hpp"
Expand All @@ -31,9 +34,6 @@
#include "entity_data_node_walker.hpp"
#include "errors.hpp"
#include "ydk_yang.hpp"
#include <memory>
#include <boost/log/trivial.hpp>
#include <libyang/libyang.h>

using namespace std;
using namespace ydk;
Expand Down Expand Up @@ -114,16 +114,6 @@ NetconfServiceProvider::~NetconfServiceProvider()
}
}

std::string NetconfServiceProvider::execute_payload(std::string payload)
{
std::string reply = client->execute_payload(payload);
BOOST_LOG_TRIVIAL(debug) <<"=============Reply payload=============";
BOOST_LOG_TRIVIAL(debug) << reply;
BOOST_LOG_TRIVIAL(debug) <<"=========================="<<endl;
// return handle_read_reply(reply, root_schema.get());
return reply;
}

path::RootSchemaNode* NetconfServiceProvider::get_root_schema() const //current
// core::RootSchemaNode* NetconfServiceProvider::get_root_schema() const //old
{
Expand Down Expand Up @@ -170,6 +160,35 @@ path::DataNode* NetconfServiceProvider::handle_edit(path::Rpc* ydk_rpc, path::An
return handle_edit_reply(reply, *client, candidate_supported);
}

path::DataNode* NetconfServiceProvider::handle_netconf_operation(path::Rpc* ydk_rpc) const
{
bool candidate_supported = is_candidate_supported(server_capabilities);

path::CodecService codec_service{};
auto netconf_payload = codec_service.encode(ydk_rpc->input(), path::CodecService::Format::XML, true);
std::string payload{"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"};
netconf_payload = payload + netconf_payload + "</rpc>";

BOOST_LOG_TRIVIAL(debug) << netconf_payload;

std::string reply = client->execute_payload(netconf_payload);
BOOST_LOG_TRIVIAL(debug) <<"=============Reply payload=============";
BOOST_LOG_TRIVIAL(debug) << reply;
BOOST_LOG_TRIVIAL(debug) << endl;
BOOST_LOG_TRIVIAL(trace) << "Executing rpc " + ydk_rpc->schema()->path();
if (ydk_rpc->schema()->path().find("get") != string::npos or ydk_rpc->schema()->path().find("get-config") != string::npos)
{
return handle_read_reply(reply, root_schema.get());
}
if(reply.find("<ok/>") == std::string::npos)
{
BOOST_LOG_TRIVIAL(error) << "No ok in reply ";
BOOST_THROW_EXCEPTION(YCPPServiceProviderError{reply});
}
return nullptr;

}

path::DataNode* NetconfServiceProvider::invoke(path::Rpc* rpc) const
{
path::SchemaNode* create_schema = get_schema_for_operation(*root_schema, "ydk:create");
Expand Down Expand Up @@ -197,7 +216,10 @@ path::DataNode* NetconfServiceProvider::invoke(path::Rpc* rpc) const
else if(rpc_schema == read_schema)
{
return handle_read(rpc);

}
else if(rpc_schema->path().find("ietf-netconf:")!= string::npos)
{
return handle_netconf_operation(rpc);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/cpp/core/src/netconf_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class NetconfServiceProvider : public path::ServiceProvider {
int port);
~NetconfServiceProvider();
path::RootSchemaNode* get_root_schema() const;
path::DataNode* handle_netconf_operation(path::Rpc* ydk_rpc) const;
path::DataNode* invoke(path::Rpc* rpc) const;
std::string execute_payload(std::string payload);

private:
path::DataNode* handle_edit(path::Rpc* rpc, path::Annotation ann) const;
Expand Down
Loading