<?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: NSO: Switching between context nodes in NSO Developer Hub Discussions</title>
    <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064920#M5093</link>
    <description>Well, don’t know what ‘devices’ is set to in your create_cb  above this code, but apparently it may not be set at all.&lt;BR /&gt;&lt;BR /&gt;Use the root input parameter to point to the devices list:&lt;BR /&gt;    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create&lt;BR /&gt;    def cb_create(self, tctx, root, service, proplist):&lt;BR /&gt;&lt;BR /&gt;    devGrp = root.ncs__devices.device_group['nx-dev']&lt;BR /&gt;    print ("members")&lt;BR /&gt;    for member in devGrp.member :&lt;BR /&gt;      print (member)&lt;BR /&gt;&lt;BR /&gt;members&lt;BR /&gt;nx-0&lt;BR /&gt;nx-1&lt;BR /&gt;nx-2&lt;BR /&gt;nx-3&lt;BR /&gt;</description>
    <pubDate>Mon, 13 Apr 2020 20:25:34 GMT</pubDate>
    <dc:creator>lmanor</dc:creator>
    <dc:date>2020-04-13T20:25:34Z</dc:date>
    <item>
      <title>NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064094#M5086</link>
      <description>&lt;P&gt;Hi, all!&lt;/P&gt;&lt;P&gt;I am trying to construct an xml template for the service. Here is an example of the YANG module I constructed:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;...
      leaf-list device-group {
        tailf:info "Device group to use";
        type leafref {
          path "/ncs:devices/ncs:device-group/ncs:name";
        }
      }
      list some-server {
        key name;
        leaf name {
          type string;
        }
      }
...&lt;/PRE&gt;&lt;P&gt;So in XML template I am looping though each device in the device-group, then looping through the some-server list. But while it is inside the some-server loop, I want to reference device name again:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;config-template xmlns="http://tail-f.com/ns/config/1.0"
                 servicepoint="some-point"&amp;gt;
  &amp;lt;devices xmlns="http://tail-f.com/ns/ncs"&amp;gt;
    &amp;lt;?foreach {/device-group}?&amp;gt;
      &amp;lt;device&amp;gt;
        &amp;lt;name&amp;gt;{deref(.)/../member}&amp;lt;/name&amp;gt;
          &amp;lt;config&amp;gt;
             &amp;lt;configuration xmlns="http://xml.juniper.net/xnm/1.1/xnm"&amp;gt;
               &amp;lt;system&amp;gt;
                &amp;lt;ntp&amp;gt;
                    &amp;lt;?foreach {/some-server}?&amp;gt;
                      &amp;lt;server&amp;gt;
                          &amp;lt;name&amp;gt;{deref(.)/../member}&amp;lt;/name&amp;gt;
                      &amp;lt;/server&amp;gt;
...&lt;/PRE&gt;&lt;P&gt;Of course, just placing&amp;nbsp;{deref(.)/../member} will not work. I need to reference the device name, while still in the foreach loop.&lt;/P&gt;&lt;P&gt;I have tried using &amp;lt;?set device-name={deref(.)/../member} ?&amp;gt; (didn't work), tried using {deref(/device-group)/../member}, but it printed devices in the current device-group, instead of a particular device-name.&lt;/P&gt;&lt;P&gt;Does anyone know how to make it work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 05:01:00 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064094#M5086</guid>
      <dc:creator>indirakas</dc:creator>
      <dc:date>2020-04-12T05:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064103#M5087</link>
      <description>&lt;P&gt;I think it might be possible to find a way to do it using the more advanced template options (if you haven't already, take a look at the development guide under "Processing instructions"), but honestly, I'd advise moving at least some of the iterations from template to Python.&lt;/P&gt;
&lt;P&gt;Even if you get it figured out, this template is going to get harder and harder to troubleshoot and to maintain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will be cool to get it solved, but it will become this sort of write-only code, which won't be as cool to maintain &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 06:36:27 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064103#M5087</guid>
      <dc:creator>yfherzog</dc:creator>
      <dc:date>2020-04-12T06:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064219#M5088</link>
      <description>&lt;P&gt;Try another foreach over the members.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Apr 2020 15:00:55 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064219#M5088</guid>
      <dc:creator>vleijon</dc:creator>
      <dc:date>2020-04-12T15:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064693#M5089</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;SPAN class=""&gt;&lt;A id="link_17" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://community.cisco.com/t5/user/viewprofilepage/user-id/607973" target="_self"&gt;yfherzog&lt;/A&gt;&amp;nbsp;suggested, with the advanced template control features, it is quite simple to save the name in a variable that can later be used in your nested loop. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;Something like this - devname:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    &amp;lt;?foreach {/device-group}?&amp;gt;
      &amp;lt;device&amp;gt;
        &amp;lt;name&amp;gt;{deref(.)/../member}&amp;lt;/name&amp;gt;&lt;BR /&gt;        &amp;lt;?set devname = {.}?&amp;gt;
          &amp;lt;config&amp;gt;
             &amp;lt;configuration xmlns="http://xml.juniper.net/xnm/1.1/xnm"&amp;gt;
               &amp;lt;system&amp;gt;
                &amp;lt;ntp&amp;gt;
                    &amp;lt;?foreach {/some-server}?&amp;gt;
                      &amp;lt;server&amp;gt;
                          &amp;lt;name&amp;gt;{$devname}&amp;lt;/name&amp;gt;
                      &amp;lt;/server&amp;gt;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 14:42:26 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064693#M5089</guid>
      <dc:creator>lmanor</dc:creator>
      <dc:date>2020-04-13T14:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064784#M5090</link>
      <description>Thank you! That worked! I used &amp;lt;?set devname = {deref(.)/../member} ?&amp;gt;, but apparently I should have used &amp;lt;?set devname = {.} ?&amp;gt; instead.</description>
      <pubDate>Mon, 13 Apr 2020 16:52:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064784#M5090</guid>
      <dc:creator>indirakas</dc:creator>
      <dc:date>2020-04-13T16:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064844#M5092</link>
      <description>&lt;P&gt;I would have done it through python main.py file, but the problem is how to use deref here?&lt;/P&gt;&lt;P&gt;So I can loop through device-groups, but then I have a problem accessing device members of that group:&lt;/P&gt;&lt;PRE&gt;        for group in service.device_group:
            self.log.info('Group name is ',group)
            for member_name in devices.device_group[group].member:
                someserver['device_name'] = member_name&lt;/PRE&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;Aborted: Python cb_create error. global name 'devices' is not defined&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 18:20:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064844#M5092</guid>
      <dc:creator>indirakas</dc:creator>
      <dc:date>2020-04-13T18:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064920#M5093</link>
      <description>Well, don’t know what ‘devices’ is set to in your create_cb  above this code, but apparently it may not be set at all.&lt;BR /&gt;&lt;BR /&gt;Use the root input parameter to point to the devices list:&lt;BR /&gt;    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create&lt;BR /&gt;    def cb_create(self, tctx, root, service, proplist):&lt;BR /&gt;&lt;BR /&gt;    devGrp = root.ncs__devices.device_group['nx-dev']&lt;BR /&gt;    print ("members")&lt;BR /&gt;    for member in devGrp.member :&lt;BR /&gt;      print (member)&lt;BR /&gt;&lt;BR /&gt;members&lt;BR /&gt;nx-0&lt;BR /&gt;nx-1&lt;BR /&gt;nx-2&lt;BR /&gt;nx-3&lt;BR /&gt;</description>
      <pubDate>Mon, 13 Apr 2020 20:25:34 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064920#M5093</guid>
      <dc:creator>lmanor</dc:creator>
      <dc:date>2020-04-13T20:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064967#M5094</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        some_service = {
            'device_group':{},
        }
        for group in service.device_group:
            syslog["device_group"][group]={}
            syslog["device_group"][group]["member"] = {}
            for member_name in root.devices.device_group[group].member:
                some_service["device_group"][group]["member"].append(member_name)
                
        some_service_tvars=ncs.template.Variables()
        some_service_tvars.add('DEVICE_GROUP',some_service['device_group'])
        some_service_template = ncs.template.Template(service)
        some_service_template.apply('some-service-template', some_service_tvars)&lt;/PRE&gt;&lt;P&gt;So in the python main.py file I will be able to pull the device-group list, members on that list. But then should I wrap them into a new dictionary, add that dictionary as a template variable and then call it from the xml file?&lt;/P&gt;&lt;PRE&gt;&amp;lt;config-template xmlns="http://tail-f.com/ns/config/1.0"
                 servicepoint="some-service"&amp;gt;
  &amp;lt;devices xmlns="http://tail-f.com/ns/ncs"&amp;gt;
    &amp;lt;?foreach {$DEVICE_GROUP}?&amp;gt;
      &amp;lt;device&amp;gt;
        &amp;lt;name&amp;gt;{$member}&amp;lt;/name&amp;gt;&lt;/PRE&gt;&lt;P&gt;Previously I saw examples of creating new global variables in main.py file:&lt;/P&gt;&lt;PRE&gt;        foo = {
            'v6_flag':'false',
            'v4_flag':'false',
        }
        bar_tvars=ncs.template.Variables()
        bar_tvars.add('V6_FLAG',foo['v6_flag'])
        bar_tvars.add('V4_FLAG',foo['v4_flag'])&lt;/PRE&gt;&lt;P&gt;Can you give an example how to add nested dictionary variable via python?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 21:34:15 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4064967#M5094</guid>
      <dc:creator>indirakas</dc:creator>
      <dc:date>2020-04-13T21:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4065014#M5095</link>
      <description>&lt;P&gt;Apparently the solution is to move&amp;nbsp;some_service_tvars under the member loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        for group in service.device_group:
            for member_name in root.devices.device_group[group].member:
                some_service["device_name"]=member_name
                some_service_tvars=ncs.template.Variables()
		some_service_tvars.add('DEVICE_NAME',some_service['device_name'])
        	some_service_template = ncs.template.Template(service)
        	some_service_template.apply('some-service-template', some_service_tvars)&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Apr 2020 23:14:01 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4065014#M5095</guid>
      <dc:creator>indirakas</dc:creator>
      <dc:date>2020-04-13T23:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: NSO: Switching between context nodes</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4065540#M5098</link>
      <description>&lt;P&gt;Correct, you would need to loop in your code and repetitively call the template with a single set of variables.&lt;/P&gt;
&lt;P&gt;Just to elaborate here a bit, the variables passed to the template from your code (be it python, java, erlang..) to the template when the template is applied are passed as _strings_ not in some construct of the code language, such as lists, dictionaries, etc. (this would be quite difficult to implement given the variability of constructs in the different supported languages)&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 14:57:56 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-switching-between-context-nodes/m-p/4065540#M5098</guid>
      <dc:creator>lmanor</dc:creator>
      <dc:date>2020-04-14T14:57:56Z</dc:date>
    </item>
  </channel>
</rss>

