ietf-netmod-yang

Calling a 'C' function inside yang file


Can we call a 'C' function in yang file ? Or is there any way to accomplish this ? Basically I want to call a 'C' fun which will return a integer value and use that in my .yang file.

Below is my leaf structure :

leaf memory {
  type int16;
  default 100;
}

I don't want static default value which is 100 here. I want the value in default field to be picked up from "C" function.

Is there any way to accomplish this ?

Thanks


Solution

  • YANG doesn't define implementation details, it just defines the schema of the data that a server and a client exchange. A possible way to achieve YANG model to implementation references is to use YANG extensions. These allow for the data model to include extra information that the YANG standard doesn't recognize, but that the YANG agent does.

    That means:

    1. define an extension on a YANG file
    2. use that extension instead of the default keyword
    3. in your implementation, have a mechanism that whenever a YANG defined value is initialized, if the extension is present, call a method with that name in order to retrieve a default value, instead of using the default value that the model describes directly.

    So as you can see, first two steps are easy, third step is harder, as that requires adaptation of a YANG parser, either at compile time or runtime, so that the C function mentioned by the extension is invoked. This requires some kind of engine that is able to process the extension, and keep it in memory so that the data is correctly initialized.

    Here's an example on how that would look like in YANG

    module extensions {
      namespace "http://example.com/extensions";
      prefix ext;
    
      extension default-function {
         argument function-name;
         description "Describes that this leaf has its default value defined by this function.";
      }
    }
    
    import extensions { prefix ext; }
    (...)
      leaf memory {
        type int16;
        ext:default-function 100;
      }