<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: foreach loop using python variable in nso template in NSO Developer Hub Discussions</title>
    <link>https://community.cisco.com/t5/nso-developer-hub-discussions/foreach-loop-using-python-variable-in-nso-template/m-p/4072621#M5155</link>
    <description>&lt;P&gt;Unfortunately, you cannot process a Python list (or Java) in a template variable - $ACL_RULES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Recall that in the interface from code (Python, Java) the variable passed is a simple _STRING_.. not a data structure of any kind.&amp;nbsp; Since you can use various languages for your create_cb/action_cb it would require an interesting design to handle list structures from each of the languages to input into variable.&lt;/P&gt;
&lt;P&gt;As you point out, if the acl_rules are a list in the service yang, the template processing can take knowledge of it being a list and loop on the available entries in the list.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally, you would loop in your python code and apply the same template with the $ACL_RULE set to a single item in the list.&lt;/P&gt;
&lt;P&gt;It is possible from your python code, although may not be worth the work, to write your ACL_RULES into a list in CDB, and then have your template point to the new list (instead of the list in the service) or change context at that point in the template to point at your new list. Then you could use the same for-each for that list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Apr 2020 17:07:28 GMT</pubDate>
    <dc:creator>lmanor</dc:creator>
    <dc:date>2020-04-23T17:07:28Z</dc:date>
    <item>
      <title>foreach loop using python variable in nso template</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/foreach-loop-using-python-variable-in-nso-template/m-p/4072561#M5154</link>
      <description>&lt;P&gt;I have a 'python-and-template' service package called dummy&lt;BR /&gt;Is it possible to run a foreach loop in the template using a variable passed from python&lt;/P&gt;
&lt;P&gt;Here is my template. Note the variable $ACL_RULES in the template:&lt;/P&gt;
&lt;PRE&gt;&lt;SPAN&gt;&amp;lt;config-template &lt;/SPAN&gt;&lt;SPAN&gt;xmlns=&lt;/SPAN&gt;&lt;SPAN&gt;"http://tail-f.com/ns/config/1.0"&lt;/SPAN&gt;&lt;SPAN&gt;&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;    &amp;lt;devices &lt;/SPAN&gt;&lt;SPAN&gt;xmlns=&lt;/SPAN&gt;&lt;SPAN&gt;"http://tail-f.com/ns/ncs" &lt;/SPAN&gt;&lt;SPAN&gt;foreach=&lt;/SPAN&gt;&lt;SPAN&gt;"{/devices}"&lt;/SPAN&gt;&lt;SPAN&gt;&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;        &amp;lt;device&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;            &amp;lt;name&amp;gt;&lt;/SPAN&gt;{.}&lt;SPAN&gt;&amp;lt;/name&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;            &amp;lt;config&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                &amp;lt;ip &lt;/SPAN&gt;&lt;SPAN&gt;xmlns=&lt;/SPAN&gt;&lt;SPAN&gt;"urn:ios"&lt;/SPAN&gt;&lt;SPAN&gt;&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                    &amp;lt;access-list&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                        &amp;lt;extended&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                            &amp;lt;ext-named-acl&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                &amp;lt;name&amp;gt;&lt;/SPAN&gt;{/acl_name}&lt;SPAN&gt;&amp;lt;/name&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                &amp;lt;?&lt;/SPAN&gt;&lt;SPAN&gt;foreach &lt;/SPAN&gt;&lt;SPAN&gt;{$ACL_RULES}&lt;/SPAN&gt;&lt;SPAN&gt;?&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                    &amp;lt;ext-access-list-rule&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                        &amp;lt;rule&amp;gt;&lt;/SPAN&gt;{.}&lt;SPAN&gt;&amp;lt;/rule&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                    &amp;lt;/ext-access-list-rule&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                                &amp;lt;?&lt;/SPAN&gt;&lt;SPAN&gt;end&lt;/SPAN&gt;&lt;SPAN&gt;?&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                            &amp;lt;/ext-named-acl&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                        &amp;lt;/extended&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                    &amp;lt;/access-list&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;                &amp;lt;/ip&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;            &amp;lt;/config&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;        &amp;lt;/device&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;    &amp;lt;/devices&amp;gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;lt;/config-template&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;and here is the main.py file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;SPAN&gt;# -*- mode: python; python-indent: 4 -*-&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;import &lt;/SPAN&gt;ncs&lt;BR /&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;ncs.application &lt;SPAN&gt;import &lt;/SPAN&gt;Service&lt;BR /&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;class &lt;/SPAN&gt;&lt;SPAN&gt;ServiceCallbacks&lt;/SPAN&gt;(Service):&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;def &lt;/SPAN&gt;&lt;SPAN&gt;cb_create&lt;/SPAN&gt;(&lt;SPAN&gt;self&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;tctx&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;root&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;service&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;proplist&lt;/SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;        &lt;SPAN&gt;self&lt;/SPAN&gt;.log.info(&lt;SPAN&gt;'Service create(service='&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;service&lt;/SPAN&gt;._path&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;')'&lt;/SPAN&gt;)&lt;BR /&gt;&lt;BR /&gt;        vars = ncs.template.Variables()&lt;BR /&gt;        vars.add(&lt;SPAN&gt;'ACL_RULES'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;service&lt;/SPAN&gt;.acl_rules)&lt;BR /&gt;        template = ncs.template.Template(&lt;SPAN&gt;service&lt;/SPAN&gt;)&lt;BR /&gt;        template.apply(&lt;SPAN&gt;'dummy-template'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;vars)&lt;BR /&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;class &lt;/SPAN&gt;&lt;SPAN&gt;Main&lt;/SPAN&gt;(ncs.application.Application):&lt;BR /&gt;    &lt;SPAN&gt;def &lt;/SPAN&gt;&lt;SPAN&gt;setup&lt;/SPAN&gt;(&lt;SPAN&gt;self&lt;/SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;        &lt;SPAN&gt;self&lt;/SPAN&gt;.log.info(&lt;SPAN&gt;'Main RUNNING'&lt;/SPAN&gt;)&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;        &lt;SPAN&gt;self&lt;/SPAN&gt;.register_service(&lt;SPAN&gt;'dummy-servicepoint'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;ServiceCallbacks)&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;def &lt;/SPAN&gt;&lt;SPAN&gt;teardown&lt;/SPAN&gt;(&lt;SPAN&gt;self&lt;/SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;        &lt;SPAN&gt;self&lt;/SPAN&gt;.log.info(&lt;SPAN&gt;'Main FINISHED'&lt;/SPAN&gt;)&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;service&lt;/SPAN&gt;.acl_rules is type leaf-list from yang.&lt;/P&gt;
&lt;P&gt;But when i do a package reload from the ncs cli i am getting the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;[dummy-template.xml:11 Invalid parameters for processing instruction foreach.]&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;I my case i could have directly used the yang leaf list called acl_rules, but i want to use the python variable ACL_RULES.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;Is there a way iteration can be done in template using python variable?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 16:32:48 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/foreach-loop-using-python-variable-in-nso-template/m-p/4072561#M5154</guid>
      <dc:creator>rrihar</dc:creator>
      <dc:date>2020-04-23T16:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: foreach loop using python variable in nso template</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/foreach-loop-using-python-variable-in-nso-template/m-p/4072621#M5155</link>
      <description>&lt;P&gt;Unfortunately, you cannot process a Python list (or Java) in a template variable - $ACL_RULES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Recall that in the interface from code (Python, Java) the variable passed is a simple _STRING_.. not a data structure of any kind.&amp;nbsp; Since you can use various languages for your create_cb/action_cb it would require an interesting design to handle list structures from each of the languages to input into variable.&lt;/P&gt;
&lt;P&gt;As you point out, if the acl_rules are a list in the service yang, the template processing can take knowledge of it being a list and loop on the available entries in the list.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally, you would loop in your python code and apply the same template with the $ACL_RULE set to a single item in the list.&lt;/P&gt;
&lt;P&gt;It is possible from your python code, although may not be worth the work, to write your ACL_RULES into a list in CDB, and then have your template point to the new list (instead of the list in the service) or change context at that point in the template to point at your new list. Then you could use the same for-each for that list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 17:07:28 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/foreach-loop-using-python-variable-in-nso-template/m-p/4072621#M5155</guid>
      <dc:creator>lmanor</dc:creator>
      <dc:date>2020-04-23T17:07:28Z</dc:date>
    </item>
  </channel>
</rss>

