Orioncontextbroker Ngsiv2
Orioncontextbroker Ngsiv2
2
Being “Smart” requires first being “Aware”
• Implementing a Smart Application requires gathering and managing
context information, referring to values of attributes characterizing
relevant entities
• There are many application domains where management of context
info is relevant: smart home, smart agrifood, smart industry, smart
logistics, smart cities
Application
standard API
Context Information
Users
• Name-Surname
Boiler
• Manufacturer • Birthday
• Preferences Flowerpot
• Last revision
• Location • Humidity
• Product id
• Watering plan
• temperature
3
Being “Smart” requires first being “Aware”
• Implementing a Smart Application requires gathering and managing
context information, referring to values of attributes characterizing
relevant entities
• There are many application domains where management of context
info is relevant: smart home, smart agrifood, smart industry, smart
logistics, smart cities
Application
standard API
Context Information
Truck
Ship • Driver Container
• Company • Location • Dimension
• Speed • … • Temperature
• Location • …
• …
4
Being “Smart” requires first being “Aware”
• Implementing a Smart Application requires gathering and managing
context information, referring to values of attributes characterizing
relevant entities
• There are many application domains where management of context
info is relevant: smart home, smart agrifood, smart industry, smart
logistics, smart cities
Application
standard API
Context Information
Citizen
• Name-Surname
Bus • Birthday
• Location • Preferences Shop
• No. passengers • Location • Location
• Driver • Business name
• Licence plate • Franchise
• offerings
5
Context Information Management in Smart Cities
6
Different sources of context need to be handle
7
Different sources of context need to be handle
8
A non-intrusive approach is required
Application/Service
Standard API
9
Context Management in FIWARE
NGSI API
Context Broker
Users
Boiler
• Name-Surname
• Manufacturer
• Birthday Flowerpot
• Last revision
• Preferences • Humidity
• Product id
• Location • Watering plan
• temperature
10
FIWARE NGSI: “The SNMP for IoT”
Context Broker
11
Connecting to the Internet of Things
Context Broker
12
Orion Context Broker
• Main functions:
– Context management
– Context availability management (advanced topic) (*)
• HTTP and REST-based
– JSON payload support
• Context in NGSI is based in an entity-attribute model:
Entity Attributes
• Name
• EntityId has
• Type
• EntityType
1 n • Value
Producers
1026
update
notify
update
1026
notify
update
DB
14
Orion Context Broker – check health
GET <cb_host>:1026/version
{
"orion" : {
"version" : "3.12.0",
"uptime" : "0 d, 0 h, 0 m, 5 s",
"git_hash" : "...",
"compile_time" : "nodate",
"compiled_by" : "fermin",
"compiled_in" : "centollo",
"release_date" : "nodate",
"machine" : "x86_64",
"doc" : "https://fiware-orion.readthedocs.org/en/3.11.0/",
"libversions": …
}
}
15
Orion Context Broker Basic Operations
Entities
• GET /v2/entities
• Retrieve all entities
• POST /v2/entities
• Creates an entity
• GET /v2/entities/{entityID}
• Retrieves an entity
• [PUT|PATCH|POST] /v2/entities/{entityID}
• Updates an entity (different “flavors”)
• DELETE /v2/entities/{entityID}
• Deletes an entity
16
Orion Context Broker Basic Operations
Attributes
• GET /v2/entities/{entityID}/attrs/{attrName}
• Retrieves an attribute’s data
• PUT /v2/entities/{entityID}/attrs/{attrName}
• Updates an attribute’s data
• DELETE /v2/entities/{entityID}/attrs/{attrName}
• Deletes an attribute
• GET /v2/entities/{entityID}/attrs/{attrName}/value
• Retrieves an attribute’s value
• PUT /v2/entities/{entityID}/attrs/{attrName}/value
• Updates an attribute’s value
17
Context Broker operations: create & pull data
update query
18
Quick Usage Example: Car Create
POST <cb_host>:1026/v2/entities
Content-Type: application/json
...
You can add "?options=upsert" to
{
the URL. In that case, the entity is
"id": "Car1",
"type": "Car", updated if already exists.
"speed": {
"type": "Float",
"value": 98
}
}
201 Created
19
Quick Usage Example: Car Speed Update (1)
PUT <cb_host>:1026/v2/entities/Car1/attrs/speed
Content-Type: application/json
...
{
"type": "Float",
"value": 110
}
204 No Content
…
20
Quick Usage Example: Car Speed Query (1)
GET <cb_host>:1026/v2/entities/Car1/attrs/speed
200 OK
Content-Type: application/json
...
{
"type": "Float",
"value": 110,
"metadata": {}
}
You can get all the attributes of the entity using the
entity URL:
GET/v2/entities/Car1/attrs
21
Quick Usage Example: Car Speed Update (2)
PUT <cb_host>:1026/v2/entities/Car1/attrs/speed/value
Content-Type: text/plain
...
115
204 No Content
…
22
Quick Usage Example: Car Speed Query (2)
GET <cb_host>:1026/v2/entities/Car1/attrs/speed/value
Accept: text/plain
200 OK
Content-Type: text/plain
...
115.000000
23
Quick Usage Example: Room Create (1)
POST <cb_host>:1026/v2/entities
Content-Type: application/json
...
{
"id": "Room1",
"type": "Room",
"temperature": {
"type": "Float",
"value": 24
},
"pressure": {
"type": "Integer",
"value": 718
}
}
201 Created
...
24
Quick Usage Example: Room Update (1)
PATCH <cb_host>:1026/v2/entities/Room1/attrs
Content-Type: application/json
...
{
"temperature“: {
"type": "Float",
"value": 25
},
"pressure": {
"type": "Integer",
"value": 720
}
}
204 No Content
…
25
Quick Usage Example: Room Query (1)
GET <cb_host>:1026/v2/entities/Room1/attrs
200 OK
Content-Type: application/json
...
{
"pressure": {
"type": "Integer",
"value": 720,
"metadata": {}
},
"temperature": {
"type": "Float",
"value": 25,
"metadata": {}
}
}
26
Quick Usage Example: Room Query (2)
GET <cb_host>:1026/v2/entities/Room1/attrs?options=keyValues
200 OK
Content-Type: application/json
...
{
"pressure": 720,
"temperature": 25
}
27
Quick Usage Example: Room Create (2)
POST <cb_host>:1026/v2/entities
Content-Type: application/json
...
{
"id": "Room2",
"type": "Room",
"temperature": {
"type": "Float",
"value": 29
},
"pressure": {
"type": "Integer",
"value": 730
}
}
201 Created
...
28
Quick Usage Example: Filters (1)
GET <cb_host>:1026/v2/entities?options=keyValues&q=temperature>27
200 OK
Content-Type: application/json
...
[
{
"id": "Room2",
"pressure": 730,
"temperature": 29,
"type": "Room"
}
]
29
Quick Usage Example: Filters (2)
GET <cb_host>:1026/v2/entities?options=keyValues&q=pressure==715..725
200 OK
The full description of the Simple Content-Type: application/json
Query Language for filtering can be ...
found in the NGSIv2 Specification
document [
{
"id": "Room1",
"pressure": 720,
"temperature": 25,
"type": "Room"
}
]
30
Context Broker operations: push data
Application
Context Consumer
Context Broker
31
Quick Usage Example: Subscription
POST <cb_host>:1026/v2/subscriptions
Content-Type: application/json
…
{
"subject": {
"entities": [
{
"id": "Room1",
"type": "Room"
}
],
"condition": {
"attrs": [ "temperature" ]
}
},
"notification": {
"http": {
"url": "http://<host>:<port>/publish"
}, 201 Created
"attrs": [ "temperature" ] Location: /v2/subscriptions/51c0ac9ed714fb3b37d7d5a8
}, ...
"expires": "2026-04-05T14:00:00.000Z"
}
32
Quick Usage Example: Notification
25
19
33
Quick Usage Example: Notification
{
"subscriptionId": "574d720dbef222abb860534a",
"data": [
{
"id": "Room1",
"type": "Room",
"temperature": {
"type": "Float",
"value": 19,
"metadata": {}
}
}
]
}
34
List existing subscriptions
GET <cb_host>:1026/v2/subscriptions 200 OK
Content-Type: application/json
…
[{
"id": " 51c0ac9ed714fb3b37d7d5a8 ",
"expires": "2026-04-05T14:00:00.000Z",
"status": "active",
"subject": {
"entities": [{
"id": "Room1",
"type": "Room"
}],
The full description of the "condition": {
subscription object (including all its "attrs": ["temperature"]
fields) can be found in the NGSIv2 }
},
Specification "notification": {
"timesSent": 3,
"lastNotification": "2016-05-31T11:19:32.000Z",
"lastSuccess": "2016-05-31T11:19:32.000Z",
"attrs": ["temperature"],
"attrsFormat": "normalized",
"http": {
"url": "http://localhost:1028/publish"
}
}
35
}]
Orion Context Broker batch operations
• All them use POST as verb and the /v2/op URL prefix, including
operation parameters in the JSON payload
36
Batch Operation Example: Create Several Rooms
POST <cb_host>:1026/v2/op/update …
Conten-Type: application/json {
... "type": "Room",
"id": "Room4",
{ "temperature": {
"actionType": "append", "value": 31.8,
"entities": [ "type": "Float"
{ },
"type": "Room", "pressure": {
"id": "Room3", "value": 712,
"temperature": { "type": "Integer"
"value": 21.2, }
"type": "Float" }
}, ]
"pressure": { }
"value": 722,
"type": "Integer"
201 Created
}
...
},
…
37
How to get Orion? (Docker containers)
38
Would you like to play with this?
39
NGSI Go
40
Would you like to know more?
• References
– This presentation, available at
https://github.com/telefonicaid/fiware-orion#introducto
ry-presentations
– NGSIv2 Specification
• https://github.com/telefonicaid/fiware-orion/blob/master/doc/man
uals/orion-api.md
– Documentation at ReadTheDocs
• https://fiware-orion.readthedocs.io
– Orion support through StackOverflow
• Ask your questions using the “fiware-orion” tag
• Look for existing questions at
http://stackoverflow.com/questions/tagged/fiware-orion
41
Managing Context Information at
Large Scale: Advanced Topics
44
Pagination
45
Pagination
46
Pagination
47
Metadata
48
Complete NGSI Model
49
Compound Attribute/Metadata Values
50
Compound Attribute/Metadata Values
• Example: we have {
"type": "Car",
a car whose four "id": "Car1",
wheels' pressure "tirePressure": {
we want to "type": "kPa",
"value": {
represent as a "frontRight": 120,
compound "frontLeft": 110,
attribute for a car "backRight": 115,
"backLeft": 130
entity. We would }
create the car }
entity like this: }
51
Type Browsing
• GET /v2/types
• Retrieve a list of all entity types currently in Orion,
including their corresponding attributes and entities
count
• GET /v2/types/{typeID}
• Retrieve attributes and entities count associated to an
entity type
PRO TIP
GET /v2/contextTypes?options=values
Retrieves just a list of all entity types without any extra info
52
Geo-location
POST <cb_host>:1026/v2/entities
{
"type": "City",
• Entities can have an attribute "id": "Madrid",
that specifies its location "position": {
"type": "geo:json",
– value: a JSON object in "value": {"type": "Point", "coordinates":
GeoJSON format [ -3.691944, 40.418889] }
}
– type: geo:json }
• Example: create an entity called
null is also supported as value,
Madrid meaning "no location". This may be
…and create a couple more towns: useful in some cases.
• Leganés
• Alcobendas
53
Geo-location – Circle
54
Geo-location – Max distance
iu s
rad
k m
3 .5
1 GET <cb_host>:1026/v2/entities?
type=City&
georel=near;maxDistance:13500&
geometry=point&
coords=40.418889,-3.691944
55
Geo-location – Min distance
iu s
rad
k m
3 .5
1
GET <cb_host>:1026/v2/entities?
type=City&
georel=near;minDistance:13500&
geometry=point&
coords=40.418889,-3.691944
56
More geo-relationships
57
Update operators
CP A CP B
Sum 2 count = 10 Sum 3
GET /v2/entity/E/attrs/count
GET /v2/entity/E/attrs/count
Calculation 10
at CP: 10
Calculation
10 +2 = 12 at CP:
PUT /v2/entity/E/attrs/count (12)
count = 12 10 +3 = 13
59
Update operators
CP A CP B
Sum 2 PUT /v2/entity/E/attrs/count count = 10 Sum 3
{$inc: 2}
count = 12
PUT /v2/entity/E/attrs/count
{$inc: 3}
60
Query filters
• By geographical location
– Described in detail in previous slides
61
Query filters
attribute
name
• By attribute value (q)
GET <cb_host>:1026/v2/entities?q=temperature>25
62
POST <cb_host>:1026/v2/subscriptions
…
Query filters {
"subject": {
"entities": [
{
"id": “Car5",
"type": "Car"
• Filters can be also used in },
subscriptions {
"idPattern": "^Room[2-5]",
– id "type": "Room"
– type },
{
– id pattern "id": "D37",
– type pattern "typePattern": "Type[ABC]"
},
– attribute values ],
– metadata value "condition": {
"attrs": [ "temperature" ],
– geographical location "expression": {
"q": "temperature>40",
"mq": "humidity.avg==80..90",
"georel": "near;maxDistance:100000",
"geometry": "point",
"coords": "40.418889,-3.691944"
}
}
},
…
63 }
Datetime support
64
Datetime support
POST /v2/entities
…
{
Example: create entity John,
"id": "John", with birthDate attribute using
"birthDate": { type DateTime
"type": "DateTime",
"value": "1979-10-14T07:21:24.238Z"
}
}
65
Transient entities
POST /v2/entities
…
{
Example: create entity
"id": "Warning1", Warning1, to expire at noon
"dateExpires": { on July 30th, 2018
"type": "DateTime",
"value": “2018-07-30T12:00:00Z"
}
}
66
Unrestricted text attributes
…
"description": {
"type": "TextUnrestricted",
"value": "Hey! I'm using <forbidden chars>"
}
…
• Use it at your own risk!
67
Flow control
Client Receiver
receiver is not
client sending so fast
updates too fast
…
at some point in time, Orion
… notification queue will saturate
and new notifications will be
discarded
68
Flow control
Client Receiver
?options=flowControl
delays
introduced
by Orion
…
…
69
Flow control
70
Custom notifications
71
Custom notifications
Text based payload
PUT /v2/entities/DC_S1-D41/attrs/temp/value?type=Room
…
23.4
…
update "httpCustom": {
"url": "http://foo.com/entity/${id}",
"headers": {
"Content-Type": "text/plain"
},
"method": "PUT",
"qs": {
"type": "${type}"
},
notification "payload": "The temperature is ${temp} degrees"
}
…
PUT http://foo.com/entity/DC_S1-D41?type=Room
Content-Type: text/plain Custom notification configuration
Content-Length: 31
72
Custom notifications
JSON based payload
PUT /v2/entities/DC_S1-D41/attrs?type=Room
…
{ "temp": {"value": 23.4, "type": "Number"},
"status": {"value": "on", "type": "Text"} }
…
update "httpCustom": {
"url": "http://foo.com/entity/${id}",
"method": "PUT",
"qs": {
"type": "${type}"
},
"json": {
"t": "${temp}",
notification "s": "${status}"
}
}
PUT http://foo.com/entity/DC_S1-D41?type=Room …
Content-Type: application/json
Content-Length: 19 Custom notification configuration
{ "t": 23.4, "s": "on"}
73
Custom notifications
NGSI patching in payload
PUT /v2/entities/DC_S1-D41/attrs?type=Room
…
{ "A": {"value": "foo", "type": "Text"} }
update …
"httpCustom": {
"url": "http://foo.com/entity/${id}",
"method": "PUT",
"ngsi": {
"id": "stamp:${id}"
notification "A:map": { "value": "${A}", "type": "Text"}
"oldId": { "value": "${id}", "type": "Text"}
}
PUT http://foo.com/entity/DC_S1-D41 }
Content-Type: application/json …
Content-Length: 140
Custom notification configuration
{
"id": "stamp:DC_S1-D41",
Notification uses NGSIv2
"A": { "value": "foo", "type": "Text" },
"A:map": { "value": "foo", "type": "Text" },
compliant format (as in
"oldId": { "value": "DC_S1-D41", "type": "Text" } regular notifications)!
} 74
Notification status
75
Notification diagnosis workflow
"status" field
value?
active/oneshot
expired inactive
YES
failsCounter > 0?
NO
"lastSuccessCode“
field value?
4xx,
2xx 5xx
Subscription is expired Subscription is inactive Notifications are being Notifications are Some problem is
so notifications are not so notifications are not sent and the receiving being sent but the precluding
being sent. Update being sent. Update endpoint confirms receiving endpoint notifications to be
subscription to make it subscription to correct reception. is reporting HTTP sent. In HTTP
permanent or extend activate it. error. Check subscriptions, the
expiration time receiving endpoint "lastFailureReason"
(e.g. logs, etc.) field value should
provide additional
information on it.
77
Subscription auto-disabling
78
Subscriptions based in alteration type
79
Covered subscriptions
80
Covered subscriptions NOTIF1:
…
SUB 1: SUB 2 (covered) {
"notification": { "notification": { …
... ... "temp": {
"attrs": [ "attrs": [ "type": "Float",
"temp", "value": 19,
"temp",
"metadata": {}
"hum" "hum
}
] ],
}
} "covered": true
}
NOTIF2 (covered)
…
{
…
"temp": {
POST /v2/entities/E/attrs/temp
"type": "Number",
{ ...
"value": 19,
"value": 19, "metadata": {}
"type": "Number" },
} "hum": {
"type": "None",
"value": null,
"metadata": {}
}
81 }
MQTT notifications
POST <cb_host>:1026/v2/subscriptions
…
{
"subject": {
"entities": [
{ "idPattern": ".*" } MQTT Broker
],
},
"notification": { Context Broker
"mqtt": {
"url": "mqtt://<mqtt_broker>:1883",
"topic": "/notif", …
"qos": 1
} MQTT broker subscribers
}
}
82
Additional subscription improvements
84
Attributes filtering and special attributes
• Examples
– Include only attributes temp and lum
• In queries: GET /v2/entities?attrs=temp,lum
• In subscriptions: "attrs": [ "temp", "lum" ]
85
Metadata filtering and special metadata
86
Metadata filtering and special metadata
• Examples
– Include only metadata MD1 and MD2
• In queries: GET /v2/entities?metadata=MD1,MD2
• In subscriptions: "metadata": [ "MD1", "MD2" ]
87
Registration & Context Providers
1. register(provider= )
5. data 4. data
2. query 3. query
Context ContextBroker ContextProvider
Consumer
db
88
Registration & Context Providers
POST <cb_host>:1026/v2/registrations
…
{
"description": "Registration for Car1",
"dataProvided": {
"entities": [
{
"id": "Car1",
"type": "Car"
}
],
"attrs": [
"speed" 201 Created
] Location: /v2/registrations/5a82be3d093af1b94ac0f730
}, …
"provider": {
"http": {
"url": "http://contextprovider.com/Cars"
},
"legacyForwarding": true
}
}
GET <cb_host>:1026/v2/entities/Car1/attrs
200 OK
Content-Type: application/json data
...
{ query
"type": "Float", ContextBroker ContextProvider
"value": 110,
"metadata": {} db
}
90
Multitenancy
Context
• Simple multitenant model based on Broker
logical database separation.
• It eases tenant-based authorization
provided by other components. Tenant1
• Just use an additional HTTP header
called "Fiware-Service", whose value
is the tenant name. Example: Tenant2
Fiware-Service: Tenant1
91
Service Paths
92
Service Paths
• In order to use a service path we put in a new HTTP header called "Fiware-
ServicePath". For example:
Fiware-ServicePath: /Madrid/Gardens/ParqueNorte/Parterre1
• Special servicePath attribute can be used to retrieve entity service
path along any other attribute
– E.g. GET /v2/entities?attrs=servicePath,*
• Properties:
– A query on a service path will look only into the specified node
– Use "ParentNode/#" to include all child nodes
– Queries without Fiware-ServicePath resolve to "/#"
– Entities will fall in the "/" node by default
ParqueSur ParqueNorte ParqueOeste
Parterre1 Parterre2
93
Service Paths
• Properties (continued):
– You can OR a query using a comma (,) operator in
A B
the header
• For example, to query all street lights that are either in
ParqueSur or in ParqueOeste you would use:
ServicePath: Madrid/Gardens/ParqueSur,
A or B
Madrid/Gardens/ParqueOeste
• You can OR up to 10 different scopes.
– Maximum scope levels: 10
• Scope1/Scope2/.../Scope10
ParqueNorte
– You can have the same element IDs in different
scopes (be careful with this!)
– You can't change scope once the element is created light1
– One entity can belong to only one scope Parterre1
94
Cross-Origin Resource Sharing (CORS)
Context Consumer
Context Broker
Private Network
no Context Consumer
Context Broker ti fy
fy
ti
no
./ngrok http 8080
http://9762d35a.ngrok.io
ngrok.io
96
Would you like to know more?
• References
– This presentation, available at
https://github.com/telefonicaid/fiware-orion#introducto
ry-presentations
– NGSIv2 Specification
• https://github.com/telefonicaid/fiware-orion/blob/master/doc/man
uals/orion-api.md
– Documentation at ReadTheDocs
• https://fiware-orion.readthedocs.io
– Orion support through StackOverflow
• Ask your questions using the “fiware-orion” tag
• Look for existing questions at
http://stackoverflow.com/questions/tagged/fiware-orion
97
Thank you!
http://fiware.org
Follow @FIWARE on Twitter
Integration with existing systems
queryContext (e1,
attr1, attr2)
System A System B
Context Broker
99
Integration with sensor networks
create/monitor IoT
Agent
Manager
IoT IoT IoT FIWARE Backend
Agent-1 Agent-2 Agent-n IoT
Device Management
(southbound interfaces)
• Security Monitoring
Security • Built-in Identity/Access/Privacy Management
101
FI-WARE Context/Data Management Platform
Applications
Processing/Analysis
OMA NGSI-9/10 Algorithms
Gathered data is
injected for
processing/analysis
102
Special update action types
103