cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1380
Views
1
Helpful
6
Replies

Python Usage of maapi_shared functions inside FASTMAP

Maan Al Bachari
Level 1
Level 1

Hi Team,

I’m trying to better understand the following:

  • how to use MAAPI functions inside cb_create within service packages, how to define maapi object/session..etc with the existing parameters NCS is passing when the functions is called. I cannot seem to find an examples on that, apologies if I missed it
  • how to use shared functions, such shared_copy_tree..my understanding we need to use the shared methods inside FASTMAP and not the normal ones

I get the following error  when running my code:

Error: Operation was aborted (49): Operation on: /ncs:devices/device{xr1}/config/cisco-ios-xr:class-map{af} failed because: sharedCreate() called outside fastmap

Here is the code snippet:

# -*- mode: python; python-indent: 4 -*-

import ncs

from ncs.application import Service

class ServiceCallbacks(Service):

     @Service.create

     def cb_create(self, tctx, root, service, proplist):

          self.log.info('Service create(service=', service._path, ')')

          srcPE = service.src_device

          srcGePhyPort = service.src_GigE_physint

          destPE = service.dest_device

          destPort = service.dest_interface

          currentClass = 'af'

          m = ncs.maagic.get_maapi(root)

          t = m.attach(tctx.th)

          t.shared_copy_tree('/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (srcPE, currentClass), '/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (destPE, currentClass))

          template = ncs.template.Template(service)

          template.apply('service-template', vars)

To me the above function is inside FASTMAP since it’s in cb_create and using the same root and session transaction handler, what am I missing?

If I pass flag 0 the error I get back is bad formatted or non-existent path, the above works fine with copy_tree but not shared, which is problematic when I want to remove or modify the service

Please excuses any obvious mistakes, my python skills are very rudimental to say the least

Thanks!

1 Accepted Solution

Accepted Solutions

Dan, thanks so much for your help! Below is how we made it work:

instead of

m = ncs.maagic.get_maapi(root)

t = m.attach(tctx)

t.shared_copy_tree('/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (srcPE, currentClass), '/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (destPE, currentClass))

we used

t = ncs.maagic.get_trans(root)

t.shared_copy_tree('/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (srcPE, currentClass), '/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (destPE, currentClass))

no need to redefine a maapi object and attaching to a transaction, just use the existing transaction by root

Thanks!

View solution in original post

6 Replies 6

Dan.Sullivan
Cisco Employee
Cisco Employee

Hi Maan,

If you don't used share_copy_tree and must copy_tree does it do what you want?

-Dan

Hi Dan,

With copy_tree I can deploy the service and see the class-map in my example being copied from xr0 to xr1, however when I delete the service, it's doesn't remove the class-map from xr1. That's why I thought I need to use shared_copy_tree, as indicated in the documentation:

shared_copy_tree(*args, **kwargs)

FASTMAP version of copy_tree().

Does that make sense?

Thanks!

Hi Maan,

No, that's not really the purpose of 'shared'. In fact, shared sets are used to insure configuration changes can be handled correctly among 1 or more services which share some common configuration items. I'm not sure exactly what is going on with your service. I'll take a closer look.

-Dan

Hi Maan,

Can you post the service modifications once your service is configured?

-Dan

Hi Dan,

admin@ncs(config)# services emergency_pe_migration_mx_mx 123 src-device xr0 src-GigE-physint 0/0/0/1 dest-device xr1 dest-interface 0/0/0/1

admin@ncs(config-emergency_pe_migration_mx_mx-123)# commit dry-run                                                                                                     cli {

    local-node {

        data  devices {

                  device xr1 {

                      config {

             +            cisco-ios-xr:class-map af {

             +                prematch match-any;

             +                match {

             +                    dscp {

             +                        dscp-list ef;

             +                    }

             +                }

             +            }

                      }

                  }

              }

              services {

             +    emergency_pe_migration_mx_mx 123 {

             +        src-device xr0;

             +        src-GigE-physint 0/0/0/1;

             +        dest-device xr1;

             +        dest-interface 0/0/0/1;

             +    }

              }

    }

}

admin@ncs(config-emergency_pe_migration_mx_mx-123)# commit

Commit complete.

admin@ncs(config-emergency_pe_migration_mx_mx-123)# exit

admin@ncs(config)# no services emergency_pe_migration_mx_mx 123

admin@ncs(config)# commit dry-run

cli {

    local-node {

        data  services {

             -    emergency_pe_migration_mx_mx 123 {

             -        src-device xr0;

             -        src-GigE-physint 0/0/0/1;

             -        dest-device xr1;

             -        dest-interface 0/0/0/1;

             -    }

              }

    }

}

This is with tree_copy , pong deleting the service, the changes earlier made on xr1 is not reverted.

Thanks!

Dan, thanks so much for your help! Below is how we made it work:

instead of

m = ncs.maagic.get_maapi(root)

t = m.attach(tctx)

t.shared_copy_tree('/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (srcPE, currentClass), '/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (destPE, currentClass))

we used

t = ncs.maagic.get_trans(root)

t.shared_copy_tree('/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (srcPE, currentClass), '/ncs:devices/device{%s}/config/cisco-ios-xr:class-map{%s}' % (destPE, currentClass))

no need to redefine a maapi object and attaching to a transaction, just use the existing transaction by root

Thanks!