Exemple #1
0
    def create_v3(self, interface):
        """
        Add new interface

        @return: Interface instance
        @raise EquipamentoNotFoundError: Equipment doesn't exist
        @raise EquipamentoError: Failed to find equipment
        @raise FrontLinkNotFoundError: FrontEnd interface doesn't exist
        @raise BackLinkNotFoundError: BackEnd interface doesn't exist
        @raise InterfaceForEquipmentDuplicatedError: An interface with the same name on the same
        equipment already exists
        @raise InterfaceError: Failed to add new interface
        """

        if Interface.objects.filter(
                equipamento__id=interface.get('equipment'),
                interface__iexact=interface.get('interface')):
            raise InterfaceForEquipmentDuplicatedError(
                None, u'Duplicate interface name for the device.')

        self.interface = interface.get('interface')
        self.tipo = TipoInterface.get_by_pk(interface.get('type'))
        self.equipamento = facade.get_equipment_by_id(
            interface.get('equipment'))
        self.descricao = interface.get('description')
        self.protegida = interface.get('protected')

        try:
            self.ligacao_front = Interface.get_by_pk(interface.get('front_interface')) \
                if interface.get('front_interface') else None
        except InterfaceNotFoundError, e:
            raise FrontLinkNotFoundError(e,
                                         u'Frontend interface does not exist')
Exemple #2
0
    def create_v3(self, interface):
        """
        Add new interface

        @return: Interface instance
        @raise EquipamentoNotFoundError: Equipment doesn't exist
        @raise EquipamentoError: Failed to find equipment
        @raise FrontLinkNotFoundError: FrontEnd interface doesn't exist
        @raise BackLinkNotFoundError: BackEnd interface doesn't exist
        @raise InterfaceForEquipmentDuplicatedError: An interface with the same name on the same
        equipment already exists
        @raise InterfaceError: Failed to add new interface
        """

        if Interface.objects.filter(equipamento__id=interface.get('equipment'),
                                    interface__iexact=interface.get('interface')):
            raise InterfaceForEquipmentDuplicatedError(None, u'Duplicate interface name for the device.')

        self.interface = interface.get('interface')
        self.tipo = TipoInterface.get_by_pk(interface.get('type'))
        self.equipamento = facade.get_equipment_by_id(interface.get('equipment'))
        self.descricao = interface.get('description')
        self.protegida = interface.get('protected')

        try:
            self.ligacao_front = Interface.get_by_pk(interface.get('front_interface')) \
                if interface.get('front_interface') else None
        except InterfaceNotFoundError, e:
            raise FrontLinkNotFoundError(e, u'Frontend interface does not exist')
Exemple #3
0
def delete_equipment(equipments):
    """Delete equipment by ids"""

    locks_list = create_lock(equipments, LOCK_EQUIPMENT)

    try:
        for equipment in equipments:
            equipment_obj = get_equipment_by_id(equipment)
            equipment_obj.delete_v4()
    except ObjectDoesNotExistException, e:
        raise ObjectDoesNotExistException(e.detail)
def delete_equipment(equipments):
    """Delete equipment by ids"""

    locks_list = create_lock(equipments, LOCK_EQUIPMENT)

    try:
        for equipment in equipments:
            equipment_obj = get_equipment_by_id(equipment)
            equipment_obj.delete_v4()
    except ObjectDoesNotExistException, e:
        raise ObjectDoesNotExistException(e.detail)
Exemple #5
0
def update_equipment(equipments, user):
    """Update equipment."""

    locks_list = create_lock(equipments, LOCK_EQUIPMENT)
    response = list()

    try:
        for equipment in equipments:
            equipment_obj = get_equipment_by_id(equipment.get('id'))
            equipment_obj.update_v4(equipment)
            response.append({'id': equipment_obj.id})
    except ObjectDoesNotExistException, e:
        raise ObjectDoesNotExistException(e.detail)
def update_equipment(equipments, user):
    """Update equipment."""

    locks_list = create_lock(equipments, LOCK_EQUIPMENT)
    response = list()

    try:
        for equipment in equipments:
            equipment_obj = get_equipment_by_id(equipment.get('id'))
            equipment_obj.update_v4(equipment)
            response.append({'id': equipment_obj.id})
    except ObjectDoesNotExistException, e:
        raise ObjectDoesNotExistException(e.detail)
Exemple #7
0
    def create_v3(self, interface):
        """
        Add new interface

        @return: Interface instance
        @raise EquipamentoNotFoundError: Equipment doesn't exist
        @raise EquipamentoError: Failed to find equipment
        @raise FrontLinkNotFoundError: FrontEnd interface doesn't exist
        @raise BackLinkNotFoundError: BackEnd interface doesn't exist
        @raise InterfaceForEquipmentDuplicatedError: An interface with the same name on the same
        equipment already exists
        @raise InterfaceError: Failed to add new interface
        """

        # validate interface name
        if Interface.objects.filter(
                equipamento__id=interface.get('equipment_id'),
                interface__iexact=interface.get('name')):
            raise InterfaceForEquipmentDuplicatedError(
                None, u'Duplicate interface name for the device.')

        self.tipo = TipoInterface.get_by_pk(interface.get('type'))

        self.equipamento = facade.get_equipment_by_id(
            interface.get('equipment_id'))

        marca = self.equipamento.modelo.marca.id if self.equipamento.tipo_equipamento.id != 2 else 0

        if marca == 0:
            regex = '^([a-zA-Z0-9-_/ ]+(:)?){1,6}$'
        elif marca == 2:
            regex = '^(Int)\s[0-9]+$'
        elif marca == 3:
            regex = '^(Fa|Gi|Te|Serial|Eth|mgmt)\s?[0-9]+(/[0-9]+(/[0-9]+)?)?$'
        elif marca == 4:
            regex = '^(interface)\s[0-9a-zA-Z]+(/[0-9a-zA-Z])+([0-9a-zA-Z-.]+)?$'
        elif marca == 5:
            regex = '^(eth)[0-9]+(/[0-9]+)?$'
        else:
            regex = ''

        # Validate if interface name conforms to the brand
        if not is_valid_regex(self.interface, regex):
            raise InvalidValueError(None, 'nome', self.interface)

        try:
            self.ligacao_front = Interface.get_by_pk(interface.get('front_connection_id')) \
                if interface.get('front_connection_id') else None
        except InterfaceNotFoundError, e:
            raise FrontLinkNotFoundError(e,
                                         u'Frontend interface does not exist')
Exemple #8
0
    def update_v3(self, interface):
        """
        Update an interface

        @return: Interface instance
        @raise EquipamentoNotFoundError: Equipment doesn't exist
        @raise EquipamentoError: Failed to find equipment
        @raise FrontLinkNotFoundError: FrontEnd interface doesn't exist
        @raise BackLinkNotFoundError: BackEnd interface doesn't exist
        @raise InterfaceForEquipmentDuplicatedError: An interface with the same name on the same
        equipment already exists
        @raise InterfaceError: Failed to add new interface
        """

        if not self.interface == interface.get('interface') \
                and Interface.objects.filter(equipamento__id=interface.get('equipment_id'),
                                             interface__iexact=interface.get('interface')):
            raise InterfaceForEquipmentDuplicatedError(
                None, u'Duplicate interface name for the device.')

        self.interface = interface.get('interface')

        self.vlan_nativa = int(interface.get('native_vlan')) if interface.get(
            'native_vlan') else 1

        if self.vlan_nativa and 3967 < self.vlan_nativa < 4048 or self.vlan_nativa == 4096:
            raise InvalidValueError(None, 'Vlan Nativa',
                                    'Intervalo reservado: 3968-4047 e 4094')

        self.tipo = TipoInterface.get_by_pk(interface.get('type'))
        self.equipamento = facade.get_equipment_by_id(
            interface.get('equipment'))
        self.descricao = interface.get('description')
        self.protegida = interface.get('protected')

        if self.ligacao_front:
            if interface.get('front_interface'):
                if not self.ligacao_front.id == interface.get(
                        'front_interface'):
                    new_front_connection = Interface.objects.get(
                        id=interface.get('front_interface'))
                    if new_front_connection.ligacao_front:
                        raise Exception(
                            'The interface %s from equipment %s is already connected.'
                            % (new_front_connection.interface,
                               new_front_connection.equipamento.nome))
                    if not self.ligacao_front.id == new_front_connection.id:
                        self.remove_connection(front=True)
                        self.create_connection(front=new_front_connection)
            else:
                self.remove_connection(front=True)
        elif interface.get('front_interface'):
            new_front_connection = Interface.objects.get(
                id=interface.get('front_interface'))
            if new_front_connection.ligacao_front:
                raise Exception(
                    'The interface %s from equipment %s is already connected.'
                    % (new_front_connection.interface,
                       new_front_connection.equipamento.nome))
            self.create_connection(front=new_front_connection)

        if self.ligacao_back:
            if interface.get('back_interface'):
                if not self.ligacao_back.id == interface.get('back_interface'):
                    new_back_connection = Interface.objects.get(
                        id=interface.get('back_interface'))
                    if new_back_connection.ligacao_back:
                        raise Exception(
                            'The interface %s from equipment %s is already connected.'
                            % (new_back_connection.interface,
                               new_back_connection.equipamento.nome))
                    if not self.ligacao_back.id == new_back_connection.id:
                        self.remove_connection(back=True)
                        self.create_connection(back=new_back_connection)
            else:
                self.remove_connection(back=True)
        elif interface.get('back_interface'):
            new_back_connection = Interface.objects.get(
                id=interface.get('back_interface'))
            if new_back_connection.ligacao_back:
                raise Exception(
                    'The interface %s from equipment %s is already connected.'
                    % (new_back_connection.interface,
                       new_back_connection.equipamento.nome))
            self.create_connection(back=new_back_connection)

        return self.save()
Exemple #9
0
    def update_v3(self, interface):
        """
        Update an interface

        @return: Interface instance
        @raise EquipamentoNotFoundError: Equipment doesn't exist
        @raise EquipamentoError: Failed to find equipment
        @raise FrontLinkNotFoundError: FrontEnd interface doesn't exist
        @raise BackLinkNotFoundError: BackEnd interface doesn't exist
        @raise InterfaceForEquipmentDuplicatedError: An interface with the same name on the same
        equipment already exists
        @raise InterfaceError: Failed to add new interface
        """

        if not self.interface == interface.get('interface') \
                and Interface.objects.filter(equipamento__id=interface.get('equipment_id'),
                                             interface__iexact=interface.get('interface')):
            raise InterfaceForEquipmentDuplicatedError(None, u'Duplicate interface name for the device.')

        self.interface = interface.get('interface')

        self.vlan_nativa = int(interface.get('native_vlan')) if interface.get('native_vlan') else 1

        if self.vlan_nativa and 3967 < self.vlan_nativa < 4048 or self.vlan_nativa == 4096:
            raise InvalidValueError(None, 'Vlan Nativa', 'Intervalo reservado: 3968-4047 e 4094')

        self.tipo = TipoInterface.get_by_pk(interface.get('type'))
        self.equipamento = facade.get_equipment_by_id(interface.get('equipment'))
        self.descricao = interface.get('description')
        self.protegida = interface.get('protected')

        if self.ligacao_front:
            if interface.get('front_interface'):
                if not self.ligacao_front.id == interface.get('front_interface'):
                    new_front_connection = Interface.objects.get(id=interface.get('front_interface'))
                    if new_front_connection.ligacao_front:
                        raise Exception('The interface %s from equipment %s is already connected.'
                                        % (new_front_connection.interface, new_front_connection.equipamento.nome))
                    if not self.ligacao_front.id == new_front_connection.id:
                        self.remove_connection(front=True)
                        self.create_connection(front=new_front_connection)
            else:
                self.remove_connection(front=True)
        elif interface.get('front_interface'):
            new_front_connection = Interface.objects.get(id=interface.get('front_interface'))
            if new_front_connection.ligacao_front:
                raise Exception('The interface %s from equipment %s is already connected.'
                                 % (new_front_connection.interface, new_front_connection.equipamento.nome))
            self.create_connection(front=new_front_connection)

        if self.ligacao_back:
            if interface.get('back_interface'):
                if not self.ligacao_back.id == interface.get('back_interface'):
                    new_back_connection = Interface.objects.get(id=interface.get('back_interface'))
                    if new_back_connection.ligacao_back:
                        raise Exception('The interface %s from equipment %s is already connected.'
                                        % (new_back_connection.interface, new_back_connection.equipamento.nome))
                    if not self.ligacao_back.id == new_back_connection.id:
                        self.remove_connection(back=True)
                        self.create_connection(back=new_back_connection)
            else:
                self.remove_connection(back=True)
        elif interface.get('back_interface'):
            new_back_connection = Interface.objects.get(id=interface.get('back_interface'))
            if new_back_connection.ligacao_back:
                raise Exception('The interface %s from equipment %s is already connected.'
                                 % (new_back_connection.interface, new_back_connection.equipamento.nome))
            self.create_connection(back=new_back_connection)

        return self.save()