def test_bgp(self):
        # Bgp.Global.AfiSafis.AfiSafi.ApplyPolicy is not supported
        bgp_cfg = bgp.Bgp()
        ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
        ipv4_afsf.afi_safi_name = 'ipv4-unicast'
        ipv4_afsf.apply_policy.config.default_export_policy = \
            DefaultPolicyTypeEnum.ACCEPT_ROUTE
        bgp_cfg.global_.afi_safis.afi_safi.append(ipv4_afsf)

        self.assertRaises(YPYDataValidationError,
            self.crud.create, self.ncc, bgp_cfg)
Exemple #2
0
def bgp_run(codec_service, provider):
    bgp_cfg = bgp.Bgp()

    bgp_cfg.global_.config.as_ = 65001

    ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = 'ipv6-unicast'
    ipv6_afsf.config.afi_safi_name = 'ipv6-unicast'
    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)
    print codec_service.encode(provider, bgp_cfg)
    # 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 = 'ipv4-unicast'
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    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
    bgp_payload = codec_service.encode(provider, bgp_cfg)
    bgp_entity = codec_service.decode(provider, bgp_payload)
    print 'Encoded payload:\n', bgp_payload, \
            '\nRe-encode the decoded payload:\n', codec_service.encode(provider, bgp_entity)
    assert bgp_payload == codec_service.encode(provider, bgp_entity)
Exemple #3
0
def bgp_run(netconf_service, session):
    bgp_cfg = bgp.Bgp()

    # set up routing policy definition
    routing_policy = RoutingPolicy()

    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

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

    bgp_cfg.global_.config.as_ = 65001

    ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = 'ipv6-unicast'
    ipv6_afsf.config.afi_safi_name = 'ipv6-unicast'
    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 = 'ipv4-unicast'
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    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

    # 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 = 'ipv6-unicast'
    nbr_ipv6_afsf.config.peer_as = 65002
    nbr_ipv6_afsf.config.afi_safi_name = 'ipv6-unicast'
    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
                        action="store_true")
    parser.add_argument("device",
                        help="NETCONF device (ssh://user:password@host:port)")
    args = parser.parse_args()
    device = urlparse(args.device)

    # log debug messages if verbose argument specified
    if args.verbose:
        logger = logging.getLogger("ydk")
        logger.setLevel(logging.DEBUG)
        handler = logging.StreamHandler()
        formatter = logging.Formatter(("%(asctime)s - %(name)s - "
                                       "%(levelname)s - %(message)s"))
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # create NETCONF provider
    provider = NetconfServiceProvider(address=device.hostname,
                                      port=device.port,
                                      username=device.username,
                                      password=device.password,
                                      protocol=device.scheme)
    # create CRUD service
    crud = CRUDService()

    bgp = oc_bgp.Bgp()  # create config object
    crud.delete(provider, bgp)  # delete object on NETCONF device
    provider.close()
    exit()
# End of script
Exemple #5
0
def bgp_run(crud_service, session):

    # Global config
    bgp_cfg = bgp.Bgp()

    try:
        crud_service.delete(session, bgp_cfg)
    except YPYError:
        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

    crud_service.create(session, routing_policy)

    bgp_cfg.global_.config.as_ = 65001

    # afi-safi config fails name verification on netsim
    # I think there needs to be some init data to fix that
    ipv4_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv4_afsf.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    ipv4_afsf.config.enabled = True

    ipv6_afsf = bgp_cfg.global_.afi_safis.AfiSafi()
    ipv6_afsf.afi_safi_name = 'ipv6-unicast'
    ipv6_afsf.config.afi_safi_name = 'ipv6-unicast'
    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 = 'ipv4-unicast'
    nbr_ipv4_afsf.config.peer_as = 65002
    nbr_ipv4_afsf.config.afi_safi_name = 'ipv4-unicast'
    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)

    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 = 'ipv6-unicast'
    nbr_ipv6_afsf.config.peer_as = 65002
    nbr_ipv6_afsf.config.afi_safi_name = 'ipv6-unicast'
    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
Exemple #6
0
from ydk.services import CRUDService 
from ydk.providers import NetconfServiceProvider
from ydk.models.bgp import bgp


provider = NetconfServiceProvider(address=xrv, port=830, username=“cisco”, password=“cisco”, protocol=“ssh")

crud = CRUDService()  #  create  CRUD  service
bgp = bgp.Bgp() #  create oc bgp object
  #  set  local  AS  number
bgp.global_.config.router-id_ = "1.1.1.1"
crud.create(provider, bgp) #create  on  NETCONF  device
provider.close()