Fiware Ngsi Api
Fiware Ngsi Api
●2
Being “Smart” requires first being “Aware”
Context Information
Citizen
• Name-Surname Shop
Bus • Birthday • Location
• Location • Preferences • Business name
• No. passengers • Location • Franchise
• Driver • ToDo list • offerings
• Licence plate
●3
Being “Smart” requires first being “Aware”
Context Information
Users
Boiler • Name-Surname
• Manufacturer • Birthday
Flowerpot
• Last revision • Preferences
• Humidity
• Product id • Location
• Watering plan
• temperature • ToDo list
●4
Different sources of context need to be handle
Application/Service
Standard API
Context Broker
Context Broker
●8
Context Management in FIWARE
The FIWARE Context Broker GE implements the OMA NGSI-9/10 API: a simple
yet powerful standard API for managing Context information complying with
the requirements of a smart city
The FIWARE NGSI API is Restful: any web/backend programmer can manage it
Application/Service
NGSI API
Boiler
Context Broker
Users
• Manufacturer • Name-Surname
• Last revision • Birthday Flowerpot
• Product id • Preferences • Humidity
• temperature • Location • Watering plan
• ToDo list
●9
Orion Context Broker
Main functions:
Context management
HTTP and REST-based
JSON payload support
Context in NGSI is based in an entity-attribute model:
Entity Attributes
• EntityId “has” • Name
• EntityType • Type
1 n
• Value
10
Two “flavors” of NGSI API
NGSIv1
Original NGSI RESTful binding of OMA-NGSI
Implemented in 2013
Uses the /v1 prefix in resource URL
NGSIv2
A revamped, simplified binding of OMA-NGSI
Simple things must be easy
Complex things should be possible
Agile, implementation-driven approach
Make it as developer-friendly as possible (RESTful, JSON, …)
11
Orion Context Broker in a nutshell
update
1026
notify
update
DB
●12
Orion Context Broker – check health
GET <cb_host>:1026/version
{
"orion" : {
"version" : "1.3.0",
"uptime" : "7 d, 21 h, 33 m, 39 s",
"git_hash" : "af44fd1fbdbbfd28d79ef4f929e871e515b5452e",
"compile_time" : "Tue Jun 15 11:52:53 CET 2016",
"compiled_by" : "fermin",
"compiled_in" : "centollo"
}
}
●13
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
●14
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
●15
Context Broker operations: create & pull data
Context Producers publish data/context elements by invoking the update
operations on a Context Broker.
update query
●16
Quick Usage Example: Car Create
POST
<cb_host>:1026/v2/entities
Content-Type: application/json
...
{
"id": "Car1",
"type": "Car",
"speed": {
"type": "Float",
"value": 98 201 Created
}
}
●17
Quick Usage Example: Car Speed Update (1)
PUT
<cb_host>:1026/v2/entities/Car1/attrs/speed
Content-Type: application/json
...
{
"type": "Float",
"value": 110 In the case of id ambiguity, you can
} use "?type=Car" to specify entity type
204 No Content
…
●18
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,
You can get all the attributes of the entity using "metadata": {}
the entity URL: }
GET/v2/entities/Car1/attrs
●19
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
…
●20
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
●21
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", 201 Created
"value": 718 ...
}
}
●22
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
…
●23
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": {}
}
}
●24
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
}
●25
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", 201 Created
"value": 730 ...
}
}
●26
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"
}
]
●27
Quick Usage Example: Filters (2)
GET
<cb_host>:1026/v2/entities?options=keyValues&q=pressure==715..7
25
200 OK
Content-Type: application/json
...
The full description of the Simple
Query Language for filtering can [
be found in the NGSIv2 {
Specification document "id": "Room1",
"pressure": 720,
"temperature": 25,
"type": "Room"
}
]
●28
Context Broker operations: push data
Application
Context Consumer
Context Broker
●29
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
}, Location: /v2/subscriptions/
"attrs": [ "temperature" ]
}, 51c0ac9ed714fb3b37d7d5a8
"expires": "2026-04-05T14:00:00.00Z" ...
}
●30
Quick Usage Example: Notification
{
"subscriptionId": "574d720dbef222abb860534a",
"data": [
{
"id": "Room1",
"type": "Room",
"temperature": {
"type": "Float",
"value": 19,
"metadata": {}
}
}
]
}
●31
List existing subscriptions
GET 200 OK
Content-Type: application/json
<cb_host>:1026/v2/subscriptions …
[{
"id": " 51c0ac9ed714fb3b37d7d5a8 ",
"expires": "2026-04-05T14:00:00.00Z",
"status": "active",
"subject": {
"entities": [{
"id": "Room1",
"type": "Room"
}],
The full description of the "condition": {
"attrs": ["temperature"]
subscription object (including all }
},
its fields) can be found in the "notification": {
NGSIv2 Specification "timesSent": 3,
"lastNotification": "2016-05-31T11:19:32.00Z",
"attrs": ["temperature"],
"attrsFormat": "normalized",
"http": {
"url": "http://localhost:1028/publish"
}
}
}]
●32
Orion Context Broker batch operations
All them use POST as verb and the /v2/op URL prefix, including
operation parameters in the JSON payload
33
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
}
}, ...
…
●34
Advanced Features
Orion Context Broker Metadata
Compound attribute/metadata values
Type browsing
Geo-location
Query filters
Custom notifications
Attribute/metadata filtering and special
attribute/metadata
Registrations & context providers
●35
Metadata
Users may attach metadata to attributes
Reserved metadata: ID, location, dateCreated, dateModified, previousValue, actionType
Examples:
… …
"temperature": { "temperature": {
"type": "Float", "type": "Float",
"value": 26.5, "value": 26.5,
"metadata": { "metadata": {
{ {
"accuracy": { “average": {
"type": "Float", "type": "Float",
"value": 0.9 "value": 22.4
} }
} }
} }
… ●36
…
Complete NGSI Model
●37
Compound Attribute/Metadata Values
●38
Compound Attribute/Metadata Values
PRO TIP
GET /v2/contextTypes?options=values
Retrieves just a list of all entity types without any extra info
●40
Geo-location
POST <cb_host>:1026/v2/entities
{
Entities can have an attribute that specifies its "type": "City",
location "id": "Madrid",
"position": {
Several attribute types can be used "type": "geo:point",
geo:point (for points)
"value": "40.418889, -3.691944"
geo:line (for lines)
geo:box (for boxes) }
geo:polygon (for polygons) }
geo:json (for arbitrary geometries, in GeoJson
standard)
●41
Geo-location – Circle
●42
Geo-location – Max distance
GET <cb_host>:1026/v2/entities?
idPattern=.*&
type=City&
georel=near;maxDistance:13500&
geometry=point&
coords=40.418889,-3691944
●43
Geo-location – Min distance
GET <cb_host>:1026/v2/entities?
idPattern=.*&
type=City&
georel=near;minDistance:13500&
geometry=point&
coords=40.418889,-3691944
●44
More geo-relationships
Apart from near, the following georel can be used
georel=coveredBy
georel=intersects
georel=equals
georel=disjoint
See NGSIv2 Specification for a detailed description
●45
Query filters
For the GET /v2/entities operation
●46
Query filters
attribute name
By attribute value (q)
GET <cb_host>:1026/v2/entities?q=temperature>25
attribute sub-key (for compound attribute values only)
GET
<cb_host>:1026/v2/entities?q=tirePressure.frontRight
attribute name
By metadata
>130 value (mq) metadata name
metadata sub-key (for compound
GET
metadata values only)
<cb_host>:1026/v2/entities?mq=temperature.avg>25
GET <cb_host>:1026/v2/entities?mq=tirePressure.accuracy.frontRight
>90
See full details about q and mq query language in NGSIv2 specification
●47
POST <cb_host>:1026/v2/subscriptions
…
{
Query filters "subject": {
"entities": [
{
"id": “Car5",
"type": “Car"
Filters can be also used in {
},
…
"httpCustom": {
update "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
Content-Length: 31 Custom notification configuration
The temperature is 23.4 degrees
50
Registration & Context Providers
POST <cb_host>:1026/v1/registry/registerContext
…
{
"contextRegistrations": [
{
"entities": [
{
"type": "Car",
"isPattern": "false",
"id": "Car1"
},
200 OK
"attributes": [ ...
{ {
"name": "speed", "duration" : "P1M",
"type": "float", "registrationId" : "52a744b011f5816465943d58"
"isDomain": "false" }
}
],
"providingApplication": "http://contextprovider.com/Cars"
}
],
Context management availability functionality not yet specified in
"duration": "P1M"
}
NGSIv2. Thus, a NGSIv1 operation is used to create the registration.
●51
Registration & Context Providers
GET
<cb_host>:1026/v2/entities/Car1/attrs
200 OK
data
Content-Type: application/json
...
query
{ ContextBroker ContextProvider
"type": "Float", db
"value": 110,
"metadata": {}
}
●52
INTEGRATION WITH OTHER
SYSTEMS
●53
FI-WARE Context/Data Management Platform
Applications
Processing/Analysi
OMA NGSI- s Algorithms
9/10
Gathered data is
injected for
processing/analysis
Data generated either by
CEP or BigData is published
Gathered data
injected for CEP-like BigData
processing (COSMOS)
Distributed Direct Big
Context Complex Event Data
Sources Processed data injection
Processing (PROTON) is injected for
Programming processing and
analysis
of rules Context/Data Management Platform
●54
How to store the context information
Cygnus
BigData
Cosmos
The 4 Storage Systems
All files are available (as template) in /usr/cygnus/conf/ folder (in detail see README.md file):
log4j.properties cp /usr/cygnus/conf/log4j.properties.template /usr/cygnus/conf/log4j.properties
cygnus_instance.conf
cygnus_instance_<id>.conf
1. Context Broker and MySQL
agent_mysql.conf
1. Context Broker and MySQL
Edit cygnus_instance_mysql.conf
1. Context Broker and MySQL
Edit agent_mysql.conf
1. Context Broker and MySQL
Edit agent_mysql.conf
Example of publish/subscribe
updateContext : queryContext : subscribeContext:
{ { {
"contextElements": [ "entities": [ "entities": [
{ { {
"type": "Car", "type": "Car", "type": "Car",
"isPattern": "false", "isPattern": "false", "isPattern": "false",
"id": "Car1", "id": "Car1" "id": "Car1"
"attributes": [ } }
{ ] ],
"name": "speed", } "attributes": [
"type": "float", "speed"
"value": "98" ],
} "reference": "http://localhost:5050/notify",
] "duration": "P1M",
} "notifyConditions": [
], {
"updateAction": "APPEND" "type": "ONCHANGE",
} "condValues": [
"speed"
]
Headers: }
Content-Type: application/json ],
"throttling": "PT1S"
Accept: application/json
}
Fiware-Service: vehicles
Fiware-ServicePath: /4wheels
1. Context Broker and MySQL - Example: check the context data
$ mysql -u root –p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| mysql |
| test |
| vehicles |
+-----------------------+
4 rows in set (0.01 sec)
mysql> use vehicles;
...
Database changed
mysql> show tables;
+--------------------+
| Tables_in_vehicles |
+--------------------+
| 4wheels_car1_car |
+--------------------+
1 row in set (0.00 sec)
mysql> select * from 4wheels_car1_car;
...
2. Context Broker and mongoDB
Edit cygnus_instance_mongo.conf
2. Context Broker and mongoDB
Edit agent_mongo.conf
2. Context Broker and mongoDB
Edit agent_mongo.conf
2. Context Broker and mongoDB - Example: check the context data
$ mongo
MongoDB shell version: 2.6.9
connecting to: test
...
> show databases
admin (empty)
local 0.031GB
orion 0.031GB
orion-vehicles 0.031GB
sth_vehicles 0.031GB
test (empty)
> use sth_vehicles
switched to db sth_vehicles
> show collections
sth_/4wheels_car1_car
system.indexes
> db['sth_/4wheels_car1_car'].find()
...
3. Context Broker and Cosmos
agent_cosmos.conf
C
y
g BigData
n Cosmos
u
s
3. Context Broker and Cosmos
Edit cygnus_instance_cosmos.conf
3. Context Broker and Cosmos
Edit agent_cosmos.conf
3. Context Broker and Cosmos
Edit agent_cosmos.conf
3. Context Broker and Cosmos - How to get the auth-token
Response:
{"access_token": "P8vBzFdJB2ZBNHwfxUHFrfc1buTx7n", "token_type": "Bearer", "expires_in": 3600,
"refresh_token": "He8aJdQiEkpbnQB4KFAS1DFra9RhNq"}
{"FileStatuses":{"FileStatus":[...]}}
3. Context Broker and Cosmos - Example: check the context data
agent_ckan.conf
C
y
g
n
u
s
4. Context Broker and CKAN
Edit cygnus_instance_ckan.conf
4. Context Broker and CKAN
Edit agent_ckan.conf
4. Context Broker and CKAN
Edit agent_ckan.conf
4. Context Broker and CKAN - Example: check the context data
Would you like to play with this?
●81
NGSI Plugin for Freeboard
Create a real time dashboard for your entities, representing gauges,
spark lines and maps. No coding required!
• https://github.com/telefonicaid/fiware-dataviz
In addition, Freeboard freemium version integrates Orion off-the-shelf
Would you like to know more?
The easy way
This presentation: google for “fermingalan slideshare” and search the one named “Managing
Context Information at large scale”
Orion User Manual: google for “Orion FIWARE manual” and use the first hit
Orion Catalogue page: google for “Orion FIWARE catalogue” and use the first hit
References
NGSIv2 Specification
http://fiware.github.io/specifications/ngsiv2/stable
http://fiware.github.io/specifications/ngsiv2/latest
NGSIv2 for NGSIv1 developers
http://bit.ly/ngsiv2-vs-ngsiv1
This presentation
http://www.slideshare.net/fermingalan/fiware-managing-context-information-at-large-scale
Orion Catalogue:
http://catalogue.fiware.org/enablers/publishsubscribe-context-broker-orion-context-broker
Orion support trhough StackOverflow
Ask your questions using the “fiware-orion” tag
Look for existing questions at http://stackoverflow.com/questions/tagged/fiware-orion
●83
TEST IT AT HOME
●84