GithubHelp home page GithubHelp logo

jason-fox / lepus Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 145 KB

An NGSI-LD wrapper for NGSI-v2 Context Brokers

License: MIT License

Dockerfile 1.16% JavaScript 98.84%
fiware fiware-orion ngsi-ld ngsi-ld-client

lepus's Introduction

FIWARE Lepus

FIWARE Core Context Management License: MIT Docker badge
NGSI v2 NGSI LD JSON LD
CI Coverage Status

An NGSI-LD wrapper for use with NGSI-v2 Context Brokers. It understands the NGSI-LD endpoints and inputs, converts them to NGSI-v2, makes a request to the NGSI-v2 broker behind it and transforms responses back to NGSI-LD using a fixed JSON-LD @context. It supports the NGSI-LD federationOps endpoints only and is designed to be used as a registered source with NGSI-LD Context Brokers in federation mode.

Example

Give me the Stores with the name "Einkauf" in NGSI-LD format

curl -L 'https://<lepus-context-broker>/ngsi-ld/v1/entities?type=Store&q=name%22Einkauf%22' \
-H 'Link: <http://context/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/ld+json'

is transformed to Give me the Stores with the name "Einkauf" in NGSI-v2 format

curl -L 'https://<ngsi-v2-context-broker>/v2/entities?type=Store&q=nameEinkauf' \
-H 'Accept: application/json'

All NGSI-v2 response attributes are converted to Property or Relationship (or subtype) and a fixed @context added to the body or Link header.

Supported Endpoints

The following endpoints are supported as defined in the latest NGSI-LD Specification

Entities

  • GET /entities/ - Query entities - §5.7.2; §6.4.3.2
  • POST /entities/ - Entity creation - §5.6.1; §6.4.3.1
  • DELETE /entities/ - Purge Entities

  • GET /entities/{entityId} - Entity retrieval by ID - §5.7.1; §6.5.3.1
  • PATCH /entities/{entityId} - Merge Entity - §5.6.17; §6.5.3.4
  • PUT /entities/{entityId} - Replace Entity - §5.6.17; §6.5.3.3
  • DELETE /entities/{entityId} - Entity retrieval by ID - §5.6.6; §6.5.3.2

  • POST /entities/{entityId}/attrs - Append Entity attributes - §5.6.3; §6.6.3.1
  • PATCH /entities/{entityId}/attrs - Update Entity attributes - §5.6.2; §6.6.3.2

  • DELETE /entities/{entityId}/{attr} - Delete Entity attribute - §5.6.5; §6.7.3.2
  • PUT /entities/{entityId}/{attr} - Replace Entity attribute - §5.6.19; §6.7.3.3

Subscriptions

  • POST /subscriptions/ - Create Subscription - §5.8.1; §6.10.3.1
  • GET /subscriptions/ - Retrieve list of Subscriptions - §5.8.4; §6.10.3.2
  • GET /subscriptions/{subscriptionId} - Subscription retrieval by ID - §5.8.3; §6.11.3.1
  • PATCH /subscriptions/{subscriptionId} - Subscription update by ID - §5.8.2; §6.11.3.2
  • DELETE /subscriptions/{subscriptionId} - Subscription deletion by ID - §5.8.5; §6.11.3.3

Types

  • GET /types/ - Retrieve available entity types - §5.7.5; §5.7.6; §6.25.3.1
  • GET /types/{type} - Details about available entity type - §5.7.7; §6.26.3.1

Attributes

  • GET /attributes/ - Available attributes - §5.7.8; §5.7.9; §6.27.3.1
  • GET /attributes/{attrId} - Details about available attribute - §5.7.10; §6.28.3.1

Batch Operations

  • POST /entityOperations/create - Batch Entity creation - §5.6.7; 6.14.3.1
  • POST /entityOperations/upsert - Batch Entity create or update (upsert) - §5.6.8; §6.15.3.1
  • POST /entityOperations/update - Batch Entity update - §5.6.9; §6.16.3.1
  • POST /entityOperations/delete - Batch Entity deletion - §5.6.10; §6.17.3.1
  • POST /entityOperations/query - Query entities based on POST - §5.7.2; §6.23.3.1 ❌
  • POST /entityOperations/merge - Batch Entity merge - §5.6.20; §6.31.3.1 ❌

Internally

  • POST /notify/ receives NGSI-v2 notifications and forwards then as NGSI-LD

Usage

To use the wrapper, run:

 docker-compose up

Then insert some data into an NGSI-v2 broker such as Telefónica's Orion Context Broker

curl -L 'http://localhost:1026/v2/entities/' \
-H 'Content-Type: application/json' \
-d '{
    "id": "urn:ngsi-ld:Store:001",
    "type": "Store",
    "address": {
        "type": "PostalAddress",
        "value": {
            "streetAddress": "Bornholmer Straße 65",
            "addressRegion": "Berlin",
            "addressLocality": "Prenzlauer Berg",
            "postalCode": "10439"
        },
        "metadata": {
    		"verified": {
        		"value": true,
        		"type": "Boolean"
    		}
    	}
    },
    "location": {
        "type": "geo:json",
        "value": {
             "type": "Point",
             "coordinates": [13.3986, 52.5547]
        }
    },
    "name": {
        "type": "Text",
        "value": "Bösebrücke Einkauf"
    }
}'
curl -L 'http://localhost:1026/v2/entities/' \
-H 'Content-Type: application/json' \
-d '{
  "type": "Store",
    "id": "urn:ngsi-ld:Store:002",
    "address": {
        "type": "PostalAddress",
        "value": {
            "streetAddress": "Friedrichstraße 44",
            "addressRegion": "Berlin",
            "addressLocality": "Kreuzberg",
            "postalCode": "10969"
        },
        "metadata": {
    		"verified": {
        		"value": true,
        		"type": "Boolean"
    		}
    	}
    },
    "location": {
        "type": "geo:json",
        "value": {
             "type": "Point",
             "coordinates": [13.3903, 52.5075]
        }
    },
    "name": {
        "type": "Text",
        "value": "Checkpoint Markt"
    }
}'

Now query Lepus Accept: application/ld+json as if it is an NGSI-LD source - e.g.

curl -L 'http://<lepus-context-broker>/ngsi-ld/v1/types' \
-H 'Link: <http://context/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/ld+json'

returns NGSI-LD with a fixed @context

{
    "id": "urn:ngsi-ld:EntityTypeList:a1993de4-83e0-44c9-990b-be2e84b31df1",
    "type": "EntityTypeList",
    "typeList": [
        "Store"
    ],
    "@context": [
        "https://fiware.github.io/tutorials.Step-by-Step/tutorials-context.jsonld",
        "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.8.jsonld"
    ]
}

Alternatively query Lepus with Accept: application/json

curl -iX 'https://<lepus-context-broker>/ngsi-ld/v1/types' \
-H 'Link: <https://<lepus-context-broker>/context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/json'

Returns

Link: <https://<lepus-context-broker>/context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"
{
    "id": "urn:ngsi-ld:EntityTypeList:a1993de4-83e0-44c9-990b-be2e84b31df1",
    "type": "EntityTypeList",
    "typeList": [
        "Store"
    ]
}

And https://<lepus-context-broker>/context.jsonld returns NGSI-LD with a fixed @context

{
    "@context": [
        "https://fiware.github.io/tutorials.Step-by-Step/tutorials-context.jsonld",
        "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.8.jsonld"
    ]
}

Configuration

Configuration occurs using Docker Environment variables:

  • DEBUG - for debug output - e.g. DEBUG=adapter:*
  • LEPUS_PORT - port used for interactions - defaults to 3000
  • LEPUS_ALIAS - host alias for Lepus
  • LEPUS_RELAY_TIMEOUT maximum length of time to forward the NGSI-LD notification
  • LEPUS_URL for the location of Lepus itself - e.g. LEPUS_URL=http://<lepus>
  • LEPUS_MULTI_CORE - whether to use multiple cores
  • INCLUDE_VALUE_TYPE - include the NGSI-v2 attribute type in the returned payload
  • NGSI_V2_CONTEXT_BROKER for the NGSI-v2 Orion Context Broker URL - e.g. NGSI_V2_CONTEXT_BROKER=http://orion2:1026
  • NGSI_V2_TIMEOUT maximum length of time to access the NGSI-v2 Orion Context Broker URL in milliseconds
  • USER_CONTEXT_URL for the fixed JSON-LD @context to be supplied - e.g. USER_CONTEXT_URL=https://fiware.github.io/tutorials.Step-by-Step/tutorials-context.jsonld
  • CORE_CONTEXT_URL for the fixed core NGSI-LD @context to be supplied - e.g. CORE_CONTEXT_URL=https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.8.jsonld

NGSI-LD Registration

Since Lepus does not understand JSON-LD directly, registrations must be created within an LD context broker using the "key": "jsonldContext" to preprocess all interactions:

curl -L 'http://localhost:9090/ngsi-ld/v1/csourceRegistrations/' \
-H 'Content-Type: application/json' \
-H 'Link: <https://<lepus-context-broker>/context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-d ' {
    "type": "ContextSourceRegistration",
    "information": [
        {
            "entities": [
                {
                    "type": "Shelf"
                }
            ]
        }
    ],
     "contextSourceInfo":[
        {
            "key": "jsonldContext",
            "value": "https://<lepus-context-broker>/context.jsonld"
        }
    ],
    "mode": "inclusive",
    "operations": [
        "federationOps"
    ],
    "endpoint": "https://<lepus-context-broker>"
}'

License

MIT © 2023 FIWARE Foundation e.V.

lepus's People

Contributors

jason-fox avatar

Stargazers

 avatar

Watchers

 avatar  avatar

lepus's Issues

Missing @context property when requesting entities

Hi @jason-fox,

When requesting entities, I realized I'm not getting the @context property in the response object. As an example:

Request: GET /ngsi-ld/v1/entities?type=WasteContainer&scopeQ=/residuos_contenedores_vlc&limit=1&offset=0&options=count 200 1117.296 ms - 1478

Response:

[
    {
        "id": "urn:ngsi-ld:WasteContainer:wastecontainer:62050d6d89ed175e1cb6be36",
        "type": "WasteContainer",
        "c_factor": {
            "type": "Property",
            "value": 1,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "cargoVolume": {
            "type": "Property",
            "value": 0,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "category": {
            "type": "Property",
            "value": [
                "Superficie"
            ],
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "depth": {
            "type": "Property",
            "value": 1,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "fillingLevel": {
            "type": "Property",
            "value": 0.52,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "height": {
            "type": "Property",
            "value": 1,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "location": {
            "type": "GeoProperty",
            "value": {
                "type": "Point",
                "coordinates": [
                    -0.390989185,
                    39.452512151
                ]
            },
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "madeOf": {
            "type": "Property",
            "value": "other",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "maintenanceOwner": {
            "type": "Property",
            "value": "Wellness ",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "modelName": {
            "type": "Property",
            "value": "-",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "offset_height": {
            "type": "Property",
            "value": 0,
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "operationalStatus": {
            "type": "Property",
            "value": "ok",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "refDevice": {
            "type": "Relationship",
            "object": "device:wt:866039045934040",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "serialNumber": {
            "type": "Property",
            "value": "-",
            "observedAt": "2023-10-17T23:00:27.789Z"
        },
        "temperature": {
            "type": "Property",
            "value": 22,
            "observedAt": "2023-10-17T23:00:27.789Z"
        }
    }
]

However, the /types example of the README is working fine:

Request: GET /ngsi-ld/v1/types 200 433.831 ms - 223

Response:

{
    "id": "urn:ngsi-ld:EntityTypeList:98724129-38b6-489d-aff3-4192ae9feb35",
    "type": "EntityTypeList",
    "typeList": [],
    "@context": "https://raw.githubusercontent.com/smart-data-models/dataModel.WasteManagement/master/context.jsonld"
}

I think the problem may come from the appendContext function in constants.js. I've checked the isJSONLD variable, whose value is true, but it appears to be appending the property outside the object. See how @context is undefined inside the object before appending and how the function creates a new array element outside the object after appending.

-----ldPayload BEFORE APPEND -----
[
  {
    '@context': undefined,
    id: 'urn:ngsi-ld:WasteContainer:wastecontainer:62050d6d89ed175e1cb6be36',
    type: 'WasteContainer',
    c_factor: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    cargoVolume: {
      type: 'Property',
      value: 0,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    category: {
      type: 'Property',
      value: [Array],
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    depth: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    fillingLevel: {
      type: 'Property',
      value: 0.52,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    height: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    location: {
      type: 'GeoProperty',
      value: [Object],
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    madeOf: {
      type: 'Property',
      value: 'other',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    maintenanceOwner: {
      type: 'Property',
      value: 'Wellness ',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    modelName: {
      type: 'Property',
      value: '-',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    offset_height: {
      type: 'Property',
      value: 0,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    operationalStatus: {
      type: 'Property',
      value: 'ok',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    refDevice: {
      type: 'Relationship',
      object: 'device:wt:866039045934040',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    serialNumber: {
      type: 'Property',
      value: '-',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    temperature: {
      type: 'Property',
      value: 22,
      observedAt: '2023-10-17T23:00:27.789Z'
    }
  }
]
----------------------------------


-----ldPayload AFTER APPEND -----
[
  {
    '@context': undefined,
    id: 'urn:ngsi-ld:WasteContainer:wastecontainer:62050d6d89ed175e1cb6be36',
    type: 'WasteContainer',
    c_factor: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    cargoVolume: {
      type: 'Property',
      value: 0,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    category: {
      type: 'Property',
      value: [Array],
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    depth: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    fillingLevel: {
      type: 'Property',
      value: 0.52,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    height: {
      type: 'Property',
      value: 1,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    location: {
      type: 'GeoProperty',
      value: [Object],
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    madeOf: {
      type: 'Property',
      value: 'other',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    maintenanceOwner: {
      type: 'Property',
      value: 'Wellness ',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    modelName: {
      type: 'Property',
      value: '-',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    offset_height: {
      type: 'Property',
      value: 0,
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    operationalStatus: {
      type: 'Property',
      value: 'ok',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    refDevice: {
      type: 'Relationship',
      object: 'device:wt:866039045934040',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    serialNumber: {
      type: 'Property',
      value: '-',
      observedAt: '2023-10-17T23:00:27.789Z'
    },
    temperature: {
      type: 'Property',
      value: 22,
      observedAt: '2023-10-17T23:00:27.789Z'
    }
  },
  '@context': 'https://raw.githubusercontent.com/smart-data-models/dataModel.WasteManagement/master/context.jsonld'
]

What are your thoughts on this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.