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

Implement Cisco-NSO nonstandard parsing #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ lazy_static = "1.4.0"
quick-error = "2.0.1"
derive-getters = "0.2.0"
url = "2.2.2"

[features]
# Enables parsing some non-standard YANG constructs used in Cisco NSO
cisco-nso-extensions = []
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ match yang {
}

```

## Feature 'cisco-nso-extensions'

Enables parsing a few nonstandard constructs Cisco allows and uses with Network Service Orchestrator (NSO):

- Empty `input` / `output` statements
- `deref` in `leafref` `path` substatements ([see RFC errata 5617](https://www.rfc-editor.org/errata/eid5617))
84 changes: 75 additions & 9 deletions src/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,22 +824,36 @@ impl StmtArg for PositionValueArg {
pub enum PathArg {
AbsolutePath(AbsolutePath),
RelativePath(RelativePath),
#[cfg(feature = "cisco-nso-extensions")]
DerefPath(DerefPath),
}

impl StmtArg for PathArg {
fn parse_arg(parser: &mut Parser) -> Result<Self, YangError> {
let str = parse_string(parser)?;

if str.starts_with('/') {
Ok(PathArg::AbsolutePath(
AbsolutePath::from_str(&str).map_err(|e| YangError::ArgumentParseError(e.str))?,
))
} else if str.starts_with("..") {
Ok(PathArg::RelativePath(
RelativePath::from_str(&str).map_err(|e| YangError::ArgumentParseError(e.str))?,
))
Self::from_str(&str).map_err(|e| YangError::ArgumentParseError(e.str))
}
}

impl FromStr for PathArg {
type Err = ArgError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with('/') {
Ok(PathArg::AbsolutePath(AbsolutePath::from_str(&s)?))
} else if s.starts_with("..") {
Ok(PathArg::RelativePath(RelativePath::from_str(&s)?))
} else if s.starts_with("deref") {
#[cfg(feature = "cisco-nso-extensions")]
return Ok(PathArg::DerefPath(DerefPath::from_str(s)?));
#[cfg(not(feature = "cisco-nso-extensions"))]
return Err(ArgError {
str: "path-arg with deref is not RFC7950 compatible, see cisco-nso-extensions feature",
});
} else {
Err(YangError::ArgumentParseError("path-arg"))
Err(ArgError {
str: "path-arg start",
})
}
}
}
Expand Down Expand Up @@ -944,6 +958,58 @@ impl FromStr for RelativePath {
}
}

/// "deref-path"
#[cfg(feature = "cisco-nso-extensions")]
#[derive(Debug, Clone, PartialEq, Getters)]
pub struct DerefPath {
deref_path: Box<PathArg>, // Needs to be boxed to prevent recursion
relative_path: RelativePath,
}

#[cfg(feature = "cisco-nso-extensions")]
impl FromStr for DerefPath {
type Err = ArgError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with("deref(") {
return Err(ArgError::new("deref-path prefix"));
}
let mut paren_count = 0;
let mut deref_arg_end = 0;
for (i, c) in s.chars().enumerate().skip("deref".len()) {
match c {
'(' => paren_count += 1,
')' => {
paren_count -= 1;
if paren_count == 0 {
deref_arg_end = i;
break;
}
}
_ => {}
};
}

if deref_arg_end == 0 {
println!("Paren mismatch in '{}': {}", s, paren_count);
return Err(ArgError::new("deref-path parens"));
}

let path_remainder_start = deref_arg_end + 2; // skip )/
if &s[deref_arg_end..path_remainder_start] != ")/" {
return Err(ArgError::new("deref-path follow"));
}

let deref_path = PathArg::from_str(&s[6..deref_arg_end])?;
let relative_path = RelativePath::from_str(&s[path_remainder_start..])?;

Ok(DerefPath {
deref_path: Box::new(deref_path),
relative_path,
})
}
}

/// "descendant-path".
#[derive(Debug, Clone, PartialEq, Getters)]
pub struct DescendantPath {
Expand Down
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,13 @@ impl Parser {
while l.len() > 0 {
let c = l.chars().next().unwrap();

if c.is_whitespace() || c == '"' || c == '\'' || c == '}' || c == '{' || c == ';' {
if c.is_whitespace() || c == ';' || c == '}' || c == '{' {
break;
}

// Cisco allows quote characters in unquoted strings :/
#[cfg(not(feature = "cisco-nso-extensions"))]
if c == '"' || c == '\'' {
break;
}

Expand Down
40 changes: 40 additions & 0 deletions src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4827,10 +4827,17 @@ impl Stmt for InputStmt {
}

/// Return true if this statement has substatements.
#[cfg(not(feature = "cisco-nso-extensions"))]
fn has_substmts() -> bool {
true
}

/// Return true if this statement has sub-statements optionally.
#[cfg(feature = "cisco-nso-extensions")]
fn opt_substmts() -> bool {
true
}

/// Return substatements definition.
fn substmts_def() -> Vec<SubStmtDef> {
vec![
Expand All @@ -4840,6 +4847,19 @@ impl Stmt for InputStmt {
]
}

/// Constructor with a single arg. Panic if it is not defined.
#[cfg(feature = "cisco-nso-extensions")]
fn new_with_arg(_arg: Self::Arg) -> YangStmt
where
Self: Sized,
{
YangStmt::InputStmt(InputStmt {
must: Vec::new(),
typedef_or_grouping: TypedefOrGrouping::new(),
data_def: DataDefStmt::new(),
})
}

/// Constructor with tuple of substatements. Panic if it is not defined.
fn new_with_substmts(_arg: Self::Arg, substmts: Self::SubStmts) -> YangStmt
where
Expand Down Expand Up @@ -4946,10 +4966,17 @@ impl Stmt for OutputStmt {
}

/// Return true if this statement has substatements.
#[cfg(not(feature = "cisco-nso-extensions"))]
fn has_substmts() -> bool {
true
}

/// Return true if this statement has sub-statements optionally.
#[cfg(feature = "cisco-nso-extensions")]
fn opt_substmts() -> bool {
true
}

/// Return substatements definition.
fn substmts_def() -> Vec<SubStmtDef> {
vec![
Expand All @@ -4959,6 +4986,19 @@ impl Stmt for OutputStmt {
]
}

/// Constructor with a single arg. Panic if it is not defined.
#[cfg(feature = "cisco-nso-extensions")]
fn new_with_arg(_arg: Self::Arg) -> YangStmt
where
Self: Sized,
{
YangStmt::OutputStmt(OutputStmt {
must: Vec::new(),
typedef_or_grouping: TypedefOrGrouping::new(),
data_def: DataDefStmt::new(),
})
}

/// Constructor with tuple of substatements. Panic if it is not defined.
fn new_with_substmts(_arg: Self::Arg, substmts: Self::SubStmts) -> YangStmt
where
Expand Down