Esempio n. 1
0
 def setUpClass(self):
     self.codec_provider = CodecServiceProvider()
     self.codec = CodecService()
     self.repo = Repository(get_local_repo_dir())
     self.provider = gNMIServiceProvider( self.repo, "127.0.0.1", "admin", "admin", port=50051)
     self.schema = self.provider.get_session().get_root_schema()
     self.gs = gNMIService()
Esempio n. 2
0
def run_test(device, repo_path, mode, ca, call_back):

    # Create gNMI Service Provider and gNMI Service
    provider = gNMIServiceProvider(Repository(repo_path),
                                   address=device.hostname,
                                   port=device.port,
                                   username=device.username,
                                   password=device.password,
                                   server_certificate=ca)
    gs = gNMIService()
    """Build entity for interface operational data"""
    ifc_oper_filter = ifoper.InterfaceProperties()
    dn = ifoper.InterfaceProperties.DataNodes.DataNode()
    dn.data_node_name = '"0/RP0/CPU0"'
    ifc_oper_filter.data_nodes.data_node.append(dn)

    lview = ifoper.InterfaceProperties.DataNodes.DataNode.Locationviews.Locationview(
    )
    lview.locationview_name = '"0/RP0/CPU0"'
    dn.locationviews.locationview.append(lview)

    ifc = ifoper.InterfaceProperties.DataNodes.DataNode.Locationviews.Locationview.Interfaces.Interface(
    )
    ifc.interface_name = '"Loopback0"'
    ifc.state = YFilter.read
    lview.interfaces.interface.append(ifc)
    """Build subscription"""
    subscription = gNMISubscription()
    subscription.entity = ifc_oper_filter
    subscription.suppress_redundant = False
    subscription.subscription_mode = "SAMPLE"
    subscription.sample_interval = 3 * 1000000000
    subscription.heartbeat_interval = 30 * 1000000000
    """Send subscribe request"""
    gs.subscribe(provider, subscription, 10, mode, "PROTO", call_back)
def subscribe(args):
    func = args[0]
    device = args[1]
    mode = args[2]

    gnmi = gNMIService()
    repository = Repository(get_local_repo_dir())
    provider = gNMIServiceProvider(repo=repository,
                                   address=device.hostname,
                                   port=device.port,
                                   username=device.username,
                                   password=device.password)

    build_int_config(provider)

    inf = openconfig_interfaces.Interfaces()
    i = openconfig_interfaces.Interfaces.Interface()
    inf.interface.append(i)

    int_subscription = gNMISubscription()
    int_subscription.entity = inf
    int_subscription.subscription_mode = "SAMPLE"
    int_subscription.sample_interval = 20 * 1000000000
    int_subscription.suppress_redundant = False
    int_subscription.heartbeat_interval = 100 * 1000000000

    gnmi.subscribe(provider, int_subscription, 10, mode, 'JSON_IETF', func)
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)

    # Fill out your BGP object properly using the Openconfig Yang Model
    # You will need to bring up the IBGP neighbor between rtr1 and rtr4

    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
def build_int_config(provider):
    ifc = openconfig_interfaces.Interfaces()
    lo10 = ifc.Interface()
    lo10.name = 'Loopback10'
    lo10.config.name = 'Loopback10'
    lo10.config.description = 'Test'
    ifc.interface.append(lo10)
    ifc.yfilter = YFilter.replace

    gs = gNMIService()
    gs.set(provider, ifc)
Esempio n. 6
0
def run_test(provider):
    root = provider.get_session().get_root_schema()
    service = gNMIService()
    
    """Create interface configuration"""
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = 'act'
    ifc.interface_name = 'Loopback10'
    ifc.description = 'Test interface'
    ifc.interface_virtual = Empty()

    ip_address = ifmgr.InterfaceConfigurations.InterfaceConfiguration.Ipv4Network.Addresses.Primary()
    ip_address.address = '172.16.255.1'
    ip_address.netmask = '255.255.255.255'
    ifc.ipv4_network.addresses.primary = ip_address

    ifc_create = ifmgr.InterfaceConfigurations()
    ifc_create.interface_configuration.append(ifc)
    ifc_create.yfilter = YFilter.update

    reply = service.set(provider, ifc_create)

    """Read interface configuration"""
    ifc_filter = ifmgr.InterfaceConfigurations()
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = '"act"'
    ifc.interface_name = '"Loopback10"'
    ifc_filter.interface_configuration.append(ifc)

    ifc_read = service.get(provider, ifc_filter)
    if ifc_read is not None:
        print_entity(ifc_read, root)

        """Need to fix bug in the XR bundle: missing directory _yang in distribution:
           RuntimeError: YIllegalStateError: Could not create repository in: /usr/local/lib/python2.7/dist-packages/ydk/models/cisco_ios_xr/_yang
        from ydk.services import CodecService
        from ydk.providers import CodecServiceProvider
        codec_service = CodecService()
        codec_provider = CodecServiceProvider(type='json')
        payload = codec_service.encode(codec_provider, ifc_read)
        print('CREATED INTERFACE CONFIGURATION:')
        print(payload)
        """

    """Delete interface configuration"""
    ifc_delete = ifmgr.InterfaceConfigurations()
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = '"act"'
    ifc.interface_name = '"Loopback10"'
    ifc_delete.interface_configuration.append(ifc)
    ifc_delete.yfilter = YFilter.delete
    reply = service.set(provider, ifc_delete)
Esempio n. 7
0
def run_test(provider):
    root = provider.get_session().get_root_schema()
    service = gNMIService()
    """Create interface configuration"""
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = 'act'
    ifc.interface_name = 'Loopback10'
    ifc.description = 'Test interface'
    ifc.interface_virtual = Empty()

    ip_address = ifmgr.InterfaceConfigurations.InterfaceConfiguration.Ipv4Network.Addresses.Primary(
    )
    ip_address.address = '172.16.255.1'
    ip_address.netmask = '255.255.255.255'
    ifc.ipv4_network.addresses.primary = ip_address

    ifc_create = ifmgr.InterfaceConfigurations()
    ifc_create.interface_configuration.append(ifc)
    ifc_create.yfilter = YFilter.update

    reply = service.set(provider, ifc_create)
    """Read interface configuration"""
    ifc_filter = ifmgr.InterfaceConfigurations()
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = '"act"'
    ifc.interface_name = '"Loopback10"'
    ifc_filter.interface_configuration.append(ifc)

    ifc_read = service.get(provider, ifc_filter)
    if ifc_read is not None:
        print_entity(ifc_read, root)
        """Need to fix bug in the XR bundle: missing directory _yang in distribution:
           RuntimeError: YIllegalStateError: Could not create repository in: /usr/local/lib/python2.7/dist-packages/ydk/models/cisco_ios_xr/_yang
        from ydk.services import CodecService
        from ydk.providers import CodecServiceProvider
        codec_service = CodecService()
        codec_provider = CodecServiceProvider(type='json')
        payload = codec_service.encode(codec_provider, ifc_read)
        print('CREATED INTERFACE CONFIGURATION:')
        print(payload)
        """
    """Delete interface configuration"""
    ifc_delete = ifmgr.InterfaceConfigurations()
    ifc = ifmgr.InterfaceConfigurations.InterfaceConfiguration()
    ifc.active = '"act"'
    ifc.interface_name = '"Loopback10"'
    ifc_delete.interface_configuration.append(ifc)
    ifc_delete.yfilter = YFilter.delete
    reply = service.set(provider, ifc_delete)
Esempio n. 8
0
def create_interface_config(provider, port_name, port_desc, port_ip, port_cidr):    
    # create a service
    gnmi_service = gNMIService()

    # create the object model instance
    interface = openconfig_interfaces.Interfaces.Interface()
    interface.name = port_name
    interface.config.name = interface.name
    interface.config.description = port_desc
    interface.config.enabled = True
#    interface.ethernet.config.duplex_mode = "FULL"
#    interface.ethernet.config.auto_negotiate = True

    # subinterface for ip address
    subinterface = interface.subinterfaces.Subinterface()
    subinterface.index = 0
    subinterface.config.index = 0
    subinterface.config.description = port_desc
    subinterface.ipv4 = subinterface.Ipv4()
    
    # address stuff
    address_list = subinterface.ipv4.Addresses()
    address = address_list.Address()
    address.ip = port_ip
    address.config.ip = port_ip
    address.config.prefix_length = port_cidr
    
    # put the model together
    subinterface.ipv4.addresses.address.append(address)
    interface.subinterfaces.subinterface.append(subinterface)

    # configure the action type in gnmi
    interface.yfilter = YFilter.update

    # do stuff
    try: 
        ok = gnmi_service.set(provider, interface)
        return ok
    except YError as errm: 
        print('An Error occurred whilst creating the interface: {}'.format(errm))
Esempio n. 9
0
def subscribe(func):
    gnmi = gNMIService()

    try:
        try:
            #Running on dev1 environment of tesuto cloud setup for Nanog75
            repository = Repository('/home/tesuto/yang/')
        except:
            #Running in a docker container started off image akshshar/nanog75-telemetry
            repository = Repository('/root/yang/')
            raise
    except Exception as e:
        print(
            "Failed to import yang models, check repository path,  aborting...."
        )
        sys.exit(1)

    provider = gNMIServiceProvider(repo=repository,
                                   address='100.96.0.14',
                                   port=57777,
                                   username='******',
                                   password='******')

    # The below will create a telemetry subscription path 'openconfig-interfaces:interfaces/interface'
    interfaces = openconfig_interfaces.Interfaces()
    interface = openconfig_interfaces.Interfaces.Interface()
    interfaces.interface.append(interface)

    network_instances = openconfig_network_instance.NetworkInstances()

    subscription = gNMISubscription()
    subscription.entity = interfaces  #network_instances

    subscription.subscription_mode = "SAMPLE"
    subscription.sample_interval = 10 * 1000000000
    subscription.suppress_redundant = False
    subscription.heartbeat_interval = 100 * 1000000000

    # Subscribe for updates in STREAM mode.
    var = gnmi.subscribe(provider, subscription, 10, "STREAM", "PROTO", func)
def run_test(device, mode, ca, call_back):

    # Create gNMI Service Provider and gNMI Service
    repo = Repository("/home/ygorelik/ydk-gen/scripts/samples/repository/192.168.122.107")
    provider = gNMIServiceProvider(repo, address=device.hostname,
                                      port=device.port,
                                      username=device.username,
                                      password=device.password,
                                      server_certificate=ca)
    gs = gNMIService()

    """Build entity for interface operational data"""
    ifc_oper_filter = ifoper.InterfaceProperties()
    dn = ifoper.InterfaceProperties.DataNodes.DataNode()
    dn.data_node_name = '"0/RP0/CPU0"'
    ifc_oper_filter.data_nodes.data_node.append(dn)

    lview = ifoper.InterfaceProperties.DataNodes.DataNode.Locationviews.Locationview()
    lview.locationview_name = '"0/RP0/CPU0"'
    dn.locationviews.locationview.append(lview)
    
    ifc = ifoper.InterfaceProperties.DataNodes.DataNode.Locationviews.Locationview.Interfaces.Interface()
    ifc.interface_name = '"Loopback10"'
    ifc.state = YFilter.read
    lview.interfaces.interface.append(ifc)

    """Build subscription"""
    subscription = gNMISubscription()
    subscription.entity = ifc_oper_filter
    subscription.suppress_redundant = False;
    subscription.subscription_mode = "SAMPLE";
    subscription.sample_interval    = 3 * 1000000000;
    subscription.heartbeat_interval = 30 * 1000000000;

    """Send sunscribe request"""
    gs.subscribe(provider, subscription, 10, mode, "PROTO", call_back);
    device = urlparse(args.device)

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

    # create gNMI provider
    repository = Repository(YDK_REPO_DIR + device.hostname)
    provider = gNMIServiceProvider(repo=repository,
                                   address=device.hostname,
                                   port=device.port,
                                   username=device.username,
                                   password=device.password)
    # create gNMI service
    gnmi = gNMIService()

    cdp = xr_cdp_cfg.Cdp()  # create object
    config_cdp(cdp)  # add object configuration

    # set configuration on gNMI device
    # gnmi.set(provider, cdp)

    exit()
# End of script
Esempio n. 12
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