Exemple #1
0
def config_bgp(bgp):
    """Add config data to bgp object."""
    # global configuration
    bgp.global_.config.as_ = 65001
    afi_safi = bgp.global_.afi_safis.AfiSafi()
    afi_safi.afi_safi_name = oc_bgp_types.Ipv6UnicastIdentity()
    afi_safi.config.afi_safi_name = oc_bgp_types.Ipv6UnicastIdentity()
    afi_safi.config.enabled = True
    bgp.global_.afi_safis.afi_safi.append(afi_safi)

    # configure IBGP peer group
    peer_group = bgp.peer_groups.PeerGroup()
    peer_group.peer_group_name = "EBGP"
    peer_group.config.peer_group_name = "EBGP"
    peer_group.config.peer_as = 65002
    peer_group.transport.config.local_address = "Loopback0"
    afi_safi = peer_group.afi_safis.AfiSafi()
    afi_safi.afi_safi_name = oc_bgp_types.Ipv6UnicastIdentity()
    afi_safi.config.afi_safi_name = oc_bgp_types.Ipv6UnicastIdentity()
    afi_safi.config.enabled = True
    afi_safi.apply_policy.config.import_policy.append("POLICY3")
    afi_safi.apply_policy.config.export_policy.append("POLICY1")
    peer_group.afi_safis.afi_safi.append(afi_safi)
    bgp.peer_groups.peer_group.append(peer_group)

    # configure IBGP neighbor
    neighbor = bgp.neighbors.Neighbor()
    neighbor.neighbor_address = "2001:db8:e:1::1"
    neighbor.config.neighbor_address = "2001:db8:e:1::1"
    neighbor.config.peer_group = "EBGP"
    bgp.neighbors.neighbor.append(neighbor)
Exemple #2
0
def bgp_run(netconf_service, session):
    # set up routing policy definition
    routing_policy = _get_routing_cfg()
    netconf_service.edit_config(session, Datastore.candidate, routing_policy)

    bgp_cfg = _get_bgp_config()
    # IPv4 Neighbor instance config done
    netconf_service.edit_config(session, Datastore.candidate, bgp_cfg)

    bgp_cfg_read = netconf_service.get_config(session, Datastore.candidate, bgp.Bgp())
    print(bgp_cfg_read)

    # IPv6 Neighbor instance config
    nbr_ipv6 = bgp.Bgp.Neighbors.Neighbor()
    nbr_ipv6.parent = bgp_cfg.neighbors
    nbr_ipv6.neighbor_address = '2001:db8:fff1::1'
    nbr_ipv6.config.neighbor_address = '2001:db8:fff1::1'
    nbr_ipv6.config.peer_as = 65002

    nbr_ipv6_afsf = nbr_ipv6.afi_safis.AfiSafi()
    nbr_ipv6_afsf.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    nbr_ipv6_afsf.config.peer_as = 65002
    nbr_ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    nbr_ipv6_afsf.config.enabled = True

    nbr_ipv6.afi_safis.afi_safi.append(nbr_ipv6_afsf)

    netconf_service.edit_config(session, Datastore.candidate, bgp_cfg)

    nbr_ipv6_filter = bgp.Bgp.Neighbors.Neighbor()
    nbr_ipv6_filter.neighbor_address = '2001:db8:fff1::1'

    nbr_ipv6_read = netconf_service.get_config(session, Datastore.candidate, bgp_cfg)

    print(nbr_ipv6_read)
def _get_bgp_config():
    bgp_cfg = openconfig_bgp.Bgp()

    bgp_cfg.global_.config.as_ = 65001

    ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    ipv6_afsf.config.enabled = True

    bgp_cfg.global_.afi_safis.afi_safi.append(ipv4_afsf)
    bgp_cfg.global_.afi_safis.afi_safi.append(ipv6_afsf)
    # Global config done

    # IPv4 Neighbor instance config
    nbr_ipv4 = bgp_cfg.neighbors.Neighbor()
    nbr_ipv4.neighbor_address = '192.168.1.1'
    nbr_ipv4.config.neighbor_address = '192.168.1.1'
    nbr_ipv4.config.peer_as = 65002

    nbr_ipv4_afsf = nbr_ipv4.afi_safis.AfiSafi()
    nbr_ipv4_afsf.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity(
    )
    nbr_ipv4_afsf.config.enabled = True

    # Create afi-safi policy instances
    nbr_ipv4.afi_safis.afi_safi.append(nbr_ipv4_afsf)

    bgp_cfg.neighbors.neighbor.append(nbr_ipv4)
    nbr_ipv4.parent = bgp_cfg.neighbors

    return bgp_cfg
Exemple #4
0
def bgp_run(crud_service, session):

    # Global config
    bgp_cfg = openconfig_bgp.Bgp()

    try:
        crud_service.delete(session, bgp_cfg)
    except Exception:
        print('BGP config does not exist!')

    #set up routing policy definition
    routing_policy = RoutingPolicy()

    #first delete
    try:
        crud_service.delete(session, routing_policy)
    except YPYError:
        print('Routing policy does not exist!')

    pass_all_policy_defn = RoutingPolicy.PolicyDefinitions.PolicyDefinition()
    pass_all_policy_defn.name = 'PASS-ALL'

    routing_policy.policy_definitions.policy_definition.append(
        pass_all_policy_defn)
    pass_all_policy_defn._parent = routing_policy.policy_definitions

    bgp_cfg.global_.config.as_ = 65001

    ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    ipv6_afsf.config.enabled = True

    bgp_cfg.global_.afi_safis.afi_safi.append(ipv4_afsf)
    bgp_cfg.global_.afi_safis.afi_safi.append(ipv6_afsf)
    # Global config done

    # IPv4 Neighbor instance config
    nbr_ipv4 = bgp_cfg.neighbors.Neighbor()
    nbr_ipv4.neighbor_address = '192.168.1.1'
    nbr_ipv4.config.neighbor_address = '192.168.1.1'
    nbr_ipv4.config.peer_as = 65002

    nbr_ipv4_afsf = nbr_ipv4.afi_safis.AfiSafi()
    nbr_ipv4_afsf.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity()
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv4UnicastIdentity(
    )
    nbr_ipv4_afsf.config.enabled = True

    # Create afi-safi policy instances
    nbr_ipv4_afsf.apply_policy.config.import_policy.append('PASS-ALL')

    nbr_ipv4_afsf.apply_policy.config.export_policy.append('PASS-ALL')

    nbr_ipv4.afi_safis.afi_safi.append(nbr_ipv4_afsf)

    bgp_cfg.neighbors.neighbor.append(nbr_ipv4)
    nbr_ipv4.parent = bgp_cfg.neighbors

    # IPv4 Neighbor instance config done

    crud_service.create(session, bgp_cfg)
    #    crud_service.create(session, routing_policy)

    bgp_cfg_read = crud_service.read(session, bgp.Bgp())

    #crud on just the neighbor

    # IPv6 Neighbor instance config
    nbr_ipv6 = bgp.Bgp.Neighbors.Neighbor()
    #nbr_ipv6.parent = bgp_cfg.neighbors
    nbr_ipv6.neighbor_address = '2001:db8:fff1::1'
    nbr_ipv6.config.neighbor_address = '2001:db8:fff1::1'
    nbr_ipv6.config.peer_as = 65002

    nbr_ipv6_afsf = nbr_ipv6.afi_safis.AfiSafi()
    nbr_ipv6_afsf.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity()
    nbr_ipv6_afsf.config.peer_as = 65002
    nbr_ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.Ipv6UnicastIdentity(
    )
    nbr_ipv6_afsf.config.enabled = True

    # Create afi-safi policy instances
    nbr_ipv6_afsf.apply_policy.config.import_policy.append('PASS-ALL')

    nbr_ipv6_afsf.apply_policy.config.export_policy.append('PASS-ALL')

    nbr_ipv6.afi_safis.afi_safi.append(nbr_ipv6_afsf)

    crud_service.create(session, nbr_ipv6)

    #now delete nbr_ipv4
    crud_service.delete(session, nbr_ipv4)

    nbr_ipv6_filter = bgp.Bgp.Neighbors.Neighbor()
    nbr_ipv6_filter.neighbor_address = '2001:db8:fff1::1'

    nbr_ipv6_read = crud_service.read(session, nbr_ipv6_filter)

    # help(nbr_ipv6_read)
    print(nbr_ipv6_read)