Example #1
0
File: views.py Project: hmpf/nav
def set_voice_vlan(handler: ManagementHandler, interface, request):
    """Set voicevlan on interface

    A voice vlan is a normal vlan that is defined by the user of NAV as
    a vlan that is used only for ip telephone traffic.

    To set a voice vlan we have to make sure the interface is configured
    to tag both the voicevlan and the "access-vlan".

    """
    if 'voicevlan' in request.POST:
        cdp_changed = False
        voice_vlan = fetch_voice_vlan_for_netbox(request, handler)
        use_cisco_voice_vlan = CONFIG.is_cisco_voice_enabled() and is_cisco(
            interface.netbox)
        enable_cdp_for_cisco_voice_port = CONFIG.is_cisco_voice_cdp_enabled()

        # Either the voicevlan is turned off or turned on
        turn_on_voice_vlan = request.POST.get('voicevlan') == 'true'
        account = get_account(request)
        try:
            if turn_on_voice_vlan:
                if use_cisco_voice_vlan:
                    handler.set_cisco_voice_vlan(interface, voice_vlan)
                    if enable_cdp_for_cisco_voice_port:
                        handler.enable_cisco_cdp(interface)
                        cdp_changed = True
                else:
                    handler.set_interface_voice_vlan(interface, voice_vlan)
                _logger.info(
                    '%s: %s:%s - %s%s',
                    account.login,
                    interface.netbox.get_short_sysname(),
                    interface.ifname,
                    'voice vlan enabled',
                    ', CDP enabled' if cdp_changed else '',
                )
            else:
                if use_cisco_voice_vlan:
                    handler.disable_cisco_voice_vlan(interface)
                    if enable_cdp_for_cisco_voice_port:
                        handler.disable_cisco_cdp(interface)
                        cdp_changed = True
                else:
                    handler.set_access(interface, interface.vlan)
                _logger.info(
                    '%s: %s:%s - %s%s',
                    account.login,
                    interface.netbox.get_short_sysname(),
                    interface.ifname,
                    'voice vlan disabled',
                    ', CDP disabled' if cdp_changed else '',
                )
        except (ManagementError, ValueError, NotImplementedError) as error:
            messages.error(request, "Error setting voicevlan: %s" % error)
Example #2
0
File: views.py Project: hmpf/nav
def set_vlan(account, handler: ManagementHandler, interface, request):
    """Set vlan on netbox if it is requested"""
    if 'vlan' in request.POST:
        try:
            vlan = int(request.POST.get('vlan'))
        except ValueError:
            messages.error(
                request,
                "Ignoring request to set vlan={}".format(
                    request.POST.get('vlan')),
            )
            return

        try:
            if is_cisco(interface.netbox):
                # If Cisco and trunk voice vlan (not Cisco voice vlan),
                # we have to set native vlan instead of access vlan
                voice_activated = request.POST.get('voice_activated', False)
                if not CONFIG.is_cisco_voice_enabled() and voice_activated:
                    handler.set_native_vlan(interface, vlan)
                else:
                    handler.set_vlan(interface, vlan)
            else:
                handler.set_vlan(interface, vlan)

            interface.vlan = vlan
            LogEntry.add_log_entry(
                account,
                u'set-vlan',
                u'{actor}: {object} - vlan set to "%s"' % vlan,
                subsystem=u'portadmin',
                object=interface,
            )
            _logger.info(
                '%s: %s:%s - vlan set to %s',
                account.login,
                interface.netbox.get_short_sysname(),
                interface.ifname,
                vlan,
            )
        except (ManagementError, TypeError) as error:
            _logger.error('Error setting vlan: %s', error)
            messages.error(request, "Error setting vlan: %s" % error)
Example #3
0
File: views.py Project: hmpf/nav
def populate_infodict(request, netbox, interfaces):
    """Populate a dictionary used in every http response"""
    allowed_vlans = []
    voice_vlan = None
    readonly = False
    handler = None

    try:
        handler = get_and_populate_livedata(netbox, interfaces)
        allowed_vlans = find_and_populate_allowed_vlans(
            request.account, netbox, interfaces, handler)
        voice_vlan = fetch_voice_vlan_for_netbox(request, handler)
        if voice_vlan:
            if CONFIG.is_cisco_voice_enabled() and is_cisco(netbox):
                set_voice_vlan_attribute_cisco(voice_vlan, interfaces, handler)
            else:
                set_voice_vlan_attribute(voice_vlan, interfaces)
        mark_detained_interfaces(interfaces)
        if CONFIG.is_dot1x_enabled():
            add_dot1x_info(interfaces, handler)
    except NoResponseError:
        readonly = True
        messages.error(
            request,
            "%s did not respond within the set timeouts. Values displayed are from database"
            % netbox.sysname,
        )
        if isinstance(handler, SNMPHandler) and not netbox.read_only:
            messages.error(request, "Read only community not set")
    except ProtocolError:
        readonly = True
        messages.error(
            request,
            "Protocol error when contacting %s. Values displayed are from database"
            % netbox.sysname,
        )
    except DeviceNotConfigurableError as error:
        readonly = True
        messages.error(request, str(error))

    if handler and not handler.is_configurable():
        add_readonly_reason(request, handler)
        readonly = True

    ifaliasformat = CONFIG.get_ifaliasformat()
    aliastemplate = ''
    if ifaliasformat:
        tmpl = get_aliastemplate()
        aliastemplate = tmpl.render({'ifaliasformat': ifaliasformat})

    save_to_database(interfaces)

    auditlog_api_parameters = {
        'object_model': 'interface',
        'object_pks': ','.join([str(i.pk) for i in interfaces]),
        'subsystem': 'portadmin',
    }

    info_dict = get_base_context([(netbox.sysname, )], form=get_form(request))
    info_dict.update({
        'handlertype':
        type(handler).__name__,
        'interfaces':
        interfaces,
        'netbox':
        netbox,
        'voice_vlan':
        voice_vlan,
        'allowed_vlans':
        allowed_vlans,
        'readonly':
        readonly,
        'aliastemplate':
        aliastemplate,
        'trunk_edit':
        CONFIG.get_trunk_edit(),
        'auditlog_api_parameters':
        json.dumps(auditlog_api_parameters),
    })
    return info_dict