Tester for the Python-based NGSI-LD API client compliant with the NGSI-LD API OpenAPI specification.
To deploy a docker-compose scenario with the Orion-LD context broker (a reference implementation of NGSI-LD context broker compliant with the NGSI-LD API specification by ETSI GS CIM 009 V1.6.1), execute the following command:
$ docker-compose upIn case you are interested in running the scenario in background, use the following command:
$ docker-compose up -dTear the scenario down as follows:
$ docker-compose downNote:
In the
.envfile, environment variables are defined and parameterized (i.e., the context broker endpoint and the NGSI-LD context URL). The context-catalog container provides a static web server where JSON-LD vocabularies can be uploaded. Interactions with the NGSI-LD API can link to this webserver rather than appending the JSON-LD vocabulary in the request's body.
Within the /ngsi-ld-models folder there is a Python library called ngsi_ld_models derived from the Pydantic class code generation for the schemas defined in a custom OpenAPI available here. This custom OpenAPI encompass the NGSI-LD API V1.6.1 with the native NGSI-LD API metamodel schemas and customizable schemas for simple demo NGSI-LD Entities used in this NGSI-LD client tester (a sample of JSON payloads for the NGSI-LD Entities of type IotDevice, TemperatureSensor, and HumiditySensor are available here). The Python code of the ngsi_ld_models library derived from the OpenAPI specification is automatically generated by the OpenAPI Generator project. To generate the code derived from the particular schemas defined within the OpenAPI, the OpenAPI Generator Docker image option has been used with the following command executed from the current folder:
$ docker run --rm -v ${PWD}:/ngsi-ld-client-tester openapitools/openapi-generator-cli generate -i /ngsi-ld-client-tester/ngsi-ld-api-schemas.yaml -g python --package-name ngsi_ld_models -o /ngsi-ld-client-tester/ngsi-ld-models --additional-properties disallowAdditionalPropertiesIfNotPresent=false --global-property models --skip-validate-specNote 1:
The OpenAPI schemas for the sample NGSI-LD Entities covered in this particular NGSI-LD API client tester are defined here, starting from line 5110 preceded by the comment
#Custom schemas for IoT sensors model.
Note 2:
By default, the OpenAPI Generator tool grants only access for the root user to the code generated. Change the owner/group on the created directory
/ngsi-ld-modelsusing the following commands:$ cd ngsi-ld-models/ $ sudo su $ chown -R <user>:<user> . $ exit
The high-level schema representation for its NGSI-LD information model is depicted below:
Note:
The
*character represents mandatory NGSI-LD Properties of the NGSI-LD Entities. ThehasSensorNGSI-LD Relationship of NGSI-LD Entity of typeIotDeviceis not mandatory and has cardinality1:Nsince it represents a multi-relationship to NGSI-LD Entities of typeTemperatureSensorand/orHumiditySentor. All these NGSI-LD information model conventions are included in the OpenAPI schema as mentioned above.
To ease management of Python dependencies we rely on Poetry tool. Poetry takes care of solving dependency conflicts and also configures virtual environments to develop our Python applications. Recommendations for preparing the virtual environment with poetry and installing the Python library ngsi_ld_models are described below:
- Download and install
poetryby following the official documentacion. - Make sure you have the right Python version for this project (Python>=3.9 in this case):
$ sudo apt-get install python3.9
- Install
distutilspackage for your specific Python release:$ sudo apt-get install python3-distutils
- Install
python-is-python3package (symlinks /usr/bin/python to Python3 as specified in link 1 and link 2):$ sudo apt-get install python-is-python3
- To build the
ngsi_ld_modelslibrary with the Python-generated classes for the sample NGSI-LD Entities of typeIotDevice,TemperatureSensor, andHumiditySensor, follow these steps:- Move to
/ngsi-ld-modelsfolder:$ cd ngsi-ld-models - Install the dependencies for the
ngsi_ld_modelslibrary withpoetry:The$ poetry install
ngsi_ld_modelsPython library is now installed and prepared to be used.
- Move to
To validate the create_entity, retrieve_entity, query_entity, update_entity, delete_entity, and upsert_batch CRUD operations for a simple NGSI-LD Entity (e.g., NGSI-LD Entity of type IotDevice available here), follow these steps:
-
To play with the NGSI-LD API
CRUDoperations for the demo NGSI-LD Entities of typeIotDevice,TemperatureSensor, andHumiditySensor, first follow these preparation steps:- Move to
/iot-sensors-testerfolder:$ cd iot-sensors-tester - Enable virtual environment for your specific Python release:
$ poetry env use 3.9
Note:
If you work with Visual Studio Code (VSC) IDE, it is recommendable to open the
/iot-sensors-testerfolder in an independet workspace so that the virtual environment can select the correct Python interpreter and thus recognize the source code well. In VSC you can specify the correct Python interpreter by using the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). - Setup the virtual environment with Poetry:
The virtual environment is now activated and the Python dependencies (i.e.,
$ poetry shell (iot-sensors-tester-py3.9) $ poetry install
ngsi_ld_clientandngsi_ld_modelsPython modules) installed and ready to be used.
- Move to
-
To create a sample NGSI-LD Entity (e.g, NGSI-LD Entity of type
IotDevice) by using of thecreate_entityoperation, run the iot-sensors-tester/create-iot-device-entity.py Python script as follow:(iot-sensors-tester-py3.9) $ python create-iot-device-entity.py
-
To retrieve the previously created NGSI-LD Entity by using its
idfield (i.e.,retrieve_entityoperation), run the iot-sensors-tester/retrieve-iot-device-entity.py Python script as follow:(iot-sensors-tester-py3.9) $ python retrieve-iot-device-entity.py
-
To query all the NGSI-LD Entities of a particular type (e.g, NGSI-LD Entity of type
IotDevice) by using of theretrieve_entityoperation, run the iot-sensors-tester/query-iot-device-entities.py Python script as follow:(iot-sensors-tester-py3.9) $ python query-iot-device-entities.py
-
To update the previously created NGSI-LD Entity of type
IotDeviceby using itsidfield (i.e.,update_entityoperation), run the iot-sensors-tester/update-iot-device-entity.py Python script as follow:(iot-sensors-tester-py3.9) $ python update-iot-device-entity.py
-
To create different NGSI-LD Entities (e.g, NGSI-LD Entities of type
IotDevice,TemperatureSensor, andHumiditySensor) by using itsidfield (i.e.,upsert_batchoperation), run the iot-sensors-tester/upsert-iot-sensors-entities.py Python script as follow:(iot-sensors-tester-py3.9) $ python upsert-iot-sensors-entities.py
The resulting JSON payloads for the created NGSI-LD Entities of type
IotDevice,TemperatureSensor, andHumiditySensorare available here, and the regarding NGSI-LD information model representation with instantiated values is available here.Also, this instance of NGSI-LD information model is depicted below:
Note:
The NGSI-LD unit codes for representing values of temperature in degrees Celsius and percent of humidity are extracted from UNECE/CEFACT Common Codes for specifying the unit of measurement [1], [2] as specified by ETSI GS CIM 009 V1.6.1.
-
To delete the previously created NGSI-LD Entities of type
IotDevice,TemperatureSensor, andHumiditySensorby using itsidfield (i.e.,delete_entityoperation), run the iot-sensors-tester/delete-iot-sensors-entities.py Python script as follow:(iot-sensors-tester-py3.9) $ python delete-iot-sensors-entities.py
Under Testing and Development:
There are additional Python scripts for validating other OpenAPI NGSI-LD operations such as the
update_attrs,append_attrs, andcreate_subscriptionbut they are still under testing and development. The following instructions can be taken for now:
-
To update a particular Attribute (i.e., NGSI-LD Property or NGSI-LD Relationship) of a NGSI-LD Entity identified by its Attribute id (i.e.,
update_attrsoperation), there is a sample Python script iot-sensors-tester/update-iot-device-property-name.py that enables updating the NGSI-LD Propertynameof a particular NGSI-LD Entity of typeIotDeviceidentified by its own id. Run the Python script as follow:(iot-sensors-tester-py3.9) $ python update-iot-device-property-name.py
-
To append Attributes (i.e., NGSI-LD Properties and NGSI-LD Relationships) to a particular NGSI-LD Entity (i.e.,
append_attrsoperation), there is a sample Python script iot-sensors-tester/append-attrs-iot-device-sensors.py that enables append the NGSI-LD Propertydescriptionof a particular NGSI-LD Entity of typeIotDeviceidentified by its own id. Run the Python script as follow:(iot-sensors-tester-py3.9) $ python append-iot-device-property-desc.py
-
For create subscriptions (i.e.,
create_subscriptionoperation), there is a simple tester microservice callednotifier-consumerthat creates a subscription based on changes to NGSI-LD Entities of typeIotDeviceand particularly to the AttributesnameandhasSensorand hope to receive notifications at a particular HTTP endpointhttp://notifier-consumer:8082/notify. The details of the main code for thenotifier-consumermicroservice is here. Once and NGSI-LD Entity of typeIotDevicewill be created or modified, thenotifier-consumerwill receive a notification. For testing purpose, follow these steps:- Move to
/docker/notifier-consumerfolder:$ cd docker/notifier-consuemer - Enable virtual environment for your specific Python release:
$ poetry env use 3.9
- Install the dependencies for the
ngsi_ld_clientandngsi_ld_modelslibraries withpoetry:The$ poetry install
ngsi_ld_clientandngsi_ld_modelsPython modules are now installed and prepared to be used by thenotifier-consumermicroservice. - Now you can deploy another docker-compose scenario with the
notifier-consumermicroservice:$ docker-compose -f docker-compose-with-notifier-consumer.yml up -d
- Once all the docker microservices are running, to check the notifications received by the
notifier-consumermicroservice, display the associated docker container logs with the as follows:$ docker logs -f notifier-consumer
- Move to
In addition, for validating example JSON payloads (e.g., IotDevice example) against the autogenerated Python class bindings (e.g., IotDevice Pydantic class available here), run the Python unit test from the iot-sensors-tester folder as follows:
$ cd iot-sensors-tester
$ poetry shell
(iot-sensors-tester-py3.9) $ python tests/test_iot_sensors.pyMoreover, the repository contains a Postman collection available here. This Postman collection has a set of HTTP requests that can be used to interact directly with the NGSI-LD Orion-LD context broker instead of using OpenAPI-based client. The collection includes most of NGSI-LD operations covered above with the Python-based NGSI-LD API client for the same demo scenario. We recommend downloading Postman Desktop for an better user experience.

