Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    Creating Translation Scripts for YANG Configuration Models

    You can load YANG modules on devices running Junos OS to add data models that are not natively supported by Junos OS but that can be supported by translation. When you extend the configuration hierarchy with non-native YANG data models, you must also supply one or more translation scripts that provide the logic to map the non-native configuration syntax to the corresponding Junos OS syntax.

    Translation scripts convert the configuration data corresponding to the non-native YANG data models into Junos OS syntax and add the translated configuration data as a transient change in the checkout configuration during the commit operation. Translation scripts can be written in either Python or SLAX and are similar to commit scripts in structure. For information about creating SLAX and Python scripts that generate transient changes in the configuration, see the Automation Scripting Feature Guide.

    You use the request system yang add or request system yang update commands to add YANG modules and their associated translation scripts to a new or existing YANG package on the device. After you add the modules and translation scripts to the device, you can configure the statements and hierarchies in the data model added by those modules. When you load and commit the configuration data, the device calls the script to perform the translation and generate the transient configuration change.

    This topic discusses the general structure for translation scripts. The specific translation logic required in the actual script depends on the custom hierarchies added to the schema and is beyond the scope of this topic.

    To create the framework for translation scripts that are used on devices running Junos OS:

    1. In your favorite editor, create a new file that uses the .slax or .py file extension, as appropriate.
    2. Include the necessary boilerplate required for that script’s language, which is identical to the boilerplate for commit scripts, and also include any required namespace declarations for your data models.
      • SLAX code:
        version 1.0;
        ns junos = "http://xml.juniper.net/junos/*/junos";
        ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
        ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
        ns prefix = "namespace"; import "../import/junos.xsl";
         
        match configuration {
        /*
        * insert your code here
        */
        }
      • Python code:
        from junos import Junos_Context
        from junos import Junos_Configuration
        import jcs
        
        if __name__ == '__main__':
           /*
        * insert your code here
        */

      Note: Translation scripts must fully qualify identifiers for non-native YANG data models in the translation code.

      Note: For information about commit script boilerplate code, see Required Boilerplate for Commit Scripts and the Automation Scripting Feature Guide.

    3. Add code that maps the non-native configuration data into the equivalent Junos OS syntax and stores the translated configuration data in a variable.
      • SLAX sample code:
        match configuration {
         
        /* translation code */  
        var $final = { /*
        * translated configuration
        */
        } }
      • Python sample code:
        if __name__ == '__main__':
         
        /* translation code */  
        final = """ <transient-change> /*
        * Junos XML elements representing translated configuration
        */
        </transient-change> """
    4. Add the translated content to the checkout configuration as a transient configuration change by calling the jcs:emit-change() template in SLAX scripts or the jcs.emit_change() function in Python scripts with the translated configuration and transient-change tag as arguments.
      • SLAX sample code:
        match configuration {
         
        /* translation code */  
        var $final = { /*
        * translated configuration
        */
        } call jcs:emit-change($content=$final, $tag='transient-change'); }
      • Python sample code:
        if __name__ == '__main__':
         
        /* translation code */  
        final = """ /*
        * Junos XML elements representing translated configuration
        */
        """ jcs.emit_change(final, "transient-change", "xml")

      Note: In SLAX scripts, you can also generate the transient change by emitting the translated configuration inside of a <transient-change> element instead of calling the jcs:emit-change() template.

    On the device, perform the following tasks before adding the translation script to a YANG package:

    1. If the translation script is written in Python, configure the language python statement.
      [edit]user@host# set system scripts language python
    2. Download the script to the device, and optionally validate the syntax.

      user@host> request system yang validate translation-script script

    Before you can use translation scripts on a device, you must add the scripts and associated modules to a new or existing YANG package using the request system yang add or request system yang update command. After the modules and scripts are added, the translation scripts are automatically invoked when you commit configuration data in the corresponding data models.

    The active and candidate configurations contain the configuration data for the non-native YANG data models in the syntax defined by those models. However, because the translated configuration data is committed as a transient change, the active and candidate configurations do not explicitly display the translated data in the Junos OS syntax when you issue the show or show configuration commands. To apply YANG translation scripts when you view the configuration, use the | display translation-scripts filter.

    To view the complete post-inheritance configuration with the translated data (transient changes) explicitly included , append the | display translation-scripts filter to the show configuration command in operational mode or the show command in configuration mode. To view just the non-native configuration data after translation, use the | display translation-scripts translated-config filter.

    In configuration mode, to display just the changes to the configuration data corresponding to non-native YANG data models before or after translation scripts are applied, append the configured-delta or translated-delta keyword, respectively, to the show | display translation-scripts command. In both cases, the XML output displays the deleted configuration data, followed by the new configuration data.

    For more information about the | display translation-scripts filter, see Committing and Displaying Configuration Data for Non-Native YANG Modules.

    Modified: 2017-07-24