11-28-2018 12:34 AM
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
Solved! Go to Solution.
11-28-2018 07:48 AM
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.
11-28-2018 07:48 AM
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.
11-28-2018 10:28 PM
12-04-2018 01:07 AM
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
12-05-2018 07:17 AM
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
12-05-2018 11:42 PM
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
12-09-2018 02:51 AM
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?
12-11-2018 01:23 AM
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.)
12-11-2018 02:23 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide