The service should use python code to generate the absolute SID and NET address. The service should take the device name as the key, and an index parameter. Restrict the index to a number from 1-1000.
class ServiceCallbacks(Service):
# The create() callback is invoked inside NCS FASTMAP and
# must always exist.
@Service.create
def cb_create(self, tctx, root, service, proplist):
self.log.info('Service create(service=', service._path, ')')
vars = ncs.template.Variables()
vars.add('ABSOLUTE_INDEX', str(service.index + 16000))
hex_index = hex(service.index).replace('0x', '')
hex_with_leading_zeros = '0000'
hex_with_leading_zeros = hex_with_leading_zeros + str(hex_index)
hex_with_leading_zeros = hex_with_leading_zeros[-4:]
vars.add('HEX_INDEX', hex_with_leading_zeros)
template = ncs.template.Template(service)
template.apply('isis-base-template', vars)
YANG module:
module isis-base {
namespace "http://example.com/isis-base";
prefix isis-base;
import tailf-ncs {
prefix ncs;
}
revision 2016-01-01 {
description
"Initial revision.";
}
list isis-base {
description "Base ISIS config with SR-MPLS enabled";
key device;
uses ncs:service-data;
ncs:servicepoint isis-base-servicepoint;
leaf device {
type leafref {
path "/ncs:devices/ncs:device/ncs:name";
}
}
leaf index {
mandatory true;
type uint16 {
range "1..1000";
}
}
}
}
Explanation
The XML template is generated by applying the config on a single router, syncing the config in NSO and then using show running-config devices device xr2 config router isis | display xml
From there, the variables are inserted. $VAR is used for variables which come from python, and normal XPATH expressions are used for variables coming from the YANG module.
Next the YANG module is created. We only need two leafs for this service: device and index. The index is restricted to a value of 1-1000.
Finally, we write the python code. The main.py file is already generated automatically. We simply add 16000 to the index to produce the ABSOLUTE_INDEX variable. We can access our service variables using service.var, so service.index gives us the index value as entered by the user in NSO.
Obtaining the hex value is a little more complex. We can use python’s builtin hex() function, which returns “0x<value>”. We can strip away the “0x” and then pad the value with leading zeros using a string slice trick.
You should now be able to apply the service from NSO: