Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rest] Add REST JSON:API for commissioner and diagnostics #2514

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

martinzi
Copy link
Contributor

@martinzi martinzi commented Sep 27, 2024

In this PR, we propose an extended REST API functionality providing capabilities for commissioning and on-mesh diagnostics to generic off-mesh http clients. The implementation is guided by the JSON:API specification and provides following parts

  • api/node - pointing to api/devices/thisdevice
  • api/devices - a collection of discovered devices
  • api/actions - indirect processing of tasks, in particular for those that may need more time
    • addThreadDeviceTask - starts the on-mesh commissioner and adds the joiner candidate into the joiner table
    • getNetworkDiagnosticTask - sends diagnostic requests and diagnostic queries to the destination
    • resetNetworkDiagCounterTask - resets the network diagnostic mle and/or mac counter
    • getEnergyScanTask - starts the commissioner and sends energy scan requests to the destination
  • api/diagnostics
    • networkDiagnostic - all the Diagnostic TLV details
    • energyScanReport - results of energy scans

The PR provides integration tests for local validation, see tests/restjsonapi.

The Readme provides more details and descriptions in src/rest/README.md.

Copy link

codecov bot commented Sep 27, 2024

Codecov Report

Attention: Patch coverage is 11.85389% with 2558 lines in your changes missing coverage. Please review.

Project coverage is 37.13%. Comparing base (2b41187) to head (548d4d8).
Report is 964 commits behind head on main.

Files with missing lines Patch % Lines
src/rest/network_diag_handler.cpp 6.19% 659 Missing and 7 partials ⚠️
src/rest/json.cpp 10.88% 454 Missing and 21 partials ⚠️
src/rest/resource.cpp 21.59% 256 Missing and 9 partials ⚠️
src/rest/commissioner_manager.cpp 3.27% 204 Missing and 3 partials ⚠️
src/rest/actions_list.cpp 6.62% 141 Missing ⚠️
src/rest/rest_generic_collection.cpp 16.41% 110 Missing and 2 partials ⚠️
src/rest/actions/energy_scan.cpp 0.00% 102 Missing ⚠️
src/rest/actions/action.cpp 0.00% 94 Missing ⚠️
src/rest/actions/add_thread_device.cpp 0.00% 93 Missing ⚠️
src/rest/actions/network_diagnostic.cpp 0.00% 80 Missing ⚠️
... and 19 more
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2514       +/-   ##
===========================================
- Coverage   55.77%   37.13%   -18.65%     
===========================================
  Files          87      135       +48     
  Lines        6890    15993     +9103     
  Branches        0     1316     +1316     
===========================================
+ Hits         3843     5939     +2096     
- Misses       3047     9707     +6660     
- Partials        0      347      +347     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@martinzi
Copy link
Contributor Author

Happy to discuss thoughts and improvements. Note: we will add a commit fixing the broken topology browser in otbr-web service asap.

@wgtdkp
Copy link
Member

wgtdkp commented Sep 30, 2024

It looks #2515 is also adding the REST commissioner API.

@wgtdkp
Copy link
Member

wgtdkp commented Sep 30, 2024

Happy to discuss thoughts and improvements. Note: we will add a commit fixing the broken topology browser in otbr-web service asap.

@martinzi Thanks for this contribution! But this PR is about 12K lines of change that it's too hard to review. Could you help split this PR by creating a separate smaller PR for each feature? For example, one PR for commissioner API and another PR for network diagnostic.

@martinzi
Copy link
Contributor Author

@martinzi Thanks for this contribution! But this PR is about 12K lines of change that it's too hard to review. Could you help split this PR by creating a separate smaller PR for each feature? For example, one PR for commissioner API and another PR for network diagnostic.

Agree, it became quite large. See a first part split into PR2517. More to follow ...

…by indirect 'addThreadDeviceTask' processing

This commit implements
- POST `addThreadDeviceTask` on api/actions/
- GET api/actions/<actionId>
- DELETE api/actions

The commit also provides integration tests, see tests/restjsonapi.

Please follow the following steps to install/build OTBR.

1. Checkout this PR

2. Build and Install OTBR as usual, e.g. on a Raspberry Pi

3. Restart the OTBR. `sudo systemctl restart otbr-agent`

4. To monitor the log [Errors|Warnings|Info] please open a different terminal instance and use following command:

```
tail -f /var/log/syslog | grep otbr
```

5. Send POST request using BRUNO or CURL, e.g. to join a new device into your network.

```
curl -X POST -H 'Content-Type: application/vnd.api+json' http://localhost:8081/api/actions -d '{"data": [{"type": "addThreadDeviceTask", "attributes": {"eui": "6234567890AACDEA", "pskd": "J01NME", "timeout": 3600}}]}' | jq
```

should return

```
{
  "data": [
    {
      "id": "2d5a8844-b1bc-4f02-93f0-d87b8c3b4e92",
      "type": "addThreadDeviceTask",
      "attributes": {
        "eui": "6234567890AACDEB",
        "pskd": "J01NME",
        "timeout": 3600,
        "status": "pending"
      },
    }
  ]
}
```

6. You may check the status and get the full collection of actions.

```
curl -X GET -H 'Accept: application/vnd.api+json' http://localhost:8081/api/actions | jq
```

should return

```
{
  "data": [
    {
      "id": "2d5a8844-b1bc-4f02-93f0-d87b8c3b4e92",
      "type": "addThreadDeviceTask",
      "attributes": {
        "eui": "6234567890AACDEB",
        "pskd": "J01NME",
        "timeout": 3600,
        "status": "pending"
      }
    }
  ],
  "meta": {
    "collection": {
      "offset": 0,
      "limit": 100,
      "total": 1
    }
  }
}
```

7. View the entry added to the commissioner's table `sudo ot-ctl commissioner joiner table` and expect

```
| ID                    | PSKd                             | Expiration |
+-----------------------+----------------------------------+------------+
|      6234567890aacdea |                           J01NME |    3459027 |
Done
```

8. Start your joiner and after a few seconds repeat above steps 6. and 7.

9. For running the included test script install Bruno-Cli and run the bash script on your border router

```
cd tests/restjsonapi
source ./install_bruno_cli
./test-restjsonapi-server
```
- Adapt test_rest.py for moving to camelCase naming convention following https://jsonapi.org/recommendations/.
- limit README.md to api/actions and add_thread_device action
@martinzi martinzi force-pushed the rest-jsonapi branch 3 times, most recently from 4406313 to 5627bd7 Compare February 25, 2025 16:43
…very providing api/devices, and diagnostic results api/diagnostics

This commit implements following parts:
- api/node
- api/devices
    . GET; POST for discovery and update of collection
- api/actions
    . getNetworkDiagnosticsTask
    . resetNetworkDiagCounterTask
    . getEnergyScanTask
- api/diagnostics
    . networkDiagnostic
    . energyScanReport

The commit also provides integration tests, see tests/restjsonapi.
For more details see Readme in src/rest.
@martinzi martinzi marked this pull request as draft February 25, 2025 20:13
@martinzi martinzi marked this pull request as ready for review March 3, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants