def network_ipv4_add(self, user, vlan_id, network_type, environment_vip, prefix=None):

        try:

            # Valid vlan ID
            if not is_valid_int_greater_zero_param(vlan_id):
                self.log.error(
                    u'Parameter id_vlan is invalid. Value: %s.', vlan_id)
                raise InvalidValueError(None, 'id_vlan', vlan_id)

            # Network Type

            # Valid network_type ID
            """
            if not is_valid_int_greater_zero_param(network_type):
                self.log.error(
                    u'Parameter id_tipo_rede is invalid. Value: %s.', network_type)
                raise InvalidValueError(None, 'id_tipo_rede', network_type)
            """
            # Find network_type by ID to check if it exist
            net = None
            if network_type:
                net = TipoRede.get_by_pk(network_type)

            # Environment Vip

            if environment_vip is not None:

                # Valid environment_vip ID
                if not is_valid_int_greater_zero_param(environment_vip):
                    self.log.error(
                        u'Parameter id_ambiente_vip is invalid. Value: %s.', environment_vip)
                    raise InvalidValueError(
                        None, 'id_ambiente_vip', environment_vip)

                # Find Environment VIP by ID to check if it exist
                evip = EnvironmentVip.get_by_pk(environment_vip)

            else:
                evip = None

            # Business Rules

            # New NetworkIPv4
            network_ipv4 = NetworkIPv4()
            vlan_map = network_ipv4.add_network_ipv4(user, vlan_id, net, evip, prefix)

            list_equip_routers_ambient = EquipamentoAmbiente.get_routers_by_environment(vlan_map['vlan']['id_ambiente'])

            if list_equip_routers_ambient:

                # Add Adds the first available ipv4 on all equipment
                # that is configured as a router for the environment related to
                # network
                ip = Ip.get_first_available_ip(vlan_map['vlan']['id_network'])

                ip = str(ip).split('.')

                ip_model = Ip()
                ip_model.oct1 = ip[0]
                ip_model.oct2 = ip[1]
                ip_model.oct3 = ip[2]
                ip_model.oct4 = ip[3]
                ip_model.networkipv4_id = network_ipv4.id

                ip_model.save()

                if len(list_equip_routers_ambient) > 1:
                    multiple_ips = True
                else:
                    multiple_ips = False

                for equip in list_equip_routers_ambient:
                    IpEquipamento().create(user, ip_model.id, equip.equipamento.id)

                    if multiple_ips:
                        router_ip = Ip.get_first_available_ip(vlan_map['vlan']['id_network'], True)
                        router_ip = str(router_ip).split('.')
                        ip_model2 = Ip()
                        ip_model2.oct1 = router_ip[0]
                        ip_model2.oct2 = router_ip[1]
                        ip_model2.oct3 = router_ip[2]
                        ip_model2.oct4 = router_ip[3]
                        ip_model2.networkipv4_id = vlan_map['vlan']['id_network']
                        ip_model2.save()
                        IpEquipamento().create(user, ip_model2.id, equip.equipamento.id)

            # Return XML
            return self.response(dumps_networkapi(vlan_map))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
Beispiel #2
0
    def network_ipv4_add(self,
                         user,
                         vlan_id,
                         network_type,
                         environment_vip,
                         prefix=None):

        try:

            # Valid vlan ID
            if not is_valid_int_greater_zero_param(vlan_id):
                self.log.error(u'Parameter id_vlan is invalid. Value: %s.',
                               vlan_id)
                raise InvalidValueError(None, 'id_vlan', vlan_id)

            # Network Type

            # Valid network_type ID
            """
            if not is_valid_int_greater_zero_param(network_type):
                self.log.error(
                    u'Parameter id_tipo_rede is invalid. Value: %s.', network_type)
                raise InvalidValueError(None, 'id_tipo_rede', network_type)
            """
            # Find network_type by ID to check if it exist
            net = None
            if network_type:
                net = TipoRede.get_by_pk(network_type)

            # Environment Vip

            if environment_vip is not None:

                # Valid environment_vip ID
                if not is_valid_int_greater_zero_param(environment_vip):
                    self.log.error(
                        u'Parameter id_ambiente_vip is invalid. Value: %s.',
                        environment_vip)
                    raise InvalidValueError(None, 'id_ambiente_vip',
                                            environment_vip)

                # Find Environment VIP by ID to check if it exist
                evip = EnvironmentVip.get_by_pk(environment_vip)

            else:
                evip = None

            # Business Rules

            # New NetworkIPv4
            network_ipv4 = NetworkIPv4()
            vlan_map = network_ipv4.add_network_ipv4(user, vlan_id, net, evip,
                                                     prefix)

            list_equip_routers_ambient = EquipamentoAmbiente.get_routers_by_environment(
                vlan_map['vlan']['id_ambiente'])

            if list_equip_routers_ambient:

                # Add Adds the first available ipv4 on all equipment
                # that is configured as a router for the environment related to
                # network
                ip = Ip.get_first_available_ip(vlan_map['vlan']['id_network'])

                ip = str(ip).split('.')

                ip_model = Ip()
                ip_model.oct1 = ip[0]
                ip_model.oct2 = ip[1]
                ip_model.oct3 = ip[2]
                ip_model.oct4 = ip[3]
                ip_model.networkipv4_id = network_ipv4.id

                ip_model.save()

                if len(list_equip_routers_ambient) > 1:
                    multiple_ips = True
                else:
                    multiple_ips = False

                for equip in list_equip_routers_ambient:
                    IpEquipamento().create(user, ip_model.id,
                                           equip.equipamento.id)

                    if multiple_ips:
                        router_ip = Ip.get_first_available_ip(
                            vlan_map['vlan']['id_network'], True)
                        router_ip = str(router_ip).split('.')
                        ip_model2 = Ip()
                        ip_model2.oct1 = router_ip[0]
                        ip_model2.oct2 = router_ip[1]
                        ip_model2.oct3 = router_ip[2]
                        ip_model2.oct4 = router_ip[3]
                        ip_model2.networkipv4_id = vlan_map['vlan'][
                            'id_network']
                        ip_model2.save()
                        IpEquipamento().create(user, ip_model2.id,
                                               equip.equipamento.id)

            # Return XML
            return self.response(dumps_networkapi(vlan_map))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def network_ipv6_add(self, user, vlan_id, network_type, environment_vip, prefix=None):

        try:
            # Valid vlan ID
            if not is_valid_int_greater_zero_param(vlan_id):
                self.log.error(
                    u'Parameter id_vlan is invalid. Value: %s.', vlan_id)
                raise InvalidValueError(None, 'id_vlan', vlan_id)

            # Network Type

            # Valid network_type ID
            if not is_valid_int_greater_zero_param(network_type):
                self.log.error(
                    u'Parameter id_tipo_rede is invalid. Value: %s.', network_type)
                raise InvalidValueError(None, 'id_tipo_rede', network_type)

            # Find network_type by ID to check if it exist
            net = TipoRede.get_by_pk(network_type)

            # Environment Vip

            if environment_vip is not None:

                # Valid environment_vip ID
                if not is_valid_int_greater_zero_param(environment_vip):
                    self.log.error(
                        u'Parameter id_ambiente_vip is invalid. Value: %s.', environment_vip)
                    raise InvalidValueError(
                        None, 'id_ambiente_vip', environment_vip)

                # Find Environment VIP by ID to check if it exist
                evip = EnvironmentVip.get_by_pk(environment_vip)

            else:
                evip = None

            # Business Rules

            # New NetworkIPv6
            network_ipv6 = NetworkIPv6()
            vlan_map = network_ipv6.add_network_ipv6(
                user, vlan_id, net, evip, prefix)

            list_equip_routers_ambient = EquipamentoAmbiente.get_routers_by_environment(vlan_map['vlan']['id_ambiente'])

            if list_equip_routers_ambient:

                # Add Adds the first available ipv6 on all equipment
                # that is configured as a router for the environment related to
                # network
                ipv6 = Ipv6.get_first_available_ip6(vlan_map['vlan']['id_network'])

                ipv6 = str(ipv6).split(':')

                ipv6_model = Ipv6()
                ipv6_model.block1 = ipv6[0]
                ipv6_model.block2 = ipv6[1]
                ipv6_model.block3 = ipv6[2]
                ipv6_model.block4 = ipv6[3]
                ipv6_model.block5 = ipv6[4]
                ipv6_model.block6 = ipv6[5]
                ipv6_model.block7 = ipv6[6]
                ipv6_model.block8 = ipv6[7]
                ipv6_model.networkipv6_id = vlan_map['vlan']['id_network']

                ipv6_model.save()

                if len(list_equip_routers_ambient) > 1:
                    multiple_ips = True
                else:
                    multiple_ips = False

                for equip in list_equip_routers_ambient:

                    Ipv6Equipament().create(
                        user, ipv6_model.id, equip.equipamento.id)

                    if multiple_ips:
                        router_ip = Ipv6.get_first_available_ip6(vlan_map['vlan']['id_network'], True)
                        ipv6_model2 = Ipv6()
                        ipv6_model2.block1 = router_ip[0]
                        ipv6_model2.block2 = router_ip[1]
                        ipv6_model2.block3 = router_ip[2]
                        ipv6_model2.block4 = router_ip[3]
                        ipv6_model2.block5 = router_ip[4]
                        ipv6_model2.block6 = router_ip[5]
                        ipv6_model2.block7 = router_ip[6]
                        ipv6_model2.block8 = router_ip[7]
                        ipv6_model2.networkipv6_id = vlan_map['vlan']['id_network']
                        ipv6_model2.save()
                        Ipv6Equipament().create(user, ipv6_model2.id, equip.equipamento.id)

            # Return XML
            return self.response(dumps_networkapi(vlan_map))

        except XMLError, e:
            self.log.error(u'Error reading the XML request.')
            return self.response_error(3, e)
    def network_ipv6_add(self, user, vlan_id, network_type, environment_vip, prefix=None):

        try:
            # Valid vlan ID
            if not is_valid_int_greater_zero_param(vlan_id):
                self.log.error(
                    u'Parameter id_vlan is invalid. Value: %s.', vlan_id)
                raise InvalidValueError(None, 'id_vlan', vlan_id)

            # Network Type

            # Valid network_type ID
            """
            if not is_valid_int_greater_zero_param(network_type):
                self.log.error(
                    u'Parameter id_tipo_rede is invalid. Value: %s.', network_type)
                raise InvalidValueError(None, 'id_tipo_rede', network_type)
            """
            # Find network_type by ID to check if it exist
            net = None
            if network_type:
                net = TipoRede.get_by_pk(network_type)

            # Environment Vip

            if environment_vip is not None:

                # Valid environment_vip ID
                if not is_valid_int_greater_zero_param(environment_vip):
                    self.log.error(
                        u'Parameter id_ambiente_vip is invalid. Value: %s.', environment_vip)
                    raise InvalidValueError(
                        None, 'id_ambiente_vip', environment_vip)

                # Find Environment VIP by ID to check if it exist
                evip = EnvironmentVip.get_by_pk(environment_vip)

            else:
                evip = None

            # Business Rules

            # New NetworkIPv6
            network_ipv6 = NetworkIPv6()
            vlan_map = network_ipv6.add_network_ipv6(user, vlan_id, net, evip, prefix)

            list_equip_routers_ambient = EquipamentoAmbiente.get_routers_by_environment(
                vlan_map['vlan']['id_ambiente'])

            if list_equip_routers_ambient:

                # Add Adds the first available ipv6 on all equipment
                # that is configured as a router for the environment related to
                # network
                ipv6 = Ipv6.get_first_available_ip6(
                    vlan_map['vlan']['id_network'])

                ipv6 = str(ipv6).split(':')

                ipv6_model = Ipv6()
                ipv6_model.block1 = ipv6[0]
                ipv6_model.block2 = ipv6[1]
                ipv6_model.block3 = ipv6[2]
                ipv6_model.block4 = ipv6[3]
                ipv6_model.block5 = ipv6[4]
                ipv6_model.block6 = ipv6[5]
                ipv6_model.block7 = ipv6[6]
                ipv6_model.block8 = ipv6[7]
                ipv6_model.networkipv6_id = vlan_map['vlan']['id_network']

                ipv6_model.save()

                if len(list_equip_routers_ambient) > 1:
                    multiple_ips = True
                else:
                    multiple_ips = False

                if vlan_map.get('vlan').get('vxlan'):
                    logging.debug('vxlan')

                    for equip in list_equip_routers_ambient:
                        Ipv6Equipament().create(user, ipv6_model.id, equip.equipamento.id)

                    if multiple_ips:
                        router_ip = Ipv6.get_first_available_ip6(vlan_map['vlan']['id_network'], True)

                        ipv6s = Ipv6()
                        ipv6s.block1, ipv6s.block2, ipv6s.block3, ipv6s.block4, ipv6s.block5, \
                        ipv6s.block6, ipv6s.block7, ipv6s.block8 = str(router_ip).split(':')
                        ipv6s.networkipv6_id = vlan_map['vlan']['id_network']
                        ipv6s.descricao = "IPv6 alocado para debug"
                        ipv6s.save(user)

                        Ipv6Equipament().create(user,
                                                ipv6s.id,
                                                list_equip_routers_ambient[0].equipamento.id)

                else:

                    for equip in list_equip_routers_ambient:
                        Ipv6Equipament().create(user, ipv6_model.id, equip.equipamento.id)

                        if multiple_ips:
                            router_ip = Ipv6.get_first_available_ip6(vlan_map['vlan']['id_network'], True)
                            router_ip = str(router_ip).split(':')
                            ipv6_model2 = Ipv6()
                            ipv6_model2.block1 = router_ip[0]
                            ipv6_model2.block2 = router_ip[1]
                            ipv6_model2.block3 = router_ip[2]
                            ipv6_model2.block4 = router_ip[3]
                            ipv6_model2.block5 = router_ip[4]
                            ipv6_model2.block6 = router_ip[5]
                            ipv6_model2.block7 = router_ip[6]
                            ipv6_model2.block8 = router_ip[7]
                            ipv6_model2.networkipv6_id = vlan_map['vlan']['id_network']
                            ipv6_model2.save(user)
                            Ipv6Equipament().create(user,
                                                    ipv6_model2.id,
                                                    equip.equipamento.id)

            # Return XML
            return self.response(dumps_networkapi(vlan_map))

        except XMLError, e:
            self.log.error(u'Error reading the XML request.')
            return self.response_error(3, e)
    def network_ipv4_add(self,
                         user,
                         vlan_id,
                         network_type,
                         environment_vip,
                         prefix=None):

        try:

            if not is_valid_int_greater_zero_param(vlan_id):
                msg = "The Vlan number is invalid. Value: {}".format(prefix)
                self.log.error(msg)
                return self.response_error(150, msg)

            net = None
            if network_type:
                net = TipoRede.get_by_pk(network_type)

            if environment_vip is not None:
                # Valid environment_vip ID
                if not is_valid_int_greater_zero_param(environment_vip):
                    msg = "The VIP Environment id is invalid. Value: {}".format(
                        prefix)
                    self.log.error(msg)
                    return self.response_error(150, msg)
                # Find Environment VIP by ID to check if it exist
                evip = EnvironmentVip.get_by_pk(environment_vip)

            else:
                evip = None

            # Business Rules

            # New NetworkIPv4
            network_ipv4 = NetworkIPv4()
            vlan_map = network_ipv4.add_network_ipv4(user, vlan_id, net, evip,
                                                     prefix)

            list_equip_routers_ambient = EquipamentoAmbiente.get_routers_by_environment(
                vlan_map['vlan']['id_ambiente'])

            if list_equip_routers_ambient:

                # Add the first available ipv4 on all equipment
                # that is configured as a router for the environment related to
                # network
                ip = Ip.get_first_available_ip(vlan_map['vlan']['id_network'])

                ip = str(ip).split('.')

                ip_model = Ip()
                ip_model.oct1 = ip[0]
                ip_model.oct2 = ip[1]
                ip_model.oct3 = ip[2]
                ip_model.oct4 = ip[3]
                ip_model.networkipv4_id = network_ipv4.id

                ip_model.save()

                if len(list_equip_routers_ambient) > 1:
                    multiple_ips = True
                else:
                    multiple_ips = False

                if vlan_map.get('vlan').get('vxlan'):

                    logging.debug('vxlan')
                    for equip in list_equip_routers_ambient:
                        IpEquipamento().create(user, ip_model.id,
                                               equip.equipamento.id)

                    if multiple_ips:
                        debug_ip = Ip.get_first_available_ip(
                            network_ipv4.id, True)

                        ips = Ip()
                        ips.oct1, ips.oct2, ips.oct3, ips.oct4 = str(
                            debug_ip).split('.')
                        ips.networkipv4_id = network_ipv4.id
                        ips.descricao = "IP alocado para debug"
                        ips.save(user)

                        IpEquipamento().create(
                            user, ips.id,
                            list_equip_routers_ambient[0].equipamento.id)

                else:

                    for equip in list_equip_routers_ambient:
                        IpEquipamento().create(user, ip_model.id,
                                               equip.equipamento.id)

                        if multiple_ips:
                            router_ip = Ip.get_first_available_ip(
                                network_ipv4.id, True)
                            router_ip = str(router_ip).split('.')
                            ip_model2 = Ip()
                            ip_model2.oct1 = router_ip[0]
                            ip_model2.oct2 = router_ip[1]
                            ip_model2.oct3 = router_ip[2]
                            ip_model2.oct4 = router_ip[3]
                            ip_model2.networkipv4_id = network_ipv4.id
                            ip_model2.save(user)
                            IpEquipamento().create(user, ip_model2.id,
                                                   equip.equipamento.id)

            # Return XML
            return self.response(dumps_networkapi(vlan_map))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)