Пример #1
0
def download_schema(request, req):
    """
    This API download yang schema from device and bundle it
    """
    logger.debug("Download Schemas")

    modules = download_yang(request, req)

    session_dir = ServerSettings.schema_path(request.session.session_key)
    http_host = request.META["HTTP_HOST"]
    current = str(datetime.now())
    current = current.replace(" ", "-")
    current = current[: current.rindex(".")]
    zfname = "schema-" + current + ".zip"
    zfile = session_dir + "/" + zfname

    homedir = os.getcwd()
    os.chdir(session_dir)
    with ZipFile(zfile, "w") as lz:
        for f in glob.glob("*.yang"):
            lz.write(f)
            os.remove(f)
    if not lz.namelist():
        os.remove(zfile)
    os.chdir(homedir)

    url = "\nhttp://" + http_host + "/" + "download/session/"
    url += request.session.session_key + "/" + zfname
    return HttpResponse(Response.success("download", msg=url))
Пример #2
0
def download_schema(request, req):
    '''
    This API download yang schema from device and bundle it
    '''
    logging.debug('Download Schemas')

    modules = download_yang(request, req)    

    session_dir = ServerSettings.schema_path(request.session.session_key)
    http_host = request.META['HTTP_HOST']
    current = str(datetime.now())
    current = current.replace(' ', '-')
    current = current[:current.rindex('.')]
    zfname = 'schema-' + current + '.zip'
    zfile = session_dir + '/' + zfname

    homedir = os.getcwd()
    os.chdir(session_dir)
    with ZipFile(zfile, "w") as lz:
        for f in glob.glob("*.yang"):
            lz.write(f)
            os.remove(f)
    if not lz.namelist():
        os.remove(zfile)
    os.chdir(homedir)

    url = '\nhttp://' + http_host + '/' + 'download/session/' + request.session.session_key + '/' + zfname
    return HttpResponse(Response.success('download', msg=url))
Пример #3
0
def download_schema(request, req):
    '''
    This API download yang schema from device and bundle it
    '''
    logging.debug('Download Schemas')

    modules = download_yang(request, req)

    session_dir = ServerSettings.schema_path(request.session.session_key)
    http_host = request.META['HTTP_HOST']
    current = str(datetime.now())
    current = current.replace(' ', '-')
    current = current[:current.rindex('.')]
    zfname = 'schema-' + current + '.zip'
    zfile = session_dir + '/' + zfname

    homedir = os.getcwd()
    os.chdir(session_dir)
    with ZipFile(zfile, "w") as lz:
        for f in glob.glob("*.yang"):
            lz.write(f)
            os.remove(f)
    if not lz.namelist():
        os.remove(zfile)
    os.chdir(homedir)

    url = '\nhttp://' + http_host + '/' + 'download/session/' + request.session.session_key + '/' + zfname
    return HttpResponse(Response.success('download', msg=url))
Пример #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
Пример #5
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