cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1513
Views
15
Helpful
6
Replies

Python post_modification - access service object

Gastonper
Level 1
Level 1

Hi,

Can someone give me an example of a service with a post_modification in python?

In particular I want to know two things:

  1. How to access the python object of the service (like "service." in cb_create).
  2. How to check if it is a create or delete operation.

 

The reason I need this is because I have a service with this particular behavior:

  • In YANG, a simple list with one key and some leafs.
  • When a new entry of the list is created, NSO send one command to the device with some parameters defined.
  • When that entry of the list is deleted, NSO send the same command, but with the same parameters as empty strings ("''").

Since this command is defined in the NED as an action, I cant do it naturally. Also, I need to make a distinction between create and delete operations.

 

With that in mind, my idea is to make a post_modification that check the operation and send an empty string in case of delete, or defined parameter in case of create.

 

1 Accepted Solution

Accepted Solutions

yfherzog
Cisco Employee
Cisco Employee

For the first part:

 

To get the service object which is available for the create callback:

service = ncs.maagic.cd(root, kp)

 

For the operation, something like that:

if op == _ncs.dp.NCS_SERVICE_CREATE:

 

I don't think I fully understand the use case however and whether this is the way to go.

View solution in original post

6 Replies 6

yfherzog
Cisco Employee
Cisco Employee

For the first part:

 

To get the service object which is available for the create callback:

service = ncs.maagic.cd(root, kp)

 

For the operation, something like that:

if op == _ncs.dp.NCS_SERVICE_CREATE:

 

I don't think I fully understand the use case however and whether this is the way to go.

Thanks,

 

Using "service = ncs.maagic.cd(root, kp)" works, but for some reason I cant get te correct operation code.

 

Regardless of the opration I always get the code "op=1", but NCS_SERVICE_CREATE=0 and NCS_SERVICE_DELETE=2.

 

Any idea of what is happening?

It should be an update operation then...

 

NCS_SERVICE_CREATE = 0,
NCS_SERVICE_UPDATE = 1,
NCS_SERVICE_DELETE = 2

 

best regards

The problem is that I always get the code 1, and I need to differentiate between the operations.

 

This is part of my python post_mod:

 

 

self.log.info('op:   ', op)

 

And when I do:

admin@ncs(config)# no services ...

 

I get this in the log:

- op:   1

 

Same when I create a service or update one.

 

I've just tested here and it worked fine...

 

Creating a new service and then commit...

<INFO> 27-Feb-2020::19:37:38.749 vpnuser ncs-dp-55125-vpnuser:main-1-th-2343: - Service create(service=/ncs:services/vpnuser:vpnuser{test123})
<INFO> 27-Feb-2020::19:37:38.780 vpnuser ncs-dp-55125-vpnuser:main-1-th-2343: - op = 0 

 

Modifying a service attribute and commit...

<INFO> 27-Feb-2020::19:38:22.612 vpnuser ncs-dp-55125-vpnuser:main-1-th-2389: - Service create(service=/ncs:services/vpnuser:vpnuser{test123})
<INFO> 27-Feb-2020::19:38:22.623 vpnuser ncs-dp-55125-vpnuser:main-1-th-2389: - op = 1

 

Deleting the service vpnuser{test123}:

<INFO> 27-Feb-2020::19:43:29.423 vpnuser ncs-dp-55125-vpnuser:main-2-th-2411: - op = 2

 

The code was:

@Service.post_modification
def cb_post_modification(self, tctx, op, kp, root, proplist):
# self.log.info('Service premod(service=', kp, ')')
self.log.info('op = ', op)

It seems to be working as expected...

This test was done with NSO 5.1.0.1

 

Best regards

I tested with  a dummy services in 4.7 and works too. I think I know what the problem is.

 

My service has the following structure in Yang:

 

list serviceA {
   key "code";
   ncs:servicepoint "serviceA";
   uses ncs:service-data;

    leaf code {
         type string;
     }		
     ...

    list listB {
        key "id";

        leaf id {
            type uint16;
        }
	...	
    }
}			

When I create, delete or update elements in listB, for NSO those are updates of servicesA. Thats why I always get the code "op=1".

 

I will have to modify my service.

 

Thanks for the help.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: