cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1079
Views
0
Helpful
3
Replies

Finding package name (not YANG module name) from Python code in service.

anderslindback
Level 1
Level 1

So, in theory a simple problem, I need to find out what NSO package some python code is running from, not the Yang model or service name, but what the actual package name is in NSO, from the python code, but is seems hard, if not impossible to do.

 

This is in a shared library for python used by many services and packages, that depending on the contents in the package data structures in root.ncs__packages.package[package_name] needs to do different things.

 

Trivial example:

package_name = get_package_name()
service_template_list = self.root.ncs__packages.package[package_name].templates

An implementation of "get_package_name()" that works regardless of what shenanigans you have in the Yang model(s), multiple python components etc.

 

So, anyone have any ideas?

1 Accepted Solution

Accepted Solutions

vleijon
Cisco Employee
Cisco Employee
That is a fun question. First of all, let me say that generally your code should not need to know which package it is in, if there are choices to be made they should generally be made based on the yang module you are in. But, I do realize there are exceptions to this.

So, I thought about this for a bit, and the package name is actually an argument to the python vm (-i package-name), so if you look in sys.argv you can find the package.

View solution in original post

3 Replies 3

vleijon
Cisco Employee
Cisco Employee
That is a fun question. First of all, let me say that generally your code should not need to know which package it is in, if there are choices to be made they should generally be made based on the yang module you are in. But, I do realize there are exceptions to this.

So, I thought about this for a bit, and the package name is actually an argument to the python vm (-i package-name), so if you look in sys.argv you can find the package.

gmuloche
Cisco Employee
Cisco Employee

[Note: Experimental]

 

I had a look a the python-vm implementation it seems that adding this name in your Main will print the package name in the logs.

class Main(ncs.application.Application):
    def setup(self):
        # The application class sets up logging for us. It is accessible
        # through 'self.log' and is a ncs.log.Log instance.
        self.log.info('Main RUNNING')
        self.log.info('Package name:', self._ncs_pname)

Now I tried several ideas to pass this name to the cb_create which sounds to be what you want but they may not be "very clean"

1. make this a global variable (I tried it out by setting it in Main and using it in cb_create and this worked but well - that makes a global variable and it may not cover everything you need)

2. pass it as an init argument in the register_service function (leveraging the def init(self, init_args) function of the Service class)

class ServiceCallbacks(Service):

      def init(self, init_args=None):
          self.package_name = init_args
          self.log.warning("Package Name: ", self.package_name)

     # The create() callback is invoked inside NCS FASTMAP and
      # must always exist.
      @Service.create
      def cb_create(self, tctx, root, service, proplist):
          self.log.info('Service create(service=', service._path, ')')
          self.log.warning("CB create Package Name: ", self.package_name)

  class Main(ncs.application.Application):
      def setup(self):
          self.log.info('Main RUNNING')
          self.register_service('test-servicepoint', ServiceCallbacks, init_args=self._ncs_pname)

Logs (the First WARNING is from the init where I set the variable for the Service, the second when I commit a new instance of the service):

<INFO> 14-Oct-2019::14:01:47.603 test MainThread: - Python 2.7.14 (default, Apr 25 2018, 15:30:04) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
<INFO> 14-Oct-2019::14:01:47.603 test MainThread: - Starting...
<INFO> 14-Oct-2019::14:01:47.605 test MainThread: - Started
<INFO> 14-Oct-2019::14:01:47.697 test ComponentThread:main: - Main RUNNING
<WARNING> 14-Oct-2019::14:01:47.700 test ncs-dp-1370-test:main: - Package Name: test
<INFO> 14-Oct-2019::14:01:53.395 test ncs-dp-1370-test:main-1-th-669: - Service create(service=/test:test{667})
<WARNING> 14-Oct-2019::14:01:53.395 test ncs-dp-1370-test:main-1-th-669: - CB create Package Name: test

It looks like _ncs_pname corresponds to the package name and there is a cname that may be the component ( I have checked with a couple of my packages and it seems to work)

 

3. Probably some other method I did not think about.. 

 

Hope that can get you started, good luck!

I should have added as criteria, without changing existing Service code.