Esempio n. 1
0
def create_matrix(scope, show_unused):
    """Creates a matrix for the given scope"""
    tree = build_tree(scope)
    if scope.version() == 6:
        if scope.prefixlen() < 60:
            end_net = IP(scope.net().strNormal() + '/64')
            matrix = MatrixIPv6(scope, end_net=end_net)
        else:
            end_net = get_max_leaf(tree)
            matrix = MatrixIPv6(scope, end_net=end_net)
    elif scope.version() == 4:
        if scope.prefixlen() < 24:
            end_net = IP(scope.net().strNormal() + '/30')
            matrix = MatrixIPv4(scope,
                                show_unused,
                                end_net=end_net,
                                bits_in_matrix=6)
        else:
            max_leaf = get_max_leaf(tree)
            bits_in_matrix = max_leaf.prefixlen() - scope.prefixlen()
            matrix = MatrixIPv4(scope,
                                show_unused,
                                end_net=max_leaf,
                                bits_in_matrix=bits_in_matrix)
    else:
        raise UnknownNetworkTypeException('version: ' + str(scope.version))

    # Invalidating the MetaIP cache to get rid of processed data.
    MetaIP.invalidateCache()

    matrix.build()
    return matrix
Esempio n. 2
0
def matrix_report(request):
    """Subnet matrix view"""

    show_unused = request.GET.get('show_unused_addresses', False)

    context = {
        'navpath': [
            ('Home', '/'),
            ('Report', '/report/'),
            ('Subnet matrix', False)
        ],
        'show_unused': show_unused
    }

    if 'scope' not in request.GET:
        cursor = connection.cursor()
        cursor.execute("""
            SELECT netaddr, description
            FROM prefix
            INNER JOIN vlan USING (vlanid)
            WHERE nettype='scope'
        """.strip())

        if cursor.rowcount == 1:
            # Id there is only one scope in the database,
            # display that scope
            scope = IP(cursor.fetchone()[0])
        else:
            # Else let the user select from a list
            scopes = cursor.fetchall()
            context['scopes'] = scopes
            return render_to_response(
                'report/matrix.html',
                context,
                context_instance=RequestContext(request))
    else:
        scope = IP(request.GET.get('scope'))

    tree = buildTree(scope)
    if scope.version() == 6:
        end_net = getMaxLeaf(tree)
        matrix = MatrixIPv6(scope, end_net=end_net)
    elif scope.version() == 4:
        if scope.prefixlen() < 24:
            end_net = IP(scope.net().strNormal() + '/30')
            matrix = MatrixIPv4(scope, show_unused, end_net=end_net,
                                bits_in_matrix=6)
        else:
            max_leaf = getMaxLeaf(tree)
            bits_in_matrix = max_leaf.prefixlen() - scope.prefixlen()
            matrix = MatrixIPv4(
                scope,
                show_unused,
                end_net=max_leaf,
                bits_in_matrix=bits_in_matrix)
    else:
        raise UnknownNetworkTypeException(
            'version: ' + str(scope.version))

    # Invalidating the MetaIP cache to get rid of processed data.
    MetaIP.invalidateCache()

    matrix.build()

    hide_content_for_colspan = []
    if scope.version() == 4:
        hide_content_for_colspan = [1, 2, 4]

    context.update({
        'matrix': matrix,
        'sub': matrix.end_net.prefixlen() - matrix.bits_in_matrix,
        'ipv4': scope.version() == 4,
        'hide_for': hide_content_for_colspan
    })

    return render_to_response(
        'report/matrix.html',
        context,
        context_instance=RequestContext(request))