- interface = kwargs.get('interface', None)
- gateway = kwargs.get('gateway', None)
-
- try:
- net_addr = IPv6Address(unicode(network))
- af_inet = AF_INET6
- is_ipv6 = 1
- except (AddressValueError, NetmaskValueError):
- net_addr = IPv4Address(unicode(network))
- af_inet = AF_INET
- is_ipv6 = 0
-
- if gateway:
- try:
- gt_addr = IPv6Address(unicode(gateway))
- af_inet_gt = AF_INET6
- except (AddressValueError, NetmaskValueError):
- gt_addr = IPv4Address(unicode(gateway))
- af_inet_gt = AF_INET
-
- cmd = 'ip_add_del_route'
- args = dict(
- next_hop_sw_if_index=InterfaceUtil.get_interface_index(
- node, interface) if interface else Constants.BITWISE_NON_ZERO,
- table_id=int(kwargs.get('vrf', 0)),
- is_add=1,
- is_ipv6=is_ipv6,
- is_local=int(kwargs.get('local', False)),
- is_multipath=int(kwargs.get('multipath', False)),
- next_hop_weight=int(kwargs.get('weight', 1)),
- next_hop_proto=1 if is_ipv6 else 0,
- dst_address_length=int(prefix_len),
- next_hop_address=inet_pton(af_inet_gt, str(gt_addr)) if gateway
- else 0,
- next_hop_table_id=int(kwargs.get('lookup_vrf', 0)))
- err_msg = 'Failed to add route(s) on host {host}'.format(
- host=node['host'])
- with PapiExecutor(node) as papi_exec:
- for i in xrange(kwargs.get('count', 1)):
- papi_exec.add(cmd, dst_address=inet_pton(
- af_inet, str(net_addr+i)), **args)
- papi_exec.get_replies(err_msg).verify_replies(err_msg=err_msg)
+ interface = kwargs.get(u"interface", u"")
+ gateway = kwargs.get(u"gateway", u"")
+
+ net_addr = ip_address(network)
+
+ prefix = IPUtil.create_prefix_object(net_addr, prefix_len)
+
+ paths = list()
+ n_hop = dict(
+ address=IPUtil.union_addr(ip_address(gateway)) if gateway else 0,
+ via_label=MPLS_LABEL_INVALID,
+ obj_id=Constants.BITWISE_NON_ZERO
+ )
+ path = dict(
+ sw_if_index=InterfaceUtil.get_interface_index(node, interface)
+ if interface else Constants.BITWISE_NON_ZERO,
+ table_id=int(kwargs.get(u"lookup_vrf", 0)),
+ rpf_id=Constants.BITWISE_NON_ZERO,
+ weight=int(kwargs.get(u"weight", 1)),
+ preference=1,
+ type=getattr(
+ FibPathType, u"FIB_PATH_TYPE_LOCAL"
+ if kwargs.get(u"local", False)
+ else u"FIB_PATH_TYPE_NORMAL"
+ ).value,
+ flags=getattr(FibPathFlags, u"FIB_PATH_FLAG_NONE").value,
+ proto=getattr(
+ FibPathNhProto, u"FIB_PATH_NH_PROTO_IP6"
+ if net_addr.version == 6
+ else u"FIB_PATH_NH_PROTO_IP4"
+ ).value,
+ nh=n_hop,
+ n_labels=0,
+ label_stack=list(0 for _ in range(16))
+ )
+ paths.append(path)
+
+ route = dict(
+ table_id=int(kwargs.get(u"vrf", 0)),
+ prefix=prefix,
+ n_paths=len(paths),
+ paths=paths
+ )
+ return route