-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample for XR json read+write (#387)
* Improve logging for codec service * Also do not build cpp bundle after code generation. Make building and installing a separate step after generation
- Loading branch information
Abhi Keshav
committed
Feb 22, 2017
1 parent
2faead8
commit b6e1a97
Showing
13 changed files
with
337 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build* | ||
config_read.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
cmake_minimum_required(VERSION 2.8.9) | ||
cmake_policy(SET CMP0048 NEW) | ||
project(samples) | ||
|
||
|
||
set(SAMPLE_DESCRIPTION "BGP sample application using YANG Development Kit Library, the library for YDK API.") | ||
|
||
|
||
set(samples bgp_xr_read | ||
bgp_xr_write) | ||
|
||
# set default build type if not specified by user | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") | ||
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") | ||
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -fprofile-arcs -ftest-coverage") | ||
|
||
# Find dependent libraries | ||
find_library(ydk_location ydk) | ||
find_library(ydk_cisco_ios_xr_location ydk_cisco_ios_xr) | ||
find_library(xml2_location xml2) | ||
find_library(curl_location curl) | ||
find_library(ssh_location ssh) | ||
find_library(ssh_threads_location ssh_threads) | ||
find_library(pcre_location pcre) | ||
find_library(xslt_location xslt) | ||
find_library(pthread_location pthread) | ||
find_library(dl_location dl) | ||
|
||
include_directories(SYSTEM) | ||
|
||
# set default build type if not specified by user | ||
if(NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release) | ||
endif() | ||
|
||
foreach(sample IN LISTS samples) | ||
add_executable(${sample} ${sample}.cpp ../args_parser.cpp) | ||
|
||
target_link_libraries(${sample} | ||
${ydk_location} | ||
${ydk_cisco_ios_xr_location} | ||
${ydk_location} | ||
${xml2_location} | ||
${curl_location} | ||
${ssh_location} | ||
${ssh_threads_location} | ||
${pcre_location} | ||
${xslt_location} | ||
${pthread_location} | ||
${dl_location} | ||
-rdynamic | ||
) | ||
|
||
set_property(TARGET ${sample} PROPERTY CXX_STANDARD 11) | ||
set_property(TARGET ${sample} PROPERTY CXX_STANDARD_REQUIRED ON) | ||
endforeach(sample) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# YDK C++ BGP JSON Samples for XR | ||
###Prerequisite | ||
First install the [system requirements](https://github.com/CiscoDevNet/ydk-gen#system-requirements). Then follow the [ydk-gen README](https://github.com/CiscoDevNet/ydk-gen#usage) and generate/install the C++ core, [ietf bundle](https://github.com/CiscoDevNet/ydk-gen/blob/master/profiles/bundles/ietf_0_1_1.json) and [cisco-ios-xr bundle](https://github.com/CiscoDevNet/ydk-gen/blob/master/profiles/bundles/cisco-ios-xr_6_1_2.json). | ||
### 1) Build the sample apps | ||
``` | ||
$ mkdir build && cd build | ||
$ cmake .. | ||
$ make && sudo make install | ||
$ cd .. | ||
``` | ||
|
||
### 2) Run the apps | ||
|
||
Note: `-v` will turn on verbose mode | ||
``` | ||
$ cd build | ||
$ ./bgp_xr_write ssh://<username>:<password>@<host address>:<port> [-v] | ||
$ ./bgp_xr_read ssh://<username>:<password>@<host address>:<port> [-v] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* ---------------------------------------------------------------- | ||
Copyright 2016 Cisco Systems | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
------------------------------------------------------------------*/ | ||
#include <iostream> | ||
#include <fstream> | ||
|
||
#include <ydk/codec_provider.hpp> | ||
#include <ydk/codec_service.hpp> | ||
#include <ydk/crud_service.hpp> | ||
#include <ydk/netconf_provider.hpp> | ||
#include <ydk/path_api.hpp> | ||
#include <ydk/types.hpp> | ||
|
||
#include <ydk_cisco_ios_xr/Cisco_IOS_XR_ipv4_bgp_cfg.hpp> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include "../args_parser.h" | ||
|
||
using namespace ydk; | ||
using namespace ydk::Cisco_IOS_XR_ipv4_bgp_cfg; | ||
using namespace std; | ||
|
||
#define CONFIG_FILE "../config_read.json" | ||
|
||
void write_json_config(string json) | ||
{ | ||
ofstream config_file; | ||
config_file.open (CONFIG_FILE); | ||
config_file << json; | ||
config_file.close(); | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
vector<string> args = parse_args(argc, argv); | ||
if(args.empty()) return 1; | ||
|
||
string host, username, password; | ||
int port; | ||
|
||
username = args[0]; password = args[1]; host = args[2]; port = stoi(args[3]); | ||
|
||
bool verbose=(args[4]=="--verbose"); | ||
if(verbose) | ||
{ | ||
auto logger = spdlog::stdout_color_mt("ydk"); | ||
logger->set_level(spdlog::level::debug); | ||
} | ||
|
||
try | ||
{ | ||
path::Repository repo{}; | ||
NetconfServiceProvider provider{repo, host, username, password, port}; | ||
CrudService crud{}; | ||
|
||
auto bgp = make_unique<Bgp>(); | ||
auto bgp_read = crud.read_config(provider, *bgp); | ||
if(bgp_read == nullptr) | ||
{ | ||
cout << "=================================================="<<endl; | ||
cout << "No entries found"<<endl<<endl; | ||
cout << "=================================================="<<endl; | ||
return 0; | ||
} | ||
cout << "Read operation success" << endl; | ||
|
||
CodecService codec_service{}; | ||
CodecServiceProvider codec_provider{repo, EncodingFormat::JSON}; | ||
|
||
auto json = codec_service.encode(codec_provider, *bgp_read); | ||
|
||
write_json_config(json); | ||
cout << "Writing JSON config" << endl; | ||
} | ||
catch(YCPPError & e) | ||
{ | ||
cerr << "Error details: " << e << endl; | ||
} | ||
} |
Oops, something went wrong.