cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2146
Views
15
Helpful
8
Replies

Applying templates from action

yfherzog
Cisco Employee
Cisco Employee

Hi,

 

I'm writing an action (Python) that, among other things, is going to create a list entry in CDB that has a very similar data model as in the action input.

 

I was hoping to use a template that will map the inputs directly, using xpath, from the action input to the target model, but I'm struggling with providing the right context when instantiating the template object - if I do something like 'template = ncs.template.Template(input)', (input being the cb_action argument with the same name) it seems as I'm getting None in return.

 

Is there a way to use the action inputs as the template's context or any other way to allow me to directly use the action inputs in such template (instead of setting each variable in the python code)?

 

Thanks,

Yftach

1 Accepted Solution

Accepted Solutions

frjansso
Cisco Employee
Cisco Employee

Hi!

 

Unfortunately there is no way of accessing the action variables directly from a template. The reason is that the parameters supplied to an action are never stored in CDB, but just passed directly to the function callback. So the parameters and never visible to the XPATH engine, which operates on CDB.

View solution in original post

8 Replies 8

frjansso
Cisco Employee
Cisco Employee

Hi!

 

Unfortunately there is no way of accessing the action variables directly from a template. The reason is that the parameters supplied to an action are never stored in CDB, but just passed directly to the function callback. So the parameters and never visible to the XPATH engine, which operates on CDB.

Alright. Thank you for confirming!

With your answer in mind, is there a way to systematically copy the action inputs into CDB?

As far as I understand, the copy_tree() don't support action inputs, and I've been also trying to implement something simple myself, but I'm running into some issues where trying to traverse some of the data types with action inputs - Namely, list keys and choice statements.

 

Anyone have an example of anything generic already implemented for that purpose?

 

Thanks,

Yftach

Yftach,

 

In the Java API I used to use a maapi method called 'setValues' to copy the action params into the CDB service elements.

I believe the action input parameters needed to be in the same yang tree format as the destination CDB service yang.

 

// Create new Datacenter instance
// Copy Datacenter info from input parameters - setValues will do create
String topUpdatePath = "/dc:topology-updates";
maapi.setValues(th, params, new ConfPath(topUpdatePath));

 

I've not tried this in the python API, just perhaps something to take a look at...

 

-Larry

 

 

 

Thank you Larry!

 

Just tried it now, and this seems to be more or less exactly what I had in mind!

I see Python maapi method with a similar name shared_set_values(), but couldn't find examples of how to use it.

Still looking to see if similar thing can be done in Python, but if not, I think the method you suggested here is a good option!

 

Yftach

I've tried using the _ncs.maapi set_values(sock, thandle, values, keypath) method which appears relatively similar to the Java setValues() method.

In the documentation, I see that 'values' needs to be a list of tagValues, which I imagine is the equivalent of the ConfXMLParam[] the Java method takes. But while the action input params in Java are provided as ConfXMLParam[], the Python action inputs are a maagic node.

 

Any easy-enough way to convert the action input parameters to tagValues?

 

As you can see in dp.Action.action, the maagic node gets created by using maagic.ActionParams._from_tagvalues. There is a sibling method _tagvalues which does what you need. It is obviously not part of the public API, so it's up to you if you dare to use it in your code; but I'd say a method like this will be always there so that this high-level API can send data to the underlying C library. (Or you can take the code and copy-paste it to yours, it's just few lines - but then again, it uses non-public _children attribute.)

I'll try looking into that option.
Thank you!