Exemplo n.º 1
0
def get_dict_v6_to_use_in_configuration_deploy(user, networkipv6,
                                               equipment_list):
    """Generate dictionary with vlan an IP information to be used to generate
    template dict for equipment configuration

    Args: networkipv4 NetworkIPv4 object
    equipment_list: Equipamento objects list

    Returns: 2-dimension dictionary with equipments information for template rendering
    """

    try:
        gateway_ip = Ipv6.get_by_blocks_and_net(
            '{0:0{1}x}'.format(int(networkipv6.block1, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block2, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block3, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block4, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block5, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block6, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block7, 16), 4),
            '{0:0{1}x}'.format(int(networkipv6.block8, 16) + 1,
                               4), networkipv6)
    except IpNotFoundError:
        log.error('Equipment IPs not correctly registered. \
            Router equipments should have first IP of network allocated for them.'
                  )
        raise exceptions.IncorrectRedundantGatewayRegistryException()

    ips = Ipv6Equipament.objects.filter(ip=gateway_ip,
                                        equipamento__in=equipment_list)
    if len(ips) != len(equipment_list):
        log.error('Equipment IPs not correctly registered. \
            Router equipments should have first IP of network allocated for them.'
                  )
        raise exceptions.IncorrectRedundantGatewayRegistryException()

    dict_ips = dict()
    if networkipv6.vlan.vrf is not None and networkipv6.vlan.vrf is not '':
        dict_ips['vrf'] = networkipv6.vlan.vrf
    elif networkipv6.vlan.ambiente.vrf is not None:
        dict_ips['vrf'] = networkipv6.vlan.ambiente.vrf

    dict_ips['gateway'] = '%s:%s:%s:%s:%s:%s:%s:%s' % (
        gateway_ip.block1, gateway_ip.block2, gateway_ip.block3,
        gateway_ip.block4, gateway_ip.block5, gateway_ip.block6,
        gateway_ip.block7, gateway_ip.block8)
    dict_ips['ip_version'] = 'IPV6'
    dict_ips['equipments'] = dict()
    dict_ips['vlan_num'] = networkipv6.vlan.num_vlan
    dict_ips['vlan_name'] = networkipv6.vlan.nome
    dict_ips['cidr_block'] = networkipv6.block
    dict_ips['mask'] = '%s:%s:%s:%s:%s:%s:%s:%s' % (
        networkipv6.mask1, networkipv6.mask2, networkipv6.mask3,
        networkipv6.mask4, networkipv6.mask5, networkipv6.mask6,
        networkipv6.mask7, networkipv6.mask8)
    dict_ips['wildmask'] = 'Not used'

    if _has_active_network_in_vlan(networkipv6.vlan):
        dict_ips['first_network'] = False
    else:
        dict_ips['first_network'] = True

    # Check IPs for routers when there are multiple gateways
    if len(equipment_list) > 1:
        dict_ips['gateway_redundancy'] = True
        equip_number = 0
        for equipment in equipment_list:
            ip_equip = Ipv6Equipament.objects.filter(equipamento=equipment, ip__networkipv6=networkipv6).exclude(ip=gateway_ip)\
                .select_related('ip')
            if ip_equip == []:
                log.error('Error: Equipment IPs not correctly registered. \
                    In case of multiple gateways, they should have an IP other than the gateway registered.'
                          )
                raise exceptions.IncorrectNetworkRouterRegistryException()
            ip = ip_equip[0].ip
            dict_ips[equipment] = dict()
            dict_ips[equipment]['ip'] = '%s:%s:%s:%s:%s:%s:%s:%s' % (
                ip.block1, ip.block2, ip.block3, ip.block4, ip.block5,
                ip.block6, ip.block7, ip.block8)
            dict_ips[equipment]['prio'] = 100 + equip_number
            equip_number += 1
    else:
        dict_ips['gateway_redundancy'] = False
        dict_ips[equipment_list[0]] = dict()
        dict_ips[equipment_list[0]]['ip'] = dict_ips['gateway']
        dict_ips[equipment_list[0]]['prio'] = 100

    return dict_ips
Exemplo n.º 2
0
def get_dict_v4_to_use_in_configuration_deploy(user, networkipv4,
                                               equipment_list):
    """Generate dictionary with vlan an IP information to be used to generate
    template dict for equipment configuration

    Args: networkipv4 NetworkIPv4 object
    equipment_list: Equipamento objects list

    Returns: 2-dimension dictionary with equipments information for template
             rendering
    """

    try:
        gateway_ip = ip_models.Ip.get_by_octs_and_net(
            networkipv4.oct1, networkipv4.oct2,
            networkipv4.oct3, networkipv4.oct4 + 1, networkipv4)
    except ip_models.IpNotFoundError:
        log.error('Equipment IPs not correctly registered.'
                  'Router equipments should have first IP of '
                  'network allocated for them.')
        raise exceptions.IncorrectRedundantGatewayRegistryException()

    # Default Vrf of environment
    default_vrf = networkipv4.vlan.ambiente.default_vrf

    for equipment in equipment_list:
        # Verify if equipments have Ip of gateway
        try:
            gateway_ip.ipequipamento_set.get(equipamento=equipment)
        except ObjectDoesNotExist:
            log.error('Equipment IPs not correctly registered.'
                      'Router equipments should have first IP '
                      'of network allocated for them. Equipment: %s' %
                      equipment)
            raise exceptions.IncorrectRedundantGatewayRegistryException()

        # Get internal name of vrf to set in equipment
        # Can be empty, a default value of environment or a
        # value by vlan + equipment
        try:
            vrf_eqpt = equipment.vrfvlanequipment_set.filter(
                vlan=networkipv4.vlan
            ).uniqueResult()
            # Customized vrf
            vrf = vrf_eqpt.vrf
        except ObjectDoesNotExist:
            # Default vrf
            vrf = default_vrf
        finally:
            try:
                # Customized internal name of vrf for this equipment
                vrf_eqpt = equipment.vrfequipment_set.filter(
                    vrf=vrf
                ).uniqueResult()
                vrf_name = vrf_eqpt.internal_name
            except ObjectDoesNotExist:
                vrf_name = vrf.internal_name

    dict_ips = dict()

    if vrf_name:
        dict_ips['vrf'] = vrf_name

    # DHCPRelay list
    dhcprelay_list = networkipv4.dhcprelay

    if dhcprelay_list:

        dict_ips['dhcprelay_list'] = list()
        for dhcprelay in dhcprelay_list:

            ipv4 = dhcprelay.ipv4.ip_formated
            dict_ips['dhcprelay_list'].append(ipv4)

    dict_ips['gateway'] = gateway_ip.ip_formated
    dict_ips['ip_version'] = 'IPV4'
    dict_ips['equipments'] = dict()
    dict_ips['vlan_num'] = networkipv4.vlan.num_vlan
    dict_ips['vlan_name'] = networkipv4.vlan.nome
    dict_ips['cidr_block'] = networkipv4.block
    dict_ips['mask'] = networkipv4.mask_formated
    dict_ips['wildmask'] = networkipv4.wildcard

    has_active = utils.has_active_network_in_vlan(networkipv4.vlan)
    dict_ips['first_network'] = has_active is False

    # Check IPs for routers when there are multiple gateways
    if len(equipment_list) > 1:
        dict_ips['gateway_redundancy'] = True
        equip_number = 0
        for equipment in equipment_list:

            # Verify if equipment have more ips
            ip_equip = equipment.ipequipamento_set.filter(
                ip__networkipv4=networkipv4
            ).exclude(ip=gateway_ip).select_related('ip')

            if not ip_equip:
                log.error('Equipment IPs not correctly registered. '
                          'In case of multiple gateways, they should '
                          'have an IP other than the gateway registered.'
                          'Equipment: %s' % equipment.id)
                raise exceptions.IncorrectNetworkRouterRegistryException()

            ip = ip_equip[0].ip
            dict_ips[equipment] = dict()
            dict_ips[equipment]['ip'] = ip.ip_formated
            dict_ips[equipment]['prio'] = 100 + equip_number
            equip_number += 1
    else:
        dict_ips['gateway_redundancy'] = False
        dict_ips[equipment_list[0]] = dict()
        dict_ips[equipment_list[0]]['ip'] = dict_ips['gateway']
        dict_ips[equipment_list[0]]['prio'] = 100

    dict_ips['is_vxlan'] = networkipv4.vlan.vxlan

    return dict_ips
Exemplo n.º 3
0
def get_dict_v4_to_use_in_configuration_deploy(user, networkipv4,
                                               equipment_list):
    """Generate dictionary with vlan an IP information to be used to generate
    template dict for equipment configuration

    Args: networkipv4 NetworkIPv4 object
    equipment_list: Equipamento objects list

    Returns: 2-dimension dictionary with equipments information for template rendering
    """

    try:
        gateway_ip = Ip.get_by_octs_and_net(networkipv4.oct1, networkipv4.oct2,
                                            networkipv4.oct3,
                                            networkipv4.oct4 + 1, networkipv4)
    except IpNotFoundError:
        log.error('Equipment IPs not correctly registered. \
            Router equipments should have first IP of network allocated for them.'
                  )
        raise exceptions.IncorrectRedundantGatewayRegistryException()

    ips = IpEquipamento.objects.filter(ip=gateway_ip,
                                       equipamento__in=equipment_list)
    if len(ips) != len(equipment_list):
        log.error('Equipment IPs not correctly registered. \
            Router equipments should have first IP of network allocated for them.'
                  )
        raise exceptions.IncorrectRedundantGatewayRegistryException()

    dict_ips = dict()
    if networkipv4.vlan.vrf is not None and networkipv4.vlan.vrf is not '':
        dict_ips['vrf'] = networkipv4.vlan.vrf
    elif networkipv4.vlan.ambiente.vrf is not None and networkipv4.vlan.ambiente.vrf is not '':
        dict_ips['vrf'] = networkipv4.vlan.ambiente.vrf

    # DHCPRelay list
    dhcprelay_list = DHCPRelayIPv4.objects.filter(networkipv4=networkipv4)
    if len(dhcprelay_list) > 0:
        dict_ips['dhcprelay_list'] = []
        for dhcprelay in dhcprelay_list:
            ipv4 = '%s.%s.%s.%s' % (dhcprelay.ipv4.oct1, dhcprelay.ipv4.oct2,
                                    dhcprelay.ipv4.oct3, dhcprelay.ipv4.oct4)
            dict_ips['dhcprelay_list'].append(ipv4)

    dict_ips['gateway'] = '%d.%d.%d.%d' % (gateway_ip.oct1, gateway_ip.oct2,
                                           gateway_ip.oct3, gateway_ip.oct4)
    dict_ips['ip_version'] = 'IPV4'
    dict_ips['equipments'] = dict()
    dict_ips['vlan_num'] = networkipv4.vlan.num_vlan
    dict_ips['vlan_name'] = networkipv4.vlan.nome
    dict_ips['cidr_block'] = networkipv4.block
    dict_ips['mask'] = '%d.%d.%d.%d' % (
        networkipv4.mask_oct1, networkipv4.mask_oct2, networkipv4.mask_oct3,
        networkipv4.mask_oct4)
    dict_ips['wildmask'] = '%d.%d.%d.%d' % (
        255 - networkipv4.mask_oct1, 255 - networkipv4.mask_oct2,
        255 - networkipv4.mask_oct3, 255 - networkipv4.mask_oct4)

    if _has_active_network_in_vlan(networkipv4.vlan):
        dict_ips['first_network'] = False
    else:
        dict_ips['first_network'] = True

    # Check IPs for routers when there are multiple gateways
    if len(equipment_list) > 1:
        dict_ips['gateway_redundancy'] = True
        equip_number = 0
        for equipment in equipment_list:
            ip_equip = IpEquipamento.objects.filter(equipamento=equipment, ip__networkipv4=networkipv4).exclude(ip=gateway_ip)\
                .select_related('ip')
            if ip_equip == []:
                log.error('Error: Equipment IPs not correctly registered. \
                    In case of multiple gateways, they should have an IP other than the gateway registered.'
                          )
                raise exceptions.IncorrectNetworkRouterRegistryException()
            ip = ip_equip[0].ip
            dict_ips[equipment] = dict()
            dict_ips[equipment]['ip'] = '%s.%s.%s.%s' % (ip.oct1, ip.oct2,
                                                         ip.oct3, ip.oct4)
            dict_ips[equipment]['prio'] = 100 + equip_number
            equip_number += 1
    else:
        dict_ips['gateway_redundancy'] = False
        dict_ips[equipment_list[0]] = dict()
        dict_ips[equipment_list[0]]['ip'] = dict_ips['gateway']
        dict_ips[equipment_list[0]]['prio'] = 100

    return dict_ips