10-11-2019 03:34 AM - edited 10-11-2019 03:35 AM
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?
Solved! Go to Solution.
10-11-2019 09:06 AM
10-11-2019 09:06 AM
10-14-2019 05:21 AM
[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!
10-14-2019 07:05 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide