cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1394
Views
5
Helpful
2
Replies

Python example to set operational data

tsiemers1
Spotlight
Spotlight

I can see in the development guide examples of setting operational data via java or the cli. Does anybody have an example of setting this via python? 

 

Is it possible to configure operational data during service creation? In this case, it is for use during re-deploy. We have a function that checks if a subinterface is already used on the router and adds 1 to find an unused one. This causes problems during re-deploy since it always wants to add 1 to the subinterface. I am trying to accomplish setting the config false leaf service_data to the assigned sub interface value during initial service creation. 

yang

 

Currently, have this going in the python but looking for a cleaner solution, still digging into the API docs but nothing is jumping out.

 

class ServiceCallbacks(Service):

    # 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, ')')

        test_var = 44
        proplist.append(('sub_int', str(test_var)))
        self.log.info(proplist)
        if int(proplist[0][1]) != 0:
            self.log.info(f"Service instance was set for {service.op_data}")
        return proplist

    @Service.pre_modification
    def cb_pre_modification(self, tctx, op, kp, root, proplist):
        self.log.info('Service premod(service=', kp, ')')
        proplist = [('deploy_status', str(op))]

        return proplist


    @Service.post_modification
    def cb_post_modification(self, tctx, op, kp, root, proplist):
        self.log.info('Service premod(service=', kp, ')')
        pattern = "{(.*?)}"
        service_name = re.search(pattern, str(kp)).group(1)
        self.log.info(service_name)
        self.log.info(proplist[1][1])
        root.test_op[service_name].op_data = proplist[1][1]

 

Then during re-deploy instead of running the custom function to find an unused sub interface ID it will use the value in the operational leaf service_data.

Is this possible or is it recommended?

2 Replies 2

rogaglia
Cisco Employee
Cisco Employee

Hi,

If you check your pre/post modification method, you have something called "op":

def cb_pre_modification(self, tctx, op, kp, root, proplist):

This will help you do your magic as the value is different for every operation:

"""
OP = 1: 'CREATED', 2: 'DELETED', 3: 'MODIFIED', 4: 'VALUE_SET', 5: 'MOVED_AFTER', 6: 'ATTR_SET'
"""
just print the value for the behavior you want to change.

In my case, I wanted a special behavior when not "deleting":

if op != 2:

   bla bla

 

Thanks. We ended up using the op in the pre_mod and setting some values into the proplist after some recommendations. This seems to do the trick since the proplist is persistent and holds the value to be used during re-deploy.