Considerations on NGSIv1 and NGSIv2 coexistence
NGSIv1 is the API offered by Orion Context Broker from its very first version. NGSIv2 development started in July 2015 in Orion 0.23.0. Although at the end NGSIv1 will be deprecated and removed from the code in some future Orion version (so only NGSIv2 will remain) this is a big work and both API versions will be coexisting during some time.
This document explains some consideration to take into account regarding such coexistence.
- Native JSON types
- Filtering
- Differences in the support to
DateTime
attribute type - Checking ID fields
orderBy
parameter- NGSIv1 notification with NGSIv2 subscriptions
- NGSIv2 query update forwarding to Context Providers
Native JSON types
NGSIv2 allows to create/update attributes (and metadata) whose values use JSON native
types (number, boolean, string, etc.). Unfortunately, NGSIv1 uses a JSON parser that converts
numbers and boolean values to string at creation/update time. Thus, an attempt of
setting A=2
using NGSIv1 will actually store A="2"
in the Orion database.
However, NGSIv1 rendering is able to correctly retrieve attribute values stored using
non-string JSON native types. Thus, if you set A=2
using NGSIv2 and retrieve that
attribute using NGSIv1, you will get A=2
.
Filtering
You can use the filtering capabilities developed for NGSIv2 (GET /v2/entities?q=<query>
) also
in NGSIv1 using a Scope element in the payload of POST /v1/queryContext
. See
the following section for details.
However, take into account that some of the filters (e. g. greater/less, range, etc.) are thought
for numeric values. Thus, in order to work properly, these filters (although using a
POST /v1/queryContext
) needs that the attributes to which they refer were created using NGSIv2 operations.
In addition, note that NGSIv2 geo-query filters can be used also in NGSIv1. See the following section for details
Differences in the support to DateTime
attribute type
NGSIv2 supports the DateTime
attribute type to identify dates. These attributes can be used with the query operators
greater-than, less-than, greater-or-equal, less-or-equal and range. See "Special Attribute Types" section at
NGSIv2 specification) and "DateTime support" section
in the NGSIv2 implementation notes.
However, note that DateTime
attribute type has not any special interpretation in the NGSIv1 API, i.e. the
attribute value is treated as any other string without any special meaning. That has two implications:
- Attributes created/updated using the NGSIv1 with type
DateTime
are treated as normal strings. - Attributes created using NGSIv2 (or which last update has been using NGSIv2) with type
DateTime
, then updated using NGSIv1 will "lose" their date nature and they are treated as normal string. - Attributes created using NGSIv1 (or which last update has been using NGSIv1) with type
DateTime
, then updated using NGSIv2 will "gain" their date nature so they can be used in date filters, etc.
Checking ID fields
NGSIv2 introduces syntax restrictions for ID fields (such as entity id/type, attribute name/type
or metadata name/type) which are described in the "Field syntax restrictions" section in the
NGSIv2 specification. In order to
keep backward compatibility, these restrictions are not used in the NGSIv1 API by default, but
you can enable them using the -strictNgsiv1Ids
CLI parameter.
Note that even when -strictNgsiv1Ids
is used the Orion DB may contain entities/attributes/medatada
that don't conform with NGSIv2 ID rules. This may happen if such entities/attributes/metadata have
been created by Orion in moments in which it has run without -strictNgsiv1Ids
enabled (maybe an old
version previous to the implementation of this feature). In that case, getting such
entities/attributes/metadata using NGSIv2 API may result in inconstent results (e.g. to use GET /v2/entities
and get entities with whitespaces in some entity ids).
The following issue at github has been created to cope with this situation in the future. However, in general it is as good practise to follow always the "Field sysntax restrictions" for IDs even if you are not forced to do so in NGSIv1. Doing so you would avoid any problem when managing context information, no matter which version of the API (either NGSIv1 or NGSIv2) you use.
orderBy
parameter
The orderBy
parameter defined for NGSIv2 can be used also in NGSIv1 queryContext operation (see
details in the pagination documentation. However, note that the "geo:distance"
order can be used only in NGSIv2.
NGSIv1 notification with NGSIv2 subscriptions
NGSIv2 allows several notification modes depending on the attrsFormat
field associated to the
subscription. Apart from the values described in the NGSIv2 specification, Orion also support
legacy
value in order to send notifications in NGSIv1 format. This way, users can have the
enhancements of NGSIv2 subscriptions (e.g. filtering or system/builtin metadata in notifications) with
NGSIv1 legacy notifications receivers.
NGSIv2 query update forwarding to Context Providers
Context availability management functionality (i.e. operations to register Context Providers) is still to be implemented for NGSIv2. However, you can register providers using NGSIv1 operations and have your NGSIv2-based updates and queries being forwarded to Context Providers, getting the response in NGSIv2. The forwarded message in the CB to CPr communication, and its response, is done using NGSIv1.
However, the following considerations have to be taken into account:
- Query filtering (e.g.
GET /v2/entities?q=temperature>40
) is not supported on query forwarding. First, Orion doesn't include the filter in thePOST /v1/queryContext
operation forwarded to CPr. Second, Orion doesn't filter the CPr results before responding them back to client. An issue corresponding to this limitation has been created: https://github.com/telefonicaid/fiware-orion/issues/2282 - On forwarding, any type of entity in the NGSIv2 update/query matches registrations without entity type. However, the
opposite doesn't work, so if you have registrations with types, then you must use
?type
in NGSIv2 update/query in order to obtain a match. - In the case of partial updates (e.g.
POST /v2/op/entities
resulting in some entities/attributes being updated and other entities/attributes not being updated due to failing or missing CPrs), 404 Not Found is returned to the client. Theerror
field in this case isPartialUpdate
and thedescription
field contains information about which entity attributes failed to update.