diff --git a/application/src/main/java/org/thingsboard/server/controller/ControllerConstants.java b/application/src/main/java/org/thingsboard/server/controller/ControllerConstants.java
index b7d014e57d..fe43ad316b 100644
--- a/application/src/main/java/org/thingsboard/server/controller/ControllerConstants.java
+++ b/application/src/main/java/org/thingsboard/server/controller/ControllerConstants.java
@@ -191,6 +191,19 @@ public class ControllerConstants {
" }\n" +
"}";
+ protected static final String DEVICE_UPDATE_CREDENTIALS_ACCESS_TOKEN_PARAM_DESCRIPTION =
+ "{\n" +
+ " \"id\": {\n" +
+ " \"id\":\"c886a090-168d-11ee-87c9-6f157dbc816a\"\n" +
+ " },\n" +
+ " \"deviceId\": {\n" +
+ " \"id\":\"c5fb3ac0-168d-11ee-87c9-6f157dbc816a\",\n" +
+ " \"entityType\":\"DEVICE\"\n" +
+ " },\n" +
+ " \"credentialsType\": \"ACCESS_TOKEN\",\n" +
+ " \"credentialsId\": \"6hmxew8pmmzng4e3une4\"\n" +
+ "}";
+
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_ACCESS_TOKEN_DEFAULT_PARAM_DESCRIPTION =
"{\n" +
" \"device\": {\n" +
@@ -210,40 +223,75 @@ public class ControllerConstants {
protected static final String certificateId = "\"84f5911765abba1f96bf4165604e9e90338fc6214081a8e623b6ff9669aedb27\"";
+ protected static final String certificateValueUpdate = "\"-----BEGIN CERTIFICATE----- " +
+ "MIICMTCCAdegAwIBAgIUUEKxS9hTz4l+oLUMF0LV6TC/gCIwCgYIKoZIzj0EAwIwbjELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE5ldyBZb3JrMRowGAYDVQQKDBFUaGluZ3NCb2FyZCwgSW5jLjEwMC4GA1UEAwwnZGV2aWNlUHJvZmlsZUNlcnRAWDUwOVByb3Zpc2lvblN0cmF0ZWd5MB4XDTIzMDMyOTE0NTczNloXDTI0MDMyODE0NTczNlowbjELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE5ldyBZb3JrMRowGAYDVQQKDBFUaGluZ3NCb2FyZCwgSW5jLjEwMC4GA1UEAwwnZGV2aWNlUHJvZmlsZUNlcnRAWDUwOVByb3Zpc2lvblN0cmF0ZWd5MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAECMlWO72krDoUL9FQjUmSCetkhaEGJUfQkdSfkLSNa0GyAEIMbfmzI4zITeapunu4rGet3EMyLydQzuQanBicp6NTMFEwHQYDVR0OBBYEFHpZ78tPnztNii4Da/yCw6mhEIL3MB8GA1UdIwQYMBaAFHpZ78tPnztNii4Da/yCw6mhEIL3MA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIgJ7qyMFqNcwSYkH6o+UlQXzLWfwZbNjVk+aR7foAZNGsCIQDsd7v3WQIGHiArfZeDs1DLEDuV/2h6L+ZNoGNhEKL+1A== " +
+ "-----END CERTIFICATE-----\"";
+
+ protected static final String certificateIdUpdate = "\"6b8adb49015500e51a527acd332b51684ab9b49b4ade03a9582a44c455e2e9b6\"";
+
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_X509_CERTIFICATE_PARAM_DESCRIPTION =
"{\n" +
- " \"device\": {\n" +
- " \"name\":\"Name_DeviceWithCredantial_X509_Certificate\",\n" +
- " \"label\":\"Label_DeviceWithCredantial_X509_Certificate\",\n" +
- " \"deviceProfileId\":{\n" +
- " \"id\":\"9d9588c0-06c9-11ee-b618-19be30fdeb60\",\n" +
- " \"entityType\":\"DEVICE_PROFILE\"\n" +
- " }\n" +
- " },\n" +
- " \"credentials\": {\n" +
- " \"credentialsType\": \"X509_CERTIFICATE\",\n" +
- " \"credentialsId\": " + certificateId + ",\n" +
- " \"credentialsValue\": " + certificateValue + "\n" +
- " }\n" +
- "}";
+ " \"device\": {\n" +
+ " \"name\":\"Name_DeviceWithCredantial_X509_Certificate\",\n" +
+ " \"label\":\"Label_DeviceWithCredantial_X509_Certificate\",\n" +
+ " \"deviceProfileId\":{\n" +
+ " \"id\":\"9d9588c0-06c9-11ee-b618-19be30fdeb60\",\n" +
+ " \"entityType\":\"DEVICE_PROFILE\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"credentials\": {\n" +
+ " \"credentialsType\": \"X509_CERTIFICATE\",\n" +
+ " \"credentialsId\": " + certificateId + ",\n" +
+ " \"credentialsValue\": " + certificateValue + "\n" +
+ " }\n" +
+ "}";
+
+ protected static final String DEVICE_UPDATE_CREDENTIALS_X509_CERTIFICATE_PARAM_DESCRIPTION =
+ "{\n" +
+ " \"id\": {\n" +
+ " \"id\":\"309bd9c0-14f4-11ee-9fc9-d9b7463abb63\"\n" +
+ " },\n" +
+ " \"deviceId\": {\n" +
+ " \"id\":\"3092b200-14f4-11ee-9fc9-d9b7463abb63\",\n" +
+ " \"entityType\":\"DEVICE\"\n" +
+ " },\n" +
+ " \"credentialsType\": \"X509_CERTIFICATE\",\n" +
+ " \"credentialsId\": " + certificateIdUpdate + ",\n" +
+ " \"credentialsValue\": " + certificateValueUpdate + "\n" +
+ "}";
protected static final String MQTT_BASIC_VALUE = "\"{\\\"clientId\\\":\\\"5euh5nzm34bjjh1efmlt\\\",\\\"userName\\\":\\\"onasd1lgwasmjl7v2v7h\\\",\\\"password\\\":\\\"b9xtm4ny8kt9zewaga5o\\\"}\"";
+ protected static final String MQTT_BASIC_VALUE_UPDATE = "\"{\\\"clientId\\\":\\\"juy03yv4owqxcmqhqtvk\\\",\\\"userName\\\":\\\"ov19fxca0cyjn7lm7w7u\\\",\\\"password\\\":\\\"twy94he114dfi9usyk1o\\\"}\"";
+
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_MQTT_BASIC_PARAM_DESCRIPTION =
"{\n" +
- " \"device\": {\n" +
- " \"name\":\"Name_DeviceWithCredantial_MQTT_Basic\",\n" +
- " \"label\":\"Label_DeviceWithCredantial_MQTT_Basic\",\n" +
- " \"deviceProfileId\":{\n" +
- " \"id\":\"9d9588c0-06c9-11ee-b618-19be30fdeb60\",\n" +
- " \"entityType\":\"DEVICE_PROFILE\"\n" +
- " }\n" +
- " },\n" +
- " \"credentials\": {\n" +
- " \"credentialsType\": \"MQTT_BASIC\",\n" +
- " \"credentialsValue\": " + MQTT_BASIC_VALUE + "\n" +
- " }\n" +
- "}";
+ " \"device\": {\n" +
+ " \"name\":\"Name_DeviceWithCredantial_MQTT_Basic\",\n" +
+ " \"label\":\"Label_DeviceWithCredantial_MQTT_Basic\",\n" +
+ " \"deviceProfileId\":{\n" +
+ " \"id\":\"9d9588c0-06c9-11ee-b618-19be30fdeb60\",\n" +
+ " \"entityType\":\"DEVICE_PROFILE\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"credentials\": {\n" +
+ " \"credentialsType\": \"MQTT_BASIC\",\n" +
+ " \"credentialsValue\": " + MQTT_BASIC_VALUE + "\n" +
+ " }\n" +
+ "}";
+
+ protected static final String DEVICE_UPDATE_CREDENTIALS_MQTT_BASIC_PARAM_DESCRIPTION =
+ "{\n" +
+ " \"id\": {\n" +
+ " \"id\":\"d877ffb0-14f5-11ee-9fc9-d9b7463abb63\"\n" +
+ " },\n" +
+ " \"deviceId\": {\n" +
+ " \"id\":\"d875dcd0-14f5-11ee-9fc9-d9b7463abb63\",\n" +
+ " \"entityType\":\"DEVICE\"\n" +
+ " },\n" +
+ " \"credentialsType\": \"MQTT_BASIC\",\n" +
+ " \"credentialsValue\": " + MQTT_BASIC_VALUE_UPDATE + "\n" +
+ "}";
protected static final String CREDENTIALS_VALUE_LVM2M_RPK_DESCRIPTION =
" \"{" +
@@ -265,6 +313,26 @@ public class ControllerConstants {
"} " +
"}\"";
+ protected static final String CREDENTIALS_VALUE_UPDATE_LVM2M_RPK_DESCRIPTION =
+ " \"{" +
+ "\\\"client\\\":{ " +
+ "\\\"endpoint\\\":\\\"LwRpk00000000\\\", " +
+ "\\\"securityConfigClientMode\\\":\\\"RPK\\\", " +
+ "\\\"key\\\":\\\"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdvBZZ2vQRK9wgDhctj6B1c7bxR3Z0wYg1+YdoYFnVUKWb+rIfTTyYK9tmQJx5Vlb5fxdLnVv1RJOPiwsLIQbAA==\\\"" +
+ " }, " +
+ "\\\"bootstrap\\\":{ " +
+ "\\\"bootstrapServer\\\":{ " +
+ "\\\"securityMode\\\":\\\"RPK\\\", " +
+ "\\\"clientPublicKeyOrId\\\":\\\"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUEBxNl/RcYJNm8mk91CyVXoIJiROYDlXcSSqK6e5bDHwOW4ZiN2lNnXalyF0Jxw8MbAytnDMERXyAja5VEMeVQ==\\\", " +
+ "\\\"clientSecretKey\\\":\\\"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgd9GAx7yZW37autew5KZykn4IgRpge/tZSjnudnZJnMahRANCAARQQHE2X9Fxgk2byaT3ULJVeggmJE5gOVdxJKorp7lsMfA5bhmI3aU2ddqXIXQnHDwxsDK2cMwRFfICNrlUQx5V\\\"" +
+ "}, " +
+ "\\\"lwm2mServer\\\":{ \\\"securityMode\\\":\\\"RPK\\\", " +
+ "\\\"clientPublicKeyOrId\\\":\\\"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUEBxNl/RcYJNm8mk91CyVXoIJiROYDlXcSSqK6e5bDHwOW4ZiN2lNnXalyF0Jxw8MbAytnDMERXyAja5VEMeVQ==\\\", " +
+ "\\\"clientSecretKey\\\":\\\"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgd9GAx7yZW37autew5KZykn4IgRpge/tZSjnudnZJnMahRANCAARQQHE2X9Fxgk2byaT3ULJVeggmJE5gOVdxJKorp7lsMfA5bhmI3aU2ddqXIXQnHDwxsDK2cMwRFfICNrlUQx5V\\\"" +
+ "}" +
+ "} " +
+ "}\"";
+
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION =
"{\n" +
" \"device\": {\n" +
@@ -282,6 +350,20 @@ public class ControllerConstants {
" }\n" +
"}";
+ protected static final String DEVICE_UPDATE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION =
+ "{\n" +
+ " \"id\": {\n" +
+ " \"id\":\"e238d4d0-1689-11ee-98c6-1713c1be5a8e\"\n" +
+ " },\n" +
+ " \"deviceId\": {\n" +
+ " \"id\":\"e232e160-1689-11ee-98c6-1713c1be5a8e\",\n" +
+ " \"entityType\":\"DEVICE\"\n" +
+ " },\n" +
+ " \"credentialsType\": \"LWM2M_CREDENTIALS\",\n" +
+ " \"credentialsId\": \"LwRpk00000000\",\n" +
+ " \"credentialsValue\":\n" + CREDENTIALS_VALUE_UPDATE_LVM2M_RPK_DESCRIPTION + "\n" +
+ "}";
+
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_ACCESS_TOKEN_DESCRIPTION_MARKDOWN =
MARKDOWN_CODE_BLOCK_START + DEVICE_WITH_DEVICE_CREDENTIALS_ACCESS_TOKEN_PARAM_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
@@ -297,8 +379,21 @@ public class ControllerConstants {
protected static final String DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION_MARKDOWN =
MARKDOWN_CODE_BLOCK_START + DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
+ protected static final String DEVICE_UPDATE_CREDENTIALS_PARAM_ACCESS_TOKEN_DESCRIPTION_MARKDOWN =
+ MARKDOWN_CODE_BLOCK_START + DEVICE_UPDATE_CREDENTIALS_ACCESS_TOKEN_PARAM_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
+
+ protected static final String DEVICE_UPDATE_CREDENTIALS_PARAM_X509_CERTIFICATE_DESCRIPTION_MARKDOWN =
+ MARKDOWN_CODE_BLOCK_START + DEVICE_UPDATE_CREDENTIALS_X509_CERTIFICATE_PARAM_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
+
+ protected static final String DEVICE_UPDATE_CREDENTIALS_PARAM_MQTT_BASIC_DESCRIPTION_MARKDOWN =
+ MARKDOWN_CODE_BLOCK_START + DEVICE_UPDATE_CREDENTIALS_MQTT_BASIC_PARAM_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
+
+ protected static final String DEVICE_UPDATE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION_MARKDOWN =
+ MARKDOWN_CODE_BLOCK_START + DEVICE_UPDATE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION + MARKDOWN_CODE_BLOCK_END;
+
+
- protected static final String FILTER_VALUE_TYPE = NEW_LINE + "## Value Type and Operations" + NEW_LINE +
+ protected static final String FILTER_VALUE_TYPE = NEW_LINE + "## Value Type and Operations" + NEW_LINE +
"Provides a hint about the data type of the entity field that is defined in the filter key. " +
"The value type impacts the list of possible operations that you may use in the corresponding predicate. For example, you may use 'STARTS_WITH' or 'END_WITH', but you can't use 'GREATER_OR_EQUAL' for string values." +
"The following filter value types and corresponding predicate operations are supported: " + NEW_LINE +
diff --git a/application/src/main/java/org/thingsboard/server/controller/DeviceController.java b/application/src/main/java/org/thingsboard/server/controller/DeviceController.java
index a0f5a96aed..8c922fd6e5 100644
--- a/application/src/main/java/org/thingsboard/server/controller/DeviceController.java
+++ b/application/src/main/java/org/thingsboard/server/controller/DeviceController.java
@@ -95,6 +95,10 @@ import static org.thingsboard.server.controller.ControllerConstants.DEVICE_NAME_
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_PROFILE_ID_PARAM_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_TEXT_SEARCH_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_TYPE_DESCRIPTION;
+import static org.thingsboard.server.controller.ControllerConstants.DEVICE_UPDATE_CREDENTIALS_PARAM_ACCESS_TOKEN_DESCRIPTION_MARKDOWN;
+import static org.thingsboard.server.controller.ControllerConstants.DEVICE_UPDATE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION_MARKDOWN;
+import static org.thingsboard.server.controller.ControllerConstants.DEVICE_UPDATE_CREDENTIALS_PARAM_MQTT_BASIC_DESCRIPTION_MARKDOWN;
+import static org.thingsboard.server.controller.ControllerConstants.DEVICE_UPDATE_CREDENTIALS_PARAM_X509_CERTIFICATE_DESCRIPTION_MARKDOWN;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_ACCESS_TOKEN_DEFAULT_DESCRIPTION_MARKDOWN;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_ACCESS_TOKEN_DESCRIPTION_MARKDOWN;
import static org.thingsboard.server.controller.ControllerConstants.DEVICE_WITH_DEVICE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION_MARKDOWN;
@@ -293,10 +297,27 @@ public class DeviceController extends BaseController {
return tbDeviceService.getDeviceCredentialsByDeviceId(device, getCurrentUser());
}
- @ApiOperation(value = "Update device credentials (updateDeviceCredentials)", notes = "During device creation, platform generates random 'ACCESS_TOKEN' credentials. " +
- "Use this method to update the device credentials. First use 'getDeviceCredentialsByDeviceId' to get the credentials id and value. " +
- "Then use current method to update the credentials type and value. It is not possible to create multiple device credentials for the same device. " +
- "The structure of device credentials id and value is simple for the 'ACCESS_TOKEN' but is much more complex for the 'MQTT_BASIC' or 'LWM2M_CREDENTIALS'." + TENANT_AUTHORITY_PARAGRAPH)
+ @ApiOperation(value = "Update device credentials (updateDeviceCredentials)",
+ notes = "During device creation, platform generates random 'ACCESS_TOKEN' credentials. \" +\n" +
+ "Use this method to update the device credentials. First use 'getDeviceCredentialsByDeviceId' to get the credentials id and value.\n" +
+ "Then use current method to update the credentials type and value. It is not possible to create multiple device credentials for the same device.\n" +
+ "The structure of device credentials id and value is simple for the 'ACCESS_TOKEN' but is much more complex for the 'MQTT_BASIC' or 'LWM2M_CREDENTIALS'.\n" +
+ "You may find the example of device with different type of credentials below: \n\n" +
+ "- Credentials type: \"Access token\" with device ID and with device ID below: \n\n" +
+ DEVICE_UPDATE_CREDENTIALS_PARAM_ACCESS_TOKEN_DESCRIPTION_MARKDOWN + "\n\n" +
+ "- Credentials type: \"X509\" with device profile ID below: \n\n" +
+ "Note: credentialsId - format Sha3Hash, certificateValue - format PEM (with \"--BEGIN CERTIFICATE----\" and -\"----END CERTIFICATE-\").\n\n" +
+ DEVICE_UPDATE_CREDENTIALS_PARAM_X509_CERTIFICATE_DESCRIPTION_MARKDOWN + "\n\n" +
+ "- Credentials type: \"MQTT_BASIC\" with device profile ID below: \n\n" +
+ DEVICE_UPDATE_CREDENTIALS_PARAM_MQTT_BASIC_DESCRIPTION_MARKDOWN + "\n\n" +
+ "- You may find the example of LwM2M device and RPK credentials below: \n\n" +
+ "Note: LwM2M device - only existing device profile ID (Transport configuration -> Transport type: \"LWM2M\".\n\n" +
+ DEVICE_UPDATE_CREDENTIALS_PARAM_LVM2M_RPK_DESCRIPTION_MARKDOWN + "\n\n" +
+ "Update to real value:\n" +
+ " - 'id' (this is id of Device Credentials -> \"Get Device Credentials (getDeviceCredentialsByDeviceId)\",\n" +
+ " - 'deviceId.id' (this is id of Device).\n" +
+ "Remove 'tenantId' and optionally 'customerId' from the request body example (below) to create new Device entity." +
+ TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/device/credentials", method = RequestMethod.POST)
@ResponseBody
diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 5522e8a5bf..3e1aa5a8e6 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -497,6 +497,9 @@ cache:
entityCount:
timeToLiveInMinutes: "${CACHE_SPECS_ENTITY_COUNT_TTL:1440}"
maxSize: "${CACHE_SPECS_ENTITY_COUNT_MAX_SIZE:100000}"
+ resourceInfo:
+ timeToLiveInMinutes: "${CACHE_SPECS_RESOURCE_INFO_TTL:1440}"
+ maxSize: "${CACHE_SPECS_RESOURCE_INFO_MAX_SIZE:100000}"
# deliberately placed outside 'specs' group above
notificationRules:
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCacheKey.java b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCacheKey.java
new file mode 100644
index 0000000000..9db53f86c6
--- /dev/null
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCacheKey.java
@@ -0,0 +1,40 @@
+/**
+ * Copyright © 2016-2023 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.cache.resourceInfo;
+
+import lombok.Builder;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.thingsboard.server.common.data.id.TbResourceId;
+import org.thingsboard.server.common.data.id.TenantId;
+
+import java.io.Serializable;
+
+@Getter
+@EqualsAndHashCode
+@RequiredArgsConstructor
+@Builder
+public class ResourceInfoCacheKey implements Serializable {
+
+ private final TenantId tenantId;
+ private final TbResourceId tbResourceId;
+
+ @Override
+ public String toString() {
+ return tenantId + "_" + tbResourceId;
+ }
+}
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCaffeineCache.java b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCaffeineCache.java
new file mode 100644
index 0000000000..95754d891a
--- /dev/null
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoCaffeineCache.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright © 2016-2023 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.cache.resourceInfo;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.cache.CacheManager;
+import org.springframework.stereotype.Service;
+import org.thingsboard.server.cache.CaffeineTbTransactionalCache;
+import org.thingsboard.server.common.data.CacheConstants;
+import org.thingsboard.server.common.data.TbResourceInfo;
+
+
+@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true)
+@Service("ResourceInfoCache")
+public class ResourceInfoCaffeineCache extends CaffeineTbTransactionalCache {
+
+ public ResourceInfoCaffeineCache(CacheManager cacheManager) {
+ super(cacheManager, CacheConstants.RESOURCE_INFO_CACHE);
+ }
+
+}
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoEvictEvent.java b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoEvictEvent.java
new file mode 100644
index 0000000000..11272a5e24
--- /dev/null
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoEvictEvent.java
@@ -0,0 +1,26 @@
+/**
+ * Copyright © 2016-2023 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.cache.resourceInfo;
+
+import lombok.Data;
+import org.thingsboard.server.common.data.id.TbResourceId;
+import org.thingsboard.server.common.data.id.TenantId;
+
+@Data
+public class ResourceInfoEvictEvent {
+ private final TenantId tenantId;
+ private final TbResourceId resourceId;
+}
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoRedisCache.java b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoRedisCache.java
new file mode 100644
index 0000000000..fee14e1ca1
--- /dev/null
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/resourceInfo/ResourceInfoRedisCache.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright © 2016-2023 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.cache.resourceInfo;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.stereotype.Service;
+import org.thingsboard.server.cache.CacheSpecsMap;
+import org.thingsboard.server.cache.RedisTbTransactionalCache;
+import org.thingsboard.server.cache.TBRedisCacheConfiguration;
+import org.thingsboard.server.cache.TbFSTRedisSerializer;
+import org.thingsboard.server.common.data.CacheConstants;
+import org.thingsboard.server.common.data.TbResourceInfo;
+
+@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis")
+@Service("ResourceInfoCache")
+public class ResourceInfoRedisCache extends RedisTbTransactionalCache {
+
+ public ResourceInfoRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) {
+ super(CacheConstants.RESOURCE_INFO_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbFSTRedisSerializer<>());
+ }
+}
diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
index ff0032f435..f21b13a674 100644
--- a/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
+++ b/common/data/src/main/java/org/thingsboard/server/common/data/CacheConstants.java
@@ -44,4 +44,5 @@ public class CacheConstants {
public static final String USER_SETTINGS_CACHE = "userSettings";
public static final String DASHBOARD_TITLES_CACHE = "dashboardTitles";
public static final String ENTITY_COUNT_CACHE = "entityCount";
+ public static final String RESOURCE_INFO_CACHE = "resourceInfo";
}
diff --git a/dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java b/dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java
index 974e3665f1..bc4f47040b 100644
--- a/dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java
+++ b/dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java
@@ -20,7 +20,11 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.stereotype.Service;
+import org.springframework.transaction.event.TransactionalEventListener;
+import org.thingsboard.server.cache.device.DeviceCacheKey;
+import org.thingsboard.server.cache.resourceInfo.ResourceInfoEvictEvent;
import org.thingsboard.server.common.data.EntityType;
+import org.thingsboard.server.cache.resourceInfo.ResourceInfoCacheKey;
import org.thingsboard.server.common.data.ResourceType;
import org.thingsboard.server.common.data.TbResource;
import org.thingsboard.server.common.data.TbResourceInfo;
@@ -31,6 +35,7 @@ import org.thingsboard.server.common.data.id.TbResourceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
+import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
@@ -45,7 +50,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId;
@Service("TbResourceDaoService")
@Slf4j
@AllArgsConstructor
-public class BaseResourceService implements ResourceService {
+public class BaseResourceService extends AbstractCachedEntityService implements ResourceService {
public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId ";
private final TbResourceDao resourceDao;
@@ -55,10 +60,12 @@ public class BaseResourceService implements ResourceService {
@Override
public TbResource saveResource(TbResource resource) {
resourceValidator.validate(resource, TbResourceInfo::getTenantId);
-
try {
- return resourceDao.save(resource.getTenantId(), resource);
+ TbResource saved = resourceDao.save(resource.getTenantId(), resource);
+ publishEvictEvent(new ResourceInfoEvictEvent(resource.getTenantId(), resource.getId()));
+ return saved;
} catch (Exception t) {
+ publishEvictEvent(new ResourceInfoEvictEvent(resource.getTenantId(), resource.getId()));
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) {
String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName";
@@ -86,7 +93,9 @@ public class BaseResourceService implements ResourceService {
public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) {
log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId);
Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
- return resourceInfoDao.findById(tenantId, resourceId.getId());
+
+ return cache.getAndPutInTransaction(new ResourceInfoCacheKey(tenantId, resourceId),
+ () -> resourceInfoDao.findById(tenantId, resourceId.getId()), true);
}
@Override
@@ -169,13 +178,11 @@ public class BaseResourceService implements ResourceService {
}
};
- protected Optional extractConstraintViolationException(Exception t) {
- if (t instanceof ConstraintViolationException) {
- return Optional.of((ConstraintViolationException) t);
- } else if (t.getCause() instanceof ConstraintViolationException) {
- return Optional.of((ConstraintViolationException) (t.getCause()));
- } else {
- return Optional.empty();
+ @TransactionalEventListener(classes = ResourceInfoEvictEvent.class)
+ @Override
+ public void handleEvictEvent(ResourceInfoEvictEvent event) {
+ if (event.getResourceId() != null) {
+ cache.evict(new ResourceInfoCacheKey(event.getTenantId(), event.getResourceId()));
}
}
}
diff --git a/dao/src/test/resources/application-test.properties b/dao/src/test/resources/application-test.properties
index d89211cb2f..98f9091318 100644
--- a/dao/src/test/resources/application-test.properties
+++ b/dao/src/test/resources/application-test.properties
@@ -74,6 +74,9 @@ cache.specs.dashboardTitles.maxSize=10000
cache.specs.entityCount.timeToLiveInMinutes=1440
cache.specs.entityCount.maxSize=10000
+cache.specs.resourceInfo.timeToLiveInMinutes=1440
+cache.specs.resourceInfo.maxSize=10000
+
redis.connection.host=localhost
redis.connection.port=6379
redis.connection.db=0