예제 #1
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
예제 #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

    return dict_ips
예제 #3
0
            # generate config file
            file_to_deploy = utils.generate_config_file(
                dict_ips, equipment, TEMPLATE_NETWORKv4_DEACTIVATE)

            # deploy config file in equipments
            status_deploy[equipment.id] = \
                deploy_config_in_equipment(file_to_deploy, equipment)

        netv4_obj.deactivate_v3()

        # transaction.commit()
        if netv4_obj.vlan.ativada == 1:

            # if there are no other networks active in vlan, remove int
            # vlan
            if not utils.has_active_network_in_vlan(netv4_obj.vlan):

                # remove int vlan
                for equipment in routers:
                    if equipment.maintenance is not True:
                        pass
                        # Delete SVI
                        status_deploy[equipment.id] += utils.remove_svi(
                            equipment, netv4_obj.vlan.num_vlan)

                # Need verify this call
                netv4_obj.vlan.deactivate_v3(locks_name)

        return status_deploy

    except ip_models.NetworkIPv4ErrorV3 as e:
예제 #4
0
            # generate config file
            file_to_deploy = utils.generate_config_file(
                dict_ips, equipment, TEMPLATE_NETWORKv4_DEACTIVATE)

            # deploy config file in equipments
            status_deploy[equipment.id] = \
                deploy_config_in_equipment(file_to_deploy, equipment)

        netv4_obj.deactivate_v3()

        # transaction.commit()
        if netv4_obj.vlan.ativada == 1:

            # if there are no other networks active in vlan, remove int
            # vlan
            if not utils.has_active_network_in_vlan(netv4_obj.vlan):

                # remove int vlan
                for equipment in routers:
                    if equipment.maintenance is not True:
                        pass
                        # Delete SVI
                        status_deploy[equipment.id] += utils.remove_svi(
                            equipment, netv4_obj.vlan.num_vlan)

                # Need verify this call
                netv4_obj.vlan.deactivate_v3(locks_name)

        return status_deploy

    except ip_models.NetworkIPv4ErrorV3, e: