Esempio n. 1
0
def request_handler(request):
    """ HTTP Request handler function to handle actions on collections """

    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    mode = request.GET.get('mode', '')
    reply_xml = None

    logger.info('request_handler: Received Request: (%s)' % mode)

    if mode == 'get-collection-list':
        reply_xml = Collection.list()

    elif mode == 'load-collection':
        metadata = request.GET.get('metadata', '')
        reply_xml = Collection.load(request.user.username, metadata)

        if reply_xml is None:
            return HttpResponse(Response.error(mode, 'Failed'))

        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode == 'add-collection':
        metadata = request.GET.get('metadata', '')
        payload = request.GET.get('payload', '')
        logger.debug('metadata: ' + metadata)
        logger.debug('payload: ' + payload)
        if not Collection.add(metadata, payload):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'delete-collection':
        metadata = request.GET.get('metadata', '')
        if not Collection.remove(metadata):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'rpc':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_rpc(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))
    elif mode == 'gen-script':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_script(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode in ['get-cap', 'run-rpc', 'run-edit-commit', 'run-commit']:
        payload = request.GET.get('payload', '')
        logger.debug('run: ' + payload)
        reply_xml = Adapter.run_request(request.user.username, payload)

    return HttpResponse(Response.success(mode, 'ok', reply_xml))
Esempio n. 2
0
def request_handler(request):
    """ HTTP Request handler function to handle actions on collections """

    if not request.user.is_authenticated():
        return HttpResponse(Response.error(None, 'User must be logged in'))

    mode = request.GET.get('mode', '')
    reply_xml = None

    logger.info('request_handler: Received Request: (%s)' % mode)

    if mode == 'get-collection-list':
        reply_xml = Collection.list()

    elif mode == 'load-collection':
        metadata = request.GET.get('metadata', '')
        reply_xml = Collection.load(request.user.username, metadata)

        if reply_xml is None:
            return HttpResponse(Response.error(mode, 'Failed'))

        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode == 'add-collection':
        metadata = request.GET.get('metadata', '')
        payload = request.GET.get('payload', '')
        logger.debug('metadata: ' + metadata)
        logger.debug('payload: ' + payload)
        if not Collection.add(metadata, payload):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'delete-collection':
        metadata = request.GET.get('metadata', '')
        if not Collection.remove(metadata):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'rpc':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_rpc(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))
    elif mode == 'gen-script':
        req = request.GET.get('payload', '')
        reply_xml = Adapter.gen_script(request.user.username, req)
        if isinstance(reply_xml, str):
            return HttpResponse(Response.success(mode, reply_xml))

    elif mode in ['get-cap', 'run-rpc', 'run-edit-commit', 'run-commit']:
        payload = request.GET.get('payload', '')
        logger.debug('run: ' + payload)
        reply_xml = Adapter.run_request(request.user.username, payload)

    return HttpResponse(Response.success(mode, 'ok', reply_xml))
Esempio n. 3
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)
Esempio n. 4
0
    def load(username, metadata):
        """ Load a collection entry """

        if metadata is None or metadata == '':
            logging.error('Invalid metadata')
            return None

        metadata = ET.fromstring(metadata)
        cname = metadata.find('collection').text
        name = metadata.find('name').text

        if not Col.objects.filter(name=cname).exists():
            logging.debug('Collection %s does not exists !!' % cname)
            return None

        _file = os.path.join('data', 'collections', cname, name + '.xml')
        if not os.path.exists(_file):
            logging.error('Collection entry not found')
            return None

        data = None
        with open(_file, 'r') as f:
            data = f.read()
            data = data.replace('>','>')
            data = data.replace('&lt;','<')
            payload = ET.fromstring(data)

        if data is None:
            logging.error('Collection entry is empty')
            return None

        fmt = payload.get('format', 'raw')
        if fmt == 'xpath':
            return Adapter.gen_rpc(username, data)
        return payload
Esempio n. 5
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)
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
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))
Esempio n. 9
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))
Esempio n. 10
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))
Esempio n. 11
0
def download_yang(request, req):
    """
    This API download yang schema from device
    """
    logger.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', 'Netconf agent address missing!!'))

    # clear session directory if it exists
    Uploader.clear_upload_files(None, request.session.session_key)

    # create session directory if it does not exist
    session_dir = Uploader.create_session_storage(request.session.session_key)
    if session_dir is None:
        logger.error('download_yang: Invalid session')
        return HttpResponse(Response.error('download', 'Invalid session_id'))

    # extact list of models from request
    req_xml = ET.fromstring(req)
    models = [sc.text.strip() for sc in req_xml.find('schemas')]

    # download all models recursively
    rpc = ET.fromstring(get_schema_rpc)
    download_helper(request.user.username, device, session_dir, rpc, models)

    # prepare list of downloaded models
    modules = ET.Element('modules')
    for _file in glob.glob(os.path.join(session_dir, '*.yang')):

        # see if we need to rename file with revision date
        parser = Parser(_file)
        new_fname = os.path.join(session_dir, parser.get_filename())
        if _file != new_fname:
            os.rename(_file, new_fname)

        module = ET.Element('module')
        module.text = os.path.basename(new_fname)
        modules.append(module)
    return modules
Esempio n. 12
0
def download_yang(request, req):
    """
    This API download yang schema from device
    """
    logger.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", "Netconf agent address missing!!"))

    # clear session directory if it exists
    Uploader.clear_upload_files(None, request.session.session_key)

    # create session directory if it does not exist
    session_dir = Uploader.create_session_storage(request.session.session_key)
    if session_dir is None:
        logger.error("download_yang: Invalid session")
        return HttpResponse(Response.error("download", "Invalid session_id"))

    # extact list of models from request
    req_xml = ET.fromstring(req)
    models = [sc.text.strip() for sc in req_xml.find("schemas")]

    # download all models recursively
    rpc = ET.fromstring(get_schema_rpc)
    download_helper(request.user.username, device, session_dir, rpc, models)

    # prepare list of downloaded models
    modules = ET.Element("modules")
    for _file in glob.glob(os.path.join(session_dir, "*.yang")):

        # see if we need to rename file with revision date
        parser = Parser(_file)
        new_fname = os.path.join(session_dir, parser.get_filename())
        if _file != new_fname:
            os.rename(_file, new_fname)

        module = ET.Element("module")
        module.text = os.path.basename(new_fname)
        modules.append(module)
    return modules
Esempio n. 13
0
def request_handler(request):
    """ HTTP Request handler function to handle actions on collections """

    mode = request.GET.get('mode', '')
    reply_xml = None

    logging.debug('request_handler: Received Request: (%s)' % mode)

    if mode == 'get-collection-list':
        reply_xml = Collection.list()

    elif mode == 'load-collection':
        metadata = request.GET.get('metadata', '')
        reply_xml = Collection.load(metadata)

        if reply_xml is None:
            return HttpResponse(Response.error(mode, 'Failed'))

    elif mode == 'add-collection':
        metadata = request.GET.get('metadata', '')
        payload = request.GET.get('payload', '')
        if not Collection.add(metadata, payload):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode == 'delete-collection':
        metadata = request.GET.get('metadata', '')
        if not Collection.remove(metadata):
            return HttpResponse(Response.error(mode, 'Failed'))

        reply_xml = Collection.list()
        mode = 'get-collection-list'

    elif mode in ['get-cap', 'run-rpc']:
        payload = request.GET.get('payload', '')
        reply_xml = Adapter.run_request(payload)

    return HttpResponse(Response.success(mode, 'ok', reply_xml))