Skip to content

Commit a3498cb

Browse files
committed
Initial commit
0 parents  commit a3498cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+15757
-0
lines changed

.github/workflows/ci.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Install libyang2
20+
run: |
21+
sudo apt-get install cmake libpcre3-dev
22+
git clone https://github.com/CESNET/libyang.git
23+
cd libyang
24+
git checkout de8d5cc7b9bf4fcce1007c8ff3d04d6000cdd081
25+
mkdir build
26+
cd build
27+
cmake -DENABLE_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE:String="Release" ..
28+
make
29+
sudo make install
30+
sudo ldconfig
31+
- name: Build
32+
run: cargo build --verbose
33+
- name: Run tests
34+
run: cargo test --jobs 1 --verbose

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "yang2"
3+
version = "0.1.0"
4+
authors = ["Renato Westphal <[email protected]>"]
5+
description = "libyang2 bindings for Rust"
6+
edition = "2018"
7+
license = "MIT"
8+
repository = "https://github.com/rwestphal/yang2-rs"
9+
documentation = "https://docs.rs/yang2"
10+
readme = "README.md"
11+
categories = ["parser-implementations"]
12+
exclude = ["assets/**"]
13+
14+
[dependencies]
15+
libyang2-sys = { path = "libyang2-sys" }
16+
bitflags = "1.0"
17+
18+
[dev-dependencies]
19+
criterion = "0.3.3"
20+
21+
[[bench]]
22+
name = "data"
23+
harness = false

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Renato Westphal
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# yang2-rs
2+
3+
[![Crates.io][crates-badge]][crates-url]
4+
[![Documentation][docs-badge]][docs-url]
5+
[![MIT licensed][mit-badge]][mit-url]
6+
[![Build Status][actions-badge]][actions-url]
7+
8+
[crates-badge]: https://img.shields.io/crates/v/yang2.svg
9+
[crates-url]: https://crates.io/crates/yang2
10+
[docs-badge]: https://docs.rs/yang2/badge.svg
11+
[docs-url]: https://docs.rs/yang2
12+
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
13+
[mit-url]: https://github.com/rwestphal/yang2-rs/blob/master/LICENSE
14+
[actions-badge]: https://github.com/rwestphal/yang2-rs/workflows/CI/badge.svg
15+
[actions-url]: https://github.com/rwestphal/yang2-rs/actions?query=workflow%3ACI+branch%3Amaster
16+
17+
Rust bindings for the [libyang2] library.
18+
19+
For raw FFI bindings for libyang2, see [libyang2-sys].
20+
21+
[libyang2]: https://github.com/CESNET/libyang/tree/libyang2
22+
[libyang2-sys]: https://github.com/rwestphal/yang2-rs/tree/master/libyang2-sys
23+
24+
#### Cargo.toml
25+
26+
```toml
27+
[dependencies]
28+
yang2 = "0.1"
29+
```
30+
## Design Goals
31+
* Provide high-level bindings for libyang2 using idiomatic Rust
32+
* Leverage Rust's ownership system to detect API misuse problems at compile time
33+
* Automatic resource management
34+
* Zero-cost abstractions
35+
36+
## Example
37+
38+
A basic example that parses and validates JSON instance data, and then converts
39+
it to the XML format:
40+
```rust,no_run
41+
use std::fs::File;
42+
use yang2::context::{Context, ContextFlags};
43+
use yang2::data::{
44+
Data, DataFormat, DataParserFlags, DataPrinterFlags, DataTree,
45+
DataValidationFlags,
46+
};
47+
48+
static SEARCH_DIR: &str = "./assets/yang/";
49+
50+
fn main() -> std::io::Result<()> {
51+
// Initialize context.
52+
let ctx = Context::new(SEARCH_DIR, ContextFlags::NO_YANGLIBRARY)
53+
.expect("Failed to create context");
54+
55+
// Load YANG modules.
56+
for module_name in &["ietf-interfaces", "iana-if-type"] {
57+
ctx.load_module(module_name, None)
58+
.expect("Failed to load module");
59+
}
60+
61+
// Parse and validate data tree in the JSON format.
62+
let dtree = DataTree::parse_file(
63+
&ctx,
64+
File::open("./assets/data/interfaces.json")?,
65+
DataFormat::JSON,
66+
DataParserFlags::empty(),
67+
DataValidationFlags::empty(),
68+
)
69+
.expect("Failed to parse data tree");
70+
71+
// Print data tree in the XML format.
72+
dtree
73+
.print_file(
74+
std::io::stdout(),
75+
DataFormat::XML,
76+
DataPrinterFlags::WD_ALL | DataPrinterFlags::WITH_SIBLINGS,
77+
)
78+
.expect("Failed to print data tree");
79+
80+
Ok(())
81+
}
82+
```
83+
84+
More examples can be found [here][examples].
85+
86+
[examples]: https://github.com/rwestphal/yang2-rs/tree/master/examples
87+
88+
## License
89+
90+
This project is licensed under the [MIT license].
91+
92+
[MIT license]: https://github.com/rwestphal/yang2-rs/blob/master/LICENSE
93+
94+
### Contributing
95+
96+
Bug reports and pull requests are welcome on GitHub at https://github.com/rwestphal/yang2-rs.

assets/data/interfaces.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"ietf-interfaces:interfaces":{
3+
"interface": [
4+
{
5+
"name": "eth/0/0",
6+
"description": "ENG",
7+
"type": "iana-if-type:ethernetCsmacd",
8+
"enabled": true
9+
}
10+
],
11+
"interface": [
12+
{
13+
"name": "eth/0/1",
14+
"description": "MKT",
15+
"type": "iana-if-type:ethernetCsmacd",
16+
"enabled": true
17+
}
18+
]
19+
}
20+
}

assets/data/isis.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"ietf-interfaces:interfaces":{
3+
"interface": [
4+
{
5+
"name": "eth/0/0",
6+
"description": "ENG",
7+
"type": "iana-if-type:ethernetCsmacd",
8+
"enabled": true
9+
}
10+
],
11+
"interface": [
12+
{
13+
"name": "eth/0/1",
14+
"description": "MKT",
15+
"type": "iana-if-type:ethernetCsmacd",
16+
"enabled": true
17+
}
18+
]
19+
},
20+
"ietf-routing:routing": {
21+
"control-plane-protocols": {
22+
"control-plane-protocol": [
23+
{
24+
"type": "ietf-isis:isis",
25+
"name": "default",
26+
"ietf-isis:isis": {
27+
"area-address": [
28+
"00"
29+
],
30+
"interfaces": {
31+
"interface": [
32+
{
33+
"name": "eth/0/0"
34+
}
35+
]
36+
}
37+
}
38+
}
39+
]
40+
}
41+
}
42+
}

assets/yang/[email protected]

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
module iana-bfd-types {
2+
3+
yang-version 1.1;
4+
5+
namespace "urn:ietf:params:xml:ns:yang:iana-bfd-types";
6+
7+
prefix "iana-bfd-types";
8+
9+
organization "IANA";
10+
11+
contact
12+
" Internet Assigned Numbers Authority
13+
14+
Postal: ICANN
15+
12025 Waterfront Drive, Suite 300
16+
Los Angeles, CA 90094-2536
17+
United States of America
18+
19+
Tel: +1 310 823 9358
20+
<mailto:[email protected]>";
21+
22+
description
23+
"This module defines YANG data types for IANA-registered
24+
BFD parameters.
25+
26+
This YANG module is maintained by IANA and reflects the
27+
'BFD Diagnostic Codes' and 'BFD Authentication Types' registries.
28+
29+
Copyright (c) 2018 IETF Trust and the persons
30+
identified as authors of the code. All rights reserved.
31+
32+
Redistribution and use in source and binary forms, with or
33+
without modification, is permitted pursuant to, and subject
34+
to the license terms contained in, the Simplified BSD License
35+
set forth in Section 4.c of the IETF Trust's Legal Provisions
36+
Relating to IETF Documents
37+
(http://trustee.ietf.org/license-info).
38+
39+
This version of this YANG module is part of RFC XXXX; see
40+
the RFC itself for full legal notices.";
41+
42+
// RFC Ed.: replace XXXX with actual RFC number and remove
43+
// this note
44+
45+
reference "RFC XXXX";
46+
47+
revision 2018-08-01 {
48+
description "Initial revision.";
49+
reference "RFC XXXX: IANA BFD YANG Data Types.";
50+
}
51+
52+
/*
53+
* Type Definitions
54+
*/
55+
typedef diagnostic {
56+
type enumeration {
57+
enum none {
58+
value 0;
59+
description "None";
60+
}
61+
enum control-expiry {
62+
value 1;
63+
description "Control timer expiry";
64+
}
65+
enum echo-failed {
66+
value 2;
67+
description "Echo failure";
68+
}
69+
enum neighbor-down {
70+
value 3;
71+
description "Neighbor down";
72+
}
73+
enum forwarding-reset {
74+
value 4;
75+
description "Forwarding reset";
76+
}
77+
enum path-down {
78+
value 5;
79+
description "Path down";
80+
}
81+
enum concatenated-path-down {
82+
value 6;
83+
description "Concatenated path down";
84+
}
85+
enum admin-down {
86+
value 7;
87+
description "Admin down";
88+
}
89+
enum reverse-concatenated-path-down {
90+
value 8;
91+
description "Reverse concatenated path down";
92+
}
93+
enum mis-connectivity-defect {
94+
value 9;
95+
description "Mis-connectivity defect as specified in RFC6428";
96+
}
97+
}
98+
description
99+
"BFD diagnostic as defined in RFC 5880, values are maintained in
100+
the 'BFD Diagnostic Codes' IANA registry. Range is 0 to 31.";
101+
}
102+
103+
typedef auth-type {
104+
type enumeration {
105+
enum reserved {
106+
value 0;
107+
description "Reserved";
108+
}
109+
enum simple-password {
110+
value 1;
111+
description "Simple password";
112+
}
113+
enum keyed-md5 {
114+
value 2;
115+
description "Keyed MD5";
116+
}
117+
enum meticulous-keyed-md5 {
118+
value 3;
119+
description "Meticulous keyed MD5";
120+
}
121+
enum keyed-sha1 {
122+
value 4;
123+
description "Keyed SHA1";
124+
}
125+
enum meticulous-keyed-sha1 {
126+
value 5;
127+
description "Meticulous keyed SHA1";
128+
}
129+
}
130+
description
131+
"BFD authentication type as defined in RFC 5880, values are
132+
maintained in the 'BFD Authentication Types' IANA registry.
133+
Range is 0 to 255.";
134+
}
135+
}

0 commit comments

Comments
 (0)