Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

312 expressionlanguage in device should be optional #313

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions filip/models/ngsi_v2/iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
BaseAttribute, \
BaseValueAttribute, \
BaseNameAttribute
from filip.utils.validators import (validate_fiware_datatype_string_protect, validate_fiware_datatype_standard,
validate_jexl_expression, validate_device_expression_language,
validate_service_group_expression_language)
from filip.utils.validators import (validate_fiware_datatype_string_protect,
validate_fiware_datatype_standard,
validate_jexl_expression,
validate_expression_language)

logger = logging.getLogger()

Expand Down Expand Up @@ -250,13 +251,13 @@ def validate_cbHost(cls, value):
"IoT Agents to store information along with the devices "
"in the Device Registry."
)
expressionLanguage: ExpressionLanguage = Field(
expressionLanguage: Optional[ExpressionLanguage] = Field(
default=ExpressionLanguage.JEXL,
description="optional boolean value, to set expression language used "
"to compute expressions, possible values are: "
"legacy or jexl, but legacy is deprecated."
"legacy or jexl, but legacy is deprecated. If it is set None, jexl is used."
)
valid_expressionLanguage = field_validator("expressionLanguage")(validate_service_group_expression_language)
valid_expressionLanguage = field_validator("expressionLanguage")(validate_expression_language)
explicitAttrs: Optional[bool] = Field(
default=False,
description="optional boolean value, to support selective ignore "
Expand Down Expand Up @@ -321,13 +322,13 @@ class DeviceSettings(BaseModel):
description="Name of the device transport protocol, for the IoT Agents "
"with multiple transport protocols."
)
expressionLanguage: ExpressionLanguage = Field(
expressionLanguage: Optional[ExpressionLanguage] = Field(
default=ExpressionLanguage.JEXL,
description="optional boolean value, to set expression language used "
"to compute expressions, possible values are: "
"legacy or jexl, but legacy is deprecated."
"legacy or jexl, but legacy is deprecated. If it is set None, jexl is used."
)
valid_expressionLanguage = field_validator("expressionLanguage")(validate_device_expression_language)
valid_expressionLanguage = field_validator("expressionLanguage")(validate_expression_language)
explicitAttrs: Optional[bool] = Field(
default=False,
description="optional boolean value, to support selective ignore "
Expand Down
16 changes: 5 additions & 11 deletions filip/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,10 @@ def validate_jexl_expression(expression, attribute_name, device_id):
return expression


def validate_device_expression_language(cls, expressionLanguage):
def validate_expression_language(cls, expressionLanguage):
if expressionLanguage == "legacy":
warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is deprecated. Use 'JEXL' instead.")

return expressionLanguage


def validate_service_group_expression_language(cls, expressionLanguage):
if expressionLanguage == "legacy":
warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is deprecated and does not work "
f"anymore, because each device uses 'JEXL' as default.")

warnings.warn(f"Using 'LEGACY' expression language inside {cls.__name__} is "
f"deprecated. Use 'JEXL' instead.")
elif expressionLanguage is None:
expressionLanguage = "jexl"
return expressionLanguage
16 changes: 16 additions & 0 deletions tests/models/test_ngsi_v2_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ def test_expression_language(self):

assert len(w) == 2

# Test for expression language set to None
service_group_null_expression = ServiceGroup(
entity_type='Thing',
apikey=api_key,
resource='/iot/json',
expressionLanguage=None)
self.assertEqual(service_group_null_expression.expressionLanguage, ExpressionLanguage.JEXL)

device4 = Device(device_id="null_expression_device",
entity_name="null_expression_entity",
entity_type="test_entity_type",
transport=TransportProtocol.MQTT,
protocol=PayloadProtocol.IOTA_JSON,
expressionLanguage=None)
self.assertEqual(device4.expressionLanguage, ExpressionLanguage.JEXL)

def test_add_device_attributes(self):
"""
Test the device model regarding the behavior with devices attributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ExpressionLanguage)
from filip.utils.cleanup import clear_all
from paho.mqtt import client as mqtt_client
from paho.mqtt.client import CallbackAPIVersion

# Host address of Context Broker
CB_URL = "http://localhost:1026"
Expand Down Expand Up @@ -103,7 +104,7 @@
)
iota_client.post_device(device=device2)

client = mqtt_client.Client()
client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2)
client.username_pw_set(username="", password="")
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT)
client.loop_start()
Expand Down Expand Up @@ -147,7 +148,7 @@
)
iota_client.post_device(device=device3)

client = mqtt_client.Client()
client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2)
client.username_pw_set(username="", password="")
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT)
client.loop_start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ExpressionLanguage)
from filip.utils.cleanup import clear_all
from paho.mqtt import client as mqtt_client
from paho.mqtt.client import CallbackAPIVersion

# Host address of Context Broker
CB_URL = "http://localhost:1026"
Expand Down Expand Up @@ -114,7 +115,7 @@
)
iota_client.post_device(device=device2)

client = mqtt_client.Client()
client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2)
client.username_pw_set(username="", password="")
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT)
client.loop_start()
Expand Down Expand Up @@ -172,7 +173,7 @@
)
iota_client.post_device(device=device3)

client = mqtt_client.Client()
client = mqtt_client.Client(callback_api_version=CallbackAPIVersion.VERSION2)
client.username_pw_set(username="", password="")
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT)
client.loop_start()
Expand Down