Exemplo n.º 1
0
    def test_deletions(self):
        """
        Test the deletion of a context entity/device if the state is always
        correctly cleared
        """
        self.tearDown()

        device_id = 'device_id'
        entity_id = 'entity_id'

        device = Device(device_id=device_id,
                        entity_name=entity_id,
                        entity_type='Thing2',
                        protocol='IoTA-JSON',
                        transport='HTTP',
                        apikey='filip-iot-test-device')

        cb_client = ContextBrokerClient(url=settings.CB_URL,
                                        fiware_header=self.fiware_header)

        # Test 1: Only delete device
        # delete without optional parameter -> entity needs to continue existing
        self.client.post_device(device=device)
        self.client.delete_device(device_id=device_id, cb_url=settings.CB_URL)
        self.assertTrue(
            len(cb_client.get_entity_list(entity_ids=[entity_id])) == 1)
        cb_client.delete_entity(entity_id=entity_id, entity_type='Thing2')

        # Test 2:Delete device and corresponding entity
        # delete with optional parameter -> entity needs to be deleted
        self.client.post_device(device=device)
        self.client.delete_device(device_id=device_id,
                                  cb_url=settings.CB_URL,
                                  delete_entity=True)
        self.assertTrue(
            len(cb_client.get_entity_list(entity_ids=[entity_id])) == 0)

        # Test 3:Delete device and corresponding entity,
        #        that is linked to multiple devices
        # delete with optional parameter -> entity needs to be deleted
        self.client.post_device(device=device)
        device2 = copy.deepcopy(device)
        device2.device_id = "device_id2"
        self.client.post_device(device=device2)
        self.assertRaises(Exception, self.client.delete_device,
                          device_id=device_id, delete_entity=True)
        self.assertTrue(
            len(cb_client.get_entity_list(entity_ids=[entity_id])) == 1)
        self.client.delete_device(device_id=device2.device_id)

        # Test 4: Only delete entity
        # delete without optional parameter -> device needs to continue existing
        self.client.post_device(device=device)
        cb_client.delete_entity(entity_id=entity_id, entity_type='Thing2')
        self.client.get_device(device_id=device_id)
        self.client.delete_device(device_id=device_id)

        # Test 5: Delete entity, and all devices
        # # delete with optional parameter -> all devices need to be deleted
        self.client.post_device(device=device)
        device2 = copy.deepcopy(device)
        device2.device_id = "device_id2"
        self.client.post_device(device=device2)
        cb_client.delete_entity(entity_id=entity_id, delete_devices=True,
                                entity_type='Thing2',
                                iota_url=settings.IOTA_JSON_URL)
        self.assertEqual(len(self.client.get_device_list()), 0)
Exemplo n.º 2
0
                                                     "temperature"))
    except:
        logger.info("There might be no historical data for some calls.")

    # ## 2.3 Delete
    #
    # delete entity in QL
    try:
        ql_client.delete_entity(entity_id=hall_entity.id,
                                entity_type=hall_entity.type)
    except:
        logger.error("Can not delete data from QL")

    # delete entity in  CV
    try:
        cb_client.delete_entity(entity_id=hall_entity.id,
                                entity_type=hall_entity.type)
    except:
        logger.error("Can not delete entity from context broker")

    # delete subscription
    try:
        cb_client.delete_subscription(subscription_id)
    except:
        logger.error("Can not delete subscription from context broker.")

    # # 3 Clean up (Optional)
    #
    # Close clients
    ql_client.close()
    cb_client.close()
Exemplo n.º 3
0
    def delete_device(self,
                      *,
                      device_id: str,
                      delete_entity: bool = False,
                      cb_url: AnyHttpUrl = settings.IOTA_URL,
                      force_entity_deletion: bool = False) -> None:
        """
        Remove a device from the device registry. No payload is required
        or received.
        
        Args:
            device_id: str, ID of Device
            delete_entity:  False -> Only delete the device entry,
                                     the automatically created and linked
                                     context-entity will continue to
                                     exist in Fiware
                            True -> Also delete the automatically
                                    created and linked context-entity
                                    If multiple devices are linked to this
                                    entity, this operation is not executed and
                                    an exception is raised
            cb_url: AnyHttpUrl ->   ContextBroker Url where the corresponding
                                    entity resigns
            force_entity_deletion: bool, if delete_entity is true and
                                    multiple devices are linked to the linked
                                    entity, delete it and do not raise an error
        Returns:
            None
        """
        url = urljoin(
            self.base_url,
            f'iot/devices/{device_id}',
        )
        headers = self.headers

        device = self.get_device(device_id=device_id)

        try:
            res = self.delete(url=url, headers=headers)
            if res.ok:
                self.logger.info("Device '%s' successfully deleted!",
                                 device_id)
            else:
                res.raise_for_status()
        except requests.RequestException as err:
            msg = f"Could not delete device {device_id}!"
            self.log_error(err=err, msg=msg)
            raise

        if delete_entity:
            # An entity can technically belong to multiple devices
            # Only delete the entity if
            devices = self.get_device_list(entity=device.entity_name)
            if len(devices) > 0 and not force_entity_deletion:
                raise Exception(f"The Corresponding Entity to the device "
                                "{device_id} is linked to multiple devices, "
                                "it was not deleted")
            else:
                try:
                    from filip.clients.ngsi_v2 import ContextBrokerClient
                    client = ContextBrokerClient(
                        url=cb_url, fiware_header=self.fiware_headers)

                    client.delete_entity(entity_id=device.entity_name,
                                         entity_type=device.entity_type)

                except requests.RequestException as err:
                    # Do not throw an error
                    # It is only important that the entity does not exists after
                    # this methode, not if this methode actively deleted it
                    pass
Exemplo n.º 4
0
                                         value=12))
    # Deleting attributes
    # logger.info(cb_client.delete_entity_attribute(entity_id=room1_entity.id,
    #                                               attr_name="temperature"))
    # ### 4.1.2 Updating the model
    #
    # Most of the time it is more convenient to update our local model,
    # and let the library handle all the needed updates to synchronise the
    # live state to the model state.
    # Hereby it is tried to only make changes that were done locally,
    # keeping as much of the current live state as possible

    # when accessing an attribute a new object is created. We need to
    # manually transmit the made changes.
    temp_attr = room2_entity.get_attribute("temperature")
    temp_attr.value = 15
    room2_entity.update_attribute([temp_attr])

    room2_entity.delete_attributes(["pressure"])

    # all changes are transmitted with one methode call
    cb_client.patch_entity(room2_entity)

    # ## 4.2 Deleting
    #
    # To delete an entry in Fiware, we can call:
    cb_client.delete_entity(entity_id=room2_entity.id,
                            entity_type=room2_entity.type)
    # cb_client.delete_entity(entity_id=room1_entity.id,
    #                         entity_type=room1_entity.type)