cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
727
Views
10
Helpful
2
Replies

Python: Access persistent operational data within code

tsiemers1
Spotlight
Spotlight

I have a created the following leaf in a yang

 

leaf allocated-service-instance {
      type uint32;
      config false;
      tailf:cdb-oper {
            tailf:persistent true;
         }
    }

In the code how do you reference this during a re-deploy?

if int(proplist[0][1]) != 0:            
print(root.nameOfService['key'].allocated_service_instance) print service.allocated_service_instance)

During the initial commit of this service the allocated_service_instance is set using a dynamic instance generated in code. I am trying to call this leaf during re-deploy to ensure it uses the existing instance that was generated. 

 

2 Replies 2

tsiemers1
Spotlight
Spotlight

Was able to figure this out. After reading you need to grab this value during pre_mod I think, not 100% on that. Then send the value in the service create within the proplist

 

@Service.pre_modification
    def cb_pre_modification(self, tctx, op, kp, root, proplist):
        self.log.info('Service premod(service=', kp, ')')
        self.log.info(f"python - user_info - PREMOD PROPLIST; {proplist}")
        info_from_kp = re.findall(r'{[A-Z0-9]+\.*[0-9]*}', str(kp))
        keys_in_kp = [x.replace('{', '').replace('}', '') for x in info_from_kp]
        circuit_key = keys_in_kp[0]
        proplist = [('deploy_status', str(op), circuit_key)]
        self.log.info(proplist[0][2])
        return proplist

Kind of weird that you have to regex out the service key(s). Anybody know of a way to pull keys send into a service without regexing out the kp?

 

Pulled the regex from this topic

with ncs.maapi.single_read_trans("admin", "python") as t:
    path = ncs.maagic.get_node(t, kp)
    id = path._parent.id. # depends on your model

You may get a service or action path and navigate from it. In my example, one step up to 'id' leaf.