Exemplo n.º 1
0
def verificar_nome_channel(nome, interfaces):
    log.info("verificar_nome_channel")

    interface = Interface()

    channels = PortChannel.objects.filter(nome=nome)
    channels_id = []
    for ch in channels:
        channels_id.append(int(ch.id))
    if channels_id:
        for var in interfaces:
            if not var == '' and var is not None:
                interface_id = int(var)
        interface_id = interface.get_by_pk(interface_id)
        equip_id = interface_id.equipamento.id
        equip_interfaces = interface.search(equip_id)
        for i in equip_interfaces:
            try:
                sw = i.get_switch_and_router_interface_from_host_interface(
                    i.protegida)
            except:
                sw = None
                pass
            if sw is not None:
                if sw.channel is not None:
                    if sw.channel.id in channels_id:
                        raise exceptions.InterfaceException(
                            u'O nome do port channel ja foi '
                            'utilizado no equipamento')
Exemplo n.º 2
0
    def delete(self, request, *args, **kwargs):
        """URL: api/interface/disconnect/(?P<id_interface_1>\d+)/(?P<id_interface_2>\d+)/"""

        try:
            log.info('API_Disconnect')

            data = dict()

            id_interface_1 = kwargs.get('id_interface_1')
            id_interface_2 = kwargs.get('id_interface_2')

            interface_1 = Interface.get_by_pk(int(id_interface_1))
            interface_2 = Interface.get_by_pk(int(id_interface_2))

            with distributedlock(LOCK_INTERFACE % id_interface_1):

                if interface_1.channel or interface_2.channel:
                    raise exceptions.InterfaceException(
                        'Interface está em um Port Channel')

                if interface_1.ligacao_front_id == interface_2.id:
                    interface_1.ligacao_front = None
                    if interface_2.ligacao_front_id == interface_1.id:
                        interface_2.ligacao_front = None
                    else:
                        interface_2.ligacao_back = None
                elif interface_1.ligacao_back_id == interface_2.id:
                    interface_1.ligacao_back = None
                    if interface_2.ligacao_back_id == interface_1.id:
                        interface_2.ligacao_back = None
                    else:
                        interface_2.ligacao_front = None
                elif not interface_1.ligacao_front_id and not interface_1.ligacao_back_id:
                    raise exceptions.InterfaceException(
                        'Interface id %s não connectada' % interface_1)

                interface_1.save()
                interface_2.save()

            return Response(data, status=status.HTTP_200_OK)

        except exceptions.InterfaceException, exception:
            raise exception
Exemplo n.º 3
0
def delete_channel(user, equip_id, interface_list, channel):

    file_to_deploy = generate_delete_file(user, equip_id, interface_list,
                                          channel)

    # TODO Deploy config file
    try:
        lockvar = LOCK_EQUIPMENT % (equip_id)
        status_deploy = deploy_config_in_equipment_synchronous(
            file_to_deploy, int(equip_id), lockvar)
        log.info('Status: %s' % status_deploy)
    except exceptions.InterfaceException, exception:
        log.error(exception)
        raise exceptions.InterfaceException(exception)
Exemplo n.º 4
0
def verificar_vlan_range(amb, vlans):
    log.info("verificar_vlan_range")

    for intervalo in vlans.split(';'):
        for i in re.split('\W+', intervalo.replace('to', '-')):
            try:
                i = int(i)
            except:
                raise InvalidValueError(None, None, 'Numero da Vlan')
            if amb.min_num_vlan_1 and not (amb.min_num_vlan_1 <= i <= amb.max_num_vlan_1) and not (
                    amb.min_num_vlan_2 <= i <= amb.max_num_vlan_2):
                raise exceptions.InterfaceException(
                    u'Numero de vlan fora do range '
                    'definido para o ambiente')
Exemplo n.º 5
0
def verificar_vlan_range(amb, vlans):
    for intervalo in vlans.split(';'):
        for i in intervalo.split('-'):
            try:
                i = int(i)
            except:
                raise InvalidValueError(None, None, 'Numero da Vlan')
            if amb.min_num_vlan_1:
                if not (i >= amb.min_num_vlan_1 and i <= amb.max_num_vlan_1):
                    if not (i >= amb.min_num_vlan_2
                            and i <= amb.max_num_vlan_2):
                        raise exceptions.InterfaceException(
                            u'Numero de vlan fora do range '
                            'definido para o ambiente')
Exemplo n.º 6
0
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat DELETE requests to remove the connection of two interfaces by front or back

        URL: interface/<id_interface>/<back_or_front>/
        """

        try:

            self.log.info("Disconnect")

            # Valid Interface ID
            id_interface = kwargs.get('id_interface')
            if not is_valid_int_greater_zero_param(id_interface):
                self.log.error(
                    u'The id_interface parameter is not a valid value: %s.',
                    id_interface)
                raise InvalidValueError(None, 'id_interface', id_interface)

            # Valid back or front param
            back_or_front = kwargs.get('back_or_front')
            if not is_valid_zero_one_param(back_or_front):
                self.log.error(
                    u'The back_or_front parameter is not a valid value: %s.',
                    back_or_front)
                raise InvalidValueError(None, 'back_or_front', back_or_front)
            else:
                back_or_front = int(back_or_front)

            # User permission equip 1
            if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            # Checks if interface exists in database
            interface_1 = Interface.get_by_pk(id_interface)

            with distributedlock(LOCK_INTERFACE % id_interface):

                if interface_1.channel:
                    raise api_interface_exceptions.InterfaceException(
                        "Interface está em um Port Channel")

                # Is valid back or front connection
                if back_or_front:
                    try:
                        interface_2 = Interface.get_by_pk(
                            interface_1.ligacao_front_id)
                    except InterfaceNotFoundError:
                        raise InterfaceInvalidBackFrontError(
                            None,
                            "Interface two has no connection with front of Interface one"
                        )
                else:
                    try:
                        interface_2 = Interface.get_by_pk(
                            interface_1.ligacao_back_id)
                    except InterfaceNotFoundError:
                        raise InterfaceInvalidBackFrontError(
                            None,
                            "Interface two has no connection with back of Interface one"
                        )

                if interface_2.channel:
                    raise api_interface_exceptions.InterfaceException(
                        "Interface está em um Port Channel")

                if interface_2.ligacao_front_id == interface_1.id:
                    back_or_front_2 = 1
                elif interface_2.ligacao_back_id == interface_1.id:
                    back_or_front_2 = 0
                else:
                    raise InterfaceInvalidBackFrontError(
                        None,
                        "Interface one has no connection with front or back of Interface two"
                    )

                # Business Rules

                # Remove Interface one connection
                if back_or_front:
                    interface_1.ligacao_front = None
                else:
                    interface_1.ligacao_back = None

                # Remove Interface two connection
                if back_or_front_2:
                    interface_2.ligacao_front = None
                else:
                    interface_2.ligacao_back = None

                # Save
                interface_1.save()
                interface_2.save()

            # Return None for success
            return self.response(dumps_networkapi({}))

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