def rib_route_monitor(dev, channel_id, **kwargs): """ To monitor the routes in Junos rib. """ if 'table' in kwargs: table = kwargs.get('table') else: raise Exception("Mandatory parameter 'table' absent") if 'monitor_operation' in kwargs: monitor_operation = kwargs.get('monitor_operation') else: raise Exception("Mandatory parameter 'monitor_operation' absent") policy = kwargs.get('policy', 'export_static') monitor_context = int(kwargs.get('monitor_context', 100)) monitor_reply_count = int(kwargs.get('route_count', 1)) tablename = prpd.RouteTableName(name=table) fl_flag = prpd.RouteMonitorRegFlags(no_eor_to_client=1) route_policy_name = prpd.RouteMonitorPolicy(rt_monitor_policy=policy) if monitor_operation == 'REGISTER_ADD': flashreq = prpd.RouteMonitorRegRequest(rt_tbl_name=tablename, monitor_op=monitor_operation, monitor_flag=fl_flag,\ monitor_reply_route_count=monitor_reply_count, monitor_policy=route_policy_name, monitor_ctx=monitor_context) else: flashreq = prpd.RouteMonitorRegRequest(rt_tbl_name=tablename, monitor_op=monitor_operation, monitor_ctx=100) #rtFlashStream = Rib.RouteMonitorRegister(flashreq, timeout=20) route_flash_stream = prpd.process_api(dev, channel_id, "RouteMonitorRegister", [flashreq, "Rib"], kwargs.get("timeout", None)) return route_flash_stream
def rib_route_remove(dev, channel_id, **kwargs): """ delete already added static routes using execute_prpd_api. ** ONLY THE ROUTES ADDED BY A UNIQUE client_id CAN BE REMOVED """ dest_prefix_iterator = prpd.dest_prefix_limiter(**kwargs) if 'table' in kwargs: table = kwargs.get('table') if 'prefix_len' in kwargs: prefix_len = kwargs.get('prefix_len') result = [] for i in dest_prefix_iterator: dest_prefix = i rtkeys = [] for index in range(0, len(dest_prefix)): #Building the Key for RIB Route Params if 'inet6' in table: destprefix = prpd.NetworkAddress(inet6=prpd.IpAddress( addr_string=dest_prefix[index])) else: if 'mpls' in table: destprefix = prpd.NetworkAddress(mpls=prpd.MplsAddress( label=int(dest_prefix[index]))) else: destprefix = prpd.NetworkAddress(inet=prpd.IpAddress( addr_string=dest_prefix[index])) tablename = prpd.RouteTable(name=prpd.RouteTableName(name=table)) route_match_field = prpd.RouteMatch(dest_prefix=destprefix, dest_prefix_len=prefix_len, table=tablename) if 'cookie' in kwargs: if isinstance(kwargs["cookie"], int): route_match_field.cookie = kwargs.get('cookie') rtkeys.append(route_match_field) remreq = prpd.RouteDeleteRequest(keys=rtkeys) result.append( prpd.process_api(dev, channel_id, "RouteDelete", [remreq, "jnx_Rib"], kwargs.get("timeout", None))) return result
def rib_route_monitor(dev, channel_id, **kwargs): """ To monitor the routes in Junos rib. """ if 'table' in kwargs: table = kwargs.get('table') tablename = prpd.RouteTableName(name=table) else: raise Exception("Mandatory parameter 'table' absent") if 'monitor_operation' in kwargs: monitor_operation = kwargs.get('monitor_operation') else: raise Exception("Mandatory parameter 'monitor_operation' absent") policy = kwargs.get('policy', 'export_static') monitor_context = int(kwargs.get('monitor_context', 100)) monitor_reply_count = int(kwargs.get('route_count', 1)) fl_flag = prpd.RouteSubscribeFlags(no_eor_to_client=1) route_policy_name = prpd.RouteSubscribePolicy(policy=policy) if monitor_operation == 'SUBSCRIBE_ADDD': flashreq = prpd.RouteSubscribeRequest(table_name=tablename, operation=monitor_operation, flag=fl_flag,\ route_count=monitor_reply_count, policy=route_policy_name, context=monitor_context) else: flashreq = prpd.RouteSubscribeRequest(table_name=tablename, operation=monitor_operation,\ context=100, flag=fl_flag) flashreq.policy.CopyFrom(route_policy_name) if "route_count" in kwargs: flashreq.route_count = kwargs.get('route_count') route_flash_stream = prpd.process_api(dev, channel_id, "RouteSubscribe", [flashreq, "jnx_Rib"], timeout=kwargs.get("timeout", 120)) return route_flash_stream
def bgp_generate_route_match(**kwargs): # pylint: disable=too-many-locals,too-many-branches,too-many-statements """ Generate route match for get and remove """ if 'api_user' in kwargs: api_user = kwargs.get('api_user', 'default') else: api_user = None if 'table' in kwargs: table = kwargs.get('table') elif api_user != 'test': raise Exception("Mandatory parameter 'table' absent") else: table = None if 'dest_prefix' in kwargs: dest_prefix = kwargs.get('dest_prefix') if isinstance(dest_prefix, str): dest_prefix = [dest_prefix] elif isinstance(dest_prefix, list): dest_prefix = dest_prefix elif isinstance(dest_prefix, dict): dest_prefix = [dest_prefix] elif api_user != 'test': raise Exception("Mandatory parameter 'dest_prefix' absent") else: dest_prefix = None if 'prefix_len' in kwargs: if isinstance(kwargs["prefix_len"], int): prefix_len = kwargs.get('prefix_len') if table == 'inet6.0': if prefix_len > 128 and api_user != 'test': raise Exception("Prefix length should be <= 128 for inet6") elif table == 'inet.0': if prefix_len > 32 and api_user != 'test': raise Exception("Prefix length should be <= 32 for inet") elif api_user != 'test': raise Exception("Please enter integer value for prefix_len") elif api_user != 'test': raise Exception("Mandatory parameter 'prefix_len' absent") else: prefix_len = 0 if 'path_cookie' in kwargs and api_user != 'test': pathcookie = kwargs.get('path_cookie') pathcookie = int(pathcookie) else: pathcookie = None if 'protocol' in kwargs and api_user != 'test': protocol = kwargs.get('protocol') protocol = int(protocol) else: protocol = None if 'communities' in kwargs: community = kwargs.get('communities') if isinstance(community, str): community_list = [community] elif isinstance(community, list): community_list = community elif api_user == 'test': community_list = prpd.Communities(communities=None) else: community_list = None if 'dpref_attr' in kwargs and isinstance(kwargs['dpref_attr'], dict): dpref_attr = kwargs["dpref_attr"] else: dpref_attr = {} prefix_type = kwargs.get('prefix_type', None) routelist = [] for destprefix in dest_prefix: routeparams = prpd.RouteMatch() if destprefix is not None and table is not None and prefix_len is not None and api_user != 'test': if isinstance( dpref_attr, dict ) and api_user != 'test' and 'table' in destprefix and dpref_attr is not None: dest_table = destprefix['table'] else: dest_table = table dpref_attr["dest_prefix"] = destprefix dprefix = prefix_gen(dest_table, dpref_attr, api_user, prefix_type) routeparams.dest_prefix.CopyFrom(dprefix) if table is not None and api_user != 'test': table_name = prpd.RouteTable(name=prpd.RouteTableName(name=table)) routeparams.table.CopyFrom(table_name) if prefix_len is not None and api_user != 'test': routeparams.dest_prefix_len = prefix_len if pathcookie is not None and api_user != 'test': routeparams.cookie = pathcookie if community_list is not None and api_user != 'test': cm_list = [] len_comm = len(community_list) for i in range(0, len_comm): comm = prpd.Community(community=community_list[i]) cm_list.append(comm) c_list = prpd.Communities(communities=cm_list) routeparams.communities.CopyFrom(c_list) if protocol is not None and api_user != 'test': routeparams.protocol = protocol routelist.append(routeparams) return routelist
def bgp_generate_update_req(**kwargs): # pylint: disable=too-many-locals,too-many-branches,too-many-statements """ Generate the update request for bgp route add/modify/update """ if 'api_user' in kwargs: api_user = kwargs.get('api_user', 'default') else: api_user = None if 'next_hop' in kwargs: next_hop = kwargs.get('next_hop') if isinstance(next_hop, str): next_hop_list = [next_hop] elif isinstance(next_hop, list): next_hop_list = next_hop elif api_user != 'test': raise Exception("Next hop is mandatory parameter.") else: next_hop_list = None if 'multi_nh' in kwargs: multi_nh = kwargs.get('multi_nh') else: multi_nh = None if 'table' in kwargs: table = kwargs.get('table') elif api_user != 'test': raise Exception("Mandatory parameter 'table' absent") else: table = None if 'dest_prefix' in kwargs: dest_prefix = kwargs.get('dest_prefix') if isinstance(dest_prefix, str): dest_prefix = [dest_prefix] elif isinstance(dest_prefix, list): dest_prefix = dest_prefix elif isinstance(dest_prefix, dict): dest_prefix = [dest_prefix] elif api_user != 'test': raise Exception("Mandatory parameter 'dest_prefix' absent") else: dest_prefix = None if 'prefix_len' in kwargs: if isinstance(kwargs["prefix_len"], int): prefix_len = kwargs.get('prefix_len') if table == 'inet6.0': if prefix_len > 128 and api_user != 'test': raise Exception("Prefix length should be <= 128 for inet6") elif table == 'inet.0': if prefix_len > 32 and api_user != 'test': raise Exception("Prefix length should be <= 32 for inet") elif api_user != 'test': raise Exception("Please enter integer value for prefix_len") elif api_user != 'test': raise Exception("Mandatory parameter 'prefix_len' absent") else: prefix_len = None #Building the Nexthop for the RIB Route Params if 'communities' in kwargs: community = kwargs.get('communities') if isinstance(community, str): community_list = [ community, ] elif isinstance(community, list): community_list = community elif api_user == 'test': community_list = prpd.Communities(communities=None) else: community_list = None if 'cluster_id' in kwargs: clusterid = kwargs.get('cluster_id') num_clusterid = prpd.ip_to_uint32(clusterid) cluster = wrappers_pb2.UInt32Value(value=num_clusterid) else: cluster = None if 'originator_id' in kwargs: originatorid = kwargs.get('originator_id') num_originatorid = prpd.ip_to_uint32(originatorid) originator = wrappers_pb2.UInt32Value(value=num_originatorid) else: originator = None if 'route_preference' in kwargs: route_preference = kwargs.get('route_preference') route_preference = int(route_preference) route_pref = wrappers_pb2.UInt32Value(value=route_preference) else: route_pref = None if 'local_preference' in kwargs: local_preference = kwargs.get('local_preference') local_preference = int(local_preference) local_pref = wrappers_pb2.UInt32Value(value=local_preference) else: local_pref = None if 'med' in kwargs: med = kwargs.get('med') med = int(med) bgpmd = wrappers_pb2.UInt32Value(value=med) else: bgpmd = None if 'aspath' in kwargs: aspath = kwargs.get('aspath') as_path = prpd.AsPath(aspath=aspath) elif api_user == 'test': as_path = prpd.AsPath(aspath=None) else: as_path = None if 'vpn_label' in kwargs: vpnlabel = kwargs.get('vpn_label') vpnlabel = int(vpnlabel) elif table is 'bgp.l3vpn.0' or table is 'bgp.l3vpn-inet6.0' and api_user != 'test': raise Exception("Mandatory parameter 'label' absent") else: vpnlabel = None if 'route_type' in kwargs: rt_type = kwargs.get('route_type') rt_type = int(rt_type) else: rt_type = None if 'path_cookie' in kwargs: pathcookie = kwargs.get('path_cookie') pathcookie = int(pathcookie) else: pathcookie = None if 'routedata' in kwargs and isinstance(kwargs['routedata'], dict): routedata = kwargs.get('routedata') elif table == 'bgp.inetsrte.0' or table == 'bgp.inet6srte.0' and api_user != 'test' and table == 'inetflow.0' and table == 'inet6flow.0': raise Exception("Mandatory parameter 'routedata' is absent") else: routedata = None if 'dpref_attr' in kwargs and isinstance(kwargs['dpref_attr'], dict): dpref_attr = kwargs["dpref_attr"] else: dpref_attr = {} routelist = list() for destprefix in dest_prefix: routeparams = prpd.RouteEntry() if destprefix is not None and table is not None and prefix_len is not None and api_user != 'test': if isinstance( dpref_attr, dict ) and api_user != 'test' and 'table' in destprefix and dpref_attr is not None: dest_table = destprefix['table'] else: dest_table = table dpref_attr["dest_prefix"] = destprefix dprefix = prefix_gen(dest_table, dpref_attr, api_user) routeparams.key.dest_prefix.CopyFrom(dprefix) if table is not None and api_user != 'test': table_name = prpd.RouteTable(name=prpd.RouteTableName(name=table)) routeparams.key.table.CopyFrom(table_name) if prefix_len is not None and api_user != 'test': routeparams.key.dest_prefix_len = prefix_len if next_hop_list is not None and api_user != 'test': nhlist = [] if multi_nh is not None: for bgpnh in next_hop_list: nhlist.append(prpd.IpAddress(addr_string=bgpnh)) elif len(dest_prefix) == len(next_hop_list): nhlist.append(prpd.IpAddress(addr_string=next_hop_list.pop(0))) else: nhlist.append(prpd.IpAddress(addr_string=next_hop_list[0])) routeparams.protocol_nexthops.extend(nhlist) if routedata is not None and api_user != 'test': routeparams.addr_family_data.CopyFrom( routedatagen(routedata, table, api_user)) if local_pref is not None and api_user != 'test': routeparams.local_preference.CopyFrom(local_pref) if route_pref is not None and api_user != 'test': routeparams.route_preference.CopyFrom(route_pref) if bgpmd is not None and api_user != 'test': routeparams.med.CopyFrom(bgpmd) if as_path is not None and api_user != 'test': routeparams.aspath.CopyFrom(as_path) if community_list is not None and api_user != 'test': cm_list = [] len_comm = len(community_list) for i in range(0, len_comm): comm = prpd.Community(community=community_list[i]) cm_list.append(comm) c_list = prpd.Communities(communities=cm_list) routeparams.key.communities.CopyFrom(c_list) if cluster is not None and api_user != 'test': routeparams.cluster_id.CopyFrom(cluster) if originator is not None and api_user != 'test': routeparams.originator_id.CopyFrom(originator) if vpnlabel is not None and api_user != 'test': routeparams.label = vpnlabel if rt_type is not None and api_user != 'test': routeparams.route_type = rt_type if pathcookie is not None and api_user != 'test': routeparams.key.cookie = pathcookie routelist.append(routeparams) updreq = prpd.RouteUpdateRequest(routes=routelist) return updreq
def rib_generate_update_req(**kwargs): # pylint: disable=too-many-branches """ Update request method common for add/modify/update """ if 'table' in kwargs: table = kwargs.get('table') else: raise Exception("Mandatory parameter 'table' absent") if 'dest_prefix' in kwargs: dest_prefix = kwargs.get('dest_prefix') if isinstance(kwargs["dest_prefix"], str): dest_prefix = [kwargs["dest_prefix"], ] elif isinstance(kwargs["dest_prefix"], list): dest_prefix = kwargs["dest_prefix"] else: raise Exception("Mandatory parameter 'destination_prefix' absent") if 'prefix_len' in kwargs: if isinstance(kwargs["prefix_len"], int): prefix_len = kwargs.get('prefix_len') if 'inet6' in table: if prefix_len > 128: raise Exception("Prefix length should be <= 128 for inet6") elif 'inet.0' in table: if prefix_len > 32: raise Exception("Prefix length should be <= 32 for inet") else: if prefix_len > 52: raise Exception("Prefix length should be <= 52 for inet") else: raise Exception("Please enter integer value for prefix_len") else: raise Exception("Mandatory parameter 'prefix_length' absent") ecmp = kwargs.get("ecmp", False) if 'next_hop' in kwargs: if isinstance(kwargs["next_hop"], str): next_hop_list = [kwargs["next_hop"], ] elif isinstance(kwargs["next_hop"], list): next_hop_list = kwargs["next_hop"] if isinstance(next_hop_list[0], list): for i in range(0, len(next_hop_list)): if len(next_hop_list[i]) > 513 and ecmp is True: raise Exception("Next hop is limit exceeded with more than 512 nexthops for ecmp") elif len(next_hop_list) > 513 and ecmp is True: raise Exception("Next hop is limit exceeded with more than 512 nexthops for ecmp") else: raise Exception("Next hop is mandatory parameter.") #Removed deepcopy for enabling next_hop_list rotation via pop() #next_hop_list = prpd.deepcopy(next_hop_list) routelist = list() for index in range(0, len(dest_prefix)): #Building the Key for RIB Route Params if 'inet6' in table: destprefix = prpd.NetworkAddress(inet6=prpd.IpAddress(addr_string=dest_prefix[index])) else: if 'mpls' in table: destprefix = prpd.NetworkAddress(mpls=prpd.MplsAddress(label=int(dest_prefix[index]))) else: destprefix = prpd.NetworkAddress(inet=prpd.IpAddress(addr_string=dest_prefix[index])) route_table = prpd.RouteTable(rtt_name=prpd.RouteTableName(name=table)) route_match_field = prpd.RouteMatchFields(dest_prefix=destprefix, dest_prefix_len=prefix_len, table=route_table) if 'cookie' in kwargs: if isinstance(kwargs["cookie"], int): route_match_field.cookie = kwargs.get('cookie') # Update: Below code is handled in rib_nexthop # Calculating nexthop for below scenarios # ecmp is None # Only single nexthop is provided # if len(next_hop_list) >= 1 and ecmp is not True: # # # single_nh = [] # single_nh.append(next_hop_list.pop(0)) # next_hop_list.append(single_nh[0]) # # nh_list = rib_nexthop(single_nh, **kwargs) # elif len(next_hop_list) == 1 and ecmp is not True: # # single_nh = [] # single_nh.append(next_hop_list.pop(0)) # next_hop_list.append(single_nh[0]) # # nh_list = rib_nexthop(single_nh, **kwargs) # elif ecmp == True and skip_rib_nexthop == False: nh_list = rib_nexthop(**kwargs) #skip_rib_nexthop = True # Commented above line to provide support for unique label stack # for each prefix routeparams = prpd.RouteEntry() attributes = 0 # Added second if loop as pylint throws error for too many conditions in single if if 'preferences0' in kwargs or 'preferences1' in kwargs or 'colors0' in kwargs or 'no_advertise' in kwargs: rtattr = prpd.RouteAttributes() if 'preferences0' in kwargs: rtattr.preferences[0].CopyFrom(route_attribute(kwargs.get('preferences0'))) if 'preferences1' in kwargs: rtattr.preferences[1].CopyFrom(route_attribute(kwargs.get('preferences1'))) if 'colors0' in kwargs: rtattr.colors[0].CopyFrom(route_attribute(kwargs.get('colors0'))) if 'no_advertise' in kwargs: rtattr.no_advertise = kwargs.get('no_advertise') attributes = 1 if 'colors1' in kwargs or 'tags0' in kwargs or 'tags1' in kwargs or attributes == 1: if attributes == 0: rtattr = prpd.RouteAttributes() if 'colors1' in kwargs: rtattr.colors[1].CopyFrom(route_attribute(kwargs.get('colors1'))) if 'tags0' in kwargs: rtattr.tags[0].CopyFrom(route_attribute(kwargs.get('tags0'))) if 'tags1' in kwargs: rtattr.tags[1].CopyFrom(route_attribute(kwargs.get('tags1'))) routeparams.attributes.CopyFrom(rtattr) routeparams.key.CopyFrom(route_match_field) routeparams.nexthop.CopyFrom(nh_list) routelist.append(routeparams) updreq = prpd.RouteUpdateRequest(routes=routelist) return updreq
def rib_route_get(dev, channel_id, **kwargs): """ To fetch routes present in the rib """ # To handle same variable name across rib and bgp for destination_prefix,prefix_length dest_prefix_iterator = prpd.dest_prefix_limiter(**kwargs) if 'table' in kwargs: table = kwargs.get('table') else: raise Exception("Mandatory parameter 'table' absent") if 'prefix_len' in kwargs: if isinstance(kwargs["prefix_len"], int): prefix_len = kwargs.get('prefix_len') if 'inet6' in table: if prefix_len > 128: raise Exception("Prefix length should be <= 128 for inet6") else: if prefix_len > 32: raise Exception("Prefix length should be <= 32 for inet") else: raise Exception("Please enter integer value for prefix_len") else: raise Exception("Mandatory parameter 'prefix_len' absent") count = kwargs.get('route_count', 1000) if 'dest_prefix' in kwargs: dest_prefix = kwargs.get('dest_prefix') else: raise Exception("Mandatory parameter 'destination_prefix' absent") route_match_type = kwargs.get('route_match_type', prpd.BEST) if 'inet6' in table: destprefix = prpd.NetworkAddress(inet6=prpd.IpAddress(addr_string=dest_prefix)) else: destprefix = prpd.NetworkAddress(inet=prpd.IpAddress(addr_string=dest_prefix)) tablename = prpd.RouteTable(rtt_name=prpd.RouteTableName(name=table)) route_match_field = prpd.RouteMatchFields(dest_prefix=destprefix, dest_prefix_len=prefix_len, table=tablename) if 'cookie' in kwargs: route_match_field.cookie = kwargs.get('cookie') #Defining the RouteGetRequest Structure getreq = prpd.RouteGetRequest(key=route_match_field, match_type=route_match_type, route_count=count) if 'active_routes' in kwargs: if kwargs['active_routes'].upper() == 'TRUE': getreq.active_only = True if 'tags0' in kwargs or 'tags1' in kwargs: rtattr = prpd.RouteExtendedMatchFields() if 'tags0' in kwargs: rtattr.tags[0].CopyFrom(route_attribute(kwargs.get('tags0'))) if 'tags1' in kwargs: rtattr.tags[1].CopyFrom(route_attribute(kwargs.get('tags1'))) getreq.extended_match.CopyFrom(rtattr) getreply = prpd.process_api(dev, channel_id, "RouteGet", [getreq, "Rib"], kwargs.get("timeout", None)) return getreply