Commit d2d95a2575376a9c58f94645b6c03d956a66cffd

Authored by Volodymyr Babak
2 parents bc3edc8d 228fddb8

Merge remote-tracking branch 'upstream/master' into edge-3.3

Showing 63 changed files with 2520 additions and 764 deletions

Too many changes to show.

To preserve performance only 63 of 328 files are displayed.

... ... @@ -33,15 +33,15 @@
33 33 "enableSearch": true,
34 34 "displayPagination": true,
35 35 "defaultPageSize": 10,
36   - "defaultSortOrder": "entityLabel",
37   - "displayEntityName": false,
  36 + "defaultSortOrder": "entityName",
  37 + "displayEntityName": true,
38 38 "displayEntityType": false,
39 39 "enableSelectColumnDisplay": false,
40 40 "enableStickyHeader": true,
41 41 "enableStickyAction": false,
42 42 "entitiesTitle": "Devices",
43   - "displayEntityLabel": true,
44   - "entityLabelColumnTitle": "Device"
  43 + "displayEntityLabel": false,
  44 + "entityNameColumnTitle": "Device"
45 45 },
46 46 "title": "New Entities table",
47 47 "dropShadow": true,
... ... @@ -828,15 +828,15 @@
828 828 "enableSearch": true,
829 829 "displayPagination": true,
830 830 "defaultPageSize": 10,
831   - "defaultSortOrder": "entityLabel",
832   - "displayEntityName": false,
  831 + "defaultSortOrder": "entityName",
  832 + "displayEntityName": true,
833 833 "displayEntityType": false,
834 834 "enableSelectColumnDisplay": false,
835 835 "enableStickyHeader": true,
836 836 "enableStickyAction": true,
837 837 "entitiesTitle": "Devices",
838   - "displayEntityLabel": true,
839   - "entityLabelColumnTitle": "Device"
  838 + "displayEntityLabel": false,
  839 + "entityNameColumnTitle": "Device"
840 840 },
841 841 "title": "New Entities table",
842 842 "dropShadow": true,
... ... @@ -1125,15 +1125,15 @@
1125 1125 "enableSearch": true,
1126 1126 "displayPagination": true,
1127 1127 "defaultPageSize": 10,
1128   - "defaultSortOrder": "entityLabel",
1129   - "displayEntityName": false,
  1128 + "defaultSortOrder": "entityName",
  1129 + "displayEntityName": true,
1130 1130 "displayEntityType": false,
1131 1131 "enableSelectColumnDisplay": false,
1132 1132 "enableStickyHeader": true,
1133 1133 "enableStickyAction": true,
1134 1134 "entitiesTitle": "Devices",
1135   - "displayEntityLabel": true,
1136   - "entityLabelColumnTitle": "Device"
  1135 + "displayEntityLabel": false,
  1136 + "entityNameColumnTitle": "Device"
1137 1137 },
1138 1138 "title": "New Entities table",
1139 1139 "dropShadow": true,
... ... @@ -1422,15 +1422,15 @@
1422 1422 "enableSearch": true,
1423 1423 "displayPagination": true,
1424 1424 "defaultPageSize": 10,
1425   - "defaultSortOrder": "entityLabel",
1426   - "displayEntityName": false,
  1425 + "defaultSortOrder": "entityName",
  1426 + "displayEntityName": true,
1427 1427 "displayEntityType": false,
1428 1428 "enableSelectColumnDisplay": false,
1429 1429 "enableStickyHeader": true,
1430 1430 "enableStickyAction": true,
1431 1431 "entitiesTitle": "Devices",
1432   - "displayEntityLabel": true,
1433   - "entityLabelColumnTitle": "Device"
  1432 + "displayEntityLabel": false,
  1433 + "entityNameColumnTitle": "Device"
1434 1434 },
1435 1435 "title": "New Entities table",
1436 1436 "dropShadow": true,
... ... @@ -1719,15 +1719,15 @@
1719 1719 "enableSearch": true,
1720 1720 "displayPagination": true,
1721 1721 "defaultPageSize": 10,
1722   - "defaultSortOrder": "entityLabel",
1723   - "displayEntityName": false,
  1722 + "defaultSortOrder": "entityName",
  1723 + "displayEntityName": true,
1724 1724 "displayEntityType": false,
1725 1725 "enableSelectColumnDisplay": false,
1726 1726 "enableStickyHeader": true,
1727 1727 "enableStickyAction": true,
1728 1728 "entitiesTitle": "Devices",
1729   - "displayEntityLabel": true,
1730   - "entityLabelColumnTitle": "Device"
  1729 + "displayEntityLabel": false,
  1730 + "entityNameColumnTitle": "Device"
1731 1731 },
1732 1732 "title": "New Entities table",
1733 1733 "dropShadow": true,
... ...
... ... @@ -33,15 +33,15 @@
33 33 "enableSearch": true,
34 34 "displayPagination": true,
35 35 "defaultPageSize": 10,
36   - "defaultSortOrder": "entityLabel",
37   - "displayEntityName": false,
  36 + "defaultSortOrder": "entityName",
  37 + "displayEntityName": true,
38 38 "displayEntityType": false,
39 39 "enableSelectColumnDisplay": false,
40 40 "enableStickyHeader": true,
41 41 "enableStickyAction": false,
42 42 "entitiesTitle": "Devices",
43   - "displayEntityLabel": true,
44   - "entityLabelColumnTitle": "Device"
  43 + "displayEntityLabel": false,
  44 + "entityNameColumnTitle": "Device"
45 45 },
46 46 "title": "New Entities table",
47 47 "dropShadow": true,
... ... @@ -828,15 +828,15 @@
828 828 "enableSearch": true,
829 829 "displayPagination": true,
830 830 "defaultPageSize": 10,
831   - "defaultSortOrder": "entityLabel",
832   - "displayEntityName": false,
  831 + "defaultSortOrder": "entityName",
  832 + "displayEntityName": true,
833 833 "displayEntityType": false,
834 834 "enableSelectColumnDisplay": false,
835 835 "enableStickyHeader": true,
836 836 "enableStickyAction": true,
837 837 "entitiesTitle": "Devices",
838   - "displayEntityLabel": true,
839   - "entityLabelColumnTitle": "Device"
  838 + "displayEntityLabel": false,
  839 + "entityNameColumnTitle": "Device"
840 840 },
841 841 "title": "New Entities table",
842 842 "dropShadow": true,
... ... @@ -1125,15 +1125,15 @@
1125 1125 "enableSearch": true,
1126 1126 "displayPagination": true,
1127 1127 "defaultPageSize": 10,
1128   - "defaultSortOrder": "entityLabel",
1129   - "displayEntityName": false,
  1128 + "defaultSortOrder": "entityName",
  1129 + "displayEntityName": true,
1130 1130 "displayEntityType": false,
1131 1131 "enableSelectColumnDisplay": false,
1132 1132 "enableStickyHeader": true,
1133 1133 "enableStickyAction": true,
1134 1134 "entitiesTitle": "Devices",
1135   - "displayEntityLabel": true,
1136   - "entityLabelColumnTitle": "Device"
  1135 + "displayEntityLabel": false,
  1136 + "entityNameColumnTitle": "Device"
1137 1137 },
1138 1138 "title": "New Entities table",
1139 1139 "dropShadow": true,
... ... @@ -1422,15 +1422,15 @@
1422 1422 "enableSearch": true,
1423 1423 "displayPagination": true,
1424 1424 "defaultPageSize": 10,
1425   - "defaultSortOrder": "entityLabel",
1426   - "displayEntityName": false,
  1425 + "defaultSortOrder": "entityName",
  1426 + "displayEntityName": true,
1427 1427 "displayEntityType": false,
1428 1428 "enableSelectColumnDisplay": false,
1429 1429 "enableStickyHeader": true,
1430 1430 "enableStickyAction": true,
1431 1431 "entitiesTitle": "Devices",
1432   - "displayEntityLabel": true,
1433   - "entityLabelColumnTitle": "Device"
  1432 + "displayEntityLabel": false,
  1433 + "entityNameColumnTitle": "Device"
1434 1434 },
1435 1435 "title": "New Entities table",
1436 1436 "dropShadow": true,
... ... @@ -1719,15 +1719,15 @@
1719 1719 "enableSearch": true,
1720 1720 "displayPagination": true,
1721 1721 "defaultPageSize": 10,
1722   - "defaultSortOrder": "entityLabel",
1723   - "displayEntityName": false,
  1722 + "defaultSortOrder": "entityName",
  1723 + "displayEntityName": true,
1724 1724 "displayEntityType": false,
1725 1725 "enableSelectColumnDisplay": false,
1726 1726 "enableStickyHeader": true,
1727 1727 "enableStickyAction": true,
1728 1728 "entitiesTitle": "Devices",
1729   - "displayEntityLabel": true,
1730   - "entityLabelColumnTitle": "Device"
  1729 + "displayEntityLabel": false,
  1730 + "entityNameColumnTitle": "Device"
1731 1731 },
1732 1732 "title": "New Entities table",
1733 1733 "dropShadow": true,
... ...
... ... @@ -18,17 +18,18 @@ CREATE OR REPLACE PROCEDURE drop_partitions_by_max_ttl(IN partition_type varchar
18 18 LANGUAGE plpgsql AS
19 19 $$
20 20 DECLARE
21   - max_tenant_ttl bigint;
22   - max_customer_ttl bigint;
23   - max_ttl bigint;
24   - date timestamp;
25   - partition_by_max_ttl_date varchar;
26   - partition_month varchar;
27   - partition_day varchar;
28   - partition_year varchar;
29   - partition varchar;
30   - partition_to_delete varchar;
31   -
  21 + max_tenant_ttl bigint;
  22 + max_customer_ttl bigint;
  23 + max_ttl bigint;
  24 + date timestamp;
  25 + partition_by_max_ttl_date varchar;
  26 + partition_by_max_ttl_month varchar;
  27 + partition_by_max_ttl_day varchar;
  28 + partition_by_max_ttl_year varchar;
  29 + partition varchar;
  30 + partition_year integer;
  31 + partition_month integer;
  32 + partition_day integer;
32 33
33 34 BEGIN
34 35 SELECT max(attribute_kv.long_v)
... ... @@ -45,53 +46,138 @@ BEGIN
45 46 if max_ttl IS NOT NULL AND max_ttl > 0 THEN
46 47 date := to_timestamp(EXTRACT(EPOCH FROM current_timestamp) - max_ttl);
47 48 partition_by_max_ttl_date := get_partition_by_max_ttl_date(partition_type, date);
  49 + RAISE NOTICE 'Date by max ttl: %', date;
48 50 RAISE NOTICE 'Partition by max ttl: %', partition_by_max_ttl_date;
49 51 IF partition_by_max_ttl_date IS NOT NULL THEN
50 52 CASE
51 53 WHEN partition_type = 'DAYS' THEN
52   - partition_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
53   - partition_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
54   - partition_day := SPLIT_PART(partition_by_max_ttl_date, '_', 5);
  54 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  55 + partition_by_max_ttl_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
  56 + partition_by_max_ttl_day := SPLIT_PART(partition_by_max_ttl_date, '_', 5);
55 57 WHEN partition_type = 'MONTHS' THEN
56   - partition_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
57   - partition_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
  58 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  59 + partition_by_max_ttl_month := SPLIT_PART(partition_by_max_ttl_date, '_', 4);
58 60 ELSE
59   - partition_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
  61 + partition_by_max_ttl_year := SPLIT_PART(partition_by_max_ttl_date, '_', 3);
60 62 END CASE;
61   - FOR partition IN SELECT tablename
62   - FROM pg_tables
63   - WHERE schemaname = 'public'
64   - AND tablename like 'ts_kv_' || '%'
65   - AND tablename != 'ts_kv_latest'
66   - AND tablename != 'ts_kv_dictionary'
67   - AND tablename != 'ts_kv_indefinite'
68   - LOOP
69   - IF partition != partition_by_max_ttl_date THEN
70   - IF partition_year IS NOT NULL THEN
71   - IF SPLIT_PART(partition, '_', 3)::integer < partition_year::integer THEN
72   - partition_to_delete := partition;
73   - ELSE
74   - IF partition_month IS NOT NULL THEN
75   - IF SPLIT_PART(partition, '_', 4)::integer < partition_month::integer THEN
76   - partition_to_delete := partition;
  63 + IF partition_by_max_ttl_year IS NULL THEN
  64 + RAISE NOTICE 'Failed to remove partitions by max ttl date due to partition_by_max_ttl_year is null!';
  65 + ELSE
  66 + IF partition_type = 'YEARS' THEN
  67 + FOR partition IN SELECT tablename
  68 + FROM pg_tables
  69 + WHERE schemaname = 'public'
  70 + AND tablename like 'ts_kv_' || '%'
  71 + AND tablename != 'ts_kv_latest'
  72 + AND tablename != 'ts_kv_dictionary'
  73 + AND tablename != 'ts_kv_indefinite'
  74 + AND tablename != partition_by_max_ttl_date
  75 + LOOP
  76 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  77 + IF partition_year < partition_by_max_ttl_year::integer THEN
  78 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  79 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  80 + deleted := deleted + 1;
  81 + END IF;
  82 + END LOOP;
  83 + ELSE
  84 + IF partition_type = 'MONTHS' THEN
  85 + IF partition_by_max_ttl_month IS NULL THEN
  86 + RAISE NOTICE 'Failed to remove months partitions by max ttl date due to partition_by_max_ttl_month is null!';
  87 + ELSE
  88 + FOR partition IN SELECT tablename
  89 + FROM pg_tables
  90 + WHERE schemaname = 'public'
  91 + AND tablename like 'ts_kv_' || '%'
  92 + AND tablename != 'ts_kv_latest'
  93 + AND tablename != 'ts_kv_dictionary'
  94 + AND tablename != 'ts_kv_indefinite'
  95 + AND tablename != partition_by_max_ttl_date
  96 + LOOP
  97 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  98 + IF partition_year > partition_by_max_ttl_year::integer THEN
  99 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  100 + CONTINUE;
77 101 ELSE
78   - IF partition_day IS NOT NULL THEN
79   - IF SPLIT_PART(partition, '_', 5)::integer < partition_day::integer THEN
80   - partition_to_delete := partition;
  102 + IF partition_year < partition_by_max_ttl_year::integer THEN
  103 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  104 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  105 + deleted := deleted + 1;
  106 + ELSE
  107 + partition_month := SPLIT_PART(partition, '_', 4)::integer;
  108 + IF partition_year = partition_by_max_ttl_year::integer THEN
  109 + IF partition_month >= partition_by_max_ttl_month::integer THEN
  110 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  111 + CONTINUE;
  112 + ELSE
  113 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  114 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  115 + deleted := deleted + 1;
  116 + END IF;
81 117 END IF;
82 118 END IF;
83 119 END IF;
  120 + END LOOP;
  121 + END IF;
  122 + ELSE
  123 + IF partition_type = 'DAYS' THEN
  124 + IF partition_by_max_ttl_month IS NULL THEN
  125 + RAISE NOTICE 'Failed to remove days partitions by max ttl date due to partition_by_max_ttl_month is null!';
  126 + ELSE
  127 + IF partition_by_max_ttl_day IS NULL THEN
  128 + RAISE NOTICE 'Failed to remove days partitions by max ttl date due to partition_by_max_ttl_day is null!';
  129 + ELSE
  130 + FOR partition IN SELECT tablename
  131 + FROM pg_tables
  132 + WHERE schemaname = 'public'
  133 + AND tablename like 'ts_kv_' || '%'
  134 + AND tablename != 'ts_kv_latest'
  135 + AND tablename != 'ts_kv_dictionary'
  136 + AND tablename != 'ts_kv_indefinite'
  137 + AND tablename != partition_by_max_ttl_date
  138 + LOOP
  139 + partition_year := SPLIT_PART(partition, '_', 3)::integer;
  140 + IF partition_year > partition_by_max_ttl_year::integer THEN
  141 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  142 + CONTINUE;
  143 + ELSE
  144 + IF partition_year < partition_by_max_ttl_year::integer THEN
  145 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  146 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  147 + deleted := deleted + 1;
  148 + ELSE
  149 + partition_month := SPLIT_PART(partition, '_', 4)::integer;
  150 + IF partition_month > partition_by_max_ttl_month::integer THEN
  151 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  152 + CONTINUE;
  153 + ELSE
  154 + IF partition_month < partition_by_max_ttl_month::integer THEN
  155 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  156 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  157 + deleted := deleted + 1;
  158 + ELSE
  159 + partition_day := SPLIT_PART(partition, '_', 5)::integer;
  160 + IF partition_day >= partition_by_max_ttl_day::integer THEN
  161 + RAISE NOTICE 'Skip iteration! Partition: % is valid!', partition;
  162 + CONTINUE;
  163 + ELSE
  164 + IF partition_day < partition_by_max_ttl_day::integer THEN
  165 + RAISE NOTICE 'Partition to delete by max ttl: %', partition;
  166 + EXECUTE format('DROP TABLE IF EXISTS %I', partition);
  167 + deleted := deleted + 1;
  168 + END IF;
  169 + END IF;
  170 + END IF;
  171 + END IF;
  172 + END IF;
  173 + END IF;
  174 + END LOOP;
84 175 END IF;
85 176 END IF;
86 177 END IF;
87   - IF partition_to_delete IS NOT NULL THEN
88   - RAISE NOTICE 'Partition to delete by max ttl: %', partition_to_delete;
89   - EXECUTE format('DROP TABLE IF EXISTS %I', partition_to_delete);
90   - partition_to_delete := NULL;
91   - deleted := deleted + 1;
92   - END IF;
93 178 END IF;
94   - END LOOP;
  179 + END IF;
  180 + END IF;
95 181 END IF;
96 182 END IF;
97 183 END
... ... @@ -107,8 +193,6 @@ BEGIN
107 193 partition := 'ts_kv_' || to_char(date, 'yyyy') || '_' || to_char(date, 'MM');
108 194 WHEN partition_type = 'YEARS' THEN
109 195 partition := 'ts_kv_' || to_char(date, 'yyyy');
110   - WHEN partition_type = 'INDEFINITE' THEN
111   - partition := NULL;
112 196 ELSE
113 197 partition := NULL;
114 198 END CASE;
... ...
... ... @@ -197,3 +197,17 @@ $$;
197 197 ALTER TABLE api_usage_state
198 198 ADD COLUMN IF NOT EXISTS alarm_exec VARCHAR(32);
199 199 UPDATE api_usage_state SET alarm_exec = 'ENABLED' WHERE alarm_exec IS NULL;
  200 +
  201 +CREATE TABLE IF NOT EXISTS rpc (
  202 + id uuid NOT NULL CONSTRAINT rpc_pkey PRIMARY KEY,
  203 + created_time bigint NOT NULL,
  204 + tenant_id uuid NOT NULL,
  205 + device_id uuid NOT NULL,
  206 + expiration_time bigint NOT NULL,
  207 + request varchar(10000000) NOT NULL,
  208 + response varchar(10000000),
  209 + status varchar(255) NOT NULL
  210 +);
  211 +
  212 +CREATE INDEX IF NOT EXISTS idx_rpc_tenant_id_device_id ON rpc(tenant_id, device_id);
  213 +
... ...
... ... @@ -65,6 +65,7 @@ import org.thingsboard.server.dao.relation.RelationService;
65 65 import org.thingsboard.server.dao.resource.ResourceService;
66 66 import org.thingsboard.server.dao.rule.RuleChainService;
67 67 import org.thingsboard.server.dao.rule.RuleNodeStateService;
  68 +import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
68 69 import org.thingsboard.server.dao.tenant.TenantProfileService;
69 70 import org.thingsboard.server.dao.tenant.TenantService;
70 71 import org.thingsboard.server.dao.timeseries.TimeseriesService;
... ... @@ -80,9 +81,9 @@ import org.thingsboard.server.service.executors.ExternalCallExecutorService;
80 81 import org.thingsboard.server.service.executors.SharedEventLoopGroupService;
81 82 import org.thingsboard.server.service.mail.MailExecutorService;
82 83 import org.thingsboard.server.service.profile.TbDeviceProfileCache;
83   -import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
84 84 import org.thingsboard.server.service.queue.TbClusterService;
85 85 import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
  86 +import org.thingsboard.server.service.rpc.TbRpcService;
86 87 import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService;
87 88 import org.thingsboard.server.service.script.JsExecutorService;
88 89 import org.thingsboard.server.service.script.JsInvokeService;
... ... @@ -303,23 +304,33 @@ public class ActorSystemContext {
303 304
304 305 @Lazy
305 306 @Autowired(required = false)
306   - @Getter private EdgeService edgeService;
  307 + @Getter
  308 + private EdgeService edgeService;
307 309
308 310 @Lazy
309 311 @Autowired(required = false)
310   - @Getter private EdgeEventService edgeEventService;
  312 + @Getter
  313 + private EdgeEventService edgeEventService;
311 314
312 315 @Lazy
313 316 @Autowired(required = false)
314   - @Getter private EdgeRpcService edgeRpcService;
  317 + @Getter
  318 + private EdgeRpcService edgeRpcService;
315 319
316 320 @Lazy
317 321 @Autowired(required = false)
318   - @Getter private ResourceService resourceService;
  322 + @Getter
  323 + private ResourceService resourceService;
319 324
320 325 @Lazy
321 326 @Autowired(required = false)
322   - @Getter private OtaPackageService otaPackageService;
  327 + @Getter
  328 + private OtaPackageService otaPackageService;
  329 +
  330 + @Lazy
  331 + @Autowired(required = false)
  332 + @Getter
  333 + private TbRpcService tbRpcService;
323 334
324 335 @Value("${actors.session.max_concurrent_sessions_per_device:1}")
325 336 @Getter
... ...
... ... @@ -46,7 +46,7 @@ public class DeviceActor extends ContextAwareActor {
46 46 super.init(ctx);
47 47 log.debug("[{}][{}] Starting device actor.", processor.tenantId, processor.deviceId);
48 48 try {
49   - processor.initSessionTimeout(ctx);
  49 + processor.init(ctx);
50 50 log.debug("[{}][{}] Device actor started.", processor.tenantId, processor.deviceId);
51 51 } catch (Exception e) {
52 52 log.warn("[{}][{}] Unknown failure", processor.tenantId, processor.deviceId, e);
... ...
... ... @@ -23,6 +23,8 @@ import com.google.common.util.concurrent.MoreExecutors;
23 23 import com.google.protobuf.InvalidProtocolBufferException;
24 24 import lombok.extern.slf4j.Slf4j;
25 25 import org.apache.commons.collections.CollectionUtils;
  26 +import org.thingsboard.common.util.JacksonUtil;
  27 +import org.thingsboard.common.util.LinkedHashMapRemoveEldest;
26 28 import org.thingsboard.rule.engine.api.RpcError;
27 29 import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg;
28 30 import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg;
... ... @@ -38,12 +40,17 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType;
38 40 import org.thingsboard.server.common.data.edge.EdgeEventType;
39 41 import org.thingsboard.server.common.data.id.DeviceId;
40 42 import org.thingsboard.server.common.data.id.EdgeId;
  43 +import org.thingsboard.server.common.data.id.RpcId;
41 44 import org.thingsboard.server.common.data.id.TenantId;
42 45 import org.thingsboard.server.common.data.kv.AttributeKey;
43 46 import org.thingsboard.server.common.data.kv.AttributeKvEntry;
44 47 import org.thingsboard.server.common.data.kv.KvEntry;
  48 +import org.thingsboard.server.common.data.page.PageData;
  49 +import org.thingsboard.server.common.data.page.PageLink;
45 50 import org.thingsboard.server.common.data.relation.EntityRelation;
46 51 import org.thingsboard.server.common.data.relation.RelationTypeGroup;
  52 +import org.thingsboard.server.common.data.rpc.Rpc;
  53 +import org.thingsboard.server.common.data.rpc.RpcStatus;
47 54 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
48 55 import org.thingsboard.server.common.data.security.DeviceCredentials;
49 56 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
... ... @@ -52,8 +59,8 @@ import org.thingsboard.server.common.msg.TbMsgMetaData;
52 59 import org.thingsboard.server.common.msg.queue.TbCallback;
53 60 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
54 61 import org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg;
55   -import org.thingsboard.server.gen.transport.TransportProtos;
56 62 import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg;
  63 +import org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg;
57 64 import org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry;
58 65 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg;
59 66 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg;
... ... @@ -68,10 +75,12 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionType;
68 75 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg;
69 76 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg;
70 77 import org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto;
  78 +import org.thingsboard.server.gen.transport.TransportProtos.ToDevicePersistedRpcResponseMsg;
71 79 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg;
72 80 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg;
73 81 import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg;
74 82 import org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg;
  83 +import org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto;
75 84 import org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg;
76 85 import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto;
77 86 import org.thingsboard.server.service.rpc.FromDeviceRpcResponse;
... ... @@ -88,6 +97,8 @@ import java.util.HashSet;
88 97 import java.util.LinkedHashMap;
89 98 import java.util.List;
90 99 import java.util.Map;
  100 +import java.util.Objects;
  101 +import java.util.Optional;
91 102 import java.util.Set;
92 103 import java.util.UUID;
93 104 import java.util.function.Consumer;
... ... @@ -102,7 +113,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
102 113
103 114 final TenantId tenantId;
104 115 final DeviceId deviceId;
105   - private final Map<UUID, SessionInfoMetaData> sessions;
  116 + final LinkedHashMapRemoveEldest<UUID, SessionInfoMetaData> sessions;
106 117 private final Map<UUID, SessionInfo> attributeSubscriptions;
107 118 private final Map<UUID, SessionInfo> rpcSubscriptions;
108 119 private final Map<Integer, ToDeviceRpcRequestMetadata> toDeviceRpcPendingMap;
... ... @@ -117,16 +128,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
117 128 super(systemContext);
118 129 this.tenantId = tenantId;
119 130 this.deviceId = deviceId;
120   - this.sessions = new LinkedHashMap<>();
121 131 this.attributeSubscriptions = new HashMap<>();
122 132 this.rpcSubscriptions = new HashMap<>();
123 133 this.toDeviceRpcPendingMap = new HashMap<>();
  134 + this.sessions = new LinkedHashMapRemoveEldest<>(systemContext.getMaxConcurrentSessionsPerDevice(), this::notifyTransportAboutClosedSessionMaxSessionsLimit);
124 135 if (initAttributes()) {
125 136 restoreSessions();
126 137 }
127 138 }
128 139
129   - private boolean initAttributes() {
  140 + boolean initAttributes() {
130 141 Device device = systemContext.getDeviceService().findDeviceById(tenantId, deviceId);
131 142 if (device != null) {
132 143 this.deviceName = device.getName();
... ... @@ -162,20 +173,19 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
162 173
163 174 void processRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg) {
164 175 ToDeviceRpcRequest request = msg.getMsg();
165   - ToDeviceRpcRequestBody body = request.getBody();
166   - ToDeviceRpcRequestMsg rpcRequest = ToDeviceRpcRequestMsg.newBuilder()
167   - .setRequestId(rpcSeq++)
168   - .setMethodName(body.getMethod())
169   - .setParams(body.getParams())
170   - .setExpirationTime(request.getExpirationTime())
171   - .setRequestIdMSB(request.getId().getMostSignificantBits())
172   - .setRequestIdLSB(request.getId().getLeastSignificantBits())
173   - .build();
  176 + ToDeviceRpcRequestMsg rpcRequest = creteToDeviceRpcRequestMsg(request);
174 177
175 178 long timeout = request.getExpirationTime() - System.currentTimeMillis();
  179 + boolean persisted = request.isPersisted();
  180 +
176 181 if (timeout <= 0) {
177 182 log.debug("[{}][{}] Ignoring message due to exp time reached, {}", deviceId, request.getId(), request.getExpirationTime());
  183 + if (persisted) {
  184 + createRpc(request, RpcStatus.TIMEOUT);
  185 + }
178 186 return;
  187 + } else if (persisted) {
  188 + createRpc(request, RpcStatus.QUEUED);
179 189 }
180 190
181 191 boolean sent;
... ... @@ -192,10 +202,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
192 202 syncSessionSet.add(key);
193 203 }
194 204 });
195   - log.trace("46) Rpc syncSessionSet [{}] subscription after sent [{}]",syncSessionSet, rpcSubscriptions);
  205 + log.trace("46) Rpc syncSessionSet [{}] subscription after sent [{}]", syncSessionSet, rpcSubscriptions);
196 206 syncSessionSet.forEach(rpcSubscriptions::remove);
197 207 }
198 208
  209 + if (persisted && !(sent || request.isOneway())) {
  210 + ObjectNode response = JacksonUtil.newObjectNode();
  211 + response.put("rpcId", request.getId().toString());
  212 + systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), JacksonUtil.toString(response), null));
  213 + }
  214 +
199 215 if (request.isOneway() && sent) {
200 216 log.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId());
201 217 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), null, null));
... ... @@ -209,6 +225,32 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
209 225 }
210 226 }
211 227
  228 + private Rpc createRpc(ToDeviceRpcRequest request, RpcStatus status) {
  229 + Rpc rpc = new Rpc(new RpcId(request.getId()));
  230 + rpc.setCreatedTime(System.currentTimeMillis());
  231 + rpc.setTenantId(tenantId);
  232 + rpc.setDeviceId(deviceId);
  233 + rpc.setExpirationTime(request.getExpirationTime());
  234 + rpc.setRequest(JacksonUtil.valueToTree(request));
  235 + rpc.setStatus(status);
  236 + systemContext.getTbRpcService().save(tenantId, rpc);
  237 + return systemContext.getTbRpcService().save(tenantId, rpc);
  238 + }
  239 +
  240 + private ToDeviceRpcRequestMsg creteToDeviceRpcRequestMsg(ToDeviceRpcRequest request) {
  241 + ToDeviceRpcRequestBody body = request.getBody();
  242 + return ToDeviceRpcRequestMsg.newBuilder()
  243 + .setRequestId(rpcSeq++)
  244 + .setMethodName(body.getMethod())
  245 + .setParams(body.getParams())
  246 + .setExpirationTime(request.getExpirationTime())
  247 + .setRequestIdMSB(request.getId().getMostSignificantBits())
  248 + .setRequestIdLSB(request.getId().getLeastSignificantBits())
  249 + .setOneway(request.isOneway())
  250 + .setPersisted(request.isPersisted())
  251 + .build();
  252 + }
  253 +
212 254 void processRpcResponsesFromEdge(TbActorCtx context, FromDeviceRpcResponseActorMsg responseMsg) {
213 255 log.debug("[{}] Processing rpc command response from edge session", deviceId);
214 256 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(responseMsg.getRequestId());
... ... @@ -230,6 +272,9 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
230 272 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(msg.getId());
231 273 if (requestMd != null) {
232 274 log.debug("[{}] RPC request [{}] timeout detected!", deviceId, msg.getId());
  275 + if (requestMd.getMsg().getMsg().isPersisted()) {
  276 + systemContext.getTbRpcService().save(tenantId, new RpcId(requestMd.getMsg().getMsg().getId()), RpcStatus.TIMEOUT, null);
  277 + }
233 278 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(),
234 279 null, requestMd.isSent() ? RpcError.TIMEOUT : RpcError.NO_ACTIVE_CONNECTION));
235 280 }
... ... @@ -271,7 +316,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
271 316 .setExpirationTime(request.getExpirationTime())
272 317 .setRequestIdMSB(request.getId().getMostSignificantBits())
273 318 .setRequestIdLSB(request.getId().getLeastSignificantBits())
  319 + .setOneway(request.isOneway())
  320 + .setPersisted(request.isPersisted())
274 321 .build();
  322 +
275 323 sendToTransport(rpcRequest, sessionId, nodeId);
276 324 };
277 325 }
... ... @@ -279,31 +327,39 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
279 327 void process(TbActorCtx context, TransportToDeviceActorMsgWrapper wrapper) {
280 328 TransportToDeviceActorMsg msg = wrapper.getMsg();
281 329 TbCallback callback = wrapper.getCallback();
  330 + var sessionInfo = msg.getSessionInfo();
  331 +
282 332 if (msg.hasSessionEvent()) {
283   - processSessionStateMsgs(msg.getSessionInfo(), msg.getSessionEvent());
  333 + processSessionStateMsgs(sessionInfo, msg.getSessionEvent());
284 334 }
285 335 if (msg.hasSubscribeToAttributes()) {
286   - processSubscriptionCommands(context, msg.getSessionInfo(), msg.getSubscribeToAttributes());
  336 + processSubscriptionCommands(context, sessionInfo, msg.getSubscribeToAttributes());
287 337 }
288 338 if (msg.hasSubscribeToRPC()) {
289   - processSubscriptionCommands(context, msg.getSessionInfo(), msg.getSubscribeToRPC());
  339 + processSubscriptionCommands(context, sessionInfo, msg.getSubscribeToRPC());
  340 + }
  341 + if (msg.hasSendPendingRPC()) {
  342 + sendPendingRequests(context, getSessionId(sessionInfo), sessionInfo);
290 343 }
291 344 if (msg.hasGetAttributes()) {
292   - handleGetAttributesRequest(context, msg.getSessionInfo(), msg.getGetAttributes());
  345 + handleGetAttributesRequest(context, sessionInfo, msg.getGetAttributes());
293 346 }
294 347 if (msg.hasToDeviceRPCCallResponse()) {
295   - processRpcResponses(context, msg.getSessionInfo(), msg.getToDeviceRPCCallResponse());
  348 + processRpcResponses(context, sessionInfo, msg.getToDeviceRPCCallResponse());
296 349 }
297 350 if (msg.hasSubscriptionInfo()) {
298   - handleSessionActivity(context, msg.getSessionInfo(), msg.getSubscriptionInfo());
  351 + handleSessionActivity(context, sessionInfo, msg.getSubscriptionInfo());
299 352 }
300 353 if (msg.hasClaimDevice()) {
301   - handleClaimDeviceMsg(context, msg.getSessionInfo(), msg.getClaimDevice());
  354 + handleClaimDeviceMsg(context, sessionInfo, msg.getClaimDevice());
  355 + }
  356 + if (msg.hasPersistedRpcResponseMsg()) {
  357 + processPersistedRpcResponses(context, sessionInfo, msg.getPersistedRpcResponseMsg());
302 358 }
303 359 callback.onSuccess();
304 360 }
305 361
306   - private void handleClaimDeviceMsg(TbActorCtx context, SessionInfoProto sessionInfo, TransportProtos.ClaimDeviceMsg msg) {
  362 + private void handleClaimDeviceMsg(TbActorCtx context, SessionInfoProto sessionInfo, ClaimDeviceMsg msg) {
307 363 DeviceId deviceId = new DeviceId(new UUID(msg.getDeviceIdMSB(), msg.getDeviceIdLSB()));
308 364 systemContext.getClaimDevicesService().registerClaimingInfo(tenantId, deviceId, msg.getSecretKey(), msg.getDurationMs());
309 365 }
... ... @@ -442,11 +498,22 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
442 498 if (success) {
443 499 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(),
444 500 responseMsg.getPayload(), null));
  501 + if (requestMd.getMsg().getMsg().isPersisted()) {
  502 + systemContext.getTbRpcService().save(tenantId, new RpcId(requestMd.getMsg().getMsg().getId()), RpcStatus.SUCCESSFUL, JacksonUtil.toJsonNode(responseMsg.getPayload()));
  503 + }
445 504 } else {
446 505 log.debug("[{}] Rpc command response [{}] is stale!", deviceId, responseMsg.getRequestId());
  506 + if (requestMd.getMsg().getMsg().isPersisted()) {
  507 + systemContext.getTbRpcService().save(tenantId, new RpcId(requestMd.getMsg().getMsg().getId()), RpcStatus.FAILED, JacksonUtil.toJsonNode(responseMsg.getPayload()));
  508 + }
447 509 }
448 510 }
449 511
  512 + private void processPersistedRpcResponses(TbActorCtx context, SessionInfoProto sessionInfo, ToDevicePersistedRpcResponseMsg responseMsg) {
  513 + UUID rpcId = new UUID(responseMsg.getRequestIdMSB(), responseMsg.getRequestIdLSB());
  514 + systemContext.getTbRpcService().save(tenantId, new RpcId(rpcId), RpcStatus.valueOf(responseMsg.getStatus()), null);
  515 + }
  516 +
450 517 private void processSubscriptionCommands(TbActorCtx context, SessionInfoProto sessionInfo, SubscribeToAttributeUpdatesMsg subscribeCmd) {
451 518 UUID sessionId = getSessionId(sessionInfo);
452 519 if (subscribeCmd.getUnsubscribe()) {
... ... @@ -488,18 +555,14 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
488 555
489 556 private void processSessionStateMsgs(SessionInfoProto sessionInfo, SessionEventMsg msg) {
490 557 UUID sessionId = getSessionId(sessionInfo);
  558 + Objects.requireNonNull(sessionId);
491 559 if (msg.getEvent() == SessionEvent.OPEN) {
492 560 if (sessions.containsKey(sessionId)) {
493 561 log.debug("[{}] Received duplicate session open event [{}]", deviceId, sessionId);
494 562 return;
495 563 }
496   - log.debug("[{}] Processing new session [{}]", deviceId, sessionId);
497   - if (sessions.size() >= systemContext.getMaxConcurrentSessionsPerDevice()) {
498   - UUID sessionIdToRemove = sessions.keySet().stream().findFirst().orElse(null);
499   - if (sessionIdToRemove != null) {
500   - notifyTransportAboutClosedSession(sessionIdToRemove, sessions.remove(sessionIdToRemove), "max concurrent sessions limit reached per device!");
501   - }
502   - }
  564 + log.info("[{}] Processing new session [{}]. Current sessions size {}", deviceId, sessionId, sessions.size());
  565 +
503 566 sessions.put(sessionId, new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfo.getNodeId())));
504 567 if (sessions.size() == 1) {
505 568 reportSessionOpen();
... ... @@ -520,8 +583,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
520 583
521 584 private void handleSessionActivity(TbActorCtx context, SessionInfoProto sessionInfoProto, SubscriptionInfoProto subscriptionInfo) {
522 585 UUID sessionId = getSessionId(sessionInfoProto);
  586 + Objects.requireNonNull(sessionId);
  587 +
523 588 SessionInfoMetaData sessionMD = sessions.computeIfAbsent(sessionId,
524   - id -> new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfoProto.getNodeId()), 0L));
  589 + id -> new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfoProto.getNodeId()), subscriptionInfo.getLastActivityTime()));
525 590
526 591 sessionMD.setLastActivityTime(subscriptionInfo.getLastActivityTime());
527 592 sessionMD.setSubscribedToAttributes(subscriptionInfo.getAttributeSubscription());
... ... @@ -551,6 +616,11 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
551 616 }
552 617 }
553 618
  619 + private void notifyTransportAboutClosedSessionMaxSessionsLimit(UUID sessionId, SessionInfoMetaData sessionMd) {
  620 + notifyTransportAboutClosedSession(sessionId, sessionMd, "max concurrent sessions limit reached per device!");
  621 + }
  622 +
  623 +
554 624 private void notifyTransportAboutClosedSession(UUID sessionId, SessionInfoMetaData sessionMd, String message) {
555 625 SessionCloseNotificationProto sessionCloseNotificationProto = SessionCloseNotificationProto
556 626 .newBuilder()
... ... @@ -565,7 +635,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
565 635
566 636 void notifyTransportAboutProfileUpdate(UUID sessionId, SessionInfoMetaData sessionMd, DeviceCredentials deviceCredentials) {
567 637 log.info("2) LwM2Mtype: ");
568   - TransportProtos.ToTransportUpdateCredentialsProto.Builder notification = TransportProtos.ToTransportUpdateCredentialsProto.newBuilder();
  638 + ToTransportUpdateCredentialsProto.Builder notification = ToTransportUpdateCredentialsProto.newBuilder();
569 639 notification.addCredentialsId(deviceCredentials.getCredentialsId());
570 640 notification.addCredentialsValue(deviceCredentials.getCredentialsValue());
571 641 ToTransportMsg msg = ToTransportMsg.newBuilder()
... ... @@ -640,7 +710,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
640 710 ListenableFuture<EdgeEvent> future = systemContext.getEdgeEventService().saveAsync(edgeEvent);
641 711 Futures.addCallback(future, new FutureCallback<EdgeEvent>() {
642 712 @Override
643   - public void onSuccess( EdgeEvent result) {
  713 + public void onSuccess(EdgeEvent result) {
644 714 systemContext.getClusterService().onEdgeEventUpdate(tenantId, edgeId);
645 715 }
646 716
... ... @@ -697,7 +767,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
697 767 return builder.build();
698 768 }
699 769
700   - private void restoreSessions() {
  770 + void restoreSessions() {
701 771 log.debug("[{}] Restoring sessions from cache", deviceId);
702 772 DeviceSessionsCacheEntry sessionsDump = null;
703 773 try {
... ... @@ -710,6 +780,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
710 780 log.debug("[{}] No session information found", deviceId);
711 781 return;
712 782 }
  783 + // TODO: Take latest max allowed sessions size from cache
713 784 for (SessionSubscriptionInfoProto sessionSubscriptionInfoProto : sessionsDump.getSessionsList()) {
714 785 SessionInfoProto sessionInfoProto = sessionSubscriptionInfoProto.getSessionInfo();
715 786 UUID sessionId = getSessionId(sessionInfoProto);
... ... @@ -756,11 +827,30 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
756 827 .addAllSessions(sessionsList).build().toByteArray());
757 828 }
758 829
759   - void initSessionTimeout(TbActorCtx ctx) {
  830 + void init(TbActorCtx ctx) {
760 831 schedulePeriodicMsgWithDelay(ctx, SessionTimeoutCheckMsg.instance(), systemContext.getSessionReportTimeout(), systemContext.getSessionReportTimeout());
  832 + PageLink pageLink = new PageLink(1024);
  833 + PageData<Rpc> pageData;
  834 + do {
  835 + pageData = systemContext.getTbRpcService().findAllByDeviceIdAndStatus(tenantId, deviceId, RpcStatus.QUEUED, pageLink);
  836 + pageData.getData().forEach(rpc -> {
  837 + ToDeviceRpcRequest msg = JacksonUtil.convertValue(rpc.getRequest(), ToDeviceRpcRequest.class);
  838 + long timeout = rpc.getExpirationTime() - System.currentTimeMillis();
  839 + if (timeout <= 0) {
  840 + rpc.setStatus(RpcStatus.TIMEOUT);
  841 + systemContext.getTbRpcService().save(tenantId, rpc);
  842 + } else {
  843 + registerPendingRpcRequest(ctx, new ToDeviceRpcRequestActorMsg(systemContext.getServiceId(), msg), false, creteToDeviceRpcRequestMsg(msg), timeout);
  844 + }
  845 + });
  846 + if (pageData.hasNext()) {
  847 + pageLink = pageLink.nextPageLink();
  848 + }
  849 + } while (pageData.hasNext());
761 850 }
762 851
763 852 void checkSessionsTimeout() {
  853 + log.debug("[{}] checkSessionsTimeout started. Size before check {}", deviceId, sessions.size());
764 854 long expTime = System.currentTimeMillis() - systemContext.getSessionInactivityTimeout();
765 855 Map<UUID, SessionInfoMetaData> sessionsToRemove = sessions.entrySet().stream().filter(kv -> kv.getValue().getLastActivityTime() < expTime).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
766 856 sessionsToRemove.forEach((sessionId, sessionMD) -> {
... ... @@ -772,6 +862,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
772 862 if (!sessionsToRemove.isEmpty()) {
773 863 dumpSessions();
774 864 }
  865 + log.debug("[{}] checkSessionsTimeout finished. Size after check {}", deviceId, sessions.size());
775 866 }
776 867
777 868 }
... ...
... ... @@ -25,5 +25,4 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionType;
25 25 public class SessionInfo {
26 26 private final SessionType type;
27 27 private final String nodeId;
28   - private long lastActivityTime;
29 28 }
... ...
... ... @@ -135,7 +135,7 @@ public class AuthController extends BaseController {
135 135 }
136 136 }
137 137
138   - @RequestMapping(value = "/noauth/activate", params = { "activateToken" }, method = RequestMethod.GET)
  138 + @RequestMapping(value = "/noauth/activate", params = {"activateToken"}, method = RequestMethod.GET)
139 139 public ResponseEntity<String> checkActivateToken(
140 140 @RequestParam(value = "activateToken") String activateToken) {
141 141 HttpHeaders headers = new HttpHeaders();
... ... @@ -159,7 +159,7 @@ public class AuthController extends BaseController {
159 159
160 160 @RequestMapping(value = "/noauth/resetPasswordByEmail", method = RequestMethod.POST)
161 161 @ResponseStatus(value = HttpStatus.OK)
162   - public void requestResetPasswordByEmail (
  162 + public void requestResetPasswordByEmail(
163 163 @RequestBody JsonNode resetPasswordByEmailRequest,
164 164 HttpServletRequest request) throws ThingsboardException {
165 165 try {
... ... @@ -170,13 +170,13 @@ public class AuthController extends BaseController {
170 170 String resetUrl = String.format("%s/api/noauth/resetPassword?resetToken=%s", baseUrl,
171 171 userCredentials.getResetToken());
172 172
173   - mailService.sendResetPasswordEmail(resetUrl, email);
  173 + mailService.sendResetPasswordEmailAsync(resetUrl, email);
174 174 } catch (Exception e) {
175   - throw handleException(e);
  175 + log.warn("Error occurred: {}", e.getMessage());
176 176 }
177 177 }
178 178
179   - @RequestMapping(value = "/noauth/resetPassword", params = { "resetToken" }, method = RequestMethod.GET)
  179 + @RequestMapping(value = "/noauth/resetPassword", params = {"resetToken"}, method = RequestMethod.GET)
180 180 public ResponseEntity<String> checkResetToken(
181 181 @RequestParam(value = "resetToken") String resetToken) {
182 182 HttpHeaders headers = new HttpHeaders();
... ...
... ... @@ -69,6 +69,7 @@ import org.thingsboard.server.common.data.id.EntityId;
69 69 import org.thingsboard.server.common.data.id.EntityIdFactory;
70 70 import org.thingsboard.server.common.data.id.EntityViewId;
71 71 import org.thingsboard.server.common.data.id.OtaPackageId;
  72 +import org.thingsboard.server.common.data.id.RpcId;
72 73 import org.thingsboard.server.common.data.id.TbResourceId;
73 74 import org.thingsboard.server.common.data.id.RuleChainId;
74 75 import org.thingsboard.server.common.data.id.RuleNodeId;
... ... @@ -85,6 +86,7 @@ import org.thingsboard.server.common.data.page.TimePageLink;
85 86 import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
86 87 import org.thingsboard.server.common.data.plugin.ComponentType;
87 88 import org.thingsboard.server.common.data.relation.EntityRelation;
  89 +import org.thingsboard.server.common.data.rpc.Rpc;
88 90 import org.thingsboard.server.common.data.rule.RuleChain;
89 91 import org.thingsboard.server.common.data.rule.RuleChainType;
90 92 import org.thingsboard.server.common.data.rule.RuleNode;
... ... @@ -108,6 +110,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
108 110 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
109 111 import org.thingsboard.server.dao.oauth2.OAuth2Service;
110 112 import org.thingsboard.server.dao.relation.RelationService;
  113 +import org.thingsboard.server.dao.rpc.RpcService;
111 114 import org.thingsboard.server.dao.rule.RuleChainService;
112 115 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
113 116 import org.thingsboard.server.dao.tenant.TenantProfileService;
... ... @@ -252,6 +255,9 @@ public abstract class BaseController {
252 255 protected OtaPackageStateService otaPackageStateService;
253 256
254 257 @Autowired
  258 + protected RpcService rpcService;
  259 +
  260 + @Autowired
255 261 protected TbQueueProducerProvider producerProvider;
256 262
257 263 @Autowired
... ... @@ -789,6 +795,18 @@ public abstract class BaseController {
789 795 }
790 796 }
791 797
  798 + Rpc checkRpcId(RpcId rpcId, Operation operation) throws ThingsboardException {
  799 + try {
  800 + validateId(rpcId, "Incorrect rpcId " + rpcId);
  801 + Rpc rpc = rpcService.findById(getCurrentUser().getTenantId(), rpcId);
  802 + checkNotNull(rpc);
  803 + accessControlService.checkPermission(getCurrentUser(), Resource.RPC, operation, rpcId, rpc);
  804 + return rpc;
  805 + } catch (Exception e) {
  806 + throw handleException(e, false);
  807 + }
  808 + }
  809 +
792 810 @SuppressWarnings("unchecked")
793 811 protected <I extends EntityId> I emptyId(EntityType entityType) {
794 812 return (I) EntityIdFactory.getByTypeAndUuid(entityType, ModelConstants.NULL_UUID);
... ...
... ... @@ -29,6 +29,7 @@ import org.springframework.web.bind.annotation.PathVariable;
29 29 import org.springframework.web.bind.annotation.RequestBody;
30 30 import org.springframework.web.bind.annotation.RequestMapping;
31 31 import org.springframework.web.bind.annotation.RequestMethod;
  32 +import org.springframework.web.bind.annotation.RequestParam;
32 33 import org.springframework.web.bind.annotation.ResponseBody;
33 34 import org.springframework.web.bind.annotation.RestController;
34 35 import org.springframework.web.context.request.async.DeferredResult;
... ... @@ -38,8 +39,13 @@ import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
38 39 import org.thingsboard.server.common.data.exception.ThingsboardException;
39 40 import org.thingsboard.server.common.data.id.DeviceId;
40 41 import org.thingsboard.server.common.data.id.EntityId;
  42 +import org.thingsboard.server.common.data.id.RpcId;
41 43 import org.thingsboard.server.common.data.id.TenantId;
42 44 import org.thingsboard.server.common.data.id.UUIDBased;
  45 +import org.thingsboard.server.common.data.page.PageData;
  46 +import org.thingsboard.server.common.data.page.PageLink;
  47 +import org.thingsboard.server.common.data.rpc.Rpc;
  48 +import org.thingsboard.server.common.data.rpc.RpcStatus;
43 49 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
44 50 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
45 51 import org.thingsboard.server.queue.util.TbCoreComponent;
... ... @@ -93,6 +99,52 @@ public class RpcController extends BaseController {
93 99 return handleDeviceRPCRequest(false, new DeviceId(UUID.fromString(deviceIdStr)), requestBody);
94 100 }
95 101
  102 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
  103 + @RequestMapping(value = "/persisted/{rpcId}", method = RequestMethod.GET)
  104 + @ResponseBody
  105 + public Rpc getPersistedRpc(@PathVariable("rpcId") String strRpc) throws ThingsboardException {
  106 + checkParameter("RpcId", strRpc);
  107 + try {
  108 + RpcId rpcId = new RpcId(UUID.fromString(strRpc));
  109 + return checkRpcId(rpcId, Operation.READ);
  110 + } catch (Exception e) {
  111 + throw handleException(e);
  112 + }
  113 + }
  114 +
  115 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
  116 + @RequestMapping(value = "/persisted/{deviceId}", method = RequestMethod.GET)
  117 + @ResponseBody
  118 + public PageData<Rpc> getPersistedRpcByDevice(@PathVariable("deviceId") String strDeviceId,
  119 + @RequestParam int pageSize,
  120 + @RequestParam int page,
  121 + @RequestParam RpcStatus rpcStatus,
  122 + @RequestParam(required = false) String textSearch,
  123 + @RequestParam(required = false) String sortProperty,
  124 + @RequestParam(required = false) String sortOrder) throws ThingsboardException {
  125 + checkParameter("DeviceId", strDeviceId);
  126 + try {
  127 + TenantId tenantId = getCurrentUser().getTenantId();
  128 + PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
  129 + DeviceId deviceId = new DeviceId(UUID.fromString(strDeviceId));
  130 + return checkNotNull(rpcService.findAllByDeviceIdAndStatus(tenantId, deviceId, rpcStatus, pageLink));
  131 + } catch (Exception e) {
  132 + throw handleException(e);
  133 + }
  134 + }
  135 +
  136 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
  137 + @RequestMapping(value = "/persisted/{rpcId}", method = RequestMethod.DELETE)
  138 + @ResponseBody
  139 + public void deleteResource(@PathVariable("rpcId") String strRpc) throws ThingsboardException {
  140 + checkParameter("RpcId", strRpc);
  141 + try {
  142 + rpcService.deleteRpc(getTenantId(), new RpcId(UUID.fromString(strRpc)));
  143 + } catch (Exception e) {
  144 + throw handleException(e);
  145 + }
  146 + }
  147 +
96 148 private DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody) throws ThingsboardException {
97 149 try {
98 150 JsonNode rpcRequestBody = jsonMapper.readTree(requestBody);
... ... @@ -103,6 +155,7 @@ public class RpcController extends BaseController {
103 155 long timeout = rpcRequestBody.has("timeout") ? rpcRequestBody.get("timeout").asLong() : defaultTimeout;
104 156 long expTime = System.currentTimeMillis() + Math.max(minTimeout, timeout);
105 157 UUID rpcRequestUUID = rpcRequestBody.has("requestUUID") ? UUID.fromString(rpcRequestBody.get("requestUUID").asText()) : UUID.randomUUID();
  158 + boolean persisted = rpcRequestBody.has("persisted") && rpcRequestBody.get("persisted").asBoolean();
106 159 accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<DeferredResult<ResponseEntity>>() {
107 160 @Override
108 161 public void onSuccess(@Nullable DeferredResult<ResponseEntity> result) {
... ... @@ -111,7 +164,8 @@ public class RpcController extends BaseController {
111 164 deviceId,
112 165 oneWay,
113 166 expTime,
114   - body
  167 + body,
  168 + persisted
115 169 );
116 170 deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse), currentUser);
117 171 }
... ...
... ... @@ -25,9 +25,12 @@ import org.springframework.security.authentication.BadCredentialsException;
25 25 import org.springframework.security.authentication.DisabledException;
26 26 import org.springframework.security.authentication.LockedException;
27 27 import org.springframework.security.core.AuthenticationException;
  28 +import org.springframework.security.core.userdetails.UsernameNotFoundException;
28 29 import org.springframework.security.web.access.AccessDeniedHandler;
29   -import org.springframework.stereotype.Component;
  30 +import org.springframework.web.bind.annotation.ExceptionHandler;
  31 +import org.springframework.web.bind.annotation.RestControllerAdvice;
30 32 import org.springframework.web.client.HttpClientErrorException;
  33 +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
31 34 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
32 35 import org.thingsboard.server.common.data.exception.ThingsboardException;
33 36 import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
... ... @@ -40,14 +43,15 @@ import javax.servlet.http.HttpServletRequest;
40 43 import javax.servlet.http.HttpServletResponse;
41 44 import java.io.IOException;
42 45
43   -@Component
44 46 @Slf4j
45   -public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
  47 +@RestControllerAdvice
  48 +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler {
46 49
47 50 @Autowired
48 51 private ObjectMapper mapper;
49 52
50 53 @Override
  54 + @ExceptionHandler(AccessDeniedException.class)
51 55 public void handle(HttpServletRequest request, HttpServletResponse response,
52 56 AccessDeniedException accessDeniedException) throws IOException,
53 57 ServletException {
... ... @@ -60,6 +64,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
60 64 }
61 65 }
62 66
  67 + @ExceptionHandler(Exception.class)
63 68 public void handle(Exception exception, HttpServletResponse response) {
64 69 log.debug("Processing exception {}", exception.getMessage(), exception);
65 70 if (!response.isCommitted()) {
... ... @@ -148,7 +153,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
148 153
149 154 private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException {
150 155 response.setStatus(HttpStatus.UNAUTHORIZED.value());
151   - if (authenticationException instanceof BadCredentialsException) {
  156 + if (authenticationException instanceof BadCredentialsException || authenticationException instanceof UsernameNotFoundException) {
152 157 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Invalid username or password", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
153 158 } else if (authenticationException instanceof DisabledException) {
154 159 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("User account is not active", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
... ... @@ -159,7 +164,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
159 164 } else if (authenticationException instanceof AuthMethodNotSupportedException) {
160 165 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
161 166 } else if (authenticationException instanceof UserPasswordExpiredException) {
162   - UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException;
  167 + UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException;
163 168 String resetToken = expiredException.getResetToken();
164 169 mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken));
165 170 } else {
... ...
... ... @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
28 28 import org.springframework.stereotype.Service;
29 29 import org.thingsboard.common.util.ThingsBoardThreadFactory;
30 30 import org.thingsboard.server.common.data.DataConstants;
  31 +import org.thingsboard.server.common.data.ResourceUtils;
31 32 import org.thingsboard.server.common.data.edge.Edge;
32 33 import org.thingsboard.server.common.data.id.EdgeId;
33 34 import org.thingsboard.server.common.data.id.TenantId;
... ... @@ -47,6 +48,7 @@ import javax.annotation.PostConstruct;
47 48 import javax.annotation.PreDestroy;
48 49 import java.io.File;
49 50 import java.io.IOException;
  51 +import java.io.InputStream;
50 52 import java.util.Collections;
51 53 import java.util.Map;
52 54 import java.util.UUID;
... ... @@ -108,9 +110,9 @@ public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase i
108 110 .addService(this);
109 111 if (sslEnabled) {
110 112 try {
111   - File certFile = new File(Resources.getResource(certFileResource).toURI());
112   - File privateKeyFile = new File(Resources.getResource(privateKeyResource).toURI());
113   - builder.useTransportSecurity(certFile, privateKeyFile);
  113 + InputStream certFileIs = ResourceUtils.getInputStream(this, certFileResource);
  114 + InputStream privateKeyFileIs = ResourceUtils.getInputStream(this, privateKeyResource);
  115 + builder.useTransportSecurity(certFileIs, privateKeyFileIs);
114 116 } catch (Exception e) {
115 117 log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e);
116 118 throw new RuntimeException("Unable to set up SSL context!", e);
... ...
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.service.install.update;
17 17
  18 +import com.fasterxml.jackson.databind.JsonNode;
18 19 import com.fasterxml.jackson.databind.node.ObjectNode;
19 20 import com.google.common.util.concurrent.Futures;
20 21 import com.google.common.util.concurrent.ListenableFuture;
... ... @@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.Tenant;
31 32 import org.thingsboard.server.common.data.alarm.Alarm;
32 33 import org.thingsboard.server.common.data.alarm.AlarmInfo;
33 34 import org.thingsboard.server.common.data.alarm.AlarmQuery;
  35 +import org.thingsboard.server.common.data.alarm.AlarmSeverity;
34 36 import org.thingsboard.server.common.data.id.EntityViewId;
35 37 import org.thingsboard.server.common.data.id.TenantId;
36 38 import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
... ... @@ -41,16 +43,21 @@ import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientsParams;
41 43 import org.thingsboard.server.common.data.page.PageData;
42 44 import org.thingsboard.server.common.data.page.PageLink;
43 45 import org.thingsboard.server.common.data.page.TimePageLink;
  46 +import org.thingsboard.server.common.data.query.DynamicValue;
  47 +import org.thingsboard.server.common.data.query.FilterPredicateValue;
44 48 import org.thingsboard.server.common.data.rule.RuleChain;
45 49 import org.thingsboard.server.common.data.rule.RuleChainMetaData;
46 50 import org.thingsboard.server.common.data.rule.RuleNode;
  51 +import org.thingsboard.server.dao.DaoUtil;
47 52 import org.thingsboard.server.dao.alarm.AlarmDao;
48 53 import org.thingsboard.server.dao.alarm.AlarmService;
49 54 import org.thingsboard.server.dao.entity.EntityService;
50 55 import org.thingsboard.server.dao.entityview.EntityViewService;
  56 +import org.thingsboard.server.dao.model.sql.DeviceProfileEntity;
51 57 import org.thingsboard.server.dao.oauth2.OAuth2Service;
52 58 import org.thingsboard.server.dao.oauth2.OAuth2Utils;
53 59 import org.thingsboard.server.dao.rule.RuleChainService;
  60 +import org.thingsboard.server.dao.sql.device.DeviceProfileRepository;
54 61 import org.thingsboard.server.dao.tenant.TenantService;
55 62 import org.thingsboard.server.dao.timeseries.TimeseriesService;
56 63 import org.thingsboard.server.service.install.InstallScripts;
... ... @@ -93,6 +100,9 @@ public class DefaultDataUpdateService implements DataUpdateService {
93 100 private AlarmDao alarmDao;
94 101
95 102 @Autowired
  103 + private DeviceProfileRepository deviceProfileRepository;
  104 +
  105 + @Autowired
96 106 private OAuth2Service oAuth2Service;
97 107
98 108 @Override
... ... @@ -114,6 +124,7 @@ public class DefaultDataUpdateService implements DataUpdateService {
114 124 log.info("Updating data from version 3.2.2 to 3.3.0 ...");
115 125 tenantsDefaultEdgeRuleChainUpdater.updateEntities(null);
116 126 tenantsAlarmsCustomerUpdater.updateEntities(null);
  127 + deviceProfileEntityDynamicConditionsUpdater.updateEntities(null);
117 128 updateOAuth2Params();
118 129 break;
119 130 default:
... ... @@ -121,6 +132,45 @@ public class DefaultDataUpdateService implements DataUpdateService {
121 132 }
122 133 }
123 134
  135 + private final PaginatedUpdater<String, DeviceProfileEntity> deviceProfileEntityDynamicConditionsUpdater =
  136 + new PaginatedUpdater<>() {
  137 +
  138 + @Override
  139 + protected String getName() {
  140 + return "Device Profile Entity Dynamic Conditions Updater";
  141 + }
  142 +
  143 + @Override
  144 + protected PageData<DeviceProfileEntity> findEntities(String id, PageLink pageLink) {
  145 + return DaoUtil.pageToPageData(deviceProfileRepository.findAll(DaoUtil.toPageable(pageLink)));
  146 + }
  147 +
  148 + @Override
  149 + protected void updateEntity(DeviceProfileEntity deviceProfile) {
  150 + if (deviceProfile.getProfileData().has("alarms") &&
  151 + !deviceProfile.getProfileData().get("alarms").isNull()) {
  152 + boolean isUpdated = false;
  153 + JsonNode array = deviceProfile.getProfileData().get("alarms");
  154 + for (JsonNode node : array) {
  155 + if (node.has("createRules")) {
  156 + JsonNode createRules = node.get("createRules");
  157 + for (AlarmSeverity severity : AlarmSeverity.values()) {
  158 + if (createRules.has(severity.name())) {
  159 + isUpdated = isUpdated || convertDeviceProfileAlarmRulesForVersion330(createRules.get(severity.name()).get("condition").get("spec"));
  160 + }
  161 + }
  162 + }
  163 + if (node.has("clearRule") && !node.get("clearRule").isNull()) {
  164 + isUpdated = isUpdated || convertDeviceProfileAlarmRulesForVersion330(node.get("clearRule").get("condition").get("spec"));
  165 + }
  166 + }
  167 + if (isUpdated) {
  168 + deviceProfileRepository.save(deviceProfile);
  169 + }
  170 + }
  171 + }
  172 + };
  173 +
124 174 private final PaginatedUpdater<String, Tenant> tenantsDefaultRuleChainUpdater =
125 175 new PaginatedUpdater<>() {
126 176
... ... @@ -370,6 +420,33 @@ public class DefaultDataUpdateService implements DataUpdateService {
370 420 }
371 421 }
372 422
  423 + private boolean convertDeviceProfileAlarmRulesForVersion330(JsonNode spec) {
  424 + if (spec != null) {
  425 + if (spec.has("type") && spec.get("type").asText().equals("DURATION")) {
  426 + if (spec.has("value")) {
  427 + long value = spec.get("value").asLong();
  428 + var predicate = new FilterPredicateValue<>(
  429 + value, null, new DynamicValue<>(null, null, false)
  430 + );
  431 + ((ObjectNode) spec).remove("value");
  432 + ((ObjectNode) spec).putPOJO("predicate", predicate);
  433 + return true;
  434 + }
  435 + } else if (spec.has("type") && spec.get("type").asText().equals("REPEATING")) {
  436 + if (spec.has("count")) {
  437 + int count = spec.get("count").asInt();
  438 + var predicate = new FilterPredicateValue<>(
  439 + count, null, new DynamicValue<>(null, null, false)
  440 + );
  441 + ((ObjectNode) spec).remove("count");
  442 + ((ObjectNode) spec).putPOJO("predicate", predicate);
  443 + return true;
  444 + }
  445 + }
  446 + }
  447 + return false;
  448 + }
  449 +
373 450 private void updateOAuth2Params() {
374 451 try {
375 452 OAuth2ClientsParams oauth2ClientsParams = oAuth2Service.findOAuth2Params();
... ... @@ -380,9 +457,8 @@ public class DefaultDataUpdateService implements DataUpdateService {
380 457 oAuth2Service.saveOAuth2Params(new OAuth2ClientsParams(false, Collections.emptyList()));
381 458 log.info("Successfully updated OAuth2 parameters!");
382 459 }
383   - }
384   - catch (Exception e) {
385   - log.error("Failed to update OAuth2 parameters", e);
  460 + } catch (Exception e) {
  461 + log.error("Failed to update OAuth2 parameters", e);
386 462 }
387 463 }
388 464
... ...
... ... @@ -22,7 +22,7 @@ import org.thingsboard.server.common.data.page.PageData;
22 22 import org.thingsboard.server.common.data.page.PageLink;
23 23
24 24 @Slf4j
25   -public abstract class PaginatedUpdater<I, D extends SearchTextBased<? extends UUIDBased>> {
  25 +public abstract class PaginatedUpdater<I, D> {
26 26
27 27 private static final int DEFAULT_LIMIT = 100;
28 28 private int updated = 0;
... ...
... ... @@ -30,8 +30,12 @@ import java.math.BigInteger;
30 30 import java.security.AlgorithmParameters;
31 31 import java.security.GeneralSecurityException;
32 32 import java.security.KeyFactory;
  33 +import java.security.KeyStore;
33 34 import java.security.KeyStoreException;
  35 +import java.security.NoSuchAlgorithmException;
  36 +import java.security.PrivateKey;
34 37 import java.security.PublicKey;
  38 +import java.security.UnrecoverableKeyException;
35 39 import java.security.cert.CertificateEncodingException;
36 40 import java.security.cert.X509Certificate;
37 41 import java.security.spec.ECGenParameterSpec;
... ... @@ -62,49 +66,23 @@ public class LwM2MServerSecurityInfoRepository {
62 66 bsServ.setPort(serverConfig.getPort());
63 67 bsServ.setSecurityHost(serverConfig.getSecureHost());
64 68 bsServ.setSecurityPort(serverConfig.getSecurePort());
65   - bsServ.setServerPublicKey(getPublicKey(serverConfig.getCertificateAlias(), this.serverConfig.getPublicX(), this.serverConfig.getPublicY()));
  69 + bsServ.setServerPublicKey(getPublicKey(serverConfig));
66 70 return bsServ;
67 71 }
68 72
69   - private String getPublicKey(String alias, String publicServerX, String publicServerY) {
70   - String publicKey = getServerPublicKeyX509(alias);
71   - return publicKey != null ? publicKey : getRPKPublicKey(publicServerX, publicServerY);
72   - }
73   -
74   - private String getServerPublicKeyX509(String alias) {
75   - try {
76   - X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(alias);
77   - return Hex.encodeHexString(serverCertificate.getEncoded());
78   - } catch (CertificateEncodingException | KeyStoreException e) {
79   - e.printStackTrace();
80   - }
81   - return null;
82   - }
83   -
84   - private String getRPKPublicKey(String publicServerX, String publicServerY) {
  73 + private String getPublicKey(LwM2MSecureServerConfig config) {
85 74 try {
86   - /** Get Elliptic Curve Parameter spec for secp256r1 */
87   - AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
88   - algoParameters.init(new ECGenParameterSpec("secp256r1"));
89   - ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
90   - if (publicServerX != null && !publicServerX.isEmpty() && publicServerY != null && !publicServerY.isEmpty()) {
91   - /** Get point values */
92   - byte[] publicX = Hex.decodeHex(publicServerX.toCharArray());
93   - byte[] publicY = Hex.decodeHex(publicServerY.toCharArray());
94   - /** Create key specs */
95   - KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
96   - parameterSpec);
97   - /** Get keys */
98   - PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
99   - if (publicKey != null && publicKey.getEncoded().length > 0) {
100   - return Hex.encodeHexString(publicKey.getEncoded());
101   - }
  75 + KeyStore keyStore = serverConfig.getKeyStoreValue();
  76 + if (keyStore != null) {
  77 + X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(config.getCertificateAlias());
  78 + return Hex.encodeHexString(serverCertificate.getPublicKey().getEncoded());
102 79 }
103   - } catch (GeneralSecurityException | IllegalArgumentException e) {
104   - log.error("[{}] Failed generate Server RPK for profile", e.getMessage());
105   - throw new RuntimeException(e);
  80 + } catch (Exception e) {
  81 + log.trace("Failed to fetch public key from key store!", e);
  82 +
106 83 }
107   - return null;
  84 + return "";
108 85 }
  86 +
109 87 }
110 88
... ...
... ... @@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
24 24 import org.springframework.context.MessageSource;
25 25 import org.springframework.context.annotation.Lazy;
26 26 import org.springframework.core.NestedRuntimeException;
  27 +import org.springframework.core.io.InputStreamSource;
  28 +import org.springframework.mail.javamail.JavaMailSender;
27 29 import org.springframework.mail.javamail.JavaMailSenderImpl;
28 30 import org.springframework.mail.javamail.MimeMessageHelper;
29 31 import org.springframework.stereotype.Service;
... ... @@ -45,8 +47,8 @@ import org.thingsboard.server.queue.usagestats.TbApiUsageClient;
45 47 import org.thingsboard.server.service.apiusage.TbApiUsageStateService;
46 48
47 49 import javax.annotation.PostConstruct;
48   -import javax.mail.MessagingException;
49 50 import javax.mail.internet.MimeMessage;
  51 +import java.io.ByteArrayInputStream;
50 52 import java.util.HashMap;
51 53 import java.util.Locale;
52 54 import java.util.Map;
... ... @@ -71,6 +73,9 @@ public class DefaultMailService implements MailService {
71 73 @Autowired
72 74 private TbApiUsageStateService apiUsageStateService;
73 75
  76 + @Autowired
  77 + private MailExecutorService mailExecutorService;
  78 +
74 79 private JavaMailSenderImpl mailSender;
75 80
76 81 private String mailFrom;
... ... @@ -220,6 +225,17 @@ public class DefaultMailService implements MailService {
220 225 }
221 226
222 227 @Override
  228 + public void sendResetPasswordEmailAsync(String passwordResetLink, String email) {
  229 + mailExecutorService.execute(() -> {
  230 + try {
  231 + this.sendResetPasswordEmail(passwordResetLink, email);
  232 + } catch (ThingsboardException e) {
  233 + log.error("Error occurred: {} ", e.getMessage());
  234 + }
  235 + });
  236 + }
  237 +
  238 + @Override
223 239 public void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException {
224 240
225 241 String subject = messages.getMessage("password.was.reset.subject", null, Locale.US);
... ... @@ -234,22 +250,47 @@ public class DefaultMailService implements MailService {
234 250 }
235 251
236 252 @Override
237   - public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body) throws MessagingException {
  253 + public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images) throws ThingsboardException {
  254 + sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, this.mailSender);
  255 + }
  256 +
  257 + @Override
  258 + public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException {
  259 + sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, javaMailSender);
  260 + }
  261 +
  262 + private void sendMail(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException {
238 263 if (apiUsageStateService.getApiUsageState(tenantId).isEmailSendEnabled()) {
239   - MimeMessage mailMsg = mailSender.createMimeMessage();
240   - MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8");
241   - helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from);
242   - helper.setTo(to.split("\\s*,\\s*"));
243   - if (!StringUtils.isBlank(cc)) {
244   - helper.setCc(cc.split("\\s*,\\s*"));
  264 + try {
  265 + MimeMessage mailMsg = javaMailSender.createMimeMessage();
  266 + boolean multipart = (images != null && !images.isEmpty());
  267 + MimeMessageHelper helper = new MimeMessageHelper(mailMsg, multipart, "UTF-8");
  268 + helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from);
  269 + helper.setTo(to.split("\\s*,\\s*"));
  270 + if (!StringUtils.isBlank(cc)) {
  271 + helper.setCc(cc.split("\\s*,\\s*"));
  272 + }
  273 + if (!StringUtils.isBlank(bcc)) {
  274 + helper.setBcc(bcc.split("\\s*,\\s*"));
  275 + }
  276 + helper.setSubject(subject);
  277 + helper.setText(body, isHtml);
  278 +
  279 + if (multipart) {
  280 + for (String imgId : images.keySet()) {
  281 + String imgValue = images.get(imgId);
  282 + String value = imgValue.replaceFirst("^data:image/[^;]*;base64,?", "");
  283 + byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(value);
  284 + String contentType = helper.getFileTypeMap().getContentType(imgId);
  285 + InputStreamSource iss = () -> new ByteArrayInputStream(bytes);
  286 + helper.addInline(imgId, iss, contentType);
  287 + }
  288 + }
  289 + javaMailSender.send(helper.getMimeMessage());
  290 + apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1);
  291 + } catch (Exception e) {
  292 + throw handleException(e);
245 293 }
246   - if (!StringUtils.isBlank(bcc)) {
247   - helper.setBcc(bcc.split("\\s*,\\s*"));
248   - }
249   - helper.setSubject(subject);
250   - helper.setText(body);
251   - mailSender.send(helper.getMimeMessage());
252   - apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1);
253 294 } else {
254 295 throw new RuntimeException("Email sending is disabled due to API limits!");
255 296 }
... ...
... ... @@ -32,6 +32,7 @@ import org.thingsboard.server.common.msg.queue.ServiceQueue;
32 32 import org.thingsboard.server.common.msg.queue.ServiceType;
33 33 import org.thingsboard.server.common.msg.queue.TbCallback;
34 34 import org.thingsboard.server.common.msg.queue.TbMsgCallback;
  35 +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
35 36 import org.thingsboard.server.common.stats.StatsFactory;
36 37 import org.thingsboard.server.common.transport.util.DataDecodingEncodingService;
37 38 import org.thingsboard.server.gen.transport.TransportProtos;
... ... @@ -64,12 +65,14 @@ import java.util.Collections;
64 65 import java.util.HashSet;
65 66 import java.util.List;
66 67 import java.util.Map;
  68 +import java.util.Queue;
67 69 import java.util.Set;
68 70 import java.util.UUID;
69 71 import java.util.concurrent.ConcurrentHashMap;
70 72 import java.util.concurrent.ConcurrentMap;
71 73 import java.util.concurrent.ExecutorService;
72 74 import java.util.concurrent.Executors;
  75 +import java.util.concurrent.ScheduledExecutorService;
73 76 import java.util.concurrent.TimeUnit;
74 77
75 78 @Service
... ... @@ -79,12 +82,15 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
79 82
80 83 public static final String SUCCESSFUL_STATUS = "successful";
81 84 public static final String FAILED_STATUS = "failed";
  85 + public static final String THREAD_TOPIC_SEPARATOR = " | ";
82 86 @Value("${queue.rule-engine.poll-interval}")
83 87 private long pollDuration;
84 88 @Value("${queue.rule-engine.pack-processing-timeout}")
85 89 private long packProcessingTimeout;
86 90 @Value("${queue.rule-engine.stats.enabled:true}")
87 91 private boolean statsEnabled;
  92 + @Value("${queue.rule-engine.prometheus-stats.enabled:false}")
  93 + boolean prometheusStatsEnabled;
88 94
89 95 private final StatsFactory statsFactory;
90 96 private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory;
... ... @@ -96,7 +102,9 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
96 102 private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>();
97 103 private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>();
98 104 private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>();
99   - private ExecutorService submitExecutor;
  105 + private final ConcurrentMap<String, TbTopicWithConsumerPerPartition> topicsConsumerPerPartition = new ConcurrentHashMap<>();
  106 + final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-service-submit-executor"));
  107 + final ScheduledExecutorService repartitionExecutor = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-repartition-executor"));
100 108
101 109 public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory,
102 110 TbRuleEngineSubmitStrategyFactory submitStrategyFactory,
... ... @@ -125,18 +133,19 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
125 133 super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer");
126 134 for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) {
127 135 consumerConfigurations.putIfAbsent(configuration.getName(), configuration);
128   - consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration));
129 136 consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory));
  137 + if (!configuration.isConsumerPerPartition()) {
  138 + consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration));
  139 + } else {
  140 + topicsConsumerPerPartition.computeIfAbsent(configuration.getName(), TbTopicWithConsumerPerPartition::new);
  141 + }
130 142 }
131   - submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-service-submit-executor"));
132 143 }
133 144
134 145 @PreDestroy
135 146 public void stop() {
136 147 super.destroy();
137   - if (submitExecutor != null) {
138   - submitExecutor.shutdownNow();
139   - }
  148 + submitExecutor.shutdownNow();
140 149 ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config));
141 150 }
142 151
... ... @@ -145,96 +154,185 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
145 154 if (event.getServiceType().equals(getServiceType())) {
146 155 ServiceQueue serviceQueue = event.getServiceQueueKey().getServiceQueue();
147 156 log.info("[{}] Subscribing to partitions: {}", serviceQueue.getQueue(), event.getPartitions());
148   - consumers.get(serviceQueue.getQueue()).subscribe(event.getPartitions());
  157 + if (!consumerConfigurations.get(serviceQueue.getQueue()).isConsumerPerPartition()) {
  158 + consumers.get(serviceQueue.getQueue()).subscribe(event.getPartitions());
  159 + } else {
  160 + log.info("[{}] Subscribing consumer per partition: {}", serviceQueue.getQueue(), event.getPartitions());
  161 + subscribeConsumerPerPartition(serviceQueue.getQueue(), event.getPartitions());
  162 + }
  163 + }
  164 + }
  165 +
  166 + void subscribeConsumerPerPartition(String queue, Set<TopicPartitionInfo> partitions) {
  167 + topicsConsumerPerPartition.get(queue).getSubscribeQueue().add(partitions);
  168 + scheduleTopicRepartition(queue);
  169 + }
  170 +
  171 + private void scheduleTopicRepartition(String queue) {
  172 + repartitionExecutor.schedule(() -> repartitionTopicWithConsumerPerPartition(queue), 1, TimeUnit.SECONDS);
  173 + }
  174 +
  175 + void repartitionTopicWithConsumerPerPartition(final String queueName) {
  176 + if (stopped) {
  177 + return;
  178 + }
  179 + TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.get(queueName);
  180 + Queue<Set<TopicPartitionInfo>> subscribeQueue = tbTopicWithConsumerPerPartition.getSubscribeQueue();
  181 + if (subscribeQueue.isEmpty()) {
  182 + return;
149 183 }
  184 + if (tbTopicWithConsumerPerPartition.getLock().tryLock()) {
  185 + try {
  186 + Set<TopicPartitionInfo> partitions = null;
  187 + while (!subscribeQueue.isEmpty()) {
  188 + partitions = subscribeQueue.poll();
  189 + }
  190 + if (partitions == null) {
  191 + return;
  192 + }
  193 +
  194 + Set<TopicPartitionInfo> addedPartitions = new HashSet<>(partitions);
  195 + ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = tbTopicWithConsumerPerPartition.getConsumers();
  196 + addedPartitions.removeAll(consumers.keySet());
  197 + log.info("calculated addedPartitions {}", addedPartitions);
  198 +
  199 + Set<TopicPartitionInfo> removedPartitions = new HashSet<>(consumers.keySet());
  200 + removedPartitions.removeAll(partitions);
  201 + log.info("calculated removedPartitions {}", removedPartitions);
  202 +
  203 + removedPartitions.forEach((tpi) -> {
  204 + removeConsumerForTopicByTpi(queueName, consumers, tpi);
  205 + });
  206 +
  207 + addedPartitions.forEach((tpi) -> {
  208 + log.info("[{}] Adding consumer for topic: {}", queueName, tpi);
  209 + TbRuleEngineQueueConfiguration configuration = consumerConfigurations.get(queueName);
  210 + TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration);
  211 + consumers.put(tpi, consumer);
  212 + launchConsumer(consumer, consumerConfigurations.get(queueName), consumerStats.get(queueName), "" + queueName + "-" + tpi.getPartition().orElse(-999999));
  213 + consumer.subscribe(Collections.singleton(tpi));
  214 + });
  215 +
  216 + } finally {
  217 + tbTopicWithConsumerPerPartition.getLock().unlock();
  218 + }
  219 + } else {
  220 + scheduleTopicRepartition(queueName); //reschedule later
  221 + }
  222 +
  223 + }
  224 +
  225 + void removeConsumerForTopicByTpi(String queue, ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers, TopicPartitionInfo tpi) {
  226 + log.info("[{}] Removing consumer for topic: {}", queue, tpi);
  227 + consumers.get(tpi).unsubscribe();
  228 + consumers.remove(tpi);
150 229 }
151 230
152 231 @Override
153 232 protected void launchMainConsumers() {
154   - consumers.forEach((queue, consumer) -> launchConsumer(consumer, consumerConfigurations.get(queue), consumerStats.get(queue)));
  233 + consumers.forEach((queue, consumer) -> launchConsumer(consumer, consumerConfigurations.get(queue), consumerStats.get(queue), queue));
155 234 }
156 235
157 236 @Override
158 237 protected void stopMainConsumers() {
159 238 consumers.values().forEach(TbQueueConsumer::unsubscribe);
  239 + topicsConsumerPerPartition.values().forEach(tbTopicWithConsumerPerPartition -> tbTopicWithConsumerPerPartition.getConsumers().keySet()
  240 + .forEach((tpi) -> removeConsumerForTopicByTpi(tbTopicWithConsumerPerPartition.getTopic(), tbTopicWithConsumerPerPartition.getConsumers(), tpi)));
  241 + }
  242 +
  243 + void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) {
  244 + consumersExecutor.execute(() -> consumerLoop(consumer, configuration, stats, threadSuffix));
160 245 }
161 246
162   - private void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats) {
163   - consumersExecutor.execute(() -> {
164   - Thread.currentThread().setName("" + Thread.currentThread().getName() + "-" + configuration.getName());
165   - while (!stopped) {
166   - try {
167   - List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration);
168   - if (msgs.isEmpty()) {
169   - continue;
  247 + void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) {
  248 + updateCurrentThreadName(threadSuffix);
  249 + while (!stopped && !consumer.isStopped()) {
  250 + try {
  251 + List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration);
  252 + if (msgs.isEmpty()) {
  253 + continue;
  254 + }
  255 + final TbRuleEngineSubmitStrategy submitStrategy = getSubmitStrategy(configuration);
  256 + final TbRuleEngineProcessingStrategy ackStrategy = getAckStrategy(configuration);
  257 + submitStrategy.init(msgs);
  258 + while (!stopped) {
  259 + TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy);
  260 + submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> submitMessage(configuration, stats, ctx, id, msg)));
  261 +
  262 + final boolean timeout = !ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS);
  263 +
  264 + TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx);
  265 + if (timeout) {
  266 + printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout");
170 267 }
171   - TbRuleEngineSubmitStrategy submitStrategy = submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy());
172   - TbRuleEngineProcessingStrategy ackStrategy = processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy());
173   -
174   - submitStrategy.init(msgs);
175   -
176   - while (!stopped) {
177   - TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy);
178   - submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> {
179   - log.trace("[{}] Creating callback for message: {}", id, msg.getValue());
180   - ToRuleEngineMsg toRuleEngineMsg = msg.getValue();
181   - TenantId tenantId = new TenantId(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB()));
182   - TbMsgCallback callback = statsEnabled ?
183   - new TbMsgPackCallback(id, tenantId, ctx, stats.getTimer(tenantId, SUCCESSFUL_STATUS), stats.getTimer(tenantId, FAILED_STATUS)) :
184   - new TbMsgPackCallback(id, tenantId, ctx);
185   - try {
186   - if (!toRuleEngineMsg.getTbMsg().isEmpty()) {
187   - forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback);
188   - } else {
189   - callback.onSuccess();
190   - }
191   - } catch (Exception e) {
192   - callback.onFailure(new RuleEngineException(e.getMessage()));
193   - }
194   - }));
195   -
196   - boolean timeout = false;
197   - if (!ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS)) {
198   - timeout = true;
199   - }
200   -
201   - TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx);
202   - if (timeout) {
203   - printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout");
204   - }
205   - if (!ctx.getFailedMap().isEmpty()) {
206   - printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed");
207   - }
208   - ctx.printProfilerStats();
209   -
210   - TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
211   - if (statsEnabled) {
212   - stats.log(result, decision.isCommit());
213   - }
214   -
215   - ctx.cleanup();
216   -
217   - if (decision.isCommit()) {
218   - submitStrategy.stop();
219   - break;
220   - } else {
221   - submitStrategy.update(decision.getReprocessMap());
222   - }
  268 + if (!ctx.getFailedMap().isEmpty()) {
  269 + printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed");
223 270 }
224   - consumer.commit();
225   - } catch (Exception e) {
226   - if (!stopped) {
227   - log.warn("Failed to process messages from queue.", e);
228   - try {
229   - Thread.sleep(pollDuration);
230   - } catch (InterruptedException e2) {
231   - log.trace("Failed to wait until the server has capacity to handle new requests", e2);
232   - }
  271 + ctx.printProfilerStats();
  272 +
  273 + TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
  274 + if (statsEnabled) {
  275 + stats.log(result, decision.isCommit());
  276 + }
  277 +
  278 + ctx.cleanup();
  279 +
  280 + if (decision.isCommit()) {
  281 + submitStrategy.stop();
  282 + break;
  283 + } else {
  284 + submitStrategy.update(decision.getReprocessMap());
  285 + }
  286 + }
  287 + consumer.commit();
  288 + } catch (Exception e) {
  289 + if (!stopped) {
  290 + log.warn("Failed to process messages from queue.", e);
  291 + try {
  292 + Thread.sleep(pollDuration);
  293 + } catch (InterruptedException e2) {
  294 + log.trace("Failed to wait until the server has capacity to handle new requests", e2);
233 295 }
234 296 }
235 297 }
236   - log.info("TB Rule Engine Consumer stopped.");
237   - });
  298 + }
  299 + log.info("TB Rule Engine Consumer stopped.");
  300 + }
  301 +
  302 + void updateCurrentThreadName(String threadSuffix) {
  303 + String name = Thread.currentThread().getName();
  304 + int spliteratorIndex = name.indexOf(THREAD_TOPIC_SEPARATOR);
  305 + if (spliteratorIndex > 0) {
  306 + name = name.substring(0, spliteratorIndex);
  307 + }
  308 + name = name + THREAD_TOPIC_SEPARATOR + threadSuffix;
  309 + Thread.currentThread().setName(name);
  310 + }
  311 +
  312 + TbRuleEngineProcessingStrategy getAckStrategy(TbRuleEngineQueueConfiguration configuration) {
  313 + return processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy());
  314 + }
  315 +
  316 + TbRuleEngineSubmitStrategy getSubmitStrategy(TbRuleEngineQueueConfiguration configuration) {
  317 + return submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy());
  318 + }
  319 +
  320 + void submitMessage(TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, TbMsgPackProcessingContext ctx, UUID id, TbProtoQueueMsg<ToRuleEngineMsg> msg) {
  321 + log.trace("[{}] Creating callback for topic {} message: {}", id, configuration.getName(), msg.getValue());
  322 + ToRuleEngineMsg toRuleEngineMsg = msg.getValue();
  323 + TenantId tenantId = new TenantId(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB()));
  324 + TbMsgCallback callback = prometheusStatsEnabled ?
  325 + new TbMsgPackCallback(id, tenantId, ctx, stats.getTimer(tenantId, SUCCESSFUL_STATUS), stats.getTimer(tenantId, FAILED_STATUS)) :
  326 + new TbMsgPackCallback(id, tenantId, ctx);
  327 + try {
  328 + if (toRuleEngineMsg.getTbMsg() != null && !toRuleEngineMsg.getTbMsg().isEmpty()) {
  329 + forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback);
  330 + } else {
  331 + callback.onSuccess();
  332 + }
  333 + } catch (Exception e) {
  334 + callback.onFailure(new RuleEngineException(e.getMessage()));
  335 + }
238 336 }
239 337
240 338 private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) {
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.queue;
  17 +
  18 +import lombok.Builder;
  19 +import lombok.Data;
  20 +import lombok.Getter;
  21 +import lombok.RequiredArgsConstructor;
  22 +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
  23 +import org.thingsboard.server.gen.transport.TransportProtos;
  24 +import org.thingsboard.server.queue.TbQueueConsumer;
  25 +import org.thingsboard.server.queue.common.TbProtoQueueMsg;
  26 +
  27 +import java.util.Collections;
  28 +import java.util.Map;
  29 +import java.util.Queue;
  30 +import java.util.Set;
  31 +import java.util.concurrent.ConcurrentHashMap;
  32 +import java.util.concurrent.ConcurrentLinkedQueue;
  33 +import java.util.concurrent.ConcurrentMap;
  34 +import java.util.concurrent.locks.ReentrantLock;
  35 +
  36 +@RequiredArgsConstructor
  37 +@Data
  38 +public class TbTopicWithConsumerPerPartition {
  39 + private final String topic;
  40 + @Getter
  41 + private final ReentrantLock lock = new ReentrantLock(); //NonfairSync
  42 + private volatile Set<TopicPartitionInfo> partitions = Collections.emptySet();
  43 + private final ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>();
  44 + private final Queue<Set<TopicPartitionInfo>> subscribeQueue = new ConcurrentLinkedQueue<>();
  45 +}
... ...
... ... @@ -86,7 +86,10 @@ public class DefaultTbResourceService implements TbResourceService {
86 86 } else {
87 87 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
88 88 }
89   - } catch (InvalidDDFFileException | IOException e) {
  89 + } catch (InvalidDDFFileException e) {
  90 + log.error("Failed to parse file {}", resource.getFileName(), e);
  91 + throw new DataValidationException("Failed to parse file " + resource.getFileName());
  92 + } catch (IOException e) {
90 93 throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL);
91 94 }
92 95 if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource, true) == null) {
... ... @@ -194,8 +197,7 @@ public class DefaultTbResourceService implements TbResourceService {
194 197 if (isSave) {
195 198 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
196 199 resources.add(lwM2MResourceObserve);
197   - }
198   - else if (v.operations.isReadable()) {
  200 + } else if (v.operations.isReadable()) {
199 201 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
200 202 resources.add(lwM2MResourceObserve);
201 203 }
... ... @@ -204,8 +206,7 @@ public class DefaultTbResourceService implements TbResourceService {
204 206 instance.setResources(resources.toArray(LwM2mResourceObserve[]::new));
205 207 lwM2mObject.setInstances(new LwM2mInstance[]{instance});
206 208 return lwM2mObject;
207   - }
208   - else {
  209 + } else {
209 210 return null;
210 211 }
211 212 }
... ...
... ... @@ -157,6 +157,7 @@ public class DefaultTbCoreDeviceRpcService implements TbCoreDeviceRpcService {
157 157 metaData.putValue("originServiceId", serviceId);
158 158 metaData.putValue("expirationTime", Long.toString(msg.getExpirationTime()));
159 159 metaData.putValue("oneway", Boolean.toString(msg.isOneway()));
  160 + metaData.putValue("persisted", Boolean.toString(msg.isPersisted()));
160 161
161 162 Device device = deviceService.findDeviceById(msg.getTenantId(), msg.getDeviceId());
162 163 if (device != null) {
... ...
... ... @@ -100,7 +100,7 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi
100 100 @Override
101 101 public void sendRpcRequestToDevice(RuleEngineDeviceRpcRequest src, Consumer<RuleEngineDeviceRpcResponse> consumer) {
102 102 ToDeviceRpcRequest request = new ToDeviceRpcRequest(src.getRequestUUID(), src.getTenantId(), src.getDeviceId(),
103   - src.isOneway(), src.getExpirationTime(), new ToDeviceRpcRequestBody(src.getMethod(), src.getBody()));
  103 + src.isOneway(), src.getExpirationTime(), new ToDeviceRpcRequestBody(src.getMethod(), src.getBody()), src.isPersisted());
104 104 forwardRpcRequestToDeviceActor(request, response -> {
105 105 if (src.isRestApiCall()) {
106 106 sendRpcResponseToTbCore(src.getOriginServiceId(), response);
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.rpc;
  17 +
  18 +import com.fasterxml.jackson.databind.JsonNode;
  19 +import lombok.RequiredArgsConstructor;
  20 +import lombok.extern.slf4j.Slf4j;
  21 +import org.springframework.stereotype.Service;
  22 +import org.thingsboard.common.util.JacksonUtil;
  23 +import org.thingsboard.server.common.data.id.DeviceId;
  24 +import org.thingsboard.server.common.data.id.RpcId;
  25 +import org.thingsboard.server.common.data.id.TenantId;
  26 +import org.thingsboard.server.common.data.page.PageData;
  27 +import org.thingsboard.server.common.data.page.PageLink;
  28 +import org.thingsboard.server.common.data.rpc.Rpc;
  29 +import org.thingsboard.server.common.data.rpc.RpcStatus;
  30 +import org.thingsboard.server.common.msg.TbMsg;
  31 +import org.thingsboard.server.common.msg.TbMsgMetaData;
  32 +import org.thingsboard.server.dao.rpc.RpcService;
  33 +import org.thingsboard.server.queue.util.TbCoreComponent;
  34 +import org.thingsboard.server.service.queue.TbClusterService;
  35 +
  36 +@TbCoreComponent
  37 +@Service
  38 +@RequiredArgsConstructor
  39 +@Slf4j
  40 +public class TbRpcService {
  41 + private final RpcService rpcService;
  42 + private final TbClusterService tbClusterService;
  43 +
  44 + public Rpc save(TenantId tenantId, Rpc rpc) {
  45 + Rpc saved = rpcService.save(rpc);
  46 + pushRpcMsgToRuleEngine(tenantId, saved);
  47 + return saved;
  48 + }
  49 +
  50 + public void save(TenantId tenantId, RpcId rpcId, RpcStatus newStatus, JsonNode response) {
  51 + Rpc foundRpc = rpcService.findById(tenantId, rpcId);
  52 + if (foundRpc != null) {
  53 + foundRpc.setStatus(newStatus);
  54 + if (response != null) {
  55 + foundRpc.setResponse(response);
  56 + }
  57 + Rpc saved = rpcService.save(foundRpc);
  58 + pushRpcMsgToRuleEngine(tenantId, saved);
  59 + } else {
  60 + log.warn("[{}] Failed to update RPC status because RPC was already deleted", rpcId);
  61 + }
  62 + }
  63 +
  64 + private void pushRpcMsgToRuleEngine(TenantId tenantId, Rpc rpc) {
  65 + TbMsg msg = TbMsg.newMsg("RPC_" + rpc.getStatus().name(), rpc.getDeviceId(), TbMsgMetaData.EMPTY, JacksonUtil.toString(rpc));
  66 + tbClusterService.pushMsgToRuleEngine(tenantId, rpc.getId(), msg, null);
  67 + }
  68 +
  69 + public Rpc findRpcById(TenantId tenantId, RpcId rpcId) {
  70 + return rpcService.findById(tenantId, rpcId);
  71 + }
  72 +
  73 + public PageData<Rpc> findAllByDeviceIdAndStatus(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink) {
  74 + return rpcService.findAllByDeviceIdAndStatus(tenantId, deviceId, rpcStatus, pageLink);
  75 + }
  76 +
  77 +}
... ...
... ... @@ -47,11 +47,13 @@ import org.thingsboard.server.common.data.id.EntityId;
47 47 import org.thingsboard.server.common.data.id.EntityIdFactory;
48 48 import org.thingsboard.server.common.data.id.EntityViewId;
49 49 import org.thingsboard.server.common.data.id.OtaPackageId;
  50 +import org.thingsboard.server.common.data.id.RpcId;
50 51 import org.thingsboard.server.common.data.id.RuleChainId;
51 52 import org.thingsboard.server.common.data.id.RuleNodeId;
52 53 import org.thingsboard.server.common.data.id.TbResourceId;
53 54 import org.thingsboard.server.common.data.id.TenantId;
54 55 import org.thingsboard.server.common.data.id.UserId;
  56 +import org.thingsboard.server.common.data.rpc.Rpc;
55 57 import org.thingsboard.server.common.data.rule.RuleChain;
56 58 import org.thingsboard.server.common.data.rule.RuleNode;
57 59 import org.thingsboard.server.controller.HttpValidationCallback;
... ... @@ -65,6 +67,7 @@ import org.thingsboard.server.dao.entityview.EntityViewService;
65 67 import org.thingsboard.server.dao.exception.IncorrectParameterException;
66 68 import org.thingsboard.server.dao.ota.OtaPackageService;
67 69 import org.thingsboard.server.dao.resource.ResourceService;
  70 +import org.thingsboard.server.dao.rpc.RpcService;
68 71 import org.thingsboard.server.dao.rule.RuleChainService;
69 72 import org.thingsboard.server.dao.tenant.TenantService;
70 73 import org.thingsboard.server.dao.usagerecord.ApiUsageStateService;
... ... @@ -137,6 +140,9 @@ public class AccessValidator {
137 140 @Autowired
138 141 protected OtaPackageService otaPackageService;
139 142
  143 + @Autowired
  144 + protected RpcService rpcService;
  145 +
140 146 private ExecutorService executor;
141 147
142 148 @PostConstruct
... ... @@ -235,6 +241,9 @@ public class AccessValidator {
235 241 case OTA_PACKAGE:
236 242 validateOtaPackage(currentUser, operation, entityId, callback);
237 243 return;
  244 + case RPC:
  245 + validateRpc(currentUser, operation, entityId, callback);
  246 + return;
238 247 default:
239 248 //TODO: add support of other entities
240 249 throw new IllegalStateException("Not Implemented!");
... ... @@ -261,6 +270,22 @@ public class AccessValidator {
261 270 }
262 271 }
263 272
  273 + private void validateRpc(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
  274 + ListenableFuture<Rpc> rpcFurure = rpcService.findRpcByIdAsync(currentUser.getTenantId(), new RpcId(entityId.getId()));
  275 + Futures.addCallback(rpcFurure, getCallback(callback, rpc -> {
  276 + if (rpc == null) {
  277 + return ValidationResult.entityNotFound("Rpc with requested id wasn't found!");
  278 + } else {
  279 + try {
  280 + accessControlService.checkPermission(currentUser, Resource.RPC, operation, entityId, rpc);
  281 + } catch (ThingsboardException e) {
  282 + return ValidationResult.accessDenied(e.getMessage());
  283 + }
  284 + return ValidationResult.ok(rpc);
  285 + }
  286 + }), executor);
  287 + }
  288 +
264 289 private void validateDeviceProfile(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
265 290 if (currentUser.isSystemAdmin()) {
266 291 callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION));
... ...
... ... @@ -204,11 +204,7 @@ public abstract class AbstractOAuth2ClientMapper {
204 204 }
205 205
206 206 private Optional<DashboardId> getDashboardId(TenantId tenantId, String dashboardName) {
207   - PageLink searchTextLink = new PageLink(1, 0, dashboardName);
208   - PageData<DashboardInfo> dashboardsPage = dashboardService.findDashboardsByTenantId(tenantId, searchTextLink);
209   - return dashboardsPage.getData().stream()
210   - .findAny()
211   - .map(IdBased::getId);
  207 + return Optional.ofNullable(dashboardService.findFirstDashboardInfoByTenantIdAndName(tenantId, dashboardName)).map(IdBased::getId);
212 208 }
213 209
214 210 private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) {
... ...
... ... @@ -41,6 +41,7 @@ public class CustomerUserPermissions extends AbstractPermissions {
41 41 put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker);
42 42 put(Resource.WIDGET_TYPE, widgetsPermissionChecker);
43 43 put(Resource.EDGE, customerEntityPermissionChecker);
  44 + put(Resource.RPC, rpcPermissionChecker);
44 45 }
45 46
46 47 private static final PermissionChecker customerEntityPermissionChecker =
... ... @@ -138,4 +139,22 @@ public class CustomerUserPermissions extends AbstractPermissions {
138 139 }
139 140
140 141 };
  142 +
  143 + private static final PermissionChecker rpcPermissionChecker = new PermissionChecker.GenericPermissionChecker(Operation.READ) {
  144 +
  145 + @Override
  146 + @SuppressWarnings("unchecked")
  147 + public boolean hasPermission(SecurityUser user, Operation operation, EntityId entityId, HasTenantId entity) {
  148 + if (!super.hasPermission(user, operation, entityId, entity)) {
  149 + return false;
  150 + }
  151 + if (entity.getTenantId() == null || entity.getTenantId().isNullUid()) {
  152 + return true;
  153 + }
  154 + if (!user.getTenantId().equals(entity.getTenantId())) {
  155 + return false;
  156 + }
  157 + return true;
  158 + }
  159 + };
141 160 }
... ...
... ... @@ -39,7 +39,8 @@ public enum Resource {
39 39 API_USAGE_STATE(EntityType.API_USAGE_STATE),
40 40 TB_RESOURCE(EntityType.TB_RESOURCE),
41 41 OTA_PACKAGE(EntityType.OTA_PACKAGE),
42   - EDGE(EntityType.EDGE);
  42 + EDGE(EntityType.EDGE),
  43 + RPC(EntityType.RPC);
43 44
44 45 private final EntityType entityType;
45 46
... ...
... ... @@ -44,6 +44,7 @@ public class TenantAdminPermissions extends AbstractPermissions {
44 44 put(Resource.TB_RESOURCE, tbResourcePermissionChecker);
45 45 put(Resource.OTA_PACKAGE, tenantEntityPermissionChecker);
46 46 put(Resource.EDGE, tenantEntityPermissionChecker);
  47 + put(Resource.RPC, tenantEntityPermissionChecker);
47 48 }
48 49
49 50 public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() {
... ...
... ... @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.Futures;
22 22 import com.google.common.util.concurrent.ListenableFuture;
23 23 import com.google.common.util.concurrent.MoreExecutors;
24 24 import com.google.protobuf.ByteString;
  25 +import lombok.RequiredArgsConstructor;
25 26 import lombok.extern.slf4j.Slf4j;
26 27 import org.springframework.stereotype.Service;
27 28 import org.springframework.util.StringUtils;
... ... @@ -40,14 +41,16 @@ import org.thingsboard.server.common.data.TbResource;
40 41 import org.thingsboard.server.common.data.TenantProfile;
41 42 import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
42 43 import org.thingsboard.server.common.data.device.credentials.ProvisionDeviceCredentialsData;
  44 +import org.thingsboard.server.common.data.device.data.Lwm2mDeviceTransportConfiguration;
  45 +import org.thingsboard.server.common.data.device.data.PowerMode;
43 46 import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
44   -import org.thingsboard.server.common.data.ota.OtaPackageType;
45   -import org.thingsboard.server.common.data.ota.OtaPackageUtil;
46 47 import org.thingsboard.server.common.data.id.CustomerId;
47 48 import org.thingsboard.server.common.data.id.DeviceId;
48 49 import org.thingsboard.server.common.data.id.DeviceProfileId;
49 50 import org.thingsboard.server.common.data.id.OtaPackageId;
50 51 import org.thingsboard.server.common.data.id.TenantId;
  52 +import org.thingsboard.server.common.data.ota.OtaPackageType;
  53 +import org.thingsboard.server.common.data.ota.OtaPackageUtil;
51 54 import org.thingsboard.server.common.data.page.PageData;
52 55 import org.thingsboard.server.common.data.page.PageLink;
53 56 import org.thingsboard.server.common.data.relation.EntityRelation;
... ... @@ -108,6 +111,7 @@ import java.util.stream.Collectors;
108 111 @Slf4j
109 112 @Service
110 113 @TbCoreComponent
  114 +@RequiredArgsConstructor
111 115 public class DefaultTransportApiService implements TransportApiService {
112 116
113 117 private static final ObjectMapper mapper = new ObjectMapper();
... ... @@ -129,28 +133,6 @@ public class DefaultTransportApiService implements TransportApiService {
129 133
130 134 private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>();
131 135
132   - public DefaultTransportApiService(TbDeviceProfileCache deviceProfileCache,
133   - TbTenantProfileCache tenantProfileCache, TbApiUsageStateService apiUsageStateService, DeviceService deviceService,
134   - RelationService relationService, DeviceCredentialsService deviceCredentialsService,
135   - DeviceStateService deviceStateService, DbCallbackExecutorService dbCallbackExecutorService,
136   - TbClusterService tbClusterService, DataDecodingEncodingService dataDecodingEncodingService,
137   - DeviceProvisionService deviceProvisionService, TbResourceService resourceService, OtaPackageService otaPackageService, OtaPackageDataCache otaPackageDataCache) {
138   - this.deviceProfileCache = deviceProfileCache;
139   - this.tenantProfileCache = tenantProfileCache;
140   - this.apiUsageStateService = apiUsageStateService;
141   - this.deviceService = deviceService;
142   - this.relationService = relationService;
143   - this.deviceCredentialsService = deviceCredentialsService;
144   - this.deviceStateService = deviceStateService;
145   - this.dbCallbackExecutorService = dbCallbackExecutorService;
146   - this.tbClusterService = tbClusterService;
147   - this.dataDecodingEncodingService = dataDecodingEncodingService;
148   - this.deviceProvisionService = deviceProvisionService;
149   - this.resourceService = resourceService;
150   - this.otaPackageService = otaPackageService;
151   - this.otaPackageDataCache = otaPackageDataCache;
152   - }
153   -
154 136 @Override
155 137 public ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> handle(TbProtoQueueMsg<TransportApiRequestMsg> tbProtoQueueMsg) {
156 138 TransportApiRequestMsg transportApiRequestMsg = tbProtoQueueMsg.getValue();
... ... @@ -479,7 +461,14 @@ public class DefaultTransportApiService implements TransportApiService {
479 461 }
480 462
481 463 private DeviceInfoProto getDeviceInfoProto(Device device) throws JsonProcessingException {
482   - return DeviceInfoProto.newBuilder()
  464 + PowerMode powerMode = null;
  465 + switch (device.getDeviceData().getTransportConfiguration().getType()) {
  466 + case LWM2M:
  467 + powerMode = ((Lwm2mDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration()).getPowerMode();
  468 + break;
  469 + }
  470 +
  471 + DeviceInfoProto.Builder builder = DeviceInfoProto.newBuilder()
483 472 .setTenantIdMSB(device.getTenantId().getId().getMostSignificantBits())
484 473 .setTenantIdLSB(device.getTenantId().getId().getLeastSignificantBits())
485 474 .setCustomerIdMSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getMostSignificantBits()).orElse(0L))
... ... @@ -490,8 +479,11 @@ public class DefaultTransportApiService implements TransportApiService {
490 479 .setDeviceType(device.getType())
491 480 .setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits())
492 481 .setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits())
493   - .setAdditionalInfo(mapper.writeValueAsString(device.getAdditionalInfo()))
494   - .build();
  482 + .setAdditionalInfo(mapper.writeValueAsString(device.getAdditionalInfo()));
  483 + if (powerMode != null) {
  484 + builder.setPowerMode(powerMode.name());
  485 + }
  486 + return builder.build();
495 487 }
496 488
497 489 private ListenableFuture<TransportApiResponseMsg> getEmptyTransportApiResponseFuture() {
... ...
... ... @@ -15,8 +15,13 @@
15 15 */
16 16 package org.thingsboard.server.service.ttl;
17 17
  18 +import lombok.RequiredArgsConstructor;
18 19 import lombok.extern.slf4j.Slf4j;
  20 +import org.springframework.beans.factory.annotation.Autowired;
19 21 import org.springframework.beans.factory.annotation.Value;
  22 +import org.thingsboard.server.common.data.id.TenantId;
  23 +import org.thingsboard.server.common.msg.queue.ServiceType;
  24 +import org.thingsboard.server.queue.discovery.PartitionService;
20 25
21 26 import java.sql.Connection;
22 27 import java.sql.DriverManager;
... ... @@ -27,43 +32,12 @@ import java.sql.Statement;
27 32
28 33
29 34 @Slf4j
  35 +@RequiredArgsConstructor
30 36 public abstract class AbstractCleanUpService {
31 37
32   - @Value("${spring.datasource.url}")
33   - protected String dbUrl;
  38 + private final PartitionService partitionService;
34 39
35   - @Value("${spring.datasource.username}")
36   - protected String dbUserName;
37   -
38   - @Value("${spring.datasource.password}")
39   - protected String dbPassword;
40   -
41   - protected long executeQuery(Connection conn, String query) throws SQLException {
42   - try (Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(query)) {
43   - if (log.isDebugEnabled()) {
44   - getWarnings(statement);
45   - }
46   - resultSet.next();
47   - return resultSet.getLong(1);
48   - }
49   - }
50   -
51   - protected void getWarnings(Statement statement) throws SQLException {
52   - SQLWarning warnings = statement.getWarnings();
53   - if (warnings != null) {
54   - log.debug("{}", warnings.getMessage());
55   - SQLWarning nextWarning = warnings.getNextWarning();
56   - while (nextWarning != null) {
57   - log.debug("{}", nextWarning.getMessage());
58   - nextWarning = nextWarning.getNextWarning();
59   - }
60   - }
  40 + protected boolean isSystemTenantPartitionMine(){
  41 + return partitionService.resolve(ServiceType.TB_CORE, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isMyPartition();
61 42 }
62   -
63   - protected abstract void doCleanUp(Connection connection) throws SQLException;
64   -
65   - protected Connection getConnection() throws SQLException {
66   - return DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
67   - }
68   -
69 43 }
... ...
application/src/main/java/org/thingsboard/server/service/ttl/AlarmsCleanUpService.java renamed from application/src/main/java/org/thingsboard/server/service/ttl/alarms/AlarmsCleanUpService.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.ttl.alarms;
  16 +package org.thingsboard.server.service.ttl;
17 17
18 18 import lombok.RequiredArgsConstructor;
19 19 import lombok.extern.slf4j.Slf4j;
... ... @@ -52,6 +52,7 @@ import java.util.concurrent.TimeUnit;
52 52 @Slf4j
53 53 @RequiredArgsConstructor
54 54 public class AlarmsCleanUpService {
  55 +
55 56 @Value("${sql.ttl.alarms.removal_batch_size}")
56 57 private Integer removalBatchSize;
57 58
... ...
application/src/main/java/org/thingsboard/server/service/ttl/EdgeEventsCleanUpService.java renamed from application/src/main/java/org/thingsboard/server/service/ttl/edge/EdgeEventsCleanUpService.java
... ... @@ -13,20 +13,17 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.ttl.edge;
  16 +package org.thingsboard.server.service.ttl;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.scheduling.annotation.Scheduled;
21 21 import org.springframework.stereotype.Service;
22   -import org.thingsboard.server.dao.util.PsqlDao;
23   -import org.thingsboard.server.service.ttl.AbstractCleanUpService;
  22 +import org.thingsboard.server.dao.edge.EdgeEventService;
  23 +import org.thingsboard.server.queue.discovery.PartitionService;
  24 +import org.thingsboard.server.queue.util.TbCoreComponent;
24 25
25   -import java.sql.Connection;
26   -import java.sql.DriverManager;
27   -import java.sql.SQLException;
28   -
29   -@PsqlDao
  26 +@TbCoreComponent
30 27 @Slf4j
31 28 @Service
32 29 public class EdgeEventsCleanUpService extends AbstractCleanUpService {
... ... @@ -37,20 +34,18 @@ public class EdgeEventsCleanUpService extends AbstractCleanUpService {
37 34 @Value("${sql.ttl.edge_events.enabled}")
38 35 private boolean ttlTaskExecutionEnabled;
39 36
  37 + private final EdgeEventService edgeEventService;
  38 +
  39 + public EdgeEventsCleanUpService(PartitionService partitionService, EdgeEventService edgeEventService) {
  40 + super(partitionService);
  41 + this.edgeEventService = edgeEventService;
  42 + }
  43 +
40 44 @Scheduled(initialDelayString = "${sql.ttl.edge_events.execution_interval_ms}", fixedDelayString = "${sql.ttl.edge_events.execution_interval_ms}")
41 45 public void cleanUp() {
42   - if (ttlTaskExecutionEnabled) {
43   - try (Connection conn = getConnection()) {
44   - doCleanUp(conn);
45   - } catch (SQLException e) {
46   - log.error("SQLException occurred during TTL task execution ", e);
47   - }
  46 + if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) {
  47 + edgeEventService.cleanupEvents(ttl);
48 48 }
49 49 }
50 50
51   - @Override
52   - protected void doCleanUp(Connection connection) throws SQLException {
53   - long totalEdgeEventsRemoved = executeQuery(connection, "call cleanup_edge_events_by_ttl(" + ttl + ", 0);");
54   - log.info("Total edge events removed by TTL: [{}]", totalEdgeEventsRemoved);
55   - }
56 51 }
... ...
application/src/main/java/org/thingsboard/server/service/ttl/EventsCleanUpService.java renamed from application/src/main/java/org/thingsboard/server/service/ttl/events/EventsCleanUpService.java
... ... @@ -13,20 +13,18 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.ttl.events;
  16 +package org.thingsboard.server.service.ttl;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.scheduling.annotation.Scheduled;
21 21 import org.springframework.stereotype.Service;
22   -import org.thingsboard.server.dao.util.PsqlDao;
  22 +import org.thingsboard.server.dao.event.EventService;
  23 +import org.thingsboard.server.queue.discovery.PartitionService;
  24 +import org.thingsboard.server.queue.util.TbCoreComponent;
23 25 import org.thingsboard.server.service.ttl.AbstractCleanUpService;
24 26
25   -import java.sql.Connection;
26   -import java.sql.DriverManager;
27   -import java.sql.SQLException;
28   -
29   -@PsqlDao
  27 +@TbCoreComponent
30 28 @Slf4j
31 29 @Service
32 30 public class EventsCleanUpService extends AbstractCleanUpService {
... ... @@ -40,20 +38,18 @@ public class EventsCleanUpService extends AbstractCleanUpService {
40 38 @Value("${sql.ttl.events.enabled}")
41 39 private boolean ttlTaskExecutionEnabled;
42 40
  41 + private final EventService eventService;
  42 +
  43 + public EventsCleanUpService(PartitionService partitionService, EventService eventService) {
  44 + super(partitionService);
  45 + this.eventService = eventService;
  46 + }
  47 +
43 48 @Scheduled(initialDelayString = "${sql.ttl.events.execution_interval_ms}", fixedDelayString = "${sql.ttl.events.execution_interval_ms}")
44 49 public void cleanUp() {
45   - if (ttlTaskExecutionEnabled) {
46   - try (Connection conn = getConnection()) {
47   - doCleanUp(conn);
48   - } catch (SQLException e) {
49   - log.error("SQLException occurred during TTL task execution ", e);
50   - }
  50 + if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) {
  51 + eventService.cleanupEvents(ttl, debugTtl);
51 52 }
52 53 }
53 54
54   - @Override
55   - protected void doCleanUp(Connection connection) throws SQLException {
56   - long totalEventsRemoved = executeQuery(connection, "call cleanup_events_by_ttl(" + ttl + ", " + debugTtl + ", 0);");
57   - log.info("Total events removed by TTL: [{}]", totalEventsRemoved);
58   - }
59 55 }
\ No newline at end of file
... ...
application/src/main/java/org/thingsboard/server/service/ttl/TimeseriesCleanUpService.java renamed from application/src/main/java/org/thingsboard/server/service/ttl/timeseries/AbstractTimeseriesCleanUpService.java
... ... @@ -13,19 +13,21 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.ttl.timeseries;
  16 +package org.thingsboard.server.service.ttl;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.scheduling.annotation.Scheduled;
  21 +import org.springframework.stereotype.Service;
  22 +import org.thingsboard.server.dao.timeseries.TimeseriesService;
  23 +import org.thingsboard.server.queue.discovery.PartitionService;
  24 +import org.thingsboard.server.queue.util.TbCoreComponent;
21 25 import org.thingsboard.server.service.ttl.AbstractCleanUpService;
22 26
23   -import java.sql.Connection;
24   -import java.sql.DriverManager;
25   -import java.sql.SQLException;
26   -
  27 +@TbCoreComponent
27 28 @Slf4j
28   -public abstract class AbstractTimeseriesCleanUpService extends AbstractCleanUpService {
  29 +@Service
  30 +public class TimeseriesCleanUpService extends AbstractCleanUpService {
29 31
30 32 @Value("${sql.ttl.ts.ts_key_value_ttl}")
31 33 protected long systemTtl;
... ... @@ -33,14 +35,17 @@ public abstract class AbstractTimeseriesCleanUpService extends AbstractCleanUpSe
33 35 @Value("${sql.ttl.ts.enabled}")
34 36 private boolean ttlTaskExecutionEnabled;
35 37
  38 + private final TimeseriesService timeseriesService;
  39 +
  40 + public TimeseriesCleanUpService(PartitionService partitionService, TimeseriesService timeseriesService) {
  41 + super(partitionService);
  42 + this.timeseriesService = timeseriesService;
  43 + }
  44 +
36 45 @Scheduled(initialDelayString = "${sql.ttl.ts.execution_interval_ms}", fixedDelayString = "${sql.ttl.ts.execution_interval_ms}")
37 46 public void cleanUp() {
38   - if (ttlTaskExecutionEnabled) {
39   - try (Connection conn = getConnection()) {
40   - doCleanUp(conn);
41   - } catch (SQLException e) {
42   - log.error("SQLException occurred during TTL task execution ", e);
43   - }
  47 + if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) {
  48 + timeseriesService.cleanup(systemTtl);
44 49 }
45 50 }
46 51
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.service.ttl.rpc;
  17 +
  18 +import lombok.RequiredArgsConstructor;
  19 +import lombok.extern.slf4j.Slf4j;
  20 +import org.springframework.beans.factory.annotation.Value;
  21 +import org.springframework.scheduling.annotation.Scheduled;
  22 +import org.springframework.stereotype.Service;
  23 +import org.thingsboard.server.common.data.id.TenantId;
  24 +import org.thingsboard.server.common.data.page.PageData;
  25 +import org.thingsboard.server.common.data.page.PageLink;
  26 +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
  27 +import org.thingsboard.server.common.msg.queue.ServiceType;
  28 +import org.thingsboard.server.dao.rpc.RpcDao;
  29 +import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
  30 +import org.thingsboard.server.dao.tenant.TenantDao;
  31 +import org.thingsboard.server.queue.discovery.PartitionService;
  32 +import org.thingsboard.server.queue.util.TbCoreComponent;
  33 +
  34 +import java.util.Date;
  35 +import java.util.Optional;
  36 +import java.util.concurrent.TimeUnit;
  37 +
  38 +@TbCoreComponent
  39 +@Service
  40 +@Slf4j
  41 +@RequiredArgsConstructor
  42 +public class RpcCleanUpService {
  43 + @Value("${sql.ttl.rpc.enabled}")
  44 + private boolean ttlTaskExecutionEnabled;
  45 +
  46 + private final TenantDao tenantDao;
  47 + private final PartitionService partitionService;
  48 + private final TbTenantProfileCache tenantProfileCache;
  49 + private final RpcDao rpcDao;
  50 +
  51 + @Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.rpc.checking_interval})}", fixedDelayString = "${sql.ttl.rpc.checking_interval}")
  52 + public void cleanUp() {
  53 + if (ttlTaskExecutionEnabled) {
  54 + PageLink tenantsBatchRequest = new PageLink(10_000, 0);
  55 + PageData<TenantId> tenantsIds;
  56 + do {
  57 + tenantsIds = tenantDao.findTenantsIds(tenantsBatchRequest);
  58 + for (TenantId tenantId : tenantsIds.getData()) {
  59 + if (!partitionService.resolve(ServiceType.TB_CORE, tenantId, tenantId).isMyPartition()) {
  60 + continue;
  61 + }
  62 +
  63 + Optional<DefaultTenantProfileConfiguration> tenantProfileConfiguration = tenantProfileCache.get(tenantId).getProfileConfiguration();
  64 + if (tenantProfileConfiguration.isEmpty() || tenantProfileConfiguration.get().getRpcTtlDays() == 0) {
  65 + continue;
  66 + }
  67 +
  68 + long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getRpcTtlDays());
  69 + long expirationTime = System.currentTimeMillis() - ttl;
  70 +
  71 + long totalRemoved = rpcDao.deleteOutdatedRpcByTenantId(tenantId, expirationTime);
  72 +
  73 + if (totalRemoved > 0) {
  74 + log.info("Removed {} outdated rpc(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime));
  75 + }
  76 + }
  77 +
  78 + tenantsBatchRequest = tenantsBatchRequest.nextPageLink();
  79 + } while (tenantsIds.hasNext());
  80 + }
  81 + }
  82 +
  83 +}
... ...
... ... @@ -26,7 +26,6 @@
26 26 </appender>
27 27
28 28 <logger name="org.thingsboard.server" level="INFO" />
29   - <logger name="org.thingsboard.server.transport.snmp" level="DEBUG" />
30 29
31 30 <!-- <logger name="org.thingsboard.server.service.queue" level="TRACE" />-->
32 31 <!-- <logger name="org.thingsboard.server.service.transport" level="TRACE" />-->
... ...
... ... @@ -276,6 +276,9 @@ sql:
276 276 alarms:
277 277 checking_interval: "${SQL_ALARMS_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours
278 278 removal_batch_size: "${SQL_ALARMS_TTL_REMOVAL_BATCH_SIZE:3000}" # To delete outdated alarms not all at once but in batches
  279 + rpc:
  280 + enabled: "${SQL_TTL_RPC_ENABLED:true}"
  281 + checking_interval: "${SQL_RPC_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours
279 282
280 283 # Actor system parameters
281 284 actors:
... ... @@ -515,7 +518,7 @@ audit-log:
515 518 index_pattern: "${AUDIT_LOG_SINK_INDEX_PATTERN:@{TENANT}_AUDIT_LOG_@{DATE}}"
516 519 # Date format. Details of the pattern could be found here:
517 520 # https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
518   - date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.DD}"
  521 + date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.dd}"
519 522 scheme_name: "${AUDIT_LOG_SINK_SCHEME_NAME:http}" # http or https
520 523 host: "${AUDIT_LOG_SINK_HOST:localhost}"
521 524 port: "${AUDIT_LOG_SINK_PORT:9200}"
... ... @@ -592,6 +595,10 @@ transport:
592 595 ssl:
593 596 # Enable/disable SSL support
594 597 enabled: "${MQTT_SSL_ENABLED:false}"
  598 + # MQTT SSL bind address
  599 + bind_address: "${MQTT_SSL_BIND_ADDRESS:0.0.0.0}"
  600 + # MQTT SSL bind port
  601 + bind_port: "${MQTT_SSL_BIND_PORT:8883}"
595 602 # SSL protocol: See http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext
596 603 protocol: "${MQTT_SSL_PROTOCOL:TLSv1.2}"
597 604 # Path to the key store that holds the SSL certificate
... ... @@ -618,8 +625,6 @@ transport:
618 625 bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}"
619 626 # CoAP DTLS bind port
620 627 bind_port: "${COAP_DTLS_BIND_PORT:5684}"
621   - # Secure mode. Allowed values: NO_AUTH, X509
622   - mode: "${COAP_DTLS_SECURE_MODE:NO_AUTH}"
623 628 # Path to the key store that holds the certificate
624 629 key_store: "${COAP_DTLS_KEY_STORE:coapserver.jks}"
625 630 # Password used to access the key store
... ... @@ -642,48 +647,40 @@ transport:
642 647 bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}"
643 648 bind_port: "${LWM2M_BIND_PORT:5685}"
644 649 security:
645   - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY:0.0.0.0}"
646   - bind_port: "${LWM2M_BIND_PORT_SECURITY:5686}"
647   - # Only for RPK: Public & Private Key. If the keystore file is missing or not working
648   - public_x: "${LWM2M_SERVER_PUBLIC_X:05064b9e6762dd8d8b8a52355d7b4d8b9a3d64e6d2ee277d76c248861353f358}"
649   - public_y: "${LWM2M_SERVER_PUBLIC_Y:5eeb1838e4f9e37b31fa347aef5ce3431eb54e0a2506910c5e0298817445721b}"
650   - private_encoded: "${LWM2M_SERVER_PRIVATE_ENCODED:308193020100301306072a8648ce3d020106082a8648ce3d030107047930770201010420dc774b309e547ceb48fee547e104ce201a9c48c449dc5414cd04e7f5cf05f67ba00a06082a8648ce3d030107a1440342000405064b9e6762dd8d8b8a52355d7b4d8b9a3d64e6d2ee277d76c248861353f3585eeb1838e4f9e37b31fa347aef5ce3431eb54e0a2506910c5e0298817445721b}"
  650 + bind_address: "${LWM2M_SECURITY_BIND_ADDRESS:0.0.0.0}"
  651 + bind_port: "${LWM2M_SECURITY_BIND_PORT:5686}"
651 652 # Only Certificate_x509:
652   - alias: "${LWM2M_KEYSTORE_ALIAS_SERVER:server}"
  653 + key_alias: "${LWM2M_SERVER_KEY_ALIAS:server}"
  654 + key_password: "${LWM2M_SERVER_KEY_PASSWORD:server_ks_password}"
653 655 skip_validity_check_for_client_cert: "${TB_LWM2M_SERVER_SECURITY_SKIP_VALIDITY_CHECK_FOR_CLIENT_CERT:false}"
654 656 bootstrap:
655 657 enable: "${LWM2M_ENABLED_BS:true}"
656 658 id: "${LWM2M_SERVER_ID_BS:111}"
657   - bind_address: "${LWM2M_BIND_ADDRESS_BS:0.0.0.0}"
658   - bind_port: "${LWM2M_BIND_PORT_BS:5687}"
  659 + bind_address: "${LWM2M_BS_BIND_ADDRESS:0.0.0.0}"
  660 + bind_port: "${LWM2M_BS_BIND_PORT:5687}"
659 661 security:
660   - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY_BS:0.0.0.0}"
661   - bind_port: "${LWM2M_BIND_PORT_SECURITY_BS:5688}"
662   - # Only for RPK: Public & Private Key. If the keystore file is missing or not working
663   - public_x: "${LWM2M_SERVER_PUBLIC_X_BS:5017c87a1c1768264656b3b355434b0def6edb8b9bf166a4762d9930cd730f91}"
664   - public_y: "${LWM2M_SERVER_PUBLIC_Y_BS:3fc4e61bcd8901ec27c424114c3e887ed372497f0c2cf85839b8443e76988b34}"
665   - private_encoded: "${LWM2M_SERVER_PRIVATE_ENCODED_BS:308193020100301306072a8648ce3d020106082a8648ce3d0301070479307702010104205ecafd90caa7be45c42e1f3f32571632b8409e6e6249d7124f4ba56fab3c8083a00a06082a8648ce3d030107a144034200045017c87a1c1768264656b3b355434b0def6edb8b9bf166a4762d9930cd730f913fc4e61bcd8901ec27c424114c3e887ed372497f0c2cf85839b8443e76988b34}"
  662 + bind_address: "${LWM2M_BS_SECURITY_BIND_ADDRESS:0.0.0.0}"
  663 + bind_port: "${LWM2M_BS_SECURITY_BIND_PORT:5688}"
666 664 # Only Certificate_x509:
667   - alias: "${LWM2M_KEYSTORE_ALIAS_BS:bootstrap}"
  665 + key_alias: "${LWM2M_BS_KEY_ALIAS:bootstrap}"
  666 + key_password: "${LWM2M_BS_KEY_PASSWORD:server_ks_password}"
668 667 security:
669 668 # Certificate_x509:
670 669 # To get helps about files format and how to generate it, see: https://github.com/eclipse/leshan/wiki/Credential-files-format
671 670 # Create new X509 Certificates: common/transport/lwm2m/src/main/resources/credentials/shell/lwM2M_credentials.sh
672 671 key_store_type: "${LWM2M_KEYSTORE_TYPE:JKS}"
673 672 # key_store_path_file: "${KEY_STORE_PATH_FILE:/common/transport/lwm2m/src/main/resources/credentials/serverKeyStore.jks"
674   - key_store: "${LWM2M_KEY_STORE:lwm2mserver.jks}"
675   - key_store_password: "${LWM2M_KEY_STORE_PASSWORD:server_ks_password}"
676   - root_alias: "${LWM2M_SERVER_ROOT_CA:rootca}"
677   - enable_gen_new_key_psk_rpk: "${ENABLE_GEN_NEW_KEY_PSK_RPK:false}"
  673 + key_store: "${LWM2M_KEYSTORE:lwm2mserver.jks}"
  674 + key_store_password: "${LWM2M_KEYSTORE_PASSWORD:server_ks_password}"
  675 + root_alias: "${LWM2M_SERVER_ROOT_CA_ALIAS:rootca}"
  676 + recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}"
  677 + recommended_supported_groups: "${LWM2M_RECOMMENDED_SUPPORTED_GROUPS:true}"
678 678 timeout: "${LWM2M_TIMEOUT:120000}"
679   - recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}"
680   - recommended_supported_groups: "${LWM2M_RECOMMENDED_SUPPORTED_GROUPS:true}"
681 679 uplink_pool_size: "${LWM2M_UPLINK_POOL_SIZE:10}"
682 680 downlink_pool_size: "${LWM2M_DOWNLINK_POOL_SIZE:10}"
683 681 ota_pool_size: "${LWM2M_OTA_POOL_SIZE:10}"
684   - registration_store_pool_size: "${LWM2M_REGISTRATION_STORE_POOL_SIZE:100}"
685 682 clean_period_in_sec: "${LWM2M_CLEAN_PERIOD_IN_SEC:2}"
686   - log_max_length: "${LWM2M_LOG_MAX_LENGTH:100}"
  683 + log_max_length: "${LWM2M_LOG_MAX_LENGTH:1024}"
687 684 # Use redis for Security and Registration stores
688 685 redis.enabled: "${LWM2M_REDIS_ENABLED:false}"
689 686 snmp:
... ... @@ -870,6 +867,7 @@ queue:
870 867 topic: "${TB_QUEUE_RE_MAIN_TOPIC:tb_rule_engine.main}"
871 868 poll-interval: "${TB_QUEUE_RE_MAIN_POLL_INTERVAL_MS:25}"
872 869 partitions: "${TB_QUEUE_RE_MAIN_PARTITIONS:10}"
  870 + consumer-per-partition: "${TB_QUEUE_RE_MAIN_CONSUMER_PER_PARTITION:true}"
873 871 pack-processing-timeout: "${TB_QUEUE_RE_MAIN_PACK_PROCESSING_TIMEOUT_MS:2000}"
874 872 submit-strategy:
875 873 type: "${TB_QUEUE_RE_MAIN_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL
... ... @@ -886,6 +884,7 @@ queue:
886 884 topic: "${TB_QUEUE_RE_HP_TOPIC:tb_rule_engine.hp}"
887 885 poll-interval: "${TB_QUEUE_RE_HP_POLL_INTERVAL_MS:25}"
888 886 partitions: "${TB_QUEUE_RE_HP_PARTITIONS:10}"
  887 + consumer-per-partition: "${TB_QUEUE_RE_HP_CONSUMER_PER_PARTITION:true}"
889 888 pack-processing-timeout: "${TB_QUEUE_RE_HP_PACK_PROCESSING_TIMEOUT_MS:2000}"
890 889 submit-strategy:
891 890 type: "${TB_QUEUE_RE_HP_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL
... ... @@ -902,6 +901,7 @@ queue:
902 901 topic: "${TB_QUEUE_RE_SQ_TOPIC:tb_rule_engine.sq}"
903 902 poll-interval: "${TB_QUEUE_RE_SQ_POLL_INTERVAL_MS:25}"
904 903 partitions: "${TB_QUEUE_RE_SQ_PARTITIONS:10}"
  904 + consumer-per-partition: "${TB_QUEUE_RE_SQ_CONSUMER_PER_PARTITION:true}"
905 905 pack-processing-timeout: "${TB_QUEUE_RE_SQ_PACK_PROCESSING_TIMEOUT_MS:2000}"
906 906 submit-strategy:
907 907 type: "${TB_QUEUE_RE_SQ_SUBMIT_STRATEGY_TYPE:SEQUENTIAL_BY_ORIGINATOR}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.actors.device;
  17 +
  18 +import org.junit.Before;
  19 +import org.junit.Test;
  20 +import org.thingsboard.common.util.LinkedHashMapRemoveEldest;
  21 +import org.thingsboard.server.actors.ActorSystemContext;
  22 +import org.thingsboard.server.common.data.id.DeviceId;
  23 +import org.thingsboard.server.common.data.id.TenantId;
  24 +import org.thingsboard.server.dao.device.DeviceService;
  25 +
  26 +import static org.hamcrest.CoreMatchers.instanceOf;
  27 +import static org.hamcrest.CoreMatchers.is;
  28 +import static org.hamcrest.CoreMatchers.notNullValue;
  29 +import static org.hamcrest.MatcherAssert.assertThat;
  30 +import static org.mockito.BDDMockito.willReturn;
  31 +import static org.mockito.Mockito.mock;
  32 +
  33 +public class DeviceActorMessageProcessorTest {
  34 +
  35 + public static final long MAX_CONCURRENT_SESSIONS_PER_DEVICE = 10L;
  36 + ActorSystemContext systemContext;
  37 + DeviceService deviceService;
  38 + TenantId tenantId = TenantId.SYS_TENANT_ID;
  39 + DeviceId deviceId = DeviceId.fromString("78bf9b26-74ef-4af2-9cfb-ad6cf24ad2ec");
  40 +
  41 + DeviceActorMessageProcessor processor;
  42 +
  43 + @Before
  44 + public void setUp() {
  45 + systemContext = mock(ActorSystemContext.class);
  46 + deviceService = mock(DeviceService.class);
  47 + willReturn(MAX_CONCURRENT_SESSIONS_PER_DEVICE).given(systemContext).getMaxConcurrentSessionsPerDevice();
  48 + willReturn(deviceService).given(systemContext).getDeviceService();
  49 + processor = new DeviceActorMessageProcessor(systemContext, tenantId, deviceId);
  50 + }
  51 +
  52 + @Test
  53 + public void givenSystemContext_whenNewInstance_thenVerifySessionMapMaxSize() {
  54 + assertThat(processor.sessions, instanceOf(LinkedHashMapRemoveEldest.class));
  55 + assertThat(processor.sessions.getMaxEntries(), is(MAX_CONCURRENT_SESSIONS_PER_DEVICE));
  56 + assertThat(processor.sessions.getRemovalConsumer(), notNullValue());
  57 + }
  58 +}
\ No newline at end of file
... ...
... ... @@ -155,6 +155,7 @@ public abstract class BaseUserControllerTest extends AbstractControllerTest {
155 155
156 156 doPost("/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest)
157 157 .andExpect(status().isOk());
  158 + Thread.sleep(1000);
158 159 doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken)
159 160 .andExpect(status().isSeeOther())
160 161 .andExpect(header().string(HttpHeaders.LOCATION, "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken));
... ...
... ... @@ -51,7 +51,7 @@ public class TestMailService {
51 51 currentResetPasswordToken = passwordResetLink.split("=")[1];
52 52 return null;
53 53 }
54   - }).when(mailService).sendResetPasswordEmail(Mockito.anyString(), Mockito.anyString());
  54 + }).when(mailService).sendResetPasswordEmailAsync(Mockito.anyString(), Mockito.anyString());
55 55 return mailService;
56 56 }
57 57
... ...
... ... @@ -17,24 +17,46 @@ package org.thingsboard.server.transport.lwm2m;
17 17
18 18 import com.fasterxml.jackson.core.type.TypeReference;
19 19 import org.apache.commons.io.IOUtils;
  20 +import org.eclipse.californium.core.network.config.NetworkConfig;
  21 +import org.eclipse.leshan.client.object.Security;
20 22 import org.eclipse.leshan.core.util.Hex;
21 23 import org.junit.After;
22 24 import org.junit.Assert;
23 25 import org.junit.Before;
  26 +import org.springframework.mock.web.MockMultipartFile;
  27 +import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
  28 +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
24 29 import org.thingsboard.common.util.JacksonUtil;
  30 +import org.thingsboard.server.common.data.Device;
25 31 import org.thingsboard.server.common.data.DeviceProfile;
26 32 import org.thingsboard.server.common.data.DeviceProfileProvisionType;
27 33 import org.thingsboard.server.common.data.DeviceProfileType;
28 34 import org.thingsboard.server.common.data.DeviceTransportType;
  35 +import org.thingsboard.server.common.data.OtaPackageInfo;
29 36 import org.thingsboard.server.common.data.ResourceType;
30 37 import org.thingsboard.server.common.data.TbResource;
  38 +import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredentials;
31 39 import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
32 40 import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
33 41 import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration;
34 42 import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration;
  43 +import org.thingsboard.server.common.data.query.EntityData;
  44 +import org.thingsboard.server.common.data.query.EntityDataPageLink;
  45 +import org.thingsboard.server.common.data.query.EntityDataQuery;
  46 +import org.thingsboard.server.common.data.query.EntityKey;
  47 +import org.thingsboard.server.common.data.query.EntityKeyType;
  48 +import org.thingsboard.server.common.data.query.SingleEntityFilter;
  49 +import org.thingsboard.server.common.data.security.DeviceCredentials;
  50 +import org.thingsboard.server.common.data.security.DeviceCredentialsType;
35 51 import org.thingsboard.server.controller.AbstractWebsocketTest;
36 52 import org.thingsboard.server.controller.TbTestWebSocketClient;
37 53 import org.thingsboard.server.dao.service.DaoSqlTest;
  54 +import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;
  55 +import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;
  56 +import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;
  57 +import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;
  58 +import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;
  59 +import org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MCredentials;
38 60
39 61 import java.io.IOException;
40 62 import java.io.InputStream;
... ... @@ -54,9 +76,16 @@ import java.security.spec.ECPrivateKeySpec;
54 76 import java.security.spec.ECPublicKeySpec;
55 77 import java.security.spec.KeySpec;
56 78 import java.util.Base64;
  79 +import java.util.Collections;
  80 +import java.util.List;
57 81 import java.util.concurrent.Executors;
58 82 import java.util.concurrent.ScheduledExecutorService;
59 83
  84 +import static org.eclipse.leshan.client.object.Security.noSec;
  85 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  86 +import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE;
  87 +import static org.thingsboard.server.common.data.ota.OtaPackageType.SOFTWARE;
  88 +
60 89 @DaoSqlTest
61 90 public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
62 91
... ... @@ -139,6 +168,15 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
139 168 // certificates trustedby the server (should contain rootCA)
140 169 protected final Certificate[] trustedCertificates = new Certificate[1];
141 170
  171 + protected static final int SECURE_PORT = 5686;
  172 + protected static final NetworkConfig SECURE_COAP_CONFIG = new NetworkConfig().setString("COAP_SECURE_PORT", Integer.toString(SECURE_PORT));
  173 + protected static final String ENDPOINT = "deviceAEndpoint";
  174 + protected static final String SECURE_URI = "coaps://localhost:" + SECURE_PORT;
  175 +
  176 + protected static final int PORT = 5685;
  177 + protected static final Security SECURITY = noSec("coap://localhost:" + PORT, 123);
  178 + protected static final NetworkConfig COAP_CONFIG = new NetworkConfig().setString("COAP_PORT", Integer.toString(PORT));
  179 +
142 180 public AbstractLwM2MIntegrationTest() {
143 181 // create client credentials
144 182 try {
... ... @@ -226,7 +264,7 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
226 264 executor = Executors.newScheduledThreadPool(10);
227 265 loginTenantAdmin();
228 266
229   - String[] resources = new String[]{"1.xml", "2.xml", "3.xml"};
  267 + String[] resources = new String[]{"1.xml", "2.xml", "3.xml", "5.xml", "9.xml"};
230 268 for (String resourceName : resources) {
231 269 TbResource lwModel = new TbResource();
232 270 lwModel.setResourceType(ResourceType.LWM2M_MODEL);
... ... @@ -262,10 +300,110 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
262 300 Assert.assertNotNull(deviceProfile);
263 301 }
264 302
  303 + protected Device createDevice(LwM2MClientCredentials clientCredentials) throws Exception {
  304 + Device device = new Device();
  305 + device.setName("Device A");
  306 + device.setDeviceProfileId(deviceProfile.getId());
  307 + device.setTenantId(tenantId);
  308 + device = doPost("/api/device", device, Device.class);
  309 + Assert.assertNotNull(device);
  310 +
  311 + DeviceCredentials deviceCredentials =
  312 + doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
  313 + Assert.assertEquals(device.getId(), deviceCredentials.getDeviceId());
  314 + deviceCredentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS);
  315 +
  316 + LwM2MCredentials credentials = new LwM2MCredentials();
  317 +
  318 + credentials.setClient(clientCredentials);
  319 +
  320 + deviceCredentials.setCredentialsValue(JacksonUtil.toString(credentials));
  321 + doPost("/api/device/credentials", deviceCredentials).andExpect(status().isOk());
  322 + return device;
  323 + }
  324 +
  325 +
  326 + protected OtaPackageInfo createFirmware() throws Exception {
  327 + String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a";
  328 +
  329 + OtaPackageInfo firmwareInfo = new OtaPackageInfo();
  330 + firmwareInfo.setDeviceProfileId(deviceProfile.getId());
  331 + firmwareInfo.setType(FIRMWARE);
  332 + firmwareInfo.setTitle("My firmware");
  333 + firmwareInfo.setVersion("v1.0");
  334 +
  335 + OtaPackageInfo savedFirmwareInfo = doPost("/api/otaPackage", firmwareInfo, OtaPackageInfo.class);
  336 +
  337 + MockMultipartFile testData = new MockMultipartFile("file", "filename.txt", "text/plain", new byte[]{1});
  338 +
  339 + return savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, "SHA256");
  340 + }
  341 +
  342 + protected OtaPackageInfo createSoftware() throws Exception {
  343 + String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a";
  344 +
  345 + OtaPackageInfo swInfo = new OtaPackageInfo();
  346 + swInfo.setDeviceProfileId(deviceProfile.getId());
  347 + swInfo.setType(SOFTWARE);
  348 + swInfo.setTitle("My sw");
  349 + swInfo.setVersion("v1.0");
  350 +
  351 + OtaPackageInfo savedFirmwareInfo = doPost("/api/otaPackage", swInfo, OtaPackageInfo.class);
  352 +
  353 + MockMultipartFile testData = new MockMultipartFile("file", "filename.txt", "text/plain", new byte[]{1});
  354 +
  355 + return savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, "SHA256");
  356 + }
  357 +
  358 + protected OtaPackageInfo savaData(String urlTemplate, MockMultipartFile content, String... params) throws Exception {
  359 + MockMultipartHttpServletRequestBuilder postRequest = MockMvcRequestBuilders.multipart(urlTemplate, params);
  360 + postRequest.file(content);
  361 + setJwtToken(postRequest);
  362 + return readResponse(mockMvc.perform(postRequest).andExpect(status().isOk()), OtaPackageInfo.class);
  363 + }
  364 +
265 365 @After
266 366 public void after() {
267 367 executor.shutdownNow();
268 368 wsClient.close();
269 369 }
270 370
  371 + public void basicTestConnectionObserveTelemetry(Security security,
  372 + LwM2MClientCredentials credentials,
  373 + NetworkConfig coapConfig,
  374 + String endpoint) throws Exception {
  375 + createDeviceProfile(TRANSPORT_CONFIGURATION);
  376 + Device device = createDevice(credentials);
  377 +
  378 + SingleEntityFilter sef = new SingleEntityFilter();
  379 + sef.setSingleEntity(device.getId());
  380 + LatestValueCmd latestCmd = new LatestValueCmd();
  381 + latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
  382 + EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
  383 + Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
  384 +
  385 + EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
  386 + TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
  387 + wrapper.setEntityDataCmds(Collections.singletonList(cmd));
  388 +
  389 + wsClient.send(mapper.writeValueAsString(wrapper));
  390 + wsClient.waitForReply();
  391 +
  392 + wsClient.registerWaitForUpdate();
  393 + LwM2MTestClient client = new LwM2MTestClient(executor, endpoint);
  394 +
  395 + client.init(security, coapConfig);
  396 + String msg = wsClient.waitForUpdate();
  397 +
  398 + EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
  399 + Assert.assertEquals(1, update.getCmdId());
  400 + List<EntityData> eData = update.getUpdate();
  401 + Assert.assertNotNull(eData);
  402 + Assert.assertEquals(1, eData.size());
  403 + Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
  404 + Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
  405 + var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
  406 + Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));
  407 + client.destroy();
  408 + }
271 409 }
... ...
... ... @@ -15,101 +15,151 @@
15 15 */
16 16 package org.thingsboard.server.transport.lwm2m;
17 17
18   -import org.eclipse.californium.core.network.config.NetworkConfig;
19   -import org.eclipse.leshan.client.object.Security;
  18 +import com.fasterxml.jackson.core.type.TypeReference;
20 19 import org.junit.Assert;
21 20 import org.junit.Test;
22   -import org.springframework.mock.web.MockMultipartFile;
23   -import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
24   -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
25   -import org.thingsboard.common.util.JacksonUtil;
26 21 import org.thingsboard.server.common.data.Device;
27 22 import org.thingsboard.server.common.data.OtaPackageInfo;
28 23 import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials;
  24 +import org.thingsboard.server.common.data.kv.KvEntry;
  25 +import org.thingsboard.server.common.data.kv.TsKvEntry;
  26 +import org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus;
29 27 import org.thingsboard.server.common.data.query.EntityData;
30 28 import org.thingsboard.server.common.data.query.EntityDataPageLink;
31 29 import org.thingsboard.server.common.data.query.EntityDataQuery;
32 30 import org.thingsboard.server.common.data.query.EntityKey;
33 31 import org.thingsboard.server.common.data.query.EntityKeyType;
34 32 import org.thingsboard.server.common.data.query.SingleEntityFilter;
35   -import org.thingsboard.server.common.data.security.DeviceCredentials;
36   -import org.thingsboard.server.common.data.security.DeviceCredentialsType;
37 33 import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;
38 34 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;
39 35 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;
40 36 import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;
41 37 import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;
42   -import org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MCredentials;
43 38
  39 +import java.util.Arrays;
44 40 import java.util.Collections;
45 41 import java.util.List;
  42 +import java.util.stream.Collectors;
46 43
47   -import static org.eclipse.leshan.client.object.Security.noSec;
48   -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
49   -import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE;
  44 +import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries;
  45 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED;
  46 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING;
  47 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.INITIATED;
  48 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.QUEUED;
  49 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATED;
  50 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATING;
  51 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.VERIFIED;
50 52
51 53 public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
52 54
53   - private final int PORT = 5685;
54   - private final Security SECURITY = noSec("coap://localhost:" + PORT, 123);
55   - private final NetworkConfig COAP_CONFIG = new NetworkConfig().setString("COAP_PORT", Integer.toString(PORT));
56   - private final String ENDPOINT = "noSecEndpoint";
  55 + private final String OTA_TRANSPORT_CONFIGURATION = "{\n" +
  56 + " \"observeAttr\": {\n" +
  57 + " \"keyName\": {\n" +
  58 + " \"/5_1.0/0/3\": \"state\",\n" +
  59 + " \"/5_1.0/0/5\": \"updateResult\",\n" +
  60 + " \"/5_1.0/0/6\": \"pkgname\",\n" +
  61 + " \"/5_1.0/0/7\": \"pkgversion\",\n" +
  62 + " \"/5_1.0/0/9\": \"firmwareUpdateDeliveryMethod\",\n" +
  63 + " \"/9_1.0/0/0\": \"pkgname\",\n" +
  64 + " \"/9_1.0/0/1\": \"pkgversion\",\n" +
  65 + " \"/9_1.0/0/7\": \"updateState\",\n" +
  66 + " \"/9_1.0/0/9\": \"updateResult\"\n" +
  67 + " },\n" +
  68 + " \"observe\": [\n" +
  69 + " \"/5_1.0/0/3\",\n" +
  70 + " \"/5_1.0/0/5\",\n" +
  71 + " \"/5_1.0/0/6\",\n" +
  72 + " \"/5_1.0/0/7\",\n" +
  73 + " \"/5_1.0/0/9\",\n" +
  74 + " \"/9_1.0/0/0\",\n" +
  75 + " \"/9_1.0/0/1\",\n" +
  76 + " \"/9_1.0/0/7\",\n" +
  77 + " \"/9_1.0/0/9\"\n" +
  78 + " ],\n" +
  79 + " \"attribute\": [],\n" +
  80 + " \"telemetry\": [\n" +
  81 + " \"/5_1.0/0/3\",\n" +
  82 + " \"/5_1.0/0/5\",\n" +
  83 + " \"/5_1.0/0/6\",\n" +
  84 + " \"/5_1.0/0/7\",\n" +
  85 + " \"/5_1.0/0/9\",\n" +
  86 + " \"/9_1.0/0/0\",\n" +
  87 + " \"/9_1.0/0/1\",\n" +
  88 + " \"/9_1.0/0/7\",\n" +
  89 + " \"/9_1.0/0/9\"\n" +
  90 + " ],\n" +
  91 + " \"attributeLwm2m\": {}\n" +
  92 + " },\n" +
  93 + " \"bootstrap\": {\n" +
  94 + " \"servers\": {\n" +
  95 + " \"binding\": \"UQ\",\n" +
  96 + " \"shortId\": 123,\n" +
  97 + " \"lifetime\": 300,\n" +
  98 + " \"notifIfDisabled\": true,\n" +
  99 + " \"defaultMinPeriod\": 1\n" +
  100 + " },\n" +
  101 + " \"lwm2mServer\": {\n" +
  102 + " \"host\": \"localhost\",\n" +
  103 + " \"port\": 5685,\n" +
  104 + " \"serverId\": 123,\n" +
  105 + " \"securityMode\": \"NO_SEC\",\n" +
  106 + " \"serverPublicKey\": \"\",\n" +
  107 + " \"clientHoldOffTime\": 1,\n" +
  108 + " \"bootstrapServerAccountTimeout\": 0\n" +
  109 + " },\n" +
  110 + " \"bootstrapServer\": {\n" +
  111 + " \"host\": \"localhost\",\n" +
  112 + " \"port\": 5687,\n" +
  113 + " \"serverId\": 111,\n" +
  114 + " \"securityMode\": \"NO_SEC\",\n" +
  115 + " \"serverPublicKey\": \"\",\n" +
  116 + " \"clientHoldOffTime\": 1,\n" +
  117 + " \"bootstrapServerAccountTimeout\": 0\n" +
  118 + " }\n" +
  119 + " },\n" +
  120 + " \"clientLwM2mSettings\": {\n" +
  121 + " \"fwUpdateStrategy\": 1,\n" +
  122 + " \"swUpdateStrategy\": 1,\n" +
  123 + " \"clientOnlyObserveAfterConnect\": 1,\n" +
  124 + " \"powerMode\": \"PSM\",\n" +
  125 + " \"fwUpdateResource\": \"\",\n" +
  126 + " \"swUpdateResource\": \"\",\n" +
  127 + " \"compositeOperationsSupport\": false\n" +
  128 + " },\n" +
  129 + " \"type\": \"LWM2M\"\n" +
  130 + "}";
57 131
58   - private Device createDevice() throws Exception {
59   - Device device = new Device();
60   - device.setName("Device A");
61   - device.setDeviceProfileId(deviceProfile.getId());
62   - device.setTenantId(tenantId);
63   - device = doPost("/api/device", device, Device.class);
64   - Assert.assertNotNull(device);
65   -
66   - DeviceCredentials deviceCredentials =
67   - doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
68   - Assert.assertEquals(device.getId(), deviceCredentials.getDeviceId());
69   - deviceCredentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS);
70   -
71   - LwM2MCredentials noSecCredentials = new LwM2MCredentials();
  132 + @Test
  133 + public void testConnectAndObserveTelemetry() throws Exception {
72 134 NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
73 135 clientCredentials.setEndpoint(ENDPOINT);
74   - noSecCredentials.setClient(clientCredentials);
75   - deviceCredentials.setCredentialsValue(JacksonUtil.toString(noSecCredentials));
76   - doPost("/api/device/credentials", deviceCredentials).andExpect(status().isOk());
77   - return device;
  136 + super.basicTestConnectionObserveTelemetry(SECURITY, clientCredentials, COAP_CONFIG, ENDPOINT);
78 137 }
79 138
80   - private OtaPackageInfo createFirmware() throws Exception {
81   - String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a";
82   -
83   - OtaPackageInfo firmwareInfo = new OtaPackageInfo();
84   - firmwareInfo.setDeviceProfileId(deviceProfile.getId());
85   - firmwareInfo.setType(FIRMWARE);
86   - firmwareInfo.setTitle("My firmware");
87   - firmwareInfo.setVersion("v1.0");
  139 + @Test
  140 + public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception {
  141 + createDeviceProfile(TRANSPORT_CONFIGURATION);
  142 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  143 + clientCredentials.setEndpoint(ENDPOINT);
  144 + Device device = createDevice(clientCredentials);
88 145
89   - OtaPackageInfo savedFirmwareInfo = doPost("/api/otaPackage", firmwareInfo, OtaPackageInfo.class);
  146 + OtaPackageInfo firmware = createFirmware();
90 147
91   - MockMultipartFile testData = new MockMultipartFile("file", "filename.txt", "text/plain", new byte[]{1});
  148 + LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);
  149 + client.init(SECURITY, COAP_CONFIG);
92 150
93   - return savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, "SHA256");
94   - }
  151 + Thread.sleep(1000);
95 152
96   - protected OtaPackageInfo savaData(String urlTemplate, MockMultipartFile content, String... params) throws Exception {
97   - MockMultipartHttpServletRequestBuilder postRequest = MockMvcRequestBuilders.multipart(urlTemplate, params);
98   - postRequest.file(content);
99   - setJwtToken(postRequest);
100   - return readResponse(mockMvc.perform(postRequest).andExpect(status().isOk()), OtaPackageInfo.class);
101   - }
  153 + device.setFirmwareId(firmware.getId());
102 154
103   - @Test
104   - public void testConnectAndObserveTelemetry() throws Exception {
105   - createDeviceProfile(TRANSPORT_CONFIGURATION);
  155 + device = doPost("/api/device", device, Device.class);
106 156
107   - Device device = createDevice();
  157 + Thread.sleep(1000);
108 158
109 159 SingleEntityFilter sef = new SingleEntityFilter();
110 160 sef.setSingleEntity(device.getId());
111 161 LatestValueCmd latestCmd = new LatestValueCmd();
112   - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
  162 + latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "fw_state")));
113 163 EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
114 164 Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
115 165
... ... @@ -121,8 +171,7 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
121 171 wsClient.waitForReply();
122 172
123 173 wsClient.registerWaitForUpdate();
124   - LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);
125   - client.init(SECURITY, COAP_CONFIG);
  174 +
126 175 String msg = wsClient.waitForUpdate();
127 176
128 177 EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
... ... @@ -132,20 +181,21 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
132 181 Assert.assertEquals(1, eData.size());
133 182 Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
134 183 Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
135   - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
136   - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));
  184 + var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("fw_state");
  185 + Assert.assertEquals("FAILED", tsValue.getValue());
137 186 client.destroy();
138 187 }
139 188
140 189 @Test
141   - public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception {
142   - createDeviceProfile(TRANSPORT_CONFIGURATION);
143   -
144   - Device device = createDevice();
  190 + public void testFirmwareUpdateByObject5() throws Exception {
  191 + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);
  192 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  193 + clientCredentials.setEndpoint("OTA_" + ENDPOINT);
  194 + Device device = createDevice(clientCredentials);
145 195
146 196 OtaPackageInfo firmware = createFirmware();
147 197
148   - LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);
  198 + LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
149 199 client.init(SECURITY, COAP_CONFIG);
150 200
151 201 Thread.sleep(1000);
... ... @@ -154,35 +204,49 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
154 204
155 205 device = doPost("/api/device", device, Device.class);
156 206
  207 + Thread.sleep(4000);
  208 +
  209 + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {
  210 + }));
  211 +
  212 + List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
  213 +
  214 + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADED, UPDATING, UPDATED);
  215 +
  216 + Assert.assertEquals(expectedStatuses, statuses);
  217 +
  218 + client.destroy();
  219 + }
  220 +
  221 + @Test
  222 + public void testSoftwareUpdateByObject9() throws Exception {
  223 + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);
  224 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  225 + clientCredentials.setEndpoint("OTA_" + ENDPOINT);
  226 + Device device = createDevice(clientCredentials);
  227 +
  228 + OtaPackageInfo software = createSoftware();
  229 +
  230 + LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
  231 + client.init(SECURITY, COAP_CONFIG);
  232 +
157 233 Thread.sleep(1000);
158 234
159   - SingleEntityFilter sef = new SingleEntityFilter();
160   - sef.setSingleEntity(device.getId());
161   - LatestValueCmd latestCmd = new LatestValueCmd();
162   - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "fw_state")));
163   - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
164   - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
  235 + device.setSoftwareId(software.getId());
165 236
166   - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
167   - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
168   - wrapper.setEntityDataCmds(Collections.singletonList(cmd));
  237 + device = doPost("/api/device", device, Device.class);
169 238
170   - wsClient.send(mapper.writeValueAsString(wrapper));
171   - wsClient.waitForReply();
  239 + Thread.sleep(4000);
172 240
173   - wsClient.registerWaitForUpdate();
  241 + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=sw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {
  242 + }));
174 243
175   - String msg = wsClient.waitForUpdate();
  244 + List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
  245 +
  246 + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED);
  247 +
  248 + Assert.assertEquals(expectedStatuses, statuses);
176 249
177   - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
178   - Assert.assertEquals(1, update.getCmdId());
179   - List<EntityData> eData = update.getUpdate();
180   - Assert.assertNotNull(eData);
181   - Assert.assertEquals(1, eData.size());
182   - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
183   - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
184   - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("fw_state");
185   - Assert.assertEquals("FAILED", tsValue.getValue());
186 250 client.destroy();
187 251 }
188 252
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.transport.lwm2m;
  17 +
  18 +import org.eclipse.leshan.client.object.Security;
  19 +import org.eclipse.leshan.core.util.Hex;
  20 +import org.junit.Test;
  21 +import org.thingsboard.server.common.data.device.credentials.lwm2m.PSKClientCredentials;
  22 +
  23 +import java.nio.charset.StandardCharsets;
  24 +
  25 +import static org.eclipse.leshan.client.object.Security.psk;
  26 +
  27 +public class PskLwm2mIntegrationTest extends AbstractLwM2MIntegrationTest {
  28 +
  29 + @Test
  30 + public void testConnectWithPSKAndObserveTelemetry() throws Exception {
  31 + String pskIdentity = "SOME_PSK_ID";
  32 + String pskKey = "73656372657450534b";
  33 + PSKClientCredentials clientCredentials = new PSKClientCredentials();
  34 + clientCredentials.setEndpoint(ENDPOINT);
  35 + clientCredentials.setKey(pskKey);
  36 + clientCredentials.setIdentity(pskIdentity);
  37 + Security security = psk(SECURE_URI,
  38 + 123,
  39 + pskIdentity.getBytes(StandardCharsets.UTF_8),
  40 + Hex.decodeHex(pskKey.toCharArray()));
  41 + super.basicTestConnectionObserveTelemetry(security, clientCredentials, SECURE_COAP_CONFIG, ENDPOINT);
  42 + }
  43 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.transport.lwm2m;
  17 +
  18 +import org.eclipse.leshan.client.object.Security;
  19 +import org.eclipse.leshan.core.util.Hex;
  20 +import org.junit.Test;
  21 +import org.thingsboard.server.common.data.device.credentials.lwm2m.RPKClientCredentials;
  22 +
  23 +import static org.eclipse.leshan.client.object.Security.rpk;
  24 +
  25 +public class RpkLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
  26 +
  27 + @Test
  28 + public void testConnectWithRPKAndObserveTelemetry() throws Exception {
  29 + RPKClientCredentials rpkClientCredentials = new RPKClientCredentials();
  30 + rpkClientCredentials.setEndpoint(ENDPOINT);
  31 + rpkClientCredentials.setKey(Hex.encodeHexString(clientPublicKey.getEncoded()));
  32 + Security security = rpk(SECURE_URI,
  33 + 123,
  34 + clientPublicKey.getEncoded(),
  35 + clientPrivateKey.getEncoded(),
  36 + serverX509Cert.getPublicKey().getEncoded());
  37 + super.basicTestConnectionObserveTelemetry(security, rpkClientCredentials, SECURE_COAP_CONFIG, ENDPOINT);
  38 + }
  39 +
  40 +}
... ...
... ... @@ -15,147 +15,40 @@
15 15 */
16 16 package org.thingsboard.server.transport.lwm2m;
17 17
18   -import org.eclipse.californium.core.network.config.NetworkConfig;
19 18 import org.eclipse.leshan.client.object.Security;
20   -import org.jetbrains.annotations.NotNull;
21   -import org.junit.Assert;
22 19 import org.junit.Ignore;
23 20 import org.junit.Test;
24   -import org.thingsboard.common.util.JacksonUtil;
25   -import org.thingsboard.server.common.data.Device;
26 21 import org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredentials;
27   -import org.thingsboard.server.common.data.query.EntityData;
28   -import org.thingsboard.server.common.data.query.EntityDataPageLink;
29   -import org.thingsboard.server.common.data.query.EntityDataQuery;
30   -import org.thingsboard.server.common.data.query.EntityKey;
31   -import org.thingsboard.server.common.data.query.EntityKeyType;
32   -import org.thingsboard.server.common.data.query.SingleEntityFilter;
33   -import org.thingsboard.server.common.data.security.DeviceCredentials;
34   -import org.thingsboard.server.common.data.security.DeviceCredentialsType;
35 22 import org.thingsboard.server.common.transport.util.SslUtil;
36   -import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;
37   -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;
38   -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;
39   -import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;
40   -import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;
41   -import org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MCredentials;
42   -
43   -import java.util.Collections;
44   -import java.util.List;
45 23
46 24 import static org.eclipse.leshan.client.object.Security.x509;
47   -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
48 25
49 26 public class X509LwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
50 27
51   - private final int port = 5686;
52   - private final NetworkConfig coapConfig = new NetworkConfig().setString("COAP_SECURE_PORT", Integer.toString(port));
53   - private final String endpoint = "deviceAEndpoint";
54   - private final String serverUri = "coaps://localhost:" + port;
55   -
56   - private Device createDevice(X509ClientCredentials clientCredentials) throws Exception {
57   - Device device = new Device();
58   - device.setName("Device A");
59   - device.setDeviceProfileId(deviceProfile.getId());
60   - device.setTenantId(tenantId);
61   - device = doPost("/api/device", device, Device.class);
62   - Assert.assertNotNull(device);
63   -
64   - DeviceCredentials deviceCredentials =
65   - doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);
66   - Assert.assertEquals(device.getId(), deviceCredentials.getDeviceId());
67   - deviceCredentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS);
68   -
69   - LwM2MCredentials credentials = new LwM2MCredentials();
70   -
71   - credentials.setClient(clientCredentials);
72   -
73   - deviceCredentials.setCredentialsValue(JacksonUtil.toString(credentials));
74   - doPost("/api/device/credentials", deviceCredentials).andExpect(status().isOk());
75   - return device;
76   - }
77   -
78   - //TODO: use different endpoints to isolate tests.
79   - @Ignore()
80 28 @Test
81 29 public void testConnectAndObserveTelemetry() throws Exception {
82   - createDeviceProfile(TRANSPORT_CONFIGURATION);
83 30 X509ClientCredentials credentials = new X509ClientCredentials();
84   - credentials.setEndpoint(endpoint+1);
85   - Device device = createDevice(credentials);
86   -
87   - SingleEntityFilter sef = new SingleEntityFilter();
88   - sef.setSingleEntity(device.getId());
89   - LatestValueCmd latestCmd = new LatestValueCmd();
90   - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
91   - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
92   - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
93   -
94   - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
95   - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
96   - wrapper.setEntityDataCmds(Collections.singletonList(cmd));
97   -
98   - wsClient.send(mapper.writeValueAsString(wrapper));
99   - wsClient.waitForReply();
100   -
101   - wsClient.registerWaitForUpdate();
102   - LwM2MTestClient client = new LwM2MTestClient(executor, endpoint+1);
103   - Security security = x509(serverUri, 123, clientX509Cert.getEncoded(), clientPrivateKeyFromCert.getEncoded(), serverX509Cert.getEncoded());
104   - client.init(security, coapConfig);
105   - String msg = wsClient.waitForUpdate();
106   -
107   - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
108   - Assert.assertEquals(1, update.getCmdId());
109   - List<EntityData> eData = update.getUpdate();
110   - Assert.assertNotNull(eData);
111   - Assert.assertEquals(1, eData.size());
112   - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
113   - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
114   - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
115   - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));
116   - client.destroy();
  31 + credentials.setEndpoint(ENDPOINT);
  32 + Security security = x509(SECURE_URI,
  33 + 123,
  34 + clientX509Cert.getEncoded(),
  35 + clientPrivateKeyFromCert.getEncoded(),
  36 + serverX509Cert.getEncoded());
  37 + super.basicTestConnectionObserveTelemetry(security, credentials, SECURE_COAP_CONFIG, ENDPOINT);
117 38 }
118 39
  40 + @Ignore //See LwM2mClientContextImpl.unregister
119 41 @Test
120 42 public void testConnectWithCertAndObserveTelemetry() throws Exception {
121   - createDeviceProfile(TRANSPORT_CONFIGURATION);
122 43 X509ClientCredentials credentials = new X509ClientCredentials();
123   - credentials.setEndpoint(endpoint);
  44 + credentials.setEndpoint(ENDPOINT);
124 45 credentials.setCert(SslUtil.getCertificateString(clientX509CertNotTrusted));
125   - Device device = createDevice(credentials);
126   -
127   - SingleEntityFilter sef = new SingleEntityFilter();
128   - sef.setSingleEntity(device.getId());
129   - LatestValueCmd latestCmd = new LatestValueCmd();
130   - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
131   - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
132   - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
133   -
134   - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
135   - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
136   - wrapper.setEntityDataCmds(Collections.singletonList(cmd));
137   -
138   - wsClient.send(mapper.writeValueAsString(wrapper));
139   - wsClient.waitForReply();
140   -
141   - wsClient.registerWaitForUpdate();
142   - LwM2MTestClient client = new LwM2MTestClient(executor, endpoint);
143   -
144   - Security security = x509(serverUri, 123, clientX509CertNotTrusted.getEncoded(), clientPrivateKeyFromCert.getEncoded(), serverX509Cert.getEncoded());
145   -
146   - client.init(security, coapConfig);
147   - String msg = wsClient.waitForUpdate();
148   -
149   - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
150   - Assert.assertEquals(1, update.getCmdId());
151   - List<EntityData> eData = update.getUpdate();
152   - Assert.assertNotNull(eData);
153   - Assert.assertEquals(1, eData.size());
154   - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
155   - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
156   - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
157   - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));
158   - client.destroy();
  46 + Security security = x509(SECURE_URI,
  47 + 123,
  48 + clientX509CertNotTrusted.getEncoded(),
  49 + clientPrivateKeyFromCert.getEncoded(),
  50 + serverX509Cert.getEncoded());
  51 + super.basicTestConnectionObserveTelemetry(security, credentials, SECURE_COAP_CONFIG, ENDPOINT);
159 52 }
160 53
161 54 }
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.transport.lwm2m.client;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
  20 +import org.eclipse.leshan.client.servers.ServerIdentity;
  21 +import org.eclipse.leshan.core.model.ObjectModel;
  22 +import org.eclipse.leshan.core.node.LwM2mResource;
  23 +import org.eclipse.leshan.core.response.ExecuteResponse;
  24 +import org.eclipse.leshan.core.response.ReadResponse;
  25 +import org.eclipse.leshan.core.response.WriteResponse;
  26 +
  27 +import javax.security.auth.Destroyable;
  28 +import java.util.Arrays;
  29 +import java.util.List;
  30 +import java.util.concurrent.Executors;
  31 +import java.util.concurrent.ScheduledExecutorService;
  32 +import java.util.concurrent.TimeUnit;
  33 +import java.util.concurrent.atomic.AtomicInteger;
  34 +
  35 +@Slf4j
  36 +public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
  37 +
  38 + private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 9);
  39 +
  40 + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  41 +
  42 + private final AtomicInteger state = new AtomicInteger(0);
  43 +
  44 + private final AtomicInteger updateResult = new AtomicInteger(0);
  45 +
  46 + @Override
  47 + public ReadResponse read(ServerIdentity identity, int resourceId) {
  48 + if (!identity.isSystem())
  49 + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
  50 + switch (resourceId) {
  51 + case 3:
  52 + return ReadResponse.success(resourceId, getState());
  53 + case 5:
  54 + return ReadResponse.success(resourceId, getUpdateResult());
  55 + case 6:
  56 + return ReadResponse.success(resourceId, getPkgName());
  57 + case 7:
  58 + return ReadResponse.success(resourceId, getPkgVersion());
  59 + case 9:
  60 + return ReadResponse.success(resourceId, getFirmwareUpdateDeliveryMethod());
  61 + default:
  62 + return super.read(identity, resourceId);
  63 + }
  64 + }
  65 +
  66 + @Override
  67 + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) {
  68 + String withParams = null;
  69 + if (params != null && params.length() != 0) {
  70 + withParams = " with params " + params;
  71 + }
  72 + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : "");
  73 +
  74 + switch (resourceId) {
  75 + case 2:
  76 + startUpdating();
  77 + return ExecuteResponse.success();
  78 + default:
  79 + return super.execute(identity, resourceId, params);
  80 + }
  81 + }
  82 +
  83 + @Override
  84 + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) {
  85 + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
  86 +
  87 + switch (resourceId) {
  88 + case 0:
  89 + startDownloading();
  90 + return WriteResponse.success();
  91 + case 1:
  92 + startDownloading();
  93 + return WriteResponse.success();
  94 + default:
  95 + return super.write(identity, replace, resourceId, value);
  96 + }
  97 + }
  98 +
  99 + private int getState() {
  100 + return state.get();
  101 + }
  102 +
  103 + private int getUpdateResult() {
  104 + return updateResult.get();
  105 + }
  106 +
  107 + private String getPkgName() {
  108 + return "firmware";
  109 + }
  110 +
  111 + private String getPkgVersion() {
  112 + return "1.0.0";
  113 + }
  114 +
  115 + private int getFirmwareUpdateDeliveryMethod() {
  116 + return 1;
  117 + }
  118 +
  119 + @Override
  120 + public List<Integer> getAvailableResourceIds(ObjectModel model) {
  121 + return supportedResources;
  122 + }
  123 +
  124 + @Override
  125 + public void destroy() {
  126 + scheduler.shutdownNow();
  127 + }
  128 +
  129 + private void startDownloading() {
  130 + scheduler.schedule(() -> {
  131 + state.set(1);
  132 + fireResourcesChange(3);
  133 + state.set(2);
  134 + fireResourcesChange(3);
  135 + }, 100, TimeUnit.MILLISECONDS);
  136 + }
  137 +
  138 + private void startUpdating() {
  139 + scheduler.schedule(() -> {
  140 + try {
  141 + state.set(3);
  142 + fireResourcesChange(3);
  143 + Thread.sleep(100);
  144 + updateResult.set(1);
  145 + fireResourcesChange(5);
  146 + } catch (Exception e) {
  147 +
  148 + }
  149 + }, 100, TimeUnit.MILLISECONDS);
  150 + }
  151 +
  152 +}
... ...
... ... @@ -53,8 +53,10 @@ import java.util.List;
53 53 import java.util.concurrent.ScheduledExecutorService;
54 54
55 55 import static org.eclipse.leshan.core.LwM2mId.DEVICE;
  56 +import static org.eclipse.leshan.core.LwM2mId.FIRMWARE;
56 57 import static org.eclipse.leshan.core.LwM2mId.SECURITY;
57 58 import static org.eclipse.leshan.core.LwM2mId.SERVER;
  59 +import static org.eclipse.leshan.core.LwM2mId.SOFTWARE_MANAGEMENT;
58 60
59 61 @Slf4j
60 62 @Data
... ... @@ -65,7 +67,7 @@ public class LwM2MTestClient {
65 67 private LeshanClient client;
66 68
67 69 public void init(Security security, NetworkConfig coapConfig) throws InvalidDDFFileException, IOException {
68   - String[] resources = new String[]{"0.xml", "1.xml", "2.xml", "3.xml"};
  70 + String[] resources = new String[]{"0.xml", "1.xml", "2.xml", "3.xml", "5.xml", "9.xml"};
69 71 List<ObjectModel> models = new ArrayList<>();
70 72 for (String resourceName : resources) {
71 73 models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName));
... ... @@ -75,6 +77,8 @@ public class LwM2MTestClient {
75 77 initializer.setInstancesForObject(SECURITY, security);
76 78 initializer.setInstancesForObject(SERVER, new Server(123, 300));
77 79 initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice());
  80 + initializer.setInstancesForObject(FIRMWARE, new FwLwM2MDevice());
  81 + initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, new SwLwM2MDevice());
78 82 initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class);
79 83
80 84 DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
... ...
... ... @@ -45,74 +45,74 @@ public class SimpleLwM2MDevice extends BaseInstanceEnabler implements Destroyabl
45 45 );
46 46
47 47 @Override
48   - public ReadResponse read(ServerIdentity identity, int resourceid) {
  48 + public ReadResponse read(ServerIdentity identity, int resourceId) {
49 49 if (!identity.isSystem())
50   - log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceid);
51   - switch (resourceid) {
  50 + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
  51 + switch (resourceId) {
52 52 case 0:
53   - return ReadResponse.success(resourceid, getManufacturer());
  53 + return ReadResponse.success(resourceId, getManufacturer());
54 54 case 1:
55   - return ReadResponse.success(resourceid, getModelNumber());
  55 + return ReadResponse.success(resourceId, getModelNumber());
56 56 case 2:
57   - return ReadResponse.success(resourceid, getSerialNumber());
  57 + return ReadResponse.success(resourceId, getSerialNumber());
58 58 case 3:
59   - return ReadResponse.success(resourceid, getFirmwareVersion());
  59 + return ReadResponse.success(resourceId, getFirmwareVersion());
60 60 case 9:
61   - return ReadResponse.success(resourceid, getBatteryLevel());
  61 + return ReadResponse.success(resourceId, getBatteryLevel());
62 62 case 10:
63   - return ReadResponse.success(resourceid, getMemoryFree());
  63 + return ReadResponse.success(resourceId, getMemoryFree());
64 64 case 11:
65 65 Map<Integer, Long> errorCodes = new HashMap<>();
66 66 errorCodes.put(0, getErrorCode());
67   - return ReadResponse.success(resourceid, errorCodes, ResourceModel.Type.INTEGER);
  67 + return ReadResponse.success(resourceId, errorCodes, ResourceModel.Type.INTEGER);
68 68 case 14:
69   - return ReadResponse.success(resourceid, getUtcOffset());
  69 + return ReadResponse.success(resourceId, getUtcOffset());
70 70 case 15:
71   - return ReadResponse.success(resourceid, getTimezone());
  71 + return ReadResponse.success(resourceId, getTimezone());
72 72 case 16:
73   - return ReadResponse.success(resourceid, getSupportedBinding());
  73 + return ReadResponse.success(resourceId, getSupportedBinding());
74 74 case 17:
75   - return ReadResponse.success(resourceid, getDeviceType());
  75 + return ReadResponse.success(resourceId, getDeviceType());
76 76 case 18:
77   - return ReadResponse.success(resourceid, getHardwareVersion());
  77 + return ReadResponse.success(resourceId, getHardwareVersion());
78 78 case 19:
79   - return ReadResponse.success(resourceid, getSoftwareVersion());
  79 + return ReadResponse.success(resourceId, getSoftwareVersion());
80 80 case 20:
81   - return ReadResponse.success(resourceid, getBatteryStatus());
  81 + return ReadResponse.success(resourceId, getBatteryStatus());
82 82 case 21:
83   - return ReadResponse.success(resourceid, getMemoryTotal());
  83 + return ReadResponse.success(resourceId, getMemoryTotal());
84 84 default:
85   - return super.read(identity, resourceid);
  85 + return super.read(identity, resourceId);
86 86 }
87 87 }
88 88
89 89 @Override
90   - public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
  90 + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) {
91 91 String withParams = null;
92 92 if (params != null && params.length() != 0) {
93 93 withParams = " with params " + params;
94 94 }
95   - log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceid, withParams != null ? withParams : "");
  95 + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : "");
96 96 return ExecuteResponse.success();
97 97 }
98 98
99 99 @Override
100   - public WriteResponse write(ServerIdentity identity, boolean replace, int resourceid, LwM2mResource value) {
101   - log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceid);
  100 + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) {
  101 + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
102 102
103   - switch (resourceid) {
  103 + switch (resourceId) {
104 104 case 13:
105 105 return WriteResponse.notFound();
106 106 case 14:
107 107 setUtcOffset((String) value.getValue());
108   - fireResourcesChange(resourceid);
  108 + fireResourcesChange(resourceId);
109 109 return WriteResponse.success();
110 110 case 15:
111 111 setTimezone((String) value.getValue());
112   - fireResourcesChange(resourceid);
  112 + fireResourcesChange(resourceId);
113 113 return WriteResponse.success();
114 114 default:
115   - return super.write(identity, replace, resourceid, value);
  115 + return super.write(identity, replace, resourceId, value);
116 116 }
117 117 }
118 118
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.transport.lwm2m.client;
  17 +
  18 +import lombok.SneakyThrows;
  19 +import lombok.extern.slf4j.Slf4j;
  20 +import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
  21 +import org.eclipse.leshan.client.servers.ServerIdentity;
  22 +import org.eclipse.leshan.core.model.ObjectModel;
  23 +import org.eclipse.leshan.core.node.LwM2mResource;
  24 +import org.eclipse.leshan.core.response.ExecuteResponse;
  25 +import org.eclipse.leshan.core.response.ReadResponse;
  26 +import org.eclipse.leshan.core.response.WriteResponse;
  27 +
  28 +import javax.security.auth.Destroyable;
  29 +import java.util.Arrays;
  30 +import java.util.List;
  31 +import java.util.concurrent.Executors;
  32 +import java.util.concurrent.ScheduledExecutorService;
  33 +import java.util.concurrent.TimeUnit;
  34 +import java.util.concurrent.atomic.AtomicInteger;
  35 +
  36 +@Slf4j
  37 +public class SwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
  38 +
  39 + private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 4, 6, 7, 9);
  40 +
  41 + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  42 +
  43 + private final AtomicInteger state = new AtomicInteger(0);
  44 +
  45 + private final AtomicInteger updateResult = new AtomicInteger(0);
  46 +
  47 + @Override
  48 + public ReadResponse read(ServerIdentity identity, int resourceId) {
  49 + if (!identity.isSystem())
  50 + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
  51 + switch (resourceId) {
  52 + case 0:
  53 + return ReadResponse.success(resourceId, getPkgName());
  54 + case 1:
  55 + return ReadResponse.success(resourceId, getPkgVersion());
  56 + case 7:
  57 + return ReadResponse.success(resourceId, getUpdateState());
  58 + case 9:
  59 + return ReadResponse.success(resourceId, getUpdateResult());
  60 + default:
  61 + return super.read(identity, resourceId);
  62 + }
  63 + }
  64 +
  65 + @Override
  66 + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) {
  67 + String withParams = null;
  68 + if (params != null && params.length() != 0) {
  69 + withParams = " with params " + params;
  70 + }
  71 + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : "");
  72 +
  73 + switch (resourceId) {
  74 + case 4:
  75 + startUpdating();
  76 + return ExecuteResponse.success();
  77 + case 6:
  78 + return ExecuteResponse.success();
  79 + default:
  80 + return super.execute(identity, resourceId, params);
  81 + }
  82 + }
  83 +
  84 + @Override
  85 + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) {
  86 + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId);
  87 +
  88 + switch (resourceId) {
  89 + case 2:
  90 + startDownloading();
  91 + return WriteResponse.success();
  92 + case 3:
  93 + startDownloading();
  94 + return WriteResponse.success();
  95 + default:
  96 + return super.write(identity, replace, resourceId, value);
  97 + }
  98 + }
  99 +
  100 + private int getUpdateState() {
  101 + return state.get();
  102 + }
  103 +
  104 + private int getUpdateResult() {
  105 + return updateResult.get();
  106 + }
  107 +
  108 + private String getPkgName() {
  109 + return "software";
  110 + }
  111 +
  112 + private String getPkgVersion() {
  113 + return "1.0.0";
  114 + }
  115 +
  116 + @Override
  117 + public List<Integer> getAvailableResourceIds(ObjectModel model) {
  118 + return supportedResources;
  119 + }
  120 +
  121 + @Override
  122 + public void destroy() {
  123 + scheduler.shutdownNow();
  124 + }
  125 +
  126 + private void startDownloading() {
  127 + scheduler.schedule(() -> {
  128 + try {
  129 + state.set(1);
  130 + updateResult.set(1);
  131 + fireResourcesChange(7, 9);
  132 + Thread.sleep(100);
  133 + state.set(2);
  134 + fireResourcesChange(7);
  135 + Thread.sleep(100);
  136 + state.set(3);
  137 + fireResourcesChange(7);
  138 + Thread.sleep(100);
  139 + updateResult.set(3);
  140 + fireResourcesChange(9);
  141 + } catch (Exception e) {
  142 +
  143 + }
  144 + }, 100, TimeUnit.MILLISECONDS);
  145 + }
  146 +
  147 + private void startUpdating() {
  148 + scheduler.schedule(() -> {
  149 + state.set(4);
  150 + updateResult.set(2);
  151 + fireResourcesChange(7, 9);
  152 + }, 100, TimeUnit.MILLISECONDS);
  153 + }
  154 +
  155 +}
... ...
... ... @@ -3,4 +3,7 @@ transport.lwm2m.security.key_store_password=server
3 3 edges.enabled=true
4 4 edges.storage.no_read_records_sleep=500
5 5 edges.storage.sleep_between_batches=500
6   -transport.lwm2m.bootstrap.security.alias=server
\ No newline at end of file
  6 +transport.lwm2m.server.security.key_alias=server
  7 +transport.lwm2m.server.security.key_password=server
  8 +transport.lwm2m.bootstrap.security.key_alias=server
  9 +transport.lwm2m.bootstrap.security.key_password=server
\ No newline at end of file
... ...
... ... @@ -14,7 +14,7 @@
14 14 <logger name="org.springframework.boot.test" level="WARN"/>
15 15 <logger name="org.apache.cassandra" level="WARN"/>
16 16 <logger name="org.cassandraunit" level="INFO"/>
17   - <logger name="org.eclipse.leshan" level="TRACE"/>
  17 + <logger name="org.eclipse.leshan" level="INFO"/>
18 18
19 19
20 20 <root level="WARN">
... ...
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +
  3 +<!--
  4 +FILE INFORMATION
  5 +
  6 +OMA Permanent Document
  7 + File: OMA-SUP-XML_LWM2M_Firmware_Update-V1_0_3-20190617-A
  8 + Type: xml
  9 + Date: 2019-Jun-17
  10 +
  11 +Public Reachable Information
  12 + Path: http://www.openmobilealliance.org/tech/profiles
  13 + Name: LWM2M_Firmware_Update-v1_0_3.xml
  14 +
  15 +NORMATIVE INFORMATION
  16 +
  17 + Information about this file can be found in the latest revision of
  18 +
  19 + OMA-TS-LightweightM2M-V1_0_2
  20 + OMA-TS-LightweightM2M_Core-V1_1_1
  21 +
  22 + This is available at http://www.openmobilealliance.org/
  23 +
  24 + Send comments to https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues
  25 +
  26 +LEGAL DISCLAIMER
  27 +
  28 + Copyright 2019 Open Mobile Alliance.
  29 +
  30 + Redistribution and use in source and binary forms, with or without
  31 + modification, are permitted provided that the following conditions
  32 + are met:
  33 +
  34 + 1. Redistributions of source code must retain the above copyright
  35 + notice, this list of conditions and the following disclaimer.
  36 + 2. Redistributions in binary form must reproduce the above copyright
  37 + notice, this list of conditions and the following disclaimer in the
  38 + documentation and/or other materials provided with the distribution.
  39 + 3. Neither the name of the copyright holder nor the names of its
  40 + contributors may be used to endorse or promote products derived
  41 + from this software without specific prior written permission.
  42 +
  43 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44 + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46 + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  47 + COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  48 + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  49 + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50 + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51 + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52 + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  53 + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54 + POSSIBILITY OF SUCH DAMAGE.
  55 +
  56 + The above license is used as a license under copyright only. Please
  57 + reference the OMA IPR Policy for patent licensing terms:
  58 + https://www.omaspecworks.org/about/intellectual-property-rights/
  59 +
  60 +-->
  61 +
  62 +<LWM2M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.openmobilealliance.org/tech/profiles/LWM2M.xsd">
  63 + <Object ObjectType="MODefinition">
  64 + <Name>Firmware Update V_1_0</Name>
  65 + <Description1><![CDATA[This LwM2M Object enables management of firmware which is to be updated. This Object includes installing a firmware package, updating firmware, and performing actions after updating firmware. The firmware update MAY require to reboot the device; it will depend on a number of factors, such as the operating system architecture and the extent of the updated software.
  66 +The envisioned functionality is to allow a LwM2M Client to connect to any LwM2M Server to obtain a firmware image using the object and resource structure defined in this section experiencing communication security protection using TLS/DTLS. There are, however, other design decisions that need to be taken into account to allow a manufacturer of a device to securely install firmware on a device. Examples for such design decisions are how to manage the firmware update repository at the server side (which may include user interface considerations), the techniques to provide additional application layer security protection of the firmware image, how many versions of firmware images to store on the device, and how to execute the firmware update process considering the hardware specific details of a given IoT hardware product. These aspects are considered to be outside the scope of this version of the specification.
  67 +A LwM2M Server may also instruct a LwM2M Client to fetch a firmware image from a dedicated server (instead of pushing firmware images to the LwM2M Client). The Package URI resource is contained in the Firmware object and can be used for this purpose.
  68 +A LwM2M Client MUST support block-wise transfer [CoAP_Blockwise] if it implements the Firmware Update object.
  69 +A LwM2M Server MUST support block-wise transfer. Other protocols, such as HTTP/HTTPs, MAY also be used for downloading firmware updates (via the Package URI resource). For constrained devices it is, however, RECOMMENDED to use CoAP for firmware downloads to avoid the need for additional protocol implementations.]]></Description1>
  70 + <ObjectID>5</ObjectID>
  71 + <ObjectURN>urn:oma:lwm2m:oma:5</ObjectURN>
  72 + <LWM2MVersion>1.0</LWM2MVersion>
  73 + <ObjectVersion>1.0</ObjectVersion>
  74 + <MultipleInstances>Single</MultipleInstances>
  75 + <Mandatory>Optional</Mandatory>
  76 + <Resources>
  77 + <Item ID="0">
  78 + <Name>Package</Name>
  79 + <Operations>W</Operations>
  80 + <MultipleInstances>Single</MultipleInstances>
  81 + <Mandatory>Mandatory</Mandatory>
  82 + <Type>Opaque</Type>
  83 + <RangeEnumeration></RangeEnumeration>
  84 + <Units></Units>
  85 + <Description><![CDATA[Firmware package]]></Description>
  86 + </Item>
  87 + <Item ID="1">
  88 + <Name>Package URI</Name>
  89 + <Operations>RW</Operations>
  90 + <MultipleInstances>Single</MultipleInstances>
  91 + <Mandatory>Mandatory</Mandatory>
  92 + <Type>String</Type>
  93 + <RangeEnumeration>0..255</RangeEnumeration>
  94 + <Units></Units>
  95 + <Description><![CDATA[URI from where the device can download the firmware package by an alternative mechanism. As soon the device has received the Package URI it performs the download at the next practical opportunity.
  96 +The URI format is defined in RFC 3986. For example, coaps://example.org/firmware is a syntactically valid URI. The URI scheme determines the protocol to be used. For CoAP this endpoint MAY be a LwM2M Server but does not necessarily need to be. A CoAP server implementing block-wise transfer is sufficient as a server hosting a firmware repository and the expectation is that this server merely serves as a separate file server making firmware images available to LwM2M Clients.]]></Description>
  97 + </Item>
  98 + <Item ID="2">
  99 + <Name>Update</Name>
  100 + <Operations>E</Operations>
  101 + <MultipleInstances>Single</MultipleInstances>
  102 + <Mandatory>Mandatory</Mandatory>
  103 + <Type></Type>
  104 + <RangeEnumeration></RangeEnumeration>
  105 + <Units></Units>
  106 + <Description><![CDATA[Updates firmware by using the firmware package stored in Package, or, by using the firmware downloaded from the Package URI.
  107 +This Resource is only executable when the value of the State Resource is Downloaded.]]></Description>
  108 + </Item>
  109 + <Item ID="3">
  110 + <Name>State</Name>
  111 + <Operations>R</Operations>
  112 + <MultipleInstances>Single</MultipleInstances>
  113 + <Mandatory>Mandatory</Mandatory>
  114 + <Type>Integer</Type>
  115 + <RangeEnumeration>0..3</RangeEnumeration>
  116 + <Units></Units>
  117 + <Description><![CDATA[Indicates current state with respect to this firmware update. This value is set by the LwM2M Client.
  118 +0: Idle (before downloading or after successful updating)
  119 +1: Downloading (The data sequence is on the way)
  120 +2: Downloaded
  121 +3: Updating
  122 +If writing the firmware package to Package Resource has completed, or, if the device has downloaded the firmware package from the Package URI the state changes to Downloaded.
  123 +Writing an empty string to Package URI Resource or setting the Package Resource to NULL (‘\0’), resets the Firmware Update State Machine: the State Resource value is set to Idle and the Update Result Resource value is set to 0.
  124 +When in Downloaded state, and the executable Resource Update is triggered, the state changes to Updating.
  125 +If the Update Resource failed, the state returns at Downloaded.
  126 +If performing the Update Resource was successful, the state changes from Updating to Idle.
  127 +The firmware update state machine is illustrated in Figure 29 of the LwM2M version 1.0 specification (and also in Figure E.6.1-1 of this specification).]]></Description>
  128 + </Item>
  129 + <Item ID="5">
  130 + <Name>Update Result</Name>
  131 + <Operations>R</Operations>
  132 + <MultipleInstances>Single</MultipleInstances>
  133 + <Mandatory>Mandatory</Mandatory>
  134 + <Type>Integer</Type>
  135 + <RangeEnumeration>0..9</RangeEnumeration>
  136 + <Units></Units>
  137 + <Description><![CDATA[Contains the result of downloading or updating the firmware
  138 +0: Initial value. Once the updating process is initiated (Download /Update), this Resource MUST be reset to Initial value.
  139 +1: Firmware updated successfully.
  140 +2: Not enough flash memory for the new firmware package.
  141 +3: Out of RAM during downloading process.
  142 +4: Connection lost during downloading process.
  143 +5: Integrity check failure for new downloaded package.
  144 +6: Unsupported package type.
  145 +7: Invalid URI.
  146 +8: Firmware update failed.
  147 +9: Unsupported protocol. A LwM2M client indicates the failure to retrieve the firmware image using the URI provided in the Package URI resource by writing the value 9 to the /5/0/5 (Update Result resource) when the URI contained a URI scheme unsupported by the client. Consequently, the LwM2M Client is unable to retrieve the firmware image using the URI provided by the LwM2M Server in the Package URI when it refers to an unsupported protocol.]]></Description>
  148 + </Item>
  149 + <Item ID="6">
  150 + <Name>PkgName</Name>
  151 + <Operations>R</Operations>
  152 + <MultipleInstances>Single</MultipleInstances>
  153 + <Mandatory>Optional</Mandatory>
  154 + <Type>String</Type>
  155 + <RangeEnumeration>0..255</RangeEnumeration>
  156 + <Units></Units>
  157 + <Description><![CDATA[Name of the Firmware Package]]></Description>
  158 + </Item>
  159 + <Item ID="7">
  160 + <Name>PkgVersion</Name>
  161 + <Operations>R</Operations>
  162 + <MultipleInstances>Single</MultipleInstances>
  163 + <Mandatory>Optional</Mandatory>
  164 + <Type>String</Type>
  165 + <RangeEnumeration>0..255</RangeEnumeration>
  166 + <Units></Units>
  167 + <Description><![CDATA[Version of the Firmware package]]></Description>
  168 + </Item>
  169 + <Item ID="8">
  170 + <Name>Firmware Update Protocol Support</Name>
  171 + <Operations>R</Operations>
  172 + <MultipleInstances>Multiple</MultipleInstances>
  173 + <Mandatory>Optional</Mandatory>
  174 + <Type>Integer</Type>
  175 + <RangeEnumeration>0..5</RangeEnumeration>
  176 + <Units></Units>
  177 + <Description><![CDATA[This resource indicates what protocols the LwM2M Client implements to retrieve firmware images. The LwM2M server uses this information to decide what URI to include in the Package URI. A LwM2M Server MUST NOT include a URI in the Package URI object that uses a protocol that is unsupported by the LwM2M client.
  178 +For example, if a LwM2M client indicates that it supports CoAP and CoAPS then a LwM2M Server must not provide an HTTP URI in the Packet URI.
  179 +The following values are defined by this version of the specification:
  180 +0: CoAP (as defined in RFC 7252) with the additional support for block-wise transfer. CoAP is the default setting.
  181 +1: CoAPS (as defined in RFC 7252) with the additional support for block-wise transfer
  182 +2: HTTP 1.1 (as defined in RFC 7230)
  183 +3: HTTPS 1.1 (as defined in RFC 7230)
  184 +4: CoAP over TCP (as defined in RFC 8323)
  185 +5: CoAP over TLS (as defined in RFC 8323)
  186 +Additional values MAY be defined in the future. Any value not understood by the LwM2M Server MUST be ignored.]]></Description>
  187 + </Item>
  188 + <Item ID="9">
  189 + <Name>Firmware Update Delivery Method</Name>
  190 + <Operations>R</Operations>
  191 + <MultipleInstances>Single</MultipleInstances>
  192 + <Mandatory>Mandatory</Mandatory>
  193 + <Type>Integer</Type>
  194 + <RangeEnumeration>0..2</RangeEnumeration>
  195 + <Units></Units>
  196 + <Description><![CDATA[The LwM2M Client uses this resource to indicate its support for transferring firmware images to the client either via the Package Resource (=push) or via the Package URI Resource (=pull) mechanism.
  197 +0: Pull only
  198 +1: Push only
  199 +2: Both. In this case the LwM2M Server MAY choose the preferred mechanism for conveying the firmware image to the LwM2M Client.]]></Description>
  200 + </Item>
  201 + </Resources>
  202 + <Description2><![CDATA[]]></Description2>
  203 + </Object>
  204 +</LWM2M>
... ...
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +
  3 +<!--
  4 +FILE INFORMATION
  5 +
  6 +OMA Permanent Document
  7 + File: OMA-SUP-XML_9-V1_0_2-20210119-A.xml
  8 + Path: http://www.openmobilealliance.org/release/LWM2M_SWMGMT
  9 +
  10 +OMNA LwM2M Registry
  11 + Path: https://github.com/OpenMobileAlliance/lwm2m-registry
  12 + Name: 9.xml
  13 +
  14 +NORMATIVE INFORMATION
  15 +
  16 + Information about this file can be found in the latest revision of
  17 +
  18 + OMA-TS-LightweightM2M-V1_0
  19 +
  20 + This is available at http://www.openmobilealliance.org/release/LightweightM2M/
  21 +
  22 + Send comments to https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues
  23 +
  24 +LEGAL DISCLAIMER
  25 +
  26 + Copyright 2021 Open Mobile Alliance.
  27 +
  28 + Redistribution and use in source and binary forms, with or without
  29 + modification, are permitted provided that the following conditions
  30 + are met:
  31 +
  32 + 1. Redistributions of source code must retain the above copyright
  33 + notice, this list of conditions and the following disclaimer.
  34 + 2. Redistributions in binary form must reproduce the above copyright
  35 + notice, this list of conditions and the following disclaimer in the
  36 + documentation and/or other materials provided with the distribution.
  37 + 3. Neither the name of the copyright holder nor the names of its
  38 + contributors may be used to endorse or promote products derived
  39 + from this software without specific prior written permission.
  40 +
  41 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  42 + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44 + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  45 + COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  46 + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  47 + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48 + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49 + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50 + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  51 + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  52 + POSSIBILITY OF SUCH DAMAGE.
  53 +
  54 + The above license is used as a license under copyright only. Please
  55 + reference the OMA IPR Policy for patent licensing terms:
  56 + https://www.omaspecworks.org/about/intellectual-property-rights/
  57 +
  58 +-->
  59 +
  60 +<LWM2M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://openmobilealliance.org/tech/profiles/LWM2M.xsd">
  61 + <Object ObjectType="MODefinition">
  62 + <Name>LWM2M Software Management</Name>
  63 + <Description1><![CDATA[This LwM2M objects provides the resources needed to perform software management on the device. Each software component is managed via a dedicated Software Management Object instance.]]></Description1>
  64 + <ObjectID>9</ObjectID>
  65 + <ObjectURN>urn:oma:lwm2m:oma:9</ObjectURN>
  66 + <LWM2MVersion>1.0</LWM2MVersion>
  67 + <ObjectVersion>1.0</ObjectVersion>
  68 + <MultipleInstances>Multiple</MultipleInstances>
  69 + <Mandatory>Optional</Mandatory>
  70 + <Resources>
  71 + <Item ID="0">
  72 + <Name>PkgName</Name>
  73 + <Operations>R</Operations>
  74 + <MultipleInstances>Single</MultipleInstances>
  75 + <Mandatory>Mandatory</Mandatory>
  76 + <Type>String</Type>
  77 + <RangeEnumeration>0..255 bytes</RangeEnumeration>
  78 + <Units></Units>
  79 + <Description><![CDATA[Name of the software package]]></Description>
  80 + </Item>
  81 + <Item ID="1">
  82 + <Name>PkgVersion</Name>
  83 + <Operations>R</Operations>
  84 + <MultipleInstances>Single</MultipleInstances>
  85 + <Mandatory>Mandatory</Mandatory>
  86 + <Type>String</Type>
  87 + <RangeEnumeration>0..255 bytes</RangeEnumeration>
  88 + <Units></Units>
  89 + <Description><![CDATA[Version of the software package]]></Description>
  90 + </Item>
  91 + <Item ID="2">
  92 + <Name>Package</Name>
  93 + <Operations>W</Operations>
  94 + <MultipleInstances>Single</MultipleInstances>
  95 + <Mandatory>Optional</Mandatory>
  96 + <Type>Opaque</Type>
  97 + <RangeEnumeration></RangeEnumeration>
  98 + <Units></Units>
  99 + <Description><![CDATA[Software package
  100 +The package can be in one single software component, or any delivery material used by the Device to determine the software component(s) and proceed to their installation.
  101 +Can be archive file, executable, manifest. This resource to be used when it is single block of delivery.
  102 +]]> </Description>
  103 + </Item>
  104 + <Item ID="3">
  105 + <Name>Package URI</Name>
  106 + <Operations>W</Operations>
  107 + <MultipleInstances>Single</MultipleInstances>
  108 + <Mandatory>Optional</Mandatory>
  109 + <Type>String</Type>
  110 + <RangeEnumeration>0..255 bytes</RangeEnumeration>
  111 + <Units></Units>
  112 + <Description><![CDATA[URI from where the device can download the software package by an alternative mechanism. As soon the device has received the Package URI it performs the download at the next practical opportunity.
  113 +Can be direct link to a single software component or link to archive file, executable, or manifest, used by the Device to determine, then access to the software component(s). This resource to be used when it is single block of delivery.
  114 +]]> </Description>
  115 + </Item>
  116 +
  117 + <Item ID="4">
  118 + <Name>Install</Name>
  119 + <Operations>E</Operations>
  120 + <MultipleInstances>Single</MultipleInstances>
  121 + <Mandatory>Mandatory</Mandatory>
  122 + <Type></Type>
  123 + <RangeEnumeration></RangeEnumeration>
  124 + <Units></Units>
  125 + <Description><![CDATA[Installs software from the package either stored in Package resource, or, downloaded from the Package URI. This Resource is only executable when the value of the State Resource is DELIVERED.]]></Description>
  126 + </Item>
  127 + <Item ID="5">
  128 + <Name>Checkpoint</Name>
  129 + <Operations>R</Operations>
  130 + <MultipleInstances>Single</MultipleInstances>
  131 + <Mandatory>Optional</Mandatory>
  132 + <Type>Objlnk</Type>
  133 + <RangeEnumeration></RangeEnumeration>
  134 + <Units></Units>
  135 + <Description><![CDATA[Link to a "Checkpoint" object which allows to specify conditions/dependencies for a software update. E.g. power connected, sufficient memory, target system.]]></Description>
  136 + </Item>
  137 + <Item ID="6">
  138 + <Name>Uninstall</Name>
  139 + <Operations>E</Operations>
  140 + <MultipleInstances>Single</MultipleInstances>
  141 + <Mandatory>Mandatory</Mandatory>
  142 + <Type></Type>
  143 + <RangeEnumeration></RangeEnumeration>
  144 + <Units></Units>
  145 + <Description><![CDATA[Uninstalls the software package.
  146 +This executable resource may have one argument.
  147 +If used with no argument or argument is 0, the Package is removed i from the Device. If the argument is 1 ("ForUpdate"), the Client MUST prepare itself for receiving a Package used to upgrade the Software already in place. Update State is set back to INITIAL state.
  148 +]]> </Description>
  149 + </Item>
  150 + <Item ID="7">
  151 + <Name>Update State</Name>
  152 + <Operations>R</Operations>
  153 + <MultipleInstances>Single</MultipleInstances>
  154 + <Mandatory>Mandatory</Mandatory>
  155 + <Type>Integer</Type>
  156 + <RangeEnumeration>0..4</RangeEnumeration>
  157 + <Units></Units>
  158 + <Description><![CDATA[Indicates current state with respect to this software update. This value is set by the LwM2M Client.
  159 +0: INITIAL
  160 +Before downloading.
  161 +(see 5.1.2.1)
  162 +1: DOWNLOAD STARTED
  163 +The downloading process has started and is on-going.
  164 +(see 5.1.2.2)
  165 +2: DOWNLOADED
  166 +The package has been completely downloaded
  167 +(see 5.1.2.3)
  168 +3: DELIVERED
  169 +In that state, the package has been correctly downloaded and is ready to be installed.
  170 +(see 5.1.2.4)
  171 +If executing the Install Resource failed, the state remains at DELIVERED.
  172 +If executing the Install Resource was successful, the state changes from DELIVERED to INSTALLED.
  173 +After executing the UnInstall Resource, the state changes to INITIAL.
  174 +4: INSTALLED
  175 +In that state the software is correctly installed and can be activated or deactivated according to the Activation State Machine.
  176 +(see 5.1.2.5)
  177 +]]> </Description>
  178 + </Item>
  179 + <Item ID="8">
  180 + <Name>Update Supported Objects</Name>
  181 + <Operations>RW</Operations>
  182 + <MultipleInstances>Single</MultipleInstances>
  183 + <Mandatory>Optional</Mandatory>
  184 + <Type>Boolean</Type>
  185 + <RangeEnumeration></RangeEnumeration>
  186 + <Units></Units>
  187 + <Description><![CDATA[If this value is true, the LwM2M Client MUST inform the registered LwM2M Servers of Objects and Object Instances parameter by sending an Update or Registration message after the software update operation at the next practical opportunity if supported Objects in the LwM2M Client have changed, in order for the LwM2M Servers to promptly manage newly installed Objects.
  188 +If false, Objects and Object Instances parameter MUST be reported at the next periodic Update message.
  189 +The default value is false.
  190 +]]> </Description>
  191 + </Item>
  192 + <Item ID="9">
  193 + <Name>Update Result</Name>
  194 + <Operations>R</Operations>
  195 + <MultipleInstances>Single</MultipleInstances>
  196 + <Mandatory>Mandatory</Mandatory>
  197 + <Type>Integer</Type>
  198 + <RangeEnumeration>0..200</RangeEnumeration>
  199 + <Units></Units>
  200 + <Description><![CDATA[Contains the result of downloading or installing/uninstalling the software
  201 +0: Initial value. Prior to download any new package in the Device, Update Result MUST be reset to this initial value. One side effect of executing the Uninstall resource is to reset Update Result to this initial value "0".
  202 +1: Downloading. The package downloading process is on-going.
  203 +2: Software successfully installed.
  204 +3: Successfully Downloaded and package integrity verified
  205 +(( 4-49, for expansion, of other scenarios))
  206 +50: Not enough storage for the new software package.
  207 +51: Out of memory during downloading process.
  208 +52: Connection lost during downloading process.
  209 +53: Package integrity check failure.
  210 +54: Unsupported package type.
  211 +56: Invalid URI
  212 +57: Device defined update error
  213 +58: Software installation failure
  214 +59: Uninstallation Failure during forUpdate(arg=0)
  215 +60-200 : (for expansion, selection to be in blocks depending on new introduction of features)
  216 +This Resource MAY be reported by sending Observe operation.
  217 +]]> </Description>
  218 + </Item>
  219 + <Item ID="10">
  220 + <Name>Activate</Name>
  221 + <Operations>E</Operations>
  222 + <MultipleInstances>Single</MultipleInstances>
  223 + <Mandatory>Mandatory</Mandatory>
  224 + <Type></Type>
  225 + <RangeEnumeration></RangeEnumeration>
  226 + <Units></Units>
  227 + <Description><![CDATA[This action activates the software previously successfully installed (the Package Installation State Machine is currently in the INSTALLED state)]]></Description>
  228 + </Item>
  229 + <Item ID="11">
  230 + <Name>Deactivate</Name>
  231 + <Operations>E</Operations>
  232 + <MultipleInstances>Single</MultipleInstances>
  233 + <Mandatory>Mandatory</Mandatory>
  234 + <Type></Type>
  235 + <RangeEnumeration></RangeEnumeration>
  236 + <Units></Units>
  237 + <Description><![CDATA[This action deactivates softwareif the Package Installation State Machine is currently in the INSTALLED state.]]></Description>
  238 + </Item>
  239 + <Item ID="12">
  240 + <Name>Activation State</Name>
  241 + <Operations>R</Operations>
  242 + <MultipleInstances>Single</MultipleInstances>
  243 + <Mandatory>Mandatory</Mandatory>
  244 + <Type>Boolean</Type>
  245 + <RangeEnumeration></RangeEnumeration>
  246 + <Units></Units>
  247 + <Description><![CDATA[Indicates the current activation state of this software:
  248 +0: DISABLED
  249 +Activation State is DISABLED if the Software Activation State Machine is in the INACTIVE state or not alive.
  250 +1: ENABLED
  251 +Activation State is ENABLED only if the Software Activation State Machine is in the ACTIVE state
  252 +]]> </Description>
  253 + </Item>
  254 + <Item ID="13">
  255 + <Name>Package Settings</Name>
  256 + <Operations>RW</Operations>
  257 + <MultipleInstances>Single</MultipleInstances>
  258 + <Mandatory>Optional</Mandatory>
  259 + <Type>Objlnk</Type>
  260 + <RangeEnumeration></RangeEnumeration>
  261 + <Units></Units>
  262 + <Description><![CDATA[Link to "Package Settings" object which allows to modify at any time software configuration settings. This is an application specific object.
  263 +Note: OMA might provide a template for a Package Settings object in a future release of this specification.
  264 +]]> </Description>
  265 + </Item>
  266 + <Item ID="14">
  267 + <Name>User Name</Name>
  268 + <Operations>W</Operations>
  269 + <MultipleInstances>Single</MultipleInstances>
  270 + <Mandatory>Optional</Mandatory>
  271 + <Type>String</Type>
  272 + <RangeEnumeration>0..255 bytes</RangeEnumeration>
  273 + <Units></Units>
  274 + <Description><![CDATA[User Name for access to SW Update Package in pull mode.
  275 +Key based mechanism can alternatively use for talking to the component server instead of user name and password combination.
  276 +]]> </Description>
  277 + </Item>
  278 + <Item ID="15">
  279 + <Name>Password</Name>
  280 + <Operations>W</Operations>
  281 + <MultipleInstances>Single</MultipleInstances>
  282 + <Mandatory>Optional</Mandatory>
  283 + <Type>String</Type>
  284 + <RangeEnumeration>0..255 bytes</RangeEnumeration>
  285 + <Units></Units>
  286 + <Description><![CDATA[Password for access to SW Update Package in pull mode.]]></Description>
  287 + </Item>
  288 + <Item ID="16">
  289 + <Name>Status Reason</Name>
  290 + <Operations>R</Operations>
  291 + <MultipleInstances>Single</MultipleInstances>
  292 + <Mandatory>Optional</Mandatory>
  293 + <Type>String</Type>
  294 + <RangeEnumeration></RangeEnumeration>
  295 + <Units></Units>
  296 + <Description><![CDATA[Contains the status of the actions done by the client on the SW Component(s) referred by the present SW Update Package. The status is defined in Appendix B.]]></Description>
  297 + </Item>
  298 + <Item ID="17">
  299 + <Name>Software Component Link</Name>
  300 + <Operations>R</Operations>
  301 + <MultipleInstances>Multiple</MultipleInstances>
  302 + <Mandatory>Optional</Mandatory>
  303 + <Type>Objlnk</Type>
  304 + <RangeEnumeration></RangeEnumeration>
  305 + <Units></Units>
  306 + <Description><![CDATA[Reference to SW Components downloaded and installed in scope of the present SW Update Package Note: When resource 17 objlink exist, resources 2, 3 and 12 in this table are ignored.]]></Description>
  307 + </Item>
  308 + <Item ID="18">
  309 + <Name>Software Component tree length</Name>
  310 + <Operations>R</Operations>
  311 + <MultipleInstances>Single</MultipleInstances>
  312 + <Mandatory>Optional</Mandatory>
  313 + <Type>Integer</Type>
  314 + <RangeEnumeration>0..255</RangeEnumeration>
  315 + <Units></Units>
  316 + <Description><![CDATA[Software Component tree length indicates the number of instances existing for this software package in the Software Component Object.]]></Description>
  317 + </Item>
  318 + </Resources>
  319 + <Description2><![CDATA[]]></Description2>
  320 + </Object>
  321 +</LWM2M>
... ...
... ... @@ -87,12 +87,6 @@ public class DefaultCoapServerService implements CoapServerService {
87 87 }
88 88
89 89 private CoapServer createCoapServer() throws UnknownHostException {
90   - server = new CoapServer();
91   -
92   - CoapEndpoint.Builder noSecCoapEndpointBuilder = new CoapEndpoint.Builder();
93   - InetAddress addr = InetAddress.getByName(coapServerContext.getHost());
94   - InetSocketAddress sockAddr = new InetSocketAddress(addr, coapServerContext.getPort());
95   - noSecCoapEndpointBuilder.setInetSocketAddress(sockAddr);
96 90 NetworkConfig networkConfig = new NetworkConfig();
97 91 networkConfig.setBoolean(NetworkConfig.Keys.BLOCKWISE_STRICT_BLOCK2_OPTION, true);
98 92 networkConfig.setBoolean(NetworkConfig.Keys.BLOCKWISE_ENTITY_TOO_LARGE_AUTO_FAILOVER, true);
... ... @@ -102,23 +96,30 @@ public class DefaultCoapServerService implements CoapServerService {
102 96 networkConfig.setInt(NetworkConfig.Keys.PREFERRED_BLOCK_SIZE, 1024);
103 97 networkConfig.setInt(NetworkConfig.Keys.MAX_MESSAGE_SIZE, 1024);
104 98 networkConfig.setInt(NetworkConfig.Keys.MAX_RETRANSMIT, 4);
  99 + networkConfig.setInt(NetworkConfig.Keys.COAP_PORT, coapServerContext.getPort());
  100 + server = new CoapServer(networkConfig);
  101 +
  102 + CoapEndpoint.Builder noSecCoapEndpointBuilder = new CoapEndpoint.Builder();
  103 + InetAddress addr = InetAddress.getByName(coapServerContext.getHost());
  104 + InetSocketAddress sockAddr = new InetSocketAddress(addr, coapServerContext.getPort());
  105 + noSecCoapEndpointBuilder.setInetSocketAddress(sockAddr);
  106 +
105 107 noSecCoapEndpointBuilder.setNetworkConfig(networkConfig);
106 108 CoapEndpoint noSecCoapEndpoint = noSecCoapEndpointBuilder.build();
107 109 server.addEndpoint(noSecCoapEndpoint);
108   -
109 110 if (isDtlsEnabled()) {
110 111 CoapEndpoint.Builder dtlsCoapEndpointBuilder = new CoapEndpoint.Builder();
111 112 TbCoapDtlsSettings dtlsSettings = coapServerContext.getDtlsSettings();
112 113 DtlsConnectorConfig dtlsConnectorConfig = dtlsSettings.dtlsConnectorConfig();
  114 + networkConfig.setInt(NetworkConfig.Keys.COAP_SECURE_PORT, dtlsConnectorConfig.getAddress().getPort());
  115 + dtlsCoapEndpointBuilder.setNetworkConfig(networkConfig);
113 116 DTLSConnector connector = new DTLSConnector(dtlsConnectorConfig);
114 117 dtlsCoapEndpointBuilder.setConnector(connector);
115 118 CoapEndpoint dtlsCoapEndpoint = dtlsCoapEndpointBuilder.build();
116 119 server.addEndpoint(dtlsCoapEndpoint);
117   - if (dtlsConnectorConfig.isClientAuthenticationRequired()) {
118   - tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
119   - dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor();
120   - dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
121   - }
  120 + tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
  121 + dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor();
  122 + dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
122 123 }
123 124 Resource root = server.getRoot();
124 125 TbCoapServerMessageDeliverer messageDeliverer = new TbCoapServerMessageDeliverer(root);
... ...
... ... @@ -78,7 +78,6 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri
78 78 @Override
79 79 public CertificateVerificationResult verifyCertificate(ConnectionId cid, ServerNames serverName, Boolean clientUsage, boolean truncateCertificatePath, CertificateMessage message, DTLSSession session) {
80 80 try {
81   - String credentialsBody = null;
82 81 CertPath certpath = message.getCertificateChain();
83 82 X509Certificate[] chain = certpath.getCertificates().toArray(new X509Certificate[0]);
84 83 for (X509Certificate cert : chain) {
... ... @@ -110,7 +109,6 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri
110 109 latch.await(10, TimeUnit.SECONDS);
111 110 ValidateDeviceCredentialsResponse msg = deviceCredentialsResponse[0];
112 111 if (msg != null && strCert.equals(msg.getCredentials())) {
113   - credentialsBody = msg.getCredentials();
114 112 DeviceProfile deviceProfile = msg.getDeviceProfile();
115 113 if (msg.hasDeviceInfo() && deviceProfile != null) {
116 114 TransportProtos.SessionInfoProto sessionInfoProto = SessionInfoCreator.create(msg, serviceInfoProvider.getServiceId(), UUID.randomUUID());
... ... @@ -123,15 +121,12 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri
123 121 CertificateExpiredException |
124 122 CertificateNotYetValidException e) {
125 123 log.error(e.getMessage(), e);
  124 + AlertMessage alert = new AlertMessage(AlertMessage.AlertLevel.FATAL, AlertMessage.AlertDescription.BAD_CERTIFICATE,
  125 + session.getPeer());
  126 + throw new HandshakeException("Certificate chain could not be validated", alert);
126 127 }
127 128 }
128   - if (credentialsBody == null) {
129   - AlertMessage alert = new AlertMessage(AlertMessage.AlertLevel.FATAL, AlertMessage.AlertDescription.BAD_CERTIFICATE,
130   - session.getPeer());
131   - throw new HandshakeException("Certificate chain could not be validated", alert);
132   - } else {
133   - return new CertificateVerificationResult(cid, certpath, null);
134   - }
  129 + return new CertificateVerificationResult(cid, certpath, null);
135 130 } catch (HandshakeException e) {
136 131 log.trace("Certificate validation failed!", e);
137 132 return new CertificateVerificationResult(cid, e, null);
... ... @@ -158,4 +153,4 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri
158 153 public long getDtlsSessionReportTimeout() {
159 154 return tbCoapDtlsSessionInMemoryStorage.getDtlsSessionReportTimeout();
160 155 }
161   -}
\ No newline at end of file
  156 +}
... ...
... ... @@ -15,17 +15,15 @@
15 15 */
16 16 package org.thingsboard.server.coapserver;
17 17
18   -import com.google.common.io.Resources;
19 18 import lombok.extern.slf4j.Slf4j;
20 19 import org.eclipse.californium.elements.util.SslContextUtil;
21 20 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
22 21 import org.eclipse.californium.scandium.dtls.CertificateType;
23   -import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier;
24 22 import org.springframework.beans.factory.annotation.Autowired;
25 23 import org.springframework.beans.factory.annotation.Value;
26   -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
27 24 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
28 25 import org.springframework.stereotype.Component;
  26 +import org.thingsboard.server.common.data.ResourceUtils;
29 27 import org.thingsboard.server.common.transport.TransportService;
30 28 import org.thingsboard.server.queue.discovery.TbServiceInfoProvider;
31 29
... ... @@ -34,9 +32,7 @@ import java.net.InetAddress;
34 32 import java.net.InetSocketAddress;
35 33 import java.net.UnknownHostException;
36 34 import java.security.GeneralSecurityException;
37   -import java.security.cert.Certificate;
38 35 import java.util.Collections;
39   -import java.util.Optional;
40 36
41 37 @Slf4j
42 38 @ConditionalOnProperty(prefix = "transport.coap.dtls", value = "enabled", havingValue = "true", matchIfMissing = false)
... ... @@ -49,9 +45,6 @@ public class TbCoapDtlsSettings {
49 45 @Value("${transport.coap.dtls.bind_port}")
50 46 private Integer port;
51 47
52   - @Value("${transport.coap.dtls.mode:NO_AUTH}")
53   - private String mode;
54   -
55 48 @Value("${transport.coap.dtls.key_store}")
56 49 private String keyStoreFile;
57 50
... ... @@ -80,34 +73,25 @@ public class TbCoapDtlsSettings {
80 73 private TbServiceInfoProvider serviceInfoProvider;
81 74
82 75 public DtlsConnectorConfig dtlsConnectorConfig() throws UnknownHostException {
83   - Optional<SecurityMode> securityModeOpt = SecurityMode.parse(mode);
84   - if (securityModeOpt.isEmpty()) {
85   - log.warn("Incorrect configuration of securityMode {}", mode);
86   - throw new RuntimeException("Failed to parse mode property: " + mode + "!");
87   - } else {
88   - DtlsConnectorConfig.Builder configBuilder = new DtlsConnectorConfig.Builder();
89   - configBuilder.setAddress(getInetSocketAddress());
90   - String keyStoreFilePath = Resources.getResource(keyStoreFile).getPath();
91   - SslContextUtil.Credentials serverCredentials = loadServerCredentials(keyStoreFilePath);
92   - SecurityMode securityMode = securityModeOpt.get();
93   - if (securityMode.equals(SecurityMode.NO_AUTH)) {
94   - configBuilder.setClientAuthenticationRequired(false);
95   - configBuilder.setServerOnly(true);
96   - } else {
97   - configBuilder.setAdvancedCertificateVerifier(
98   - new TbCoapDtlsCertificateVerifier(
99   - transportService,
100   - serviceInfoProvider,
101   - dtlsSessionInactivityTimeout,
102   - dtlsSessionReportTimeout,
103   - skipValidityCheckForClientCert
104   - )
105   - );
106   - }
107   - configBuilder.setIdentity(serverCredentials.getPrivateKey(), serverCredentials.getCertificateChain(),
108   - Collections.singletonList(CertificateType.X_509));
109   - return configBuilder.build();
110   - }
  76 + DtlsConnectorConfig.Builder configBuilder = new DtlsConnectorConfig.Builder();
  77 + configBuilder.setAddress(getInetSocketAddress());
  78 + String keyStoreFilePath = ResourceUtils.getUri(this, keyStoreFile);
  79 + SslContextUtil.Credentials serverCredentials = loadServerCredentials(keyStoreFilePath);
  80 + configBuilder.setServerOnly(true);
  81 + configBuilder.setClientAuthenticationRequired(false);
  82 + configBuilder.setClientAuthenticationWanted(true);
  83 + configBuilder.setAdvancedCertificateVerifier(
  84 + new TbCoapDtlsCertificateVerifier(
  85 + transportService,
  86 + serviceInfoProvider,
  87 + dtlsSessionInactivityTimeout,
  88 + dtlsSessionReportTimeout,
  89 + skipValidityCheckForClientCert
  90 + )
  91 + );
  92 + configBuilder.setIdentity(serverCredentials.getPrivateKey(), serverCredentials.getCertificateChain(),
  93 + Collections.singletonList(CertificateType.X_509));
  94 + return configBuilder.build();
111 95 }
112 96
113 97 private SslContextUtil.Credentials loadServerCredentials(String keyStoreFilePath) {
... ... @@ -119,43 +103,9 @@ public class TbCoapDtlsSettings {
119 103 }
120 104 }
121 105
122   - private void loadTrustedCertificates(DtlsConnectorConfig.Builder config, String keyStoreFilePath) {
123   - StaticNewAdvancedCertificateVerifier.Builder trustBuilder = StaticNewAdvancedCertificateVerifier.builder();
124   - try {
125   - Certificate[] trustedCertificates = SslContextUtil.loadTrustedCertificates(
126   - keyStoreFilePath, keyAlias,
127   - keyStorePassword.toCharArray());
128   - trustBuilder.setTrustedCertificates(trustedCertificates);
129   - if (trustBuilder.hasTrusts()) {
130   - config.setAdvancedCertificateVerifier(trustBuilder.build());
131   - }
132   - } catch (GeneralSecurityException | IOException e) {
133   - throw new RuntimeException("Failed to load trusted certificates due to: ", e);
134   - }
135   - }
136   -
137 106 private InetSocketAddress getInetSocketAddress() throws UnknownHostException {
138 107 InetAddress addr = InetAddress.getByName(host);
139 108 return new InetSocketAddress(addr, port);
140 109 }
141 110
142   - private enum SecurityMode {
143   - X509,
144   - NO_AUTH;
145   -
146   - static Optional<SecurityMode> parse(String name) {
147   - SecurityMode mode = null;
148   - if (name != null) {
149   - for (SecurityMode securityMode : SecurityMode.values()) {
150   - if (securityMode.name().equalsIgnoreCase(name)) {
151   - mode = securityMode;
152   - break;
153   - }
154   - }
155   - }
156   - return Optional.ofNullable(mode);
157   - }
158   -
159   - }
160   -
161   -}
\ No newline at end of file
  111 +}
... ...
... ... @@ -58,4 +58,6 @@ public interface DashboardService {
58 58 Dashboard unassignDashboardFromEdge(TenantId tenantId, DashboardId dashboardId, EdgeId edgeId);
59 59
60 60 PageData<DashboardInfo> findDashboardsByTenantIdAndEdgeId(TenantId tenantId, EdgeId edgeId, PageLink pageLink);
  61 +
  62 + DashboardInfo findFirstDashboardInfoByTenantIdAndName(TenantId tenantId, String name);
61 63 }
... ...
... ... @@ -28,4 +28,5 @@ public interface EdgeEventService {
28 28
29 29 PageData<EdgeEvent> findEdgeEvents(TenantId tenantId, EdgeId edgeId, TimePageLink pageLink, boolean withTsUpdate);
30 30
  31 + void cleanupEvents(long ttl);
31 32 }
... ...
... ... @@ -46,4 +46,6 @@ public interface EventService {
46 46
47 47 void removeEvents(TenantId tenantId, EntityId entityId);
48 48
  49 + void cleanupEvents(long ttl, long debugTtl);
  50 +
49 51 }
... ...
  1 +/**
  2 + * Copyright © 2016-2021 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.rpc;
  17 +
  18 +import com.google.common.util.concurrent.ListenableFuture;
  19 +import org.thingsboard.server.common.data.id.DeviceId;
  20 +import org.thingsboard.server.common.data.id.RpcId;
  21 +import org.thingsboard.server.common.data.id.TenantId;
  22 +import org.thingsboard.server.common.data.page.PageData;
  23 +import org.thingsboard.server.common.data.page.PageLink;
  24 +import org.thingsboard.server.common.data.rpc.Rpc;
  25 +import org.thingsboard.server.common.data.rpc.RpcStatus;
  26 +
  27 +public interface RpcService {
  28 + Rpc save(Rpc rpc);
  29 +
  30 + void deleteRpc(TenantId tenantId, RpcId id);
  31 +
  32 + void deleteAllRpcByTenantId(TenantId tenantId);
  33 +
  34 + Rpc findById(TenantId tenantId, RpcId id);
  35 +
  36 + ListenableFuture<Rpc> findRpcByIdAsync(TenantId tenantId, RpcId id);
  37 +
  38 + PageData<Rpc> findAllByDeviceIdAndStatus(TenantId tenantId, DeviceId deviceId, RpcStatus rpcStatus, PageLink pageLink);
  39 +}
... ...
... ... @@ -52,4 +52,6 @@ public interface TimeseriesService {
52 52 List<String> findAllKeysByDeviceProfileId(TenantId tenantId, DeviceProfileId deviceProfileId);
53 53
54 54 List<String> findAllKeysByEntityIds(TenantId tenantId, List<EntityId> entityIds);
  55 +
  56 + void cleanup(long systemTtl);
55 57 }
... ...
... ... @@ -76,6 +76,12 @@ public class DataConstants {
76 76
77 77 public static final String RPC_CALL_FROM_SERVER_TO_DEVICE = "RPC_CALL_FROM_SERVER_TO_DEVICE";
78 78
  79 + public static final String RPC_QUEUED = "RPC_QUEUED";
  80 + public static final String RPC_DELIVERED = "RPC_DELIVERED";
  81 + public static final String RPC_SUCCESSFUL = "RPC_SUCCESSFUL";
  82 + public static final String RPC_TIMEOUT = "RPC_TIMEOUT";
  83 + public static final String RPC_FAILED = "RPC_FAILED";
  84 +
79 85 public static final String DEFAULT_SECRET_KEY = "";
80 86 public static final String SECRET_KEY_FIELD_NAME = "secretKey";
81 87 public static final String DURATION_MS_FIELD_NAME = "durationMs";
... ...
... ... @@ -19,5 +19,5 @@ package org.thingsboard.server.common.data;
19 19 * @author Andrew Shvayka
20 20 */
21 21 public enum EntityType {
22   - TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE, ENTITY_VIEW, WIDGETS_BUNDLE, WIDGET_TYPE, TENANT_PROFILE, DEVICE_PROFILE, API_USAGE_STATE, TB_RESOURCE, OTA_PACKAGE, EDGE;
  22 + TENANT, CUSTOMER, USER, DASHBOARD, ASSET, DEVICE, ALARM, RULE_CHAIN, RULE_NODE, ENTITY_VIEW, WIDGETS_BUNDLE, WIDGET_TYPE, TENANT_PROFILE, DEVICE_PROFILE, API_USAGE_STATE, TB_RESOURCE, OTA_PACKAGE, EDGE, RPC;
23 23 }
... ...