<?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 NSO Python create nested service call in NSO Developer Hub Discussions</title>
    <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827699#M7802</link>
    <description>&lt;P&gt;Is it possible for an nso (python) service to call an other nso (python) service?&lt;/P&gt;&lt;P&gt;For instance:&lt;/P&gt;&lt;P&gt;service X -&amp;gt; service Y&lt;/P&gt;&lt;P&gt;I was looking at&amp;nbsp;&lt;SPAN&gt;cb_post_modification, but it looks like it doesn't&amp;nbsp;&lt;SPAN&gt;achieve&amp;nbsp;&lt;/SPAN&gt;the desired result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Example python code of service X:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class ServiceCallbacks(Service):
    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create
    def cb_create(self, tctx, root, service, proplist):
        self.log.info('Service create(service=', service._path, ')')

        template = ncs.template.Template(service)
        template_vars = ncs.template.Variables()
        
    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.post_modification
    def cb_post_modification(self, tctx, op, kp, root, proplist):
        self.log.info('Service postmod(service=', kp, ')')
        service = ncs.maagic.cd(root, kp)
        leafref = root.services.X[service.X]
        with ncs.maapi.single_write_trans('admin', 'python') as t:
                root = ncs.maagic.get_root(t)
                root.services.Y.create(leafref)
                t.apply()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Also because service Y uses a leafref, i am not sure how it should be referenced&lt;/P&gt;</description>
    <pubDate>Wed, 03 May 2023 13:45:07 GMT</pubDate>
    <dc:creator>ArneV</dc:creator>
    <dc:date>2023-05-03T13:45:07Z</dc:date>
    <item>
      <title>NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827699#M7802</link>
      <description>&lt;P&gt;Is it possible for an nso (python) service to call an other nso (python) service?&lt;/P&gt;&lt;P&gt;For instance:&lt;/P&gt;&lt;P&gt;service X -&amp;gt; service Y&lt;/P&gt;&lt;P&gt;I was looking at&amp;nbsp;&lt;SPAN&gt;cb_post_modification, but it looks like it doesn't&amp;nbsp;&lt;SPAN&gt;achieve&amp;nbsp;&lt;/SPAN&gt;the desired result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Example python code of service X:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class ServiceCallbacks(Service):
    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create
    def cb_create(self, tctx, root, service, proplist):
        self.log.info('Service create(service=', service._path, ')')

        template = ncs.template.Template(service)
        template_vars = ncs.template.Variables()
        
    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.post_modification
    def cb_post_modification(self, tctx, op, kp, root, proplist):
        self.log.info('Service postmod(service=', kp, ')')
        service = ncs.maagic.cd(root, kp)
        leafref = root.services.X[service.X]
        with ncs.maapi.single_write_trans('admin', 'python') as t:
                root = ncs.maagic.get_root(t)
                root.services.Y.create(leafref)
                t.apply()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Also because service Y uses a leafref, i am not sure how it should be referenced&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 13:45:07 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827699#M7802</guid>
      <dc:creator>ArneV</dc:creator>
      <dc:date>2023-05-03T13:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827750#M7803</link>
      <description>&lt;P&gt;Hi, I am not sure I follow exactly what you are trying to achieve but I will try to answer anyway.&lt;/P&gt;
&lt;P&gt;One service creating another service is something we usually call a stacked service.&lt;/P&gt;
&lt;P&gt;See the following example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;    leaf device {
      type leafref {
        path "/ncs:devices/ncs:device/ncs:name";
      }
    }

    leaf dummy {
      type inet:ipv4-address;
    }
&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create
    def cb_create(self, tctx, root, service, proplist):
        self.log.info('Service create(service=', service._path, ')')

        servicey = root.servicey.create(service.name + '-created-y')
        servicey.dummy = '127.0.0.1'
        servicey.device = 'c0'
&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;admin@ncs% commit dry-run
cli {
    local-node {
        data +servicey test-service-created-y {
             +    device c0;
             +    dummy 127.0.0.1;
             +}
             +servicex test-service {
             +}
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here ServiceX will create ServiceY inside the create callback. The leafref to the device is referenced by name "c0". If there is no such device the commit will fail with "Aborted: illegal reference 'servicey test-service-created-y device'&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 14:52:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827750#M7803</guid>
      <dc:creator>mmollber</dc:creator>
      <dc:date>2023-05-03T14:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827921#M7805</link>
      <description>&lt;P&gt;Creation seems to work perfect.&lt;/P&gt;&lt;P&gt;Only the deletion of the stacked service doesn't seems to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2023 18:31:34 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4827921#M7805</guid>
      <dc:creator>ArneV</dc:creator>
      <dc:date>2023-05-03T18:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828183#M7807</link>
      <description>&lt;P&gt;What does your code look like now?&lt;/P&gt;
&lt;P&gt;This is what you should see when deleting servicex.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;admin@ncs% delete servicex test-service
[ok][2023-05-04 08:13:27]

[edit]
admin@ncs% commit dry-run
cli {
    local-node {
        data -servicey test-service-created-y {
             -    device c0;
             -    dummy 127.0.0.1;
             -}
             -servicex test-service {
             -}
    }
}
[ok][2023-05-04 08:13:28]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 06:14:24 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828183#M7807</guid>
      <dc:creator>mmollber</dc:creator>
      <dc:date>2023-05-04T06:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828284#M7808</link>
      <description>&lt;P&gt;Just tested, if i delete it from the ncs_cli, it can be deleted.&lt;/P&gt;&lt;P&gt;But if i delete it from the GUI (NSO 5.6.1), then it gives a reference error. (a bug maybe?)&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 09:05:06 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828284#M7808</guid>
      <dc:creator>ArneV</dc:creator>
      <dc:date>2023-05-04T09:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828371#M7809</link>
      <description>&lt;P&gt;It should work for both, I did not see any issues using the GUI.&lt;/P&gt;
&lt;P&gt;I saw that you used post_modification in your first post and just want to mention that configuration set in post_mod (and pre_mod) is not automatically removed when the service is deleted. Those callbacks are handled differently.&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 09:31:28 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828371#M7809</guid>
      <dc:creator>mmollber</dc:creator>
      <dc:date>2023-05-04T09:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828476#M7810</link>
      <description>&lt;P&gt;Config now looks like this, already removed the&amp;nbsp;&lt;SPAN&gt;post_modification&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;Yang service X&lt;/P&gt;&lt;LI-CODE lang="python"&gt;key "name";
      leaf name{
        type string;
      }&lt;/LI-CODE&gt;&lt;P&gt;Python Service X&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class ServiceCallbacks(Service):

    &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/9199"&gt;@service&lt;/a&gt;.create
    def cb_create(self, tctx, root, service, proplist):
        self.log.info('Service create(service=', service._path, ')')

        template = ncs.template.Template(service)
        template_vars = ncs.template.Variables()
        root.services.Y.create(service.X)&lt;/LI-CODE&gt;&lt;P&gt;Yang service Y&lt;/P&gt;&lt;LI-CODE lang="python"&gt;      key name ;
      leaf name {
                type leafref {
                  path "/ncs:services/X:X/X:name";
              }
      }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 11:11:04 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828476#M7810</guid>
      <dc:creator>ArneV</dc:creator>
      <dc:date>2023-05-04T11:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828523#M7811</link>
      <description>&lt;P&gt;In this case the GUI is trying to be helpful by validating the changes before doing commit. You will see the same thing in the CLI if you do "validate".&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;admin@ncs% commit dry-run
cli {
    local-node {
        data -servicey newservice {
             -}
             -servicex newservice {
             -}
    }
}&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;admin@ncs% validate
Failed: illegal reference 'servicey newservice name'
[error][2023-05-04 13:38:05]&lt;/LI-CODE&gt;
&lt;P&gt;And this isn't incorrect since ServiceY requires a ServiceX to exist, but behaves differently in the CLI because it doesn't prevent the commit the way the GUI does. In order to remove this dependency you can use a non-strict leafref. &lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;leaf name {
      type string;
      tailf:non-strict-leafref {
        path "/ncs:services/X:X/X:name";
      }
    }
&lt;/LI-CODE&gt;
&lt;P&gt;Since ServiceY has a dependency on ServiceX these types of issues can occur.&lt;/P&gt;
&lt;P&gt;I am curious why you want to have a leafref to ServiceX from ServiceY and if there is an alternative solution to your problem. These kinds of circular dependencies can be difficult to work with.&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 12:05:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828523#M7811</guid>
      <dc:creator>mmollber</dc:creator>
      <dc:date>2023-05-04T12:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828551#M7812</link>
      <description>&lt;P&gt;Yes you can.&lt;/P&gt;
&lt;P&gt;But you should not be creating a separate transaction. Just create the service directly in the create callback if you want to do it in python, or alternatively and I think typically better, I would add the service that needs to be created into the template of the X service.&lt;/P&gt;
&lt;P&gt;A leafref is just a leaf so it should just be referenced like any leaf. Setting the type as&amp;nbsp; leafref just constrains its value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 May 2023 12:46:25 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4828551#M7812</guid>
      <dc:creator>snovello</dc:creator>
      <dc:date>2023-05-04T12:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: NSO Python create nested service call</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4830644#M7814</link>
      <description>&lt;P&gt;With the non-strict-leafref it works perfectly.&lt;/P&gt;&lt;P&gt;For us its important service X is an service which only exists in the cdb and relates to different services.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Except in some cases there is a little bit of configuration needed, and we don't want to create 1 very complex service.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 08:12:50 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/nso-python-create-nested-service-call/m-p/4830644#M7814</guid>
      <dc:creator>ArneV</dc:creator>
      <dc:date>2023-05-08T08:12:50Z</dc:date>
    </item>
  </channel>
</rss>

