cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
892
Views
0
Helpful
1
Replies

NSO 6.1 Python API: How to get hash tag in get_object()?

JennyW
Level 1
Level 1

First of all, I went through the post Python DataCallback daemon  but my case is more complicated than this.

I have a yang module defined as

 

module pydp {
  namespace "http://tail-f.com/ns/example/pydp";
  prefix pydp; yang-version 1.1;

  import tailf-common {prefix tailf;}
  import ietf-inet-types {prefix inet;}

  list cont {
    key name;
    leaf name {
      type string;
    }
    container brief {
      tailf:callpoint mydp-cp;
      config false;
      leaf status {
        type string;
     }
     leaf up-time {
       type string;
     }
     list device {
       key device-name;
       leaf device-name {
         type string;
       }
       leaf mac {
         type string;
         description "MAC address of the device";
       }
     } 
     leaf-list ds-name{
        type string; 
     }
     leaf-list us-name {
       type string;
     }
  }
}

 

Python:

 

import ncs 
import _ncs 
from ncs.experimental import DataCallbacks 

def dcb_start(state): 
  dcb = DataCallbacks(state.log)
  dcb.register('/mydp:container', Handler())
  _ncs.dp.register_data_cb(state.ctx, 'mydp-cp', dcb)
  return state


def dcb_stop(start_state):
    pass


class Handler(object): 
    def get_object(self, tctx, kp, args): 
      cont_name = args.get("cont")
      with ncs.maapi.single_read_trans(tctx.uinfo.username, "passwd") as t: 
        root = ncs.maagic.get_root(t) 
        key = root.cont[device_name] 
        .... 
        return { 
          "brief": { 
            'status': 'value', 
            'up-time': 'value', 
            'device': {'device-name': 'test', 'mac': 'value'}, 
            'ds-name": ['name11', 'name12'], 
            'us-name": ['name21', 'name22'], } 


    def get_next(self, tctx, kp, args, next): 
      return None 


    def count(self): 
      return 0 


class Main(ncs.application.Application): 
  def setup(self): 
    ...
    self.register_fun(dcb_start, dcb_stop) 


  def teardown(self): 
    pass

 

 

 

I noticed that get_object() was invoked multiple times, guess it's because list/leaflist are supposed to be processed separately. If that's the case how can I get the hash tag from kp? I don't see class HKeyPathRef provides a method to get the hash tag. The NSO Python API is poorly documented and I couldn't find any example of using python Dp callback to implement oper commands callback. Any help will be appreciated.

Because the config/action callbacks were implemented in python, we'd like to use Python for oper callback as well.

1 Accepted Solution

Accepted Solutions

JennyW
Level 1
Level 1

Ok, I figured it out myself: use kp[idx].tag for hashed tag. For each leaflist/list one needs to return different object, in the format of {'brief': {'ds-name': ['name11', 'name12']}} (for leaflist 'ds-name').

View solution in original post

1 Reply 1

JennyW
Level 1
Level 1

Ok, I figured it out myself: use kp[idx].tag for hashed tag. For each leaflist/list one needs to return different object, in the format of {'brief': {'ds-name': ['name11', 'name12']}} (for leaflist 'ds-name').

Polls
AI-powered tools for network troubleshooting are likely to be part of everyone’s workflow sooner or later. What is the single biggest challenge or concern you see with adopting these tools in your organization?