Skip to content

Commit

Permalink
ngsild-tenant header support (#721)
Browse files Browse the repository at this point in the history
* ngsild-tenant header support | Fix for issue #669 & #664 (#695)

* ngsild-tenant header support

* resolved tests

* removed boolean config

* updated release notes

* reused srv for LD support

* resloved lint issue

* added tests

* added service argument

* removed unsupported attributes

* updated entityId

* test rollback

* test complete rollback

* test circleci

* added nl for circleci check

* updated notify tests

* updated tests

* updated notify_ld_header call

* code cleanup

* add comments to fiware svc header getter.

* refactor request formatter to use fiware_s func.

---------

Co-authored-by: Rohit <[email protected]>
  • Loading branch information
c0c0n3 and rohit-vrrr authored Mar 20, 2023
1 parent 637deba commit 94c7802
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Resolved TODO at src/translators/sql_translator.py#L768.py (#683)
- Resolved TODO at src/reporter/tests/utils.py (#692)
- Added error handling in src/wq/ql/notify.py (#673)
- NGSI-LD tenant header (#664, #669)

### Bug fixes

Expand Down
3 changes: 2 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from reporter.httputil import fiware_s
import server.wsgi as flask
import server.grunner as gunicorn
from flask import has_request_context, request
Expand All @@ -11,7 +12,7 @@ def format(self, record):
if has_request_context():
record.corr = request.headers.get('fiware_correlator', None)
record.remote_addr = request.remote_addr
record.srv = request.headers.get('fiware-service', None)
record.srv = fiware_s()
if record.srv:
record.subserv = request.headers.get(
'fiware-servicepath', '/')
Expand Down
11 changes: 9 additions & 2 deletions src/reporter/httputil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

def fiware_s() -> str:
"""
:return: The content of the FIWARE service header if any.
Read the tenant header.
If the request has an `ngsild-tenant` header, return its value.
Otherwise if there's a `fiware-service` return that value. If
none of those headers are present, return `None`. Notice if both
headers are present, `ngsild-tenant` takes precedence.
:return: The content of the tenant header if any.
"""
return request.headers.get('fiware-service', None)
return request.headers.get('ngsild-tenant', None) \
or request.headers.get('fiware-service', None)


def fiware_sp() -> str:
Expand Down
89 changes: 89 additions & 0 deletions src/reporter/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def notify_header(service=None, service_path=None):
return headers(service, service_path, True)


def notify_ld_header(service=None, service_path=None):
return ld_headers(service, service_path, True)


def query_header(service=None, service_path=None):
return headers(service, service_path, False)

Expand All @@ -51,6 +55,18 @@ def headers(service=None, service_path=None, content_type=True):
return h


def ld_headers(service=None, service_path=None, content_type=True):
h = {}
if content_type:
h['Content-Type'] = 'application/json'
if service:
h['NGSILD-Tenant'] = service
if service_path:
h['Fiware-ServicePath'] = service_path

return h


def insert_data(notification: dict, http_headers: dict, service: str):
etype = type_of(notification['data'][0])
res_post = requests.post(
Expand Down Expand Up @@ -765,6 +781,79 @@ def test_json_ld(service, notification):
delete_entity_type(service, notification['data'][0]['type'])


@pytest.mark.parametrize("service", services)
def test_ngsi_ld(service, notification):
# example json-ld entity
notification['data'][0] = {
"id": "urn:ngsi-ld:Streetlight:streetlight:guadalajara:4568",
"type": "Streetlight",
"location": {
"type": "GeoProperty",
"value": {
"type": "Point",
"coordinates": [-3.164485591715449, 40.62785133667262]
}
},
"areaServed": {
"type": "Property",
"value": "Roundabouts city entrance"
},
"status": {
"type": "Property",
"value": "ok"
},
"refStreetlightGroup": {
"type": "Relationship",
"object": "urn:ngsi-ld:StreetlightGroup:streetlightgroup:G345"
},
"refStreetlightModel": {
"type": "Relationship",
"object": "urn:ngsi-ld:StreetlightModel:streetlightmodel:STEEL_Tubular_10m"
},
"circuit": {
"type": "Property",
"value": "C-456-A467"
},
"lanternHeight": {
"type": "Property",
"value": 10
},
"locationCategory": {
"type": "Property",
"value": "centralIsland"
},
"powerState": {
"type": "Property",
"value": "off"
},
"controllingMethod": {
"type": "Property",
"value": "individual"
},
"dateLastLampChange": {
"type": "Property",
"value": {
"@type": "DateTime",
"@value": "2016-07-08T08:02:21.753Z"
}
},
"@context": [
"https://schema.lab.fiware.org/ld/context",
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
]
}
url = '{}'.format(notify_url)
get_url = "{}/entities/urn:ngsi-ld:Streetlight:streetlight:guadalajara:4568/attrs/lanternHeight/value".format(
QL_URL)
url_new = '{}'.format(get_url)
insert_data(notification, notify_ld_header(service), service)

res_get = requests.get(url_new, headers=query_header(service))
assert res_get.status_code == 200
assert res_get.json()['values'][0] == 10
delete_entity_type(service, notification['data'][0]['type'])


@pytest.mark.parametrize("service", services)
def test_json_ld_observed_at_meta(service, notification):
# example json-ld entity with observedAt as metadata
Expand Down

0 comments on commit 94c7802

Please sign in to comment.