34 changed files with 694 additions and 48 deletions
@ -0,0 +1,74 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
||||
|
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
package org.thingsboard.server.common.transport.service; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.junit.jupiter.api.io.TempDir; |
||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
||||
|
import org.springframework.test.util.ReflectionTestUtils; |
||||
|
import org.thingsboard.server.common.transport.config.ssl.SslCredentials; |
||||
|
import org.thingsboard.server.common.transport.config.ssl.SslCredentialsConfig; |
||||
|
|
||||
|
import java.nio.file.Path; |
||||
|
import java.util.List; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.mockito.Mockito.mock; |
||||
|
import static org.mockito.Mockito.when; |
||||
|
|
||||
|
public class CertificateReloadManagerDiscoveryTest { |
||||
|
|
||||
|
@TempDir |
||||
|
Path tempDir; |
||||
|
|
||||
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||
|
.withUserConfiguration(CertificateReloadManager.class); |
||||
|
|
||||
|
@Test |
||||
|
public void givenNoSslCredentialsConfigs_whenContextStarts_thenManagerCreatedWithoutScheduler() { |
||||
|
contextRunner.run(context -> { |
||||
|
assertThat(context).hasSingleBean(CertificateReloadManager.class); |
||||
|
assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void givenDisabledSslCredentialsConfig_whenContextStarts_thenSchedulerNotStarted() { |
||||
|
SslCredentialsConfig config = mockSslCredentialsConfig(false, List.of(tempDir.resolve("server.pem"))); |
||||
|
contextRunner |
||||
|
.withBean(SslCredentialsConfig.class, () -> config) |
||||
|
.run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void givenSslCredentialsConfigWithCertificateFile_whenContextStarts_thenSchedulerStarted() { |
||||
|
SslCredentialsConfig config = mockSslCredentialsConfig(true, List.of(tempDir.resolve("server.pem"))); |
||||
|
contextRunner |
||||
|
.withBean(SslCredentialsConfig.class, () -> config) |
||||
|
.run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNotNull()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void givenReloadDisabled_whenContextStarts_thenSchedulerNotStarted() { |
||||
|
SslCredentialsConfig config = mockSslCredentialsConfig(true, List.of(tempDir.resolve("server.pem"))); |
||||
|
contextRunner |
||||
|
.withPropertyValues("transport.ssl.certificate.reload.enabled=false") |
||||
|
.withBean(SslCredentialsConfig.class, () -> config) |
||||
|
.run(context -> assertThat(getScheduler(context.getBean(CertificateReloadManager.class))).isNull()); |
||||
|
} |
||||
|
|
||||
|
private static SslCredentialsConfig mockSslCredentialsConfig(boolean enabled, List<Path> certificateFilePaths) { |
||||
|
SslCredentials credentials = mock(SslCredentials.class); |
||||
|
when(credentials.getCertificateFilePaths()).thenReturn(certificateFilePaths); |
||||
|
SslCredentialsConfig config = mock(SslCredentialsConfig.class); |
||||
|
when(config.isEnabled()).thenReturn(enabled); |
||||
|
when(config.getName()).thenReturn("Test SSL Credentials"); |
||||
|
when(config.getCredentials()).thenReturn(credentials); |
||||
|
return config; |
||||
|
} |
||||
|
|
||||
|
private static Object getScheduler(CertificateReloadManager manager) { |
||||
|
return ReflectionTestUtils.getField(manager, "scheduler"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// ChirpStack reads DevEUI and fPort from the metadata below to enqueue the downlink (POST .../devices/{DevEUI}/queue). |
||||
|
// Both are client-side device attributes that could be populated via an Originator attributes rule node. |
||||
|
// ThingsBoard prefixes client-scope attributes with "cs_": |
||||
|
// metadata.cs_eui — ChirpStack device EUI stored on first uplink (e.g. "466ECD510CF7B021") |
||||
|
// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var base64Payload = (msg.powerState != null) |
||||
|
? btoa(msg.powerState) // if powerState is present, encode it to base64 |
||||
|
: btoa('unspecified'); // fall back to 'unspecified' when absent |
||||
|
|
||||
|
// Result object with encoded downlink payload |
||||
|
var result = { |
||||
|
// downlink data content type: JSON, TEXT or BINARY (base64 format) |
||||
|
contentType: "TEXT", |
||||
|
// downlink data |
||||
|
data: base64Payload, |
||||
|
// Metadata required by ChirpStack for downlink delivery. |
||||
|
metadata: { |
||||
|
// Target device EUI; ChirpStack rejects the downlink if this is missing. |
||||
|
DevEUI: metadata.cs_eui, |
||||
|
// LoRaWAN frame port (1-223); ChirpStack parses this as an integer. |
||||
|
fPort: metadata.cs_fPort |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message. |
||||
|
// msg - JSON message payload (e.g. { "state": "on" }) |
||||
|
// msgType - message type, e.g. 'ATTRIBUTES_UPDATED' |
||||
|
// metadata - enriched by the Originator attributes node with device client attributes. |
||||
|
// ThingsBoard prefixes client-scope attributes with "cs_": |
||||
|
// metadata.cs_eui — LORIOT device EUI stored on first uplink (e.g. "466ECD510CF7B021") |
||||
|
// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") |
||||
|
|
||||
|
var eui = metadata.cs_eui; |
||||
|
|
||||
|
// Default port to 1 when the device fPort attribute is missing or empty. |
||||
|
var port = (metadata.cs_fPort != null && metadata.cs_fPort != '') |
||||
|
? parseInt(metadata.cs_fPort) |
||||
|
: 1; |
||||
|
|
||||
|
// EXAMPLE — replace this: it sends a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var payload = (msg.powerState != null) |
||||
|
? msg.powerState |
||||
|
: 'unspecified'; |
||||
|
|
||||
|
var result = { |
||||
|
// Downlink data content type: JSON, TEXT, or BINARY (base64 format). |
||||
|
contentType: "TEXT", |
||||
|
|
||||
|
// Downlink data — the command value; LORIOT hex-encodes it before delivery. |
||||
|
data: payload, |
||||
|
|
||||
|
// Metadata required by LORIOT for downlink delivery. |
||||
|
metadata: { |
||||
|
"EUI": eui, |
||||
|
"port": port |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,20 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// Publish the ThingsBoard message as JSON to the device upload topic. |
||||
|
// metadata.topic is read by the integration when Downlink topic pattern is ${topic}. |
||||
|
var result = { |
||||
|
contentType: "JSON", |
||||
|
data: JSON.stringify(msg), |
||||
|
metadata: { |
||||
|
topic: metadata['deviceType'] + '/' + metadata['deviceName'] + '/upload' |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,49 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
// Shared by the TTN and TTI integrations (The Things Stack). |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// Target device identifier used to build the TTN/TTI downlink topic (devId is assumed to be added via Originator fields rule node) |
||||
|
var devId = metadata.devId; |
||||
|
// LoRaWAN frame port the downlink is scheduled on (cs_fPort is assumed to be added via Originator attributes rule node, prefix 'cs_' (client-side) is added by rule node) |
||||
|
// Default port to 1 when the device fPort attribute is missing or empty. parseInt keeps f_port a number, as the TTN/TTI API expects. |
||||
|
var fPort = (metadata.cs_fPort != null && metadata.cs_fPort != '') |
||||
|
? parseInt(metadata.cs_fPort) |
||||
|
: 1; |
||||
|
|
||||
|
// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var base64Payload = (msg.powerState != null) |
||||
|
? btoa(msg.powerState) // if powerState is present, encode it to base64 |
||||
|
: btoa('unspecified'); // fall back to 'unspecified' when absent |
||||
|
|
||||
|
// The Things Stack (TTN/TTI) expects a 'downlinks' array; each entry is a single scheduled downlink. |
||||
|
var data = { |
||||
|
downlinks: [{ |
||||
|
// Frame port to deliver the payload on (must match the device's expected port). |
||||
|
f_port: fPort, |
||||
|
// false - best-effort downlink; set to true to request an acknowledgement from the device. |
||||
|
confirmed: false, |
||||
|
// Downlink payload, base64-encoded as required by the TTN/TTI API. |
||||
|
frm_payload: base64Payload, |
||||
|
// Scheduling priority for the network server: LOWEST, LOW, BELOW_NORMAL, NORMAL, ABOVE_NORMAL, HIGH, HIGHEST. |
||||
|
priority: "NORMAL" |
||||
|
}] |
||||
|
}; |
||||
|
|
||||
|
// Result object with encoded downlink payload |
||||
|
var result = { |
||||
|
contentType: "JSON", |
||||
|
data: JSON.stringify(data), |
||||
|
metadata: { |
||||
|
devId: devId |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// ChirpStack reads DevEUI and fPort from the metadata below to enqueue the downlink (POST .../devices/{DevEUI}/queue). |
||||
|
// Both are client-side device attributes that could be populated via an Originator attributes rule node. |
||||
|
// ThingsBoard prefixes client-scope attributes with "cs_": |
||||
|
// metadata.cs_eui — ChirpStack device EUI stored on first uplink (e.g. "466ECD510CF7B021") |
||||
|
// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var base64Payload = (msg.powerState != null) |
||||
|
? btoa(msg.powerState) // if powerState is present, encode it to base64 |
||||
|
: btoa('unspecified'); // fall back to 'unspecified' when absent |
||||
|
|
||||
|
// Result object with encoded downlink payload |
||||
|
var result = { |
||||
|
// downlink data content type: JSON, TEXT or BINARY (base64 format) |
||||
|
contentType: "TEXT", |
||||
|
// downlink data |
||||
|
data: base64Payload, |
||||
|
// Metadata required by ChirpStack for downlink delivery. |
||||
|
metadata: { |
||||
|
// Target device EUI; ChirpStack rejects the downlink if this is missing. |
||||
|
DevEUI: metadata.cs_eui, |
||||
|
// LoRaWAN frame port (1-223); ChirpStack parses this as an integer. |
||||
|
fPort: metadata.cs_fPort |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message. |
||||
|
// msg - JSON message payload (e.g. { "state": "on" }) |
||||
|
// msgType - message type, e.g. 'ATTRIBUTES_UPDATED' |
||||
|
// metadata - enriched by the Originator attributes node with device client attributes. |
||||
|
// ThingsBoard prefixes client-scope attributes with "cs_": |
||||
|
// metadata.cs_eui — LORIOT device EUI stored on first uplink (e.g. "466ECD510CF7B021") |
||||
|
// metadata.cs_fPort — LoRaWAN frame port stored on first uplink (e.g. "1") |
||||
|
|
||||
|
var eui = metadata.cs_eui; |
||||
|
|
||||
|
// Default port to 1 when the device fPort attribute is missing or empty. |
||||
|
var port = (metadata.cs_fPort != null && metadata.cs_fPort != '') |
||||
|
? parseInt(metadata.cs_fPort) |
||||
|
: 1; |
||||
|
|
||||
|
// EXAMPLE — replace this: it sends a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var payload = (msg.powerState != null) |
||||
|
? msg.powerState |
||||
|
: 'unspecified'; |
||||
|
|
||||
|
var result = { |
||||
|
// Downlink data content type: JSON, TEXT, or BINARY (base64 format). |
||||
|
contentType: "TEXT", |
||||
|
|
||||
|
// Downlink data — the command value; LORIOT hex-encodes it before delivery. |
||||
|
data: payload, |
||||
|
|
||||
|
// Metadata required by LORIOT for downlink delivery. |
||||
|
metadata: { |
||||
|
"EUI": eui, |
||||
|
"port": port |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,20 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// Publish the ThingsBoard message as JSON to the device upload topic. |
||||
|
// metadata.topic is read by the integration when Downlink topic pattern is ${topic}. |
||||
|
var result = { |
||||
|
contentType: "JSON", |
||||
|
data: JSON.stringify(msg), |
||||
|
metadata: { |
||||
|
topic: metadata['deviceType'] + '/' + metadata['deviceName'] + '/upload' |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
@ -0,0 +1,49 @@ |
|||||
|
// Encode downlink data from incoming Rule Engine message |
||||
|
// Shared by the TTN and TTI integrations (The Things Stack). |
||||
|
|
||||
|
// msg - JSON message payload downlink message json |
||||
|
// msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. |
||||
|
// metadata - list of key-value pairs with additional data about the message |
||||
|
// integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter |
||||
|
|
||||
|
/** Encoder **/ |
||||
|
|
||||
|
// Target device identifier used to build the TTN/TTI downlink topic (devId is assumed to be added via Originator fields rule node) |
||||
|
var devId = metadata.devId; |
||||
|
// LoRaWAN frame port the downlink is scheduled on (cs_fPort is assumed to be added via Originator attributes rule node, prefix 'cs_' (client-side) is added by rule node) |
||||
|
// Default port to 1 when the device fPort attribute is missing or empty. parseInt keeps f_port a number, as the TTN/TTI API expects. |
||||
|
var fPort = (metadata.cs_fPort != null && metadata.cs_fPort != '') |
||||
|
? parseInt(metadata.cs_fPort) |
||||
|
: 1; |
||||
|
|
||||
|
// EXAMPLE — replace this: it encodes a single field ('powerState') from the downlink message. |
||||
|
// Swap 'powerState' (and the 'unspecified' fallback) for the field(s) and encoding your device expects; |
||||
|
// as written, any downlink without a 'powerState' field sends the constant value 'unspecified'. |
||||
|
var base64Payload = (msg.powerState != null) |
||||
|
? btoa(msg.powerState) // if powerState is present, encode it to base64 |
||||
|
: btoa('unspecified'); // fall back to 'unspecified' when absent |
||||
|
|
||||
|
// The Things Stack (TTN/TTI) expects a 'downlinks' array; each entry is a single scheduled downlink. |
||||
|
var data = { |
||||
|
downlinks: [{ |
||||
|
// Frame port to deliver the payload on (must match the device's expected port). |
||||
|
f_port: fPort, |
||||
|
// false - best-effort downlink; set to true to request an acknowledgement from the device. |
||||
|
confirmed: false, |
||||
|
// Downlink payload, base64-encoded as required by the TTN/TTI API. |
||||
|
frm_payload: base64Payload, |
||||
|
// Scheduling priority for the network server: LOWEST, LOW, BELOW_NORMAL, NORMAL, ABOVE_NORMAL, HIGH, HIGHEST. |
||||
|
priority: "NORMAL" |
||||
|
}] |
||||
|
}; |
||||
|
|
||||
|
// Result object with encoded downlink payload |
||||
|
var result = { |
||||
|
contentType: "JSON", |
||||
|
data: JSON.stringify(data), |
||||
|
metadata: { |
||||
|
devId: devId |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
Loading…
Reference in new issue