|
| 1 | +# Connect to C8Y MQTT service endpoint |
| 2 | + |
| 3 | +* Date: __2024-06-23__ |
| 4 | +* Status: __New__ |
| 5 | + |
| 6 | +## Requirement |
| 7 | + |
| 8 | +Cumulocity has introduced a new [MQTT service endpoint](https://cumulocity.com/docs/device-integration/mqtt-service) |
| 9 | +that allows users to send free-form messages (any arbitrary topic and payload) to Cumulocity. |
| 10 | +It does not replace the existing core MQTT endpoint and hence a separate connection is required. |
| 11 | +To support the same, `tedge connect` must be enhanced to connect to this new endpoint as well, |
| 12 | +in addition to the existing connection to the core endpoint. |
| 13 | + |
| 14 | +## MQTT service specifications/limitations |
| 15 | + |
| 16 | +- The mqtt service endpoint is exposed at the same tenant URL via port 9883 (with TLS). |
| 17 | +- Both username/password based and cert based authentication is supported. |
| 18 | +- Only connections with clean session enabled are supported. Persistent connection attempts are rejected. |
| 19 | +- Subscribing to wildcard topics (`#` and `+`) and system topics (starting with `$SYS`) are not supported. |
| 20 | +- Publishing/subscribing to legacy C8Y topics (SmartREST and JSON over MQTT) are not supported either. |
| 21 | +- QoS 2 is not supported. |
| 22 | +- Retained flag is accepted, but ignored. |
| 23 | +- There are limits to the number of topics that can be created |
| 24 | + and the number of messages that can be queued per topic. |
| 25 | + Publishes are rejected when the queue is full. |
| 26 | +- Each published message has a time-to-live, after which it is cleared from the queue. |
| 27 | + The subscribed clients are not notified of expired messages. |
| 28 | + |
| 29 | +## Solutions |
| 30 | + |
| 31 | +To establish a connection to the mqtt service endpoint, the following info must be provided in the connect request: |
| 32 | + |
| 33 | +* host: Same as legacy mqtt endpoint |
| 34 | +* port: 9883 by default |
| 35 | +* auth: |
| 36 | + * for username/password based authentication |
| 37 | + * username: `<tenant_id>/<user_name>` |
| 38 | + * password |
| 39 | + * for cert based authentication |
| 40 | + * device cert path |
| 41 | + * device key path |
| 42 | + * server root ca path |
| 43 | + * username: `<tenant_id>` |
| 44 | +* bridge topics: `<bridge_topic_prefix>/#` |
| 45 | + |
| 46 | +:::note |
| 47 | +Even for cert based auth, the `username` must be provided with the tenant id. |
| 48 | +::: |
| 49 | + |
| 50 | +The following are the solution approaches that are considered: |
| 51 | + |
| 52 | +### Approach 1: Connect to mqtt service along with core mqtt connection |
| 53 | + |
| 54 | +The mqtt service bridge connection is established along with `tedge connect c8y` command. |
| 55 | + |
| 56 | +:::note |
| 57 | +For the builtin bridge functionality, whether to spawn a different mapper, |
| 58 | +or establish a secondary connection from the existing mapper itself is to be decided. |
| 59 | +The same applies for the trivial mapping rule that adds the timestamp to the payload. |
| 60 | +::: |
| 61 | + |
| 62 | +#### Configuration |
| 63 | + |
| 64 | +The mqtt service connection related configs are defined under the `[c8y]` table in `tedge.toml` : |
| 65 | + |
| 66 | +| Config | Description | Default | |
| 67 | +| ------ | ----------- | ------- | |
| 68 | +| `c8y.mqtt_service.enabled` | Whether the mqtt server connection must be established or not | `false` | |
| 69 | +| `c8y.mqtt_service.url` | The config URL for the MQTT service | `<c8y.url>:9883` | |
| 70 | +| `c8y.mqtt_service.topic_prefix` | topic prefix used to bridge mqtt topics | `c8y-mqtt` | |
| 71 | + |
| 72 | +To connect to the mqtt service, it must be enabled before `tedge connect c8y` is executed: |
| 73 | + |
| 74 | +```sh |
| 75 | +tedge config set c8y.mqtt_service.enabled "true" |
| 76 | +``` |
| 77 | + |
| 78 | +:::note |
| 79 | +The tenant id which must be passed in the username is retrieved using the `https://<c8y.url>/tenant/currentTenant` REST API. |
| 80 | +An alternative is to take this as an additional `c8y.tenant_id` configuration. |
| 81 | +::: |
| 82 | + |
| 83 | +#### Connection |
| 84 | + |
| 85 | +The command to connect to C8Y remains the same: |
| 86 | + |
| 87 | +```sh |
| 88 | +tedge connect c8y |
| 89 | +``` |
| 90 | + |
| 91 | +The connection to the mqtt service is established only if it is enabled. |
| 92 | + |
| 93 | +**Pros** |
| 94 | + |
| 95 | +- Makes the mqtt service connection seamless for existing users, with minimal configuration. |
| 96 | +- Reuse most of the existing configurations like `c8y.url`, `c8y.device.key_path`, `c8y.device.cert_path` etc. |
| 97 | +- Users can choose not to connect to the new endpoint, by keeping it disabled. |
| 98 | + |
| 99 | +**Cons*** |
| 100 | + |
| 101 | +- The mqtt service connection is tied to the direct `c8y` connection, to retrieve the tenant id |
| 102 | + (although this dependency can be removed if tenant id is accepted via config). |
| 103 | + |
| 104 | +### Approach 2: Connect to mqtt service as an independent cloud endpoint |
| 105 | + |
| 106 | +Do not establish both the core mqtt connection and the mqtt service connection together with `tedge connect c8y` command, |
| 107 | +but treat the mqtt service as an independent/generic MQTT broker endpoint like connecting to AWS or AZ endpoints. |
| 108 | + |
| 109 | +#### Configuration |
| 110 | + |
| 111 | +As an independent cloud endpoint, the following config values must be provided: |
| 112 | + |
| 113 | +```sh |
| 114 | +tedge config set remote.url example.cumulocity.com:9883 |
| 115 | +tedge config set remote.username t123456 |
| 116 | +tedge config set remote.bridge.topic_prefix c8y-mqtt |
| 117 | +``` |
| 118 | + |
| 119 | +The following setting can also be configured, though they all have default values: |
| 120 | + |
| 121 | +| Config | Description | Default | |
| 122 | +| ------ | ----------- | ------- | |
| 123 | +| `remote.device.cert_path` | The device cert path used for the authentication | Same as `device.cert_path` | |
| 124 | +| `remote.device.key_path` | The device cert path used for the authentication | Same as `device.key_path` | |
| 125 | + |
| 126 | +In addition to the above, pretty much all the configs defined in the `tedge.toml` for `az` and `aws` can be defined |
| 127 | +for this generic `remote` endpoint as well, as none of them are cloud specific but applies to any mqtt broker. |
| 128 | + |
| 129 | +#### Connection |
| 130 | + |
| 131 | +```sh |
| 132 | +tedge connect remote |
| 133 | +``` |
| 134 | + |
| 135 | +**Pros** |
| 136 | + |
| 137 | +- Clear separation from `tedge connect c8y` allows the second connection to be established and disconnected on demand. |
| 138 | +- Existing users are not affected, as the `tedge connect c8y` behavior stays the same. |
| 139 | +- Allows customers to connect exclusively to the new mqtt service, if they don't need the core mqtt connection. |
| 140 | +- There is no real dependency on Cumulocity as multiple `remote` profiles can be used to connect to any generic broker. |
| 141 | + |
| 142 | +**Cons** |
| 143 | + |
| 144 | +- Many of the config parameters defined in the `c8y` profile like the `url`, `cert_path` etc |
| 145 | + will have to be duplicated in `remote` profile as well. |
| 146 | +- When the device is connected to multiple c8y cloud profiles, |
| 147 | + corresponding `remote` profiles must be configured explicitly per c8y instance. |
| 148 | + |
| 149 | +### Approach 3: Connect to all configured cloud instances with tedge connect |
| 150 | + |
| 151 | +This is more of an extension of `Approach 2`, where instead of explicitly connecting to each cloud instance/profile, |
| 152 | +a `tedge connect` command (without the cloud arg), detects all the cloud profiles that are configured/enabled |
| 153 | +and connects to each endpoint one by one. |
| 154 | +Only the cloud instances with a `url` explicitly configured are considered as enabled. |
| 155 | +An additional `enabled` flag can be added to each cloud profile, to skip connecting to it, even when it is configured. |
| 156 | + |
| 157 | +#### Configuration |
| 158 | + |
| 159 | +TBD |
| 160 | + |
| 161 | +#### Connection |
| 162 | + |
| 163 | +TBD |
| 164 | + |
0 commit comments