How to pass input parameters to request Action via python

Hi All,

I have an “reload” action to be triggered from my python code. The action expects two inputs as parameters:

±-ro actions
| | ±–x reload
| | | ±–w input
| | | | ±–w force? empty
| | | | ±–w noprompt? empty

I am opening a maapi session and using the request_action_th API to do the job. My doubt is, how to pass the params args which expects ‘force’ and ‘noprompt’ to my reload leaf via python?
Also, both leaves force and prompt have types as empty so I am confused.

My code:

   th = maapi.start_trans(sock, _ncs.RUNNING, _ncs.READ_WRITE)
   path = '/ncs:devices/device{netconfd}/live-status/hw-module/oper/location{lc_loc}/actions'
   ns_hash = _ncs.str2hash("")
   action_reload_hash = _ncs.str2hash('reload')
   action_force_hash = _ncs.str2hash('force')
   action_noprompt_hash = _ncs.str2hash('noprompt')
   params = []
   params += [_ncs.TagValue(_ncs.XmlTag(ns_hash, action_reload_hash), 
   _ncs.Value((action_reload_hash, ns_hash), _ncs.C_XMLBEGIN))]
   params += [_ncs.TagValue(ncs.XmlTag(ns_hash, action_force_hash), _ncs.Value(''))]
   params += [_ncs.TagValue(ncs.XmlTag(ns_hash, action_noprompt_hash), _ncs.Value(''))]
   params += [_ncs.TagValue(_ncs.XmlTag(ns_hash, 
  action_reload_hash),_ncs.Value((action_reload_hash, ns_hash), _ncs.C_XMLEND))]
  running = maapi.request_action_th(sock, th, params, path)


With the low-level API it needs to be a bit complicated (showing only the important parts):

ns_hash = _ncs.str2hash("http://your.module/uri")
path = '/ncs:devices/device{netconfd}/live-status/' \
force_value = _ncs.Value((ns_hash, action_force_hash), _ncs.C_XMLTAG)
noprompt_value = _ncs.Value((ns_hash, action_noprompt_hash), _ncs.C_XMLTAG)
params = [_ncs.TagValue(ncs.XmlTag(ns_hash, action_force_hash), force_value),
                 _ncs.TagValue(ncs.XmlTag(ns_hash, action_noprompt_hash), noprompt_value)]
maapi.request_action_th(sock, the, params, path)

So the important differences are:

  • the namespace is the namespace of your action, not of the root node
  • the action name is part of the path
  • the empty-typed leaves are represented as C_XMLTAG values; if you want to indicate that an empty-typed leaf is not present, you just omit it from the TagValue list params.

Minor improvement would be to use the generated namespace Python module instead of repeated _ncs.str2hash calls, so you would do something like

import hw_module_ns as ns
ns_hash = ns.ns.hash
action_reload_hash = ns.ns.hw_module_reload

Larger improvement would be to use the high-level API. So instead of opening a socket, a session, a transaction, then using all the stuff above you would do

from ncs import maapi, maagic
with maapi.single_write_trans(user, context) as trans:
    root = maagic.get_root(trans)
    action = root.devices.device['netconfd'].live_status.hw_module.oper.location['lc_loc'].actions.reload
    params = action.get_input()

(You may also want to check if how you are opening the transaction corresponds to what you need to do - if it really needs to be READ_WRITE towards RUNNING.)

1 Like

Thanks, I used the high level API and it worked!