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.IPV4UNICAST()
    afi_safi.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    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 = "IBGP"
    peer_group.config.peer_group_name = "IBGP"
    peer_group.config.peer_as = 65001
    peer_group.transport.config.local_address = "Loopback0"
    afi_safi = peer_group.afi_safis.AfiSafi()
    afi_safi.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    afi_safi.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    afi_safi.config.enabled = True
    afi_safi.apply_policy.config.export_policy.append("POLICY2")
    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 = "172.16.255.2"
    neighbor.config.neighbor_address = "172.16.255.2"
    neighbor.config.peer_group = "IBGP"
    bgp.neighbors.neighbor.append(neighbor)
Exemplo n.º 2
0
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.IPV4UNICAST()
    ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.IPV4UNICAST()
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = openconfig_bgp_types.IPV6UNICAST()
    ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.IPV6UNICAST()
    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

    return bgp_cfg
Exemplo n.º 3
0
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.IPV4UNICAST()
    ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.IPV4UNICAST()
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = openconfig_bgp_types.IPV6UNICAST()
    ipv6_afsf.config.afi_safi_name = openconfig_bgp_types.IPV6UNICAST()
    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.IPV4UNICAST()
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = openconfig_bgp_types.IPV4UNICAST()
    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
Exemplo n.º 4
0
def config_oc_bgp_ipv4():

    ni = oc_ni.NetworkInstances.NetworkInstance()
    ni.name = "default"

    protocol = ni.protocols.Protocol()

    protocol.identifier = oc_policy_types.BGP()
    protocol.name = "default"
    protocol.config.identifier = oc_policy_types.BGP()
    protocol.config.name = "default"

    protocol.bgp.global_.config.as_ = 65000

    protocol.bgp.global_.config.router_id = "172.16.1.1"
    peer_group = protocol.bgp.peer_groups.PeerGroup()
    peer_group.peer_group_name = "IBGP"
    peer_group.config.peer_group_name = "IBGP"
    peer_group.config.peer_as = 65000
    peer_group.transport.config.local_address = "172.16.1.1"

    pg_ipv4_afsf = peer_group.afi_safis.AfiSafi()
    pg_ipv4_afsf.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    pg_ipv4_afsf.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    pg_ipv4_afsf.config.enabled = True
    peer_group.afi_safis.afi_safi.append(pg_ipv4_afsf)
    protocol.bgp.peer_groups.peer_group.append(peer_group)

    #pdb.set_trace()
    neighbor = protocol.bgp.neighbors.Neighbor()
    neighbor.neighbor_address = "172.16.4.1"
    neighbor.config.peer_group = "IBGP"
    #neighbor.config.enabled = True
    ng_ipv4_afsf = neighbor.afi_safis.AfiSafi()
    ng_ipv4_afsf.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    ng_ipv4_afsf.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    ng_ipv4_afsf.config.enabled = True
    neighbor.afi_safis.afi_safi.append(ng_ipv4_afsf)
    protocol.bgp.neighbors.neighbor.append(neighbor)

    ipv4_afsf = protocol.bgp.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    ipv4_afsf.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    ipv4_afsf.config.enabled = True
    protocol.bgp.global_.afi_safis.afi_safi.append(ipv4_afsf)

    # neighbor =

    ni.protocols.protocol.append(protocol)

    return ni
Exemplo n.º 5
0
def config_bgp_ipv4(yang_repo="",
                    address="",
                    grpc_port="",
                    username="",
                    password="",
                    crud_op="add",
                    bgp={}):

    repository = Repository(yang_repo)
    provider = gNMIServiceProvider(repo=repository,
                                   address=address,
                                   port=int(grpc_port),
                                   username=username,
                                   password=password)

    gnmi_service = gNMIService()
    crud = CRUDService()

    ni = oc_ni.NetworkInstances.NetworkInstance()

    if "vrf" in list(bgp.keys()):
        ni.name = bgp["vrf"]
    else:
        print("Vrf for network Instance not specified")
        sys.exit(1)

    protocol = ni.protocols.Protocol()

    protocol.identifier = oc_policy_types.BGP()
    protocol.name = "default"
    protocol.config.identifier = oc_policy_types.BGP()
    protocol.config.name = "default"

    if "as" in list(bgp.keys()):
        protocol.bgp.global_.config.as_ = int(bgp["as"])
    else:
        print("AS for BGP instance not specified")
        sys.exit(1)

    if "router_id" in list(bgp.keys()):
        protocol.bgp.global_.config.router_id = bgp["router_id"]

    afi_safi = protocol.bgp.global_.afi_safis.AfiSafi()
    afi_safi.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    afi_safi.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
    afi_safi.config.enabled = True
    protocol.bgp.global_.afi_safis.afi_safi.append(afi_safi)

    if "peer-group-name" in list(bgp.keys()):
        peer_group = protocol.bgp.peer_groups.PeerGroup()
        peer_group.peer_group_name = bgp["peer-group-name"]
        peer_group.config.peer_group_name = bgp["peer-group-name"]
        if "peer-as" in list(bgp.keys()):
            peer_group.config.peer_as = int(bgp["peer-as"])
        if "peer-group-local-address" in list(bgp.keys()):
            peer_group.transport.config.local_address = bgp[
                "peer-group-local-address"]

        afi_safi = peer_group.afi_safis.AfiSafi()
        afi_safi.afi_safi_name = oc_bgp_types.IPV4UNICAST()
        afi_safi.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
        afi_safi.config.enabled = True
        peer_group.afi_safis.afi_safi.append(afi_safi)
        protocol.bgp.peer_groups.peer_group.append(peer_group)

    if "neighbor" in list(bgp.keys()):
        neighbor = protocol.bgp.neighbors.Neighbor()
        neighbor.neighbor_address = bgp["neighbor"]
        neighbor.config.neighbor_address = bgp["neighbor"]
        #neighbor.config.peer_as = bgp["peer-as"]
        if "peer-group-name" in list(bgp.keys()):
            neighbor.config.peer_group = bgp["peer-group-name"]

        afi_safi = neighbor.afi_safis.AfiSafi()
        afi_safi.afi_safi_name = oc_bgp_types.IPV4UNICAST()
        afi_safi.config.afi_safi_name = oc_bgp_types.IPV4UNICAST()
        afi_safi.config.enabled = True
        neighbor.afi_safis.afi_safi.append(afi_safi)

        protocol.bgp.neighbors.neighbor.append(neighbor)

    ni.protocols.protocol.append(protocol)

    if crud_op == "add":
        response = crud.create(provider, ni)
    elif crud_op == "delete":
        response = crud.delete(provider, ni)
    elif crud_op is "update":
        response = crud.update(provider, ni)
    else:
        print(
            "Invalid operation requested, allowed values =  add, update, delete"
        )
        return False
    return response