Ejemplo n.º 1
0
def module_handler(request):
    '''
    Handle module request from UI. Response from this request builds
    UI Explorer tree
    '''
    logging.debug("module_handler: enter")
    lst = []
    if request.user.is_authenticated():
        modules = []
        path = request.GET.get('node', '')

        username = request.user.username
        if path == 'root':
             # Request for root models
            modules = ModuleAdmin.get_modulelist(username)
            modules.sort()
            path = ''
        else:
            modules = [path.split('/')[0]]

        for module in modules:
            filename = ModuleAdmin.cxml_path(username, module)
            if filename is not None:
                logging.debug("module_handler: loading " + filename)
                module = Cxml(filename)
                nodes = module.get_lazy_node(path)
                lst.extend([ET.tostring(node) for node in nodes])
            else:
                logging.error("module_handler: %s not found !!" + module)

    logging.debug("module_handler: exit")
    return render_to_response('loader.xml', {'nodes': lst}, RequestContext(request))
Ejemplo n.º 2
0
def module_handler(request):
    """
    Handle module request from UI. Response from this request builds
    UI Explorer tree
    """
    logging.debug("module_handler: enter")
    lst = []
    if request.user.is_authenticated():
        path = request.GET.get('node', '')

        username = request.user.username
        if path == 'root':
            # Request for root models
            modules = ModuleAdmin.get_modulelist(username)
            modules.sort()
            path = ''
        else:
            modules = [path.split('/')[0]]

        for module in modules:
            filename = ModuleAdmin.cxml_path(username, module)
            if filename is not None:
                logging.debug("module_handler: loading " + filename)
                module = Cxml(filename)
                nodes = module.get_lazy_node(path)
                lst.extend([ET.tostring(node) for node in nodes])
            else:
                logging.error("module_handler: %s not found !!" + module)

    logging.debug("module_handler: exit")
    return render_to_response('loader.xml', {'nodes': lst}, RequestContext(request))
Ejemplo n.º 3
0
def admin_handler(request):
    """ HTTP Request handler function to handle actions on yang modules """

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

    if request.method != 'GET':
        return HttpResponse(Response.error(None, 'Invalid admin Request'))

    action = request.GET.get('action', '')
    logger.info('Received admin request %s for user %s' %
                (action, request.user.username))

    if action in ['subscribe', 'unsubscribe', 'delete', 'graph']:
        payload = request.GET.get('payload', None)
        print(str(payload))
        (rc, msg) = ModuleAdmin.admin_action(request.user.username, payload,
                                             action)
        if not rc:
            return HttpResponse(Response.error(action, msg))

    if action == 'graph':
        return HttpResponse(Response.success(action, msg))

    modules = ModuleAdmin.get_modules(request.user.username)
    return HttpResponse(Response.success(action, 'ok', xml=modules))
Ejemplo n.º 4
0
def module_handler(request):
    """
    Handle module request from UI. Response from this request builds
    UI Explorer tree
    """
    logger.debug("module_handler: enter")
    lst = []
    if request.user.is_authenticated():
        path = request.GET.get('node', '')
        deep = request.GET.get('deep', '')
        username = request.user.username
        if path == 'root':
            # Request for root models
            modules = ModuleAdmin.get_modulelist(username)
            modules.sort()
            for m in modules:
                lst.append(node_t.format(m.split('@')[0]))
        else:
            modules = [path.split('/')[0]]
            for module in modules:
                filename = ModuleAdmin.cxml_path(username, module)
                if filename is not None:
                    logger.debug("module_handler: loading " + filename)
                    module = cxml.get_cxml(filename)
                    nodes = module.get_lazy_subtree(path, deep)
                    lst.extend([ET.tostring(node) for node in annotate(nodes)])
                else:
                    logger.error("module_handler: %s not found !!" + module)

    logger.debug("module_handler: exit")
    return render_to_response('loader.xml', {'nodes': lst}, RequestContext(request))
Ejemplo n.º 5
0
def search_module(username, module, query):
    """ Search query in one module """
    result = []
    filename = ModuleAdmin.cxml_path(username, module)
    cxml_i = CxmlIterator(filename)
    for path, _ in cxml_i:
        if query in path:
            result.append(path)
    return result
Ejemplo n.º 6
0
def search_module(username, module, query):
    """ Search query in one module """
    result = []
    filename = ModuleAdmin.cxml_path(username, module)
    cxml_i = CxmlIterator(filename)
    for path, _ in cxml_i:
        if query in path:
            result.append(path)
    return result
Ejemplo n.º 7
0
def admin_handler(request):
    """ HTTP Request handler function to handle actions on yang modules """

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

    if request.method != 'GET':
        return HttpResponse(Response.error(None, 'Invalid admin Request'))

    action = request.GET.get('action', '')
    logging.debug('Received admin request %s for user %s' % (action, request.user.username))

    if action in ['subscribe', 'unsubscribe', 'delete']:
        payload = request.GET.get('payload', None)
        if not ModuleAdmin.admin_action(request.user.username, payload, action):
            return HttpResponse(Response.error(action, 'Failed to %s' % action))

    modules = ModuleAdmin.get_modules(request.user.username)
    return HttpResponse(Response.success('modulelist', 'ok', xml=modules))
Ejemplo n.º 8
0
def gen_netconf(username, request, mode):
    msg = ''
    rpc = ''

    _format = request.get('format', 'xpath')
    if mode == '': mode = request.get('operation', '')

    logging.debug('Generating netconf RPC, operation : "%s" format: %s' %
                  (mode, _format))

    if _format == 'raw':
        rpc = request.find('rpc').text
        rpc = rpc.replace('>', '>')
        rpc = rpc.replace('&lt;', '<')
        return convert_rpc(rpc, mode)
    # Parse test-payload key-values pairs for each modules and
    # create a dictionary of modules.
    modules = parseRequest(request)

    # Process each module key-value separatly
    for name in modules:
        module = modules[name]
        kvDict = module.get_keyvalues()
        logging.debug('Opening file %s.xml' % name)

        filename = ModuleAdmin.cxml_path(username, name)
        if filename is None:
            logging.debug('file %s.xml not found !!' % name)
            continue

        with open(filename, 'r') as f:
            tree = ET.parse(f).getroot()
            logging.info("Root node %s" % tree.get('name'))
            # get root namespace for module
            ns = get_namespace(tree)

            # get derived namespaces
            prefixes = module.get_namespace_pfx()
            for pfx in prefixes:
                ns += get_namespace(tree, pfx)

            # start processing CXML
            for child in tree:
                if child.tag != 'namespace':
                    msg += processXML(request, tree, kvDict, child, name, ns,
                                      mode)

    # Finally build RPC header
    rpc += buildRPC(request, tree, msg, mode)
    logging.debug('Generated netconf RPC')
    logging.debug(rpc)
    return rpc
Ejemplo n.º 9
0
def gen_netconf(username, request, mode):
    msg = ''
    rpc = ''

    _format = request.get('format', 'xpath')    
    if mode == '': mode = request.get('operation', '')

    logging.debug('Generating netconf RPC, operation : "%s" format: %s' % (mode, _format))
    
    if _format == 'raw':
        rpc = request.find('rpc').text
        rpc = rpc.replace('&gt;','>')
        rpc = rpc.replace('&lt;','<')
        return convert_rpc(rpc, mode)
    # Parse test-payload key-values pairs for each modules and
    # create a dictionary of modules.
    modules =  parseRequest(request)

    # Process each module key-value separatly
    for name in modules:
        module = modules[name]
        kvDict = module.get_keyvalues()
        logging.debug('Opening file %s.xml' % name)
        
        filename = ModuleAdmin.cxml_path(username, name)
        if filename is None:
            logging.debug('file %s.xml not found !!' % name)
            continue

        with open(filename, 'r') as f:
            tree = ET.parse(f).getroot()
            logging.info("Root node %s" % tree.get('name'))
            # get root namespace for module
            ns = get_namespace(tree)

            # get derived namespaces
            prefixes  = module.get_namespace_pfx()
            for pfx in prefixes:
                ns += get_namespace(tree, pfx)

            # start processing CXML
            for child in tree:
                if child.tag != 'namespace':
                    msg += process_xml(tree, kvDict, child, name, ns, mode)

    # Finally build RPC header
    rpc += build_rpc(request, msg, mode)
    logging.debug('Generated netconf RPC')
    logging.debug(rpc)
    return rpc
Ejemplo n.º 10
0
def admin_handler(request):
    """ HTTP Request handler function to handle actions on yang modules """

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

    if request.method != 'GET':
        return HttpResponse(Response.error(None, 'Invalid admin Request'))

    action = request.GET.get('action', '')
    logger.info('Received admin request %s for user %s' % (action, request.user.username))

    if action in ['subscribe', 'unsubscribe', 'delete', 'graph']:
        payload = request.GET.get('payload', None)
        print(str(payload))
        (rc, msg) = ModuleAdmin.admin_action(request.user.username, payload, action)
        if not rc:
            return HttpResponse(Response.error(action, msg))

    if action == 'graph':
        return HttpResponse(Response.success(action, msg))

    modules = ModuleAdmin.get_modules(request.user.username)
    return HttpResponse(Response.success(action, 'ok', xml=modules))
Ejemplo n.º 11
0
def search(username, query):
    """
    Search query text in user modules
    Args:
        username: Request username
        query: Search String

    Returns: An XML object with result XPATHs
    """
    logging.debug('Searching query %s in user (%s) modules' % (query, username))
    response = ET.Element('result')

    modulenames = ModuleAdmin.get_modulelist(username)
    for module in modulenames:
        result = search_module(username, module, query)
        for xpath in result:
            path = ET.Element('path')
            path.set('module', module)
            path.text = xpath
            response.append(path)

    return True, response
Ejemplo n.º 12
0
def search(username, query):
    """
    Search query text in user modules
    Args:
        username: Request username
        query: Search String

    Returns: An XML object with result XPATHs
    """
    logging.debug('Searching query %s in user (%s) modules' %
                  (query, username))
    response = ET.Element('result')

    modulenames = ModuleAdmin.get_modulelist(username)
    for module in modulenames:
        result = search_module(username, module, query)
        for xpath in result:
            path = ET.Element('path')
            path.set('module', module)
            path.text = xpath
            response.append(path)

    return True, response
Ejemplo n.º 13
0
def parse_url(username, request, mode):
    '''
    Main function that creates a URL and message body that uses the cxml (lxml)
    Element nodes from a defined test from the YangExplorer GUI

    Rules:
    https://tools.ietf.org/html/draft-ietf-netconf-restconf-09

    No option attribute defaults to PATCH operation with shortest possible URL.
    Rest of data is put in message body in JSON format.

    Option found in path denotes length of URL.  Any data beyond option is put
    into message body.
    '''
    keyvalue = request.find('keyvalue')
    cxml = None
    name = ''
    tpath = []
    master_segment = None
    op, op_location = get_op(keyvalue, mode)
    paths = []
    url = None
    pdict = {}
    msg = {}

    #pdb.set_trace()
    for child in keyvalue:

        path = child.get('path', '')
        path = path.split("/")
        if not cxml:
            name = path[0]
            url = [path[1]]
            #if op not in ['delete', 'replace']:
            #    return (name, op, url)
            filename = ModuleAdmin.cxml_path(username, path[0])
            cxml = et.parse(filename)
        paths.append((path, child))

    prev_seg = False

    for path, child in paths:

        rt = cxml.getroot()
        prev_list = False
        depth = 0

        for p in path:
            depth += 1
            chld = rt.getchildren()
            for n in chld:
                if n.attrib and n.attrib.get('name') == p:
                    if prev_list:
                        if n.attrib.get('is_key') == 'true':
                            if n not in prev_list.keys:
                                t = n.attrib.get('name')
                                index = prev_list.keys.index(t[t.find(':') +
                                                               1:])
                                s = Segment(n)
                                s.text = child.text
                                prev_list.keys[index] = s
                            more = [
                                f for f in prev_list.keys
                                if not isinstance(f, Segment)
                            ]
                            if not more:
                                prev_list = False
                            rt = n
                            continue
                    if n.attrib.get('type') in [
                            'leaf', 'leafref', 'leaf-list'
                    ]:
                        if prev_seg:
                            prev_seg.leaves.append(Segment(n, child.text))
                        if n not in tpath:
                            tpath.append(Segment(n, child.text))
                    elif n.attrib.get('type') == 'list':
                        if n in tpath:
                            for t in tpath:
                                if n == t:
                                    prev_list = t
                        else:
                            prev_list = Segment(n)
                            if not master_segment:
                                master_segment = prev_list
                            elif prev_seg:
                                prev_seg.segments.append(prev_list)
                            prev_list.depth = depth
                            tpath.append(prev_list)
                            prev_list.keys = n.attrib.get('key').split()
                        prev_seg = prev_list
                        rt = n
                        break
                    elif n.attrib.get('type') in ['container']:
                        if n in tpath:
                            for t in tpath:
                                if n == t:
                                    prev_seg = t
                        else:
                            cont = Segment(n)
                            cont.depth = depth
                            if not master_segment:
                                master_segment = cont
                            elif prev_seg:
                                for i, t in enumerate(tpath):
                                    if t.name == prev_seg.name and t.depth == depth - 1:
                                        t.segments.append(cont)
                                        break
                            prev_seg = cont
                            tpath.append(prev_seg)

                        rt = n
                        break
                    elif n.attrib.get('type') in ['case', 'choice']:
                        depth -= 1
                        rt = n
                        break

    if op not in ["get", "get-config", 'delete']:
        msg = build_msg([tpath[op_location:][0]], OrderedDict())
    if op_location:
        url = []
        for i, seg in enumerate(tpath):
            if seg.type in ['leaf', 'leafref', 'leaf-list']:
                if op in ["get", "get-config", 'delete']:
                    if len(tpath) - 1 >= i:
                        continue
                else:
                    continue
            s = url_escape_chars(seg.name)
            url.append(s)
            if op not in ["get", "get-config", 'delete'] and i == op_location:
                break
            if seg.type == 'list':
                keys = []
                for key in seg.keys:
                    if key is None: break
                    if isinstance(key, str): continue
                    keys.append(key.text)

                if len(keys) > 0:
                    k = ','.join(keys)
                    k = url_escape_chars(k)
                    url.append(k)

    return (name, op, '/'.join(url), json.dumps(msg, indent=2))
Ejemplo n.º 14
0
def parse_url(username, request, mode):
    '''
    Main function that creates a URL and message body that uses the cxml (lxml)
    Element nodes from a defined test from the YangExplorer GUI

    Rules:
    https://tools.ietf.org/html/draft-ietf-netconf-restconf-09

    No option attribute defaults to PATCH operation with shortest possible URL.
    Rest of data is put in message body in JSON format.

    Option found in path denotes length of URL.  Any data beyond option is put
    into message body.
    '''
    keyvalue = request.find('keyvalue')
    cxml = None
    name = ''
    tpath = []
    master_segment = None
    op, op_location = get_op(keyvalue, mode)
    paths = []
    url = None
    pdict = {}
    msg = {}

    #pdb.set_trace()
    for child in keyvalue:

        path = child.get('path', '')
        path = path.split("/")
        if not cxml:
            name = path[0]
            url = [path[1]]
            #if op not in ['delete', 'replace']:
            #    return (name, op, url)
            filename = ModuleAdmin.cxml_path(username, path[0])
            cxml = et.parse(filename)
        paths.append((path, child))

    prev_seg = False

    for path, child in paths:

        rt = cxml.getroot()
        prev_list = False
        depth = 0

        for p in path:
            depth += 1
            chld = rt.getchildren()
            for n in chld:
                if n.attrib and n.attrib.get('name') == p:
                    if prev_list:
                        if n.attrib.get('is_key') == 'true':
                            if n not in prev_list.keys:
                                t = n.attrib.get('name')
                                index = prev_list.keys.index(t[t.find(':')+1:])
                                s = Segment(n)
                                s.text = child.text
                                prev_list.keys[index] = s
                            more = [f for f in prev_list.keys if not isinstance(f, Segment)]
                            if not more:
                                prev_list = False
                            rt = n
                            continue
                    if n.attrib.get('type') in ['leaf', 'leafref', 'leaf-list']:
                        if prev_seg:
                            prev_seg.leaves.append(Segment(n, child.text))
                        if n not in tpath:
                            tpath.append(Segment(n, child.text))
                    elif n.attrib.get('type') == 'list':
                        if n in tpath:
                            for t in tpath:
                                if n == t:
                                    prev_list = t
                        else:
                            prev_list = Segment(n)
                            if not master_segment:
                                master_segment = prev_list
                            elif prev_seg:
                                prev_seg.segments.append(prev_list)
                            prev_list.depth = depth
                            tpath.append(prev_list)
                            prev_list.keys = n.attrib.get('key').split()
                        prev_seg = prev_list
                        rt = n
                        break
                    elif n.attrib.get('type') in ['container']:
                        if n in tpath:
                            for t in tpath:
                                if n == t:
                                    prev_seg = t
                        else:
                            cont = Segment(n)
                            cont.depth = depth
                            if not master_segment:
                                master_segment = cont
                            elif prev_seg:
                                for i, t in enumerate(tpath):
                                    if t.name == prev_seg.name and t.depth == depth-1:
                                        t.segments.append(cont)
                                        break
                            prev_seg = cont
                            tpath.append(prev_seg)

                        rt = n
                        break
                    elif n.attrib.get('type') in ['case', 'choice']:
                        depth -= 1
                        rt = n
                        break

    if op not in ["get", "get-config", 'delete']:
        msg = build_msg([tpath[op_location:][0]], OrderedDict())
    if op_location:
        url = []
        for i, seg in enumerate(tpath):
            if seg.type in ['leaf', 'leafref', 'leaf-list']:
                if op in ["get", "get-config", 'delete']:
                    if len(tpath)-1 >= i:
                        continue
                else:
                    continue
            s = url_escape_chars(seg.name)
            url.append(s)
            if op not in ["get", "get-config", 'delete'] and i == op_location:
                break
            if seg.type == 'list':
                keys = []
                for key in seg.keys:
                    if key is None: break
                    if isinstance(key, str): continue;
                    keys.append(key.text)

                if len(keys) > 0:
                    k = ','.join(keys)
                    k = url_escape_chars(k)
                    url.append(k)

    return (name, op, '/'.join(url), json.dumps(msg, indent=2))