Exemplo n.º 1
0
def download_helper(username, device, dest, rpc, models):
    """Download list of models in destination directory from device"""
    if not models:
        return

    logger.info("Downloading " + str(models))

    identifier = rpc[0][0]
    dep_models = set()

    # dowload all models in the list
    for modelname in models:
        identifier.text = modelname.split("@")[0]
        fname = os.path.join(dest, identifier.text + ".yang")

        if not os.path.exists(fname):
            schema = Adapter.run_netconf(username, device, rpc)

            # write to file
            with open(fname, "w") as f:
                f.write(schema[0][0].text)

            # calculate dependency
            parser = Parser(fname)
            dep_models |= set(parser.get_dependency())

    # recursively download dependency
    download_helper(username, device, dest, rpc, dep_models)
Exemplo n.º 2
0
def download_helper(username, device, dest, rpc, models):
    """Download list of models in destination directory from device"""
    if not models:
        return

    logger.info('Downloading ' + str(models))

    identifier = rpc[0][0]
    dep_models = set()

    # dowload all models in the list
    for modelname in models:
        identifier.text = modelname.split('@')[0]
        fname = os.path.join(dest, identifier.text + '.yang')

        if not os.path.exists(fname):
            schema = Adapter.run_netconf(username, device, rpc)

            # write to file
            with open(fname, 'w') as f:
                f.write(schema[0][0].text)

            # calculate dependency
            parser = Parser(fname)
            dep_models |= set(parser.get_dependency())

    # recursively download dependency
    download_helper(username, device, dest, rpc, dep_models)
Exemplo n.º 3
0
def download_yang(request, req):
    '''
    This API download yang schema from device
    '''
    logging.debug('Download Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('download', 'no host info'))

    session_dir = ServerSettings.schema_path(request.session.session_key)
    if not os.path.exists(session_dir):
        os.makedirs(session_dir)
    if not os.path.exists(session_dir):
        return HttpResponse(Response.error('download', 'No session directory'))

    for fname in os.listdir(session_dir):
        if fname.endswith('.yang'):
            fn = os.path.join(session_dir, fname)
            os.remove(fn)

    modules = ET.Element('modules')
    reqxml = ET.fromstring(req)
    schemas = reqxml.find('schemas')
    for sc in schemas:
        id = sc.text
        module = ET.Element('module')
        get_sc = ET.Element('get-schema')
        get_sc.set("xmlns",
                   "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring")
        identifier = ET.Element("identifier")
        sfile = id.split('@')[0]
        identifier.text = sfile
        module.text = id + '.yang'
        get_sc.append(identifier)
        rpc.append(get_sc)
        modules.append(module)
        schema = Adapter.run_netconf(request.user.username, device, rpc)
        fname = os.path.join(session_dir, id + '.yang')
        with open(fname, 'w') as f:
            f.write(schema[0][0].text)
        rpc.remove(get_sc)

    return modules
Exemplo n.º 4
0
def download_yang(request, req):
    '''
    This API download yang schema from device
    '''
    logging.debug('Download Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('download', 'no host info'))

    session_dir = ServerSettings.schema_path(request.session.session_key)
    if not os.path.exists(session_dir):
        os.makedirs(session_dir)
    if not os.path.exists(session_dir):
        return HttpResponse(Response.error('download', 'No session directory'))

    for fname in os.listdir(session_dir):
        if fname.endswith('.yang'):
            fn = os.path.join(session_dir, fname)
            os.remove(fn)

    modules = ET.Element('modules')
    reqxml = ET.fromstring(req)
    schemas = reqxml.find('schemas')
    for sc in schemas:
        id = sc.text
        module = ET.Element('module')
        get_sc = ET.Element('get-schema')
        get_sc.set("xmlns", "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring")
        identifier = ET.Element("identifier")
        sfile = id.split('@')[0]
        identifier.text = sfile
        module.text = id+'.yang'
        get_sc.append(identifier)
        rpc.append(get_sc)
        modules.append(module)
        schema = Adapter.run_netconf(request.user.username, device, rpc)
        fname = os.path.join(session_dir, id+'.yang')
        with open(fname, 'w') as f:
            f.write(schema[0][0].text)
        rpc.remove(get_sc)

    return modules
Exemplo n.º 5
0
def get_schema(request, req, all=True):
    """
    This API get yang schema from device
    """
    logger.debug("Get Yang Schema")

    req = req.replace("<metadata>", "")
    req = req.replace("</metadata>", "")

    protocol, device, fmt, _, payload = Adapter.parse_request(req)
    if device.get("host", None) is None:
        return HttpResponse(Response.error("get", "no host info"))

    rpc_xml = ET.fromstring(get_schema_list_rpc)
    xml = Adapter.run_netconf(request.user.username, device, rpc_xml)
    if xml is None:
        return HttpResponse(Response.error("get", "failed to get schema"))

    if xml.text is not None:
        if xml.text.find("error"):
            return HttpResponse(Response.error("get", xml.text))

    sclist = xml[0][0][0][0]
    schemas = ET.Element("schemas")
    for sc in sclist:
        schema = ET.Element("schema")
        id = sc[0].text
        ver = sc[1].text
        if all == False:
            if id.isupper() or "-MIB" in id or "SNMP" in id:
                continue
        schema.set("name", id)
        if ver is None:
            schema.set("version", "")
        else:
            schema.set("version", ver)
        unmatched = validate_schema(request.user.username, id, ver)
        if unmatched is not None:
            schema.set("unmatched", unmatched)

        schemas.append(schema)

    return HttpResponse(Response.success("get", "ok", xml=schemas))
Exemplo n.º 6
0
def get_schema(request, req, all=False):
    '''
    This API get yang schema from device
    '''
    logging.debug('Get Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('get', 'no host info'))

    rpc_xml= ET.fromstring(get_schema_rpc)
    xml = Adapter.run_netconf(request.user.username, device, rpc_xml) 
    if xml is None:
        return HttpResponse(Response.error('get', 'failed to get schema'))
 
    if xml.text is not None:
        if xml.text.find('error'):
            return HttpResponse(Response.error('get', xml.text))

    sclist = xml[0][0][0][0]
    schemas = ET.Element('schemas')
    for sc in sclist:
        schema = ET.Element('schema')
        id  = sc[0].text
        ver = sc[1].text
        if all == False:
            if id.isupper() or '-MIB' in id or 'SNMP' in id:
                continue
        schema.set('name', id)
        if ver is None:
            schema.set('version', '')
        else:
            schema.set('version', ver)
        unmatched = validate_schema(request.user.username, id, ver)
        if unmatched is not None:
            schema.set('unmatched', unmatched)

        schemas.append(schema) 

    return HttpResponse(Response.success('get', 'ok', xml=schemas))
Exemplo n.º 7
0
def get_schema(request, req, all=False):
    '''
    This API get yang schema from device
    '''
    logging.debug('Get Yang Schema')

    req = req.replace('<metadata>', '')
    req = req.replace('</metadata>', '')

    protocol, device, fmt, payload = Adapter.parse_request(req)
    if device.get('host', None) is None:
        return HttpResponse(Response.error('get', 'no host info'))

    rpc_xml = ET.fromstring(get_schema_rpc)
    xml = Adapter.run_netconf(request.user.username, device, rpc_xml)
    if xml is None:
        return HttpResponse(Response.error('get', 'failed to get schema'))

    if xml.text is not None:
        if xml.text.find('error'):
            return HttpResponse(Response.error('get', xml.text))

    sclist = xml[0][0][0][0]
    schemas = ET.Element('schemas')
    for sc in sclist:
        schema = ET.Element('schema')
        id = sc[0].text
        ver = sc[1].text
        if all == False:
            if id.isupper() or '-MIB' in id or 'SNMP' in id:
                continue
        schema.set('name', id)
        if ver is None:
            schema.set('version', '')
        else:
            schema.set('version', ver)
        unmatched = validate_schema(request.user.username, id, ver)
        if unmatched is not None:
            schema.set('unmatched', unmatched)

        schemas.append(schema)

    return HttpResponse(Response.success('get', 'ok', xml=schemas))