yang-js
v0.24.70
Published
YANG parser and evaluator
Downloads
1,101
Maintainers
Readme
yang-js
YANG parser and evaluator
Super light-weight and fast. Produces adaptive JS objects bound by YANG schema expressions according to RFC 6020 specifications. Composes dynamic YANG schema expressions by analyzing arbitrary JS objects.
const Yang = require('yang-js');
const schema = `
container foo {
leaf a { type string; }
leaf b { type uint8; }
list bar {
key "b1";
leaf b1 { type uint16; }
container blob;
}
}
`;
const model = Yang(schema)({
foo: {
a: 'apple',
b: 10,
bar: [
{ b1: 100 }
],
}
});
Installation
$ npm install yang-js
For development/testing, clone from repo and initialize:
$ git clone https://github.com/corenova/yang-js
$ cd yang-js
$ npm install
When using with the web browser, you can generate a browserified version
from the repo (or find it in dist/yang.js
):
$ npm run prepublishOnly
It will produce dist/yang.js
that can then be used directly
inside the web browser.
Features
- Robust parsing
- Focus on high performance
- Extensive test coverage
- Flexible control logic binding
- Powerful XPATH expressions
- Isomorphic runtime
- Adaptive validations
- Dynamic schema generation
- Granular event subscriptions
Please note that yang-js
is not a code-stub generator based on YANG
schema input. It directly embeds YANG schema compliance into ordinary
JS objects as well as generates YANG schema(s) from ordinary JS
objects.
Quick Start
Here's a quick example for using this module:
const Yang = require('yang-js');
const schema = `
container foo {
leaf a { type string; }
leaf b { type uint8; }
}
`;
const model = Yang.parse(schema).eval({
foo: {
a: 'apple',
b: 10,
}
});
The example above uses the explict long-hand version of using this module, which uses the parse method to generate the Yang expression and immediately perform an eval using the Yang expression for the passed-in JS data object.
Since the above is a common usage pattern sequence, this module also provides a cast-style short-hand version as follows:
const model = Yang(schema)({
foo: {
a: 'apple',
b: 10,
}
});
It is functionally equivalent to the explicit version but provides
cleaner syntactic expression regarding how the data object is being
cast with the Yang
expression to get back a new schema-driven
object.
Once you have the model
instance, you can directly interact with its
properties and see the schema enforcement and validations in action.
As the above example illustrates, the yang-js
module takes a
free-form approach when dealing with YANG schema statements. You can
use any YANG statement as the top of the expression and
parse it to return a
corresponding YANG expression instance. However, only YANG expressions
that represent a data node element will
eval to generate a new
Property instance. Also, only module
schemas will eval to generate a
new Model instance.
Reference Guides
Bundled YANG Modules
- iana-crypt-hash.yang
- ietf-yang-types.yang
- ietf-inet-types.yang
- ietf-yang-library.yang (bindings)
- yang-meta-types.yang
Please refer to Working with Multiple Schemas section of the Getting Started Guide for usage examples.
API
Below are the list of methods provided by the yang-js
module. You
can click on each method entry for detailed info on usage.
Main module
The following operations are available from require('yang-js')
.
Please note that when you load the main module, it will attempt to
automatically register .yang
extension into require.extensions
.
Yang instance
The Yang instance is created from
parse/compose
operations from the main module.
Property instance
The Property instances are created during Yang.eval operation and are bound to every node element defined by the underlying Yang schema expression.
- join (obj)
- get (pattern)
- set (value)
- merge (value)
- create (value)
- remove ()
- find (pattern)
- in (pattern)
- do (args...)
- toJSON ()
Please refer to Property for a list of all available properties on this instance.
Model instance
The Model instance is created from
Yang.eval operation for
YANG module
schema and aggregates
Property instances.
This instance also inherits all Property methods and properties.
Please refer to Model for a list of all available properties on this instance.
Examples
Jukebox is a simple example YANG module extracted from RFC 6020. This example implementation is included in this repository's example folder and exercised as part of the test suite. It demonstrates use of the register and import facilities for loading the YANG schema file and binding various control logic behavior.
Promise is a resource reservation module implemented for
OPNFV. This example implementation is hosted in a
separate GitHub repository
opnfv/promise and utilizes
yang-js
for the complete implementation. It demonstrates use of
multiple YANG data models in modeling complex systems. Please be sure
to check it out to learn more about
advanced usage of yang-js
.
Tests
To run the test suite, first install the dependencies, then run npm
test
:
$ npm install
$ npm test
Also refer to Compliance Report for the latest RFC 6020 YANG specification compliance. There's also active effort to support the latest YANG 1.1 draft specifications. You can take a look at the mocha test suite in the test directory for compliance coverage unit-tests and other examples.
License
This software is brought to you by Corenova Technologies. We'd love to hear your feedback. Please feel free to reach me at [email protected] anytime with questions, suggestions, etc.