yanger icon indicating copy to clipboard operation
yanger copied to clipboard

Add yin plugin

Open ljbevemyr opened this issue 3 years ago • 5 comments

This commit adds the yin plugin which generates YIN from the input YANG model, along with tests to compare the Pyang YIN plugin.

Two different options are available for the plugin:

  • pretty strings, will print strings on multiple lines
  • canonical, prints elements in canonical order

ljbevemyr avatar Aug 08 '21 10:08 ljbevemyr

Evening Lisa, great to see yin-support coming to yanger! The only observation is the generation of xml-elements from imported yang models such as tailf-common.yang with prefix 'tailf'. In this case the implementation is using the modulename instead of the prefix.

I attempted a quick fix:

diff --git a/plugins/yanger_yin.erl b/plugins/yanger_yin.erl
index cdd91c0..a90f010 100644
--- a/plugins/yanger_yin.erl
+++ b/plugins/yanger_yin.erl
@@ -104,6 +104,7 @@ emit_stmts([], _Lvl, _S) ->
     ok;
 emit_stmts([Stmt|Stmts], Lvl, S) ->
     OutF = S#state.outf,
+    Ctx = S#state.ctx,
     Keyword = yang:stmt_keyword(Stmt),
     emit_stmt_keyword(Keyword, Lvl, S),
     YinElement = emit_stmt_arg(yang:stmt_arg(Stmt), Lvl, S, Keyword),
@@ -121,7 +122,14 @@ emit_stmts([Stmt|Stmts], Lvl, S) ->
                         {S, SubStmts0}
                 end,
             emit_stmts(SubStmts, Lvl+1, NewS),
-            KeywordStr = make_str(Keyword),
+            KeywordStr = case is_tuple(Keyword) of
+                true ->
+                    {ModuleName, Key} = Keyword,
+                    Prefix  = search_for_prefix(ModuleName,Ctx),
+                    make_str({Prefix,Key});
+                _ ->
+                    make_str(Keyword)
+            end,
             OutF([indent(Lvl),ot(),fs(),KeywordStr,ct(),nl()])
     end,
     emit_stmts(Stmts, Lvl, NewS).
@@ -146,7 +154,8 @@ emit_stmt_arg(Arg, Lvl, S, Keyword) ->
     if is_tuple(Keyword) ->
             {Type, YinElement} = search_for_extension(Keyword, Ctx),
             if YinElement ->
-                    {Prefix, _} = Keyword,
+                    {ModuleName, _} = Keyword,
+                    Prefix  = search_for_prefix(ModuleName,Ctx),
                     TypeStr = make_str({Prefix, Type});
                true ->
                     TypeStr = make_str(Type)
@@ -243,6 +252,20 @@ search_for_namespace(undefined, Mod, Ctx) ->
 search_for_namespace(Namespace, _Mod, _Ctx) ->
     Namespace.
 
+
+search_for_prefix(ModuleName, Ctx) ->
+    Target = yang:search_module(Ctx, ModuleName, undefined),
+    case Target of
+        {true, _, TargetMod} ->
+            make_str(TargetMod#module.prefix);
+        _ ->
+            undefined
+    end.
+
+
+
+
+
 module_arg_imports(_S, _Ctx, [], _Indent) ->
     skip;
 module_arg_imports(S, Ctx, [Import|Imports], Indent) ->

And this produces valid xml-files where the prefixes are correctly used as namespace.

Kind regards, and thanks for this great plugin!

fnchooft avatar Sep 07 '21 02:09 fnchooft

Sorry for the late response... @fnchooft perhaps you can do a new PR from your branch, with this fix?

mbj4668 avatar Dec 09 '21 19:12 mbj4668

Hi @mbj4668 - I have added a patch-request with the fixes for the yin-plugin. Apologies for the delay - but vacation was needed after 2021! :)

fnchooft avatar Jan 13 '22 11:01 fnchooft

I believe this patch entered into main-branch after applied fixes. The only thing that would need to be rebased is the xml-skeleton addition from @ljbevemyr in order to have all the functionality complete.

fnchooft avatar Mar 04 '22 15:03 fnchooft

Hi @fnchooft, thank you for adding patch to the yin-plugin! I will rebase the xml-skeleton.

ljbevemyr avatar Mar 21 '22 08:03 ljbevemyr