06-14-2022 02:36 AM - edited 06-14-2022 02:38 AM
Hello,
is there any one had the problem when try to create anew route policy for the NCS5500 XR netconf via Python in NSO?
i try to put some logic in python till can can create new route policy as like below example :
route-policy EBGP_PE1_IN
if extcommunity rt matches-any (65535:400400) then
set extcommunity rt (65102:104104) additive
set community (1:1) additive
set local-preference 300
pass
elseif extcommunity rt matches-any (65535:400200) then
set extcommunity rt (65102:102003) additive
set extcommunity rt (65102:102004) additive
set community (1:1) additive
pass
endif
pass
end-policy
but the problem that when try to deploy the package , it seems created on NSO but without devices and no configuration deployed on the device .
so is there any one know why the package does not deploy on the devices ?
Solved! Go to Solution.
06-16-2022 02:55 AM
06-14-2022 06:44 AM
Hello,
Can you put your python code ? You can enable trace on the device that might help you to find the root cause.
06-14-2022 07:08 AM
here are my python code
def cb_create(self, tctx, root, service, proplist):
self.log.info('Service create(service=', service._path, ')')
self.log.info(service.device)
# starting with empty value
val = ''
# looping the list of extcommunity
#for comm in service.device:
DEVs = []
for DEV in service.device:
DEVs.append(DEV.dev)
self.log.info(DEVs)
for rpl in DEV.route_policies:
for comm in rpl.extcommunity:
self.log.info("{} {} {} ".format(type(service.device),len(service.device),comm))
val += f"""route-policy {rpl.route_policy_name}
if extcommunity rt matches-any ({comm.rt_match_as}:{comm.rt_match_id1}) then
set extcommunity rt ({comm.bgp_rt_set_local_as}:{comm.bgp_rt_set_id1}) additive
set community ({comm.bgp_community_as}:{comm.bgp_community_id}) additive
set local-preference {comm.bgp_local_preference}
pass
elseif extcommunity rt matches-any ({comm.rt_match_as}:{comm.rt_match_id2}) then
set extcommunity rt ({comm.bgp_rt_set_local_as}:{comm.rt_set_id2}) additive
set extcommunity rt ({comm.bgp_rt_set_local_as}:{comm.rt_set_id3}) additive
set community ({comm.bgp_community_as}:{comm.bgp_community_id}) additive
pass
endif
pass
end-policy\n"""
self.log.info(val)
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session(m, 'admin', 'python'):
with ncs.maapi.single_write_trans('admin', 'python', groups=['ncsadmin']) as t:
root = ncs.maagic.get_root(t)
for dev in DEVs:
self.log.info(dev)
device_list = root.devices.device[dev]
self.log.info("Device to read : " + device_list.name)
RPL = device_list.config.Cisco_IOS_XR_policy_repository_cfg__routing_policy.route_policies.route_policy
RPL_create = RPL.create(rpl.route_policy_name).rpl_route_policy
self.log.info("create new RPL : " + rpl.route_policy_name)
vars = ncs.template.Variables()
vars.add('VAL', val)
template = ncs.template.Template(service)
template.apply('route_policy_config-template', vars)
self.log.info("deploy service ")
06-14-2022 07:57 AM
Can you reformat your code ?
06-14-2022 08:32 AM
How i can do that ?
06-14-2022 11:12 AM
You have to click on insert code
Concerning your code , you made some mistake. You create the RPL but you in a transaction that you had never apply ( commit) so the RPL is not created. If you try to add t.apply() it will fails because you are inside cb_create so you already acquire cdb lock.
I suggest you to create your RPL with a template and you apply your template inside your loop so you can create all the RPL that you want instead of doing
with ncs.maapi.Maapi() as m: with ncs.maapi.Session(m, 'admin', 'python'): with ncs.maapi.single_write_trans('admin', 'python', groups=['ncsadmin']) as t: root = ncs.maagic.get_root(t) for dev in DEVs: self.log.info(dev) device_list = root.devices.device[dev] self.log.info("Device to read : " + device_list.name) RPL = device_list.config.Cisco_IOS_XR_policy_repository_cfg__routing_policy.route_policies.route_policy RPL_create = RPL.create(rpl.route_policy_name).rpl_route_policy self.log.info("create new RPL : " + rpl.route_policy_name)
It would be something like this
vars = ncs.template.Variables() template = ncs.template.Template(service) for dev in DEVs: self.log.info(dev) vars.add("DEVICE",dev) vars.add("RPL",rpl.route_policy_name) self.log.info("create new RPL : " + rpl.route_policy_name)
06-16-2022 01:16 AM
Hello
it is worked fine now after that change , many thanks
06-16-2022 02:55 AM
Hello ,
Since it’s working .Can you mark it as solved ?
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