Commit 22933e54026c0d3b0b64ed63edaa900776774a3e

Authored by AndrewVolosytnykhThingsboard
2 parents 3c4c7ebd 228fddb8

Merge branch 'master' into ota-service-url-validation

Showing 67 changed files with 2389 additions and 743 deletions

Too many changes to show.

To preserve performance only 67 of 466 files are displayed.

@@ -33,15 +33,15 @@ @@ -33,15 +33,15 @@
33 "enableSearch": true, 33 "enableSearch": true,
34 "displayPagination": true, 34 "displayPagination": true,
35 "defaultPageSize": 10, 35 "defaultPageSize": 10,
36 - "defaultSortOrder": "entityLabel",  
37 - "displayEntityName": false, 36 + "defaultSortOrder": "entityName",
  37 + "displayEntityName": true,
38 "displayEntityType": false, 38 "displayEntityType": false,
39 "enableSelectColumnDisplay": false, 39 "enableSelectColumnDisplay": false,
40 "enableStickyHeader": true, 40 "enableStickyHeader": true,
41 "enableStickyAction": false, 41 "enableStickyAction": false,
42 "entitiesTitle": "Devices", 42 "entitiesTitle": "Devices",
43 - "displayEntityLabel": true,  
44 - "entityLabelColumnTitle": "Device" 43 + "displayEntityLabel": false,
  44 + "entityNameColumnTitle": "Device"
45 }, 45 },
46 "title": "New Entities table", 46 "title": "New Entities table",
47 "dropShadow": true, 47 "dropShadow": true,
@@ -828,15 +828,15 @@ @@ -828,15 +828,15 @@
828 "enableSearch": true, 828 "enableSearch": true,
829 "displayPagination": true, 829 "displayPagination": true,
830 "defaultPageSize": 10, 830 "defaultPageSize": 10,
831 - "defaultSortOrder": "entityLabel",  
832 - "displayEntityName": false, 831 + "defaultSortOrder": "entityName",
  832 + "displayEntityName": true,
833 "displayEntityType": false, 833 "displayEntityType": false,
834 "enableSelectColumnDisplay": false, 834 "enableSelectColumnDisplay": false,
835 "enableStickyHeader": true, 835 "enableStickyHeader": true,
836 "enableStickyAction": true, 836 "enableStickyAction": true,
837 "entitiesTitle": "Devices", 837 "entitiesTitle": "Devices",
838 - "displayEntityLabel": true,  
839 - "entityLabelColumnTitle": "Device" 838 + "displayEntityLabel": false,
  839 + "entityNameColumnTitle": "Device"
840 }, 840 },
841 "title": "New Entities table", 841 "title": "New Entities table",
842 "dropShadow": true, 842 "dropShadow": true,
@@ -1125,15 +1125,15 @@ @@ -1125,15 +1125,15 @@
1125 "enableSearch": true, 1125 "enableSearch": true,
1126 "displayPagination": true, 1126 "displayPagination": true,
1127 "defaultPageSize": 10, 1127 "defaultPageSize": 10,
1128 - "defaultSortOrder": "entityLabel",  
1129 - "displayEntityName": false, 1128 + "defaultSortOrder": "entityName",
  1129 + "displayEntityName": true,
1130 "displayEntityType": false, 1130 "displayEntityType": false,
1131 "enableSelectColumnDisplay": false, 1131 "enableSelectColumnDisplay": false,
1132 "enableStickyHeader": true, 1132 "enableStickyHeader": true,
1133 "enableStickyAction": true, 1133 "enableStickyAction": true,
1134 "entitiesTitle": "Devices", 1134 "entitiesTitle": "Devices",
1135 - "displayEntityLabel": true,  
1136 - "entityLabelColumnTitle": "Device" 1135 + "displayEntityLabel": false,
  1136 + "entityNameColumnTitle": "Device"
1137 }, 1137 },
1138 "title": "New Entities table", 1138 "title": "New Entities table",
1139 "dropShadow": true, 1139 "dropShadow": true,
@@ -1422,15 +1422,15 @@ @@ -1422,15 +1422,15 @@
1422 "enableSearch": true, 1422 "enableSearch": true,
1423 "displayPagination": true, 1423 "displayPagination": true,
1424 "defaultPageSize": 10, 1424 "defaultPageSize": 10,
1425 - "defaultSortOrder": "entityLabel",  
1426 - "displayEntityName": false, 1425 + "defaultSortOrder": "entityName",
  1426 + "displayEntityName": true,
1427 "displayEntityType": false, 1427 "displayEntityType": false,
1428 "enableSelectColumnDisplay": false, 1428 "enableSelectColumnDisplay": false,
1429 "enableStickyHeader": true, 1429 "enableStickyHeader": true,
1430 "enableStickyAction": true, 1430 "enableStickyAction": true,
1431 "entitiesTitle": "Devices", 1431 "entitiesTitle": "Devices",
1432 - "displayEntityLabel": true,  
1433 - "entityLabelColumnTitle": "Device" 1432 + "displayEntityLabel": false,
  1433 + "entityNameColumnTitle": "Device"
1434 }, 1434 },
1435 "title": "New Entities table", 1435 "title": "New Entities table",
1436 "dropShadow": true, 1436 "dropShadow": true,
@@ -1719,15 +1719,15 @@ @@ -1719,15 +1719,15 @@
1719 "enableSearch": true, 1719 "enableSearch": true,
1720 "displayPagination": true, 1720 "displayPagination": true,
1721 "defaultPageSize": 10, 1721 "defaultPageSize": 10,
1722 - "defaultSortOrder": "entityLabel",  
1723 - "displayEntityName": false, 1722 + "defaultSortOrder": "entityName",
  1723 + "displayEntityName": true,
1724 "displayEntityType": false, 1724 "displayEntityType": false,
1725 "enableSelectColumnDisplay": false, 1725 "enableSelectColumnDisplay": false,
1726 "enableStickyHeader": true, 1726 "enableStickyHeader": true,
1727 "enableStickyAction": true, 1727 "enableStickyAction": true,
1728 "entitiesTitle": "Devices", 1728 "entitiesTitle": "Devices",
1729 - "displayEntityLabel": true,  
1730 - "entityLabelColumnTitle": "Device" 1729 + "displayEntityLabel": false,
  1730 + "entityNameColumnTitle": "Device"
1731 }, 1731 },
1732 "title": "New Entities table", 1732 "title": "New Entities table",
1733 "dropShadow": true, 1733 "dropShadow": true,
@@ -33,15 +33,15 @@ @@ -33,15 +33,15 @@
33 "enableSearch": true, 33 "enableSearch": true,
34 "displayPagination": true, 34 "displayPagination": true,
35 "defaultPageSize": 10, 35 "defaultPageSize": 10,
36 - "defaultSortOrder": "entityLabel",  
37 - "displayEntityName": false, 36 + "defaultSortOrder": "entityName",
  37 + "displayEntityName": true,
38 "displayEntityType": false, 38 "displayEntityType": false,
39 "enableSelectColumnDisplay": false, 39 "enableSelectColumnDisplay": false,
40 "enableStickyHeader": true, 40 "enableStickyHeader": true,
41 "enableStickyAction": false, 41 "enableStickyAction": false,
42 "entitiesTitle": "Devices", 42 "entitiesTitle": "Devices",
43 - "displayEntityLabel": true,  
44 - "entityLabelColumnTitle": "Device" 43 + "displayEntityLabel": false,
  44 + "entityNameColumnTitle": "Device"
45 }, 45 },
46 "title": "New Entities table", 46 "title": "New Entities table",
47 "dropShadow": true, 47 "dropShadow": true,
@@ -828,15 +828,15 @@ @@ -828,15 +828,15 @@
828 "enableSearch": true, 828 "enableSearch": true,
829 "displayPagination": true, 829 "displayPagination": true,
830 "defaultPageSize": 10, 830 "defaultPageSize": 10,
831 - "defaultSortOrder": "entityLabel",  
832 - "displayEntityName": false, 831 + "defaultSortOrder": "entityName",
  832 + "displayEntityName": true,
833 "displayEntityType": false, 833 "displayEntityType": false,
834 "enableSelectColumnDisplay": false, 834 "enableSelectColumnDisplay": false,
835 "enableStickyHeader": true, 835 "enableStickyHeader": true,
836 "enableStickyAction": true, 836 "enableStickyAction": true,
837 "entitiesTitle": "Devices", 837 "entitiesTitle": "Devices",
838 - "displayEntityLabel": true,  
839 - "entityLabelColumnTitle": "Device" 838 + "displayEntityLabel": false,
  839 + "entityNameColumnTitle": "Device"
840 }, 840 },
841 "title": "New Entities table", 841 "title": "New Entities table",
842 "dropShadow": true, 842 "dropShadow": true,
@@ -1125,15 +1125,15 @@ @@ -1125,15 +1125,15 @@
1125 "enableSearch": true, 1125 "enableSearch": true,
1126 "displayPagination": true, 1126 "displayPagination": true,
1127 "defaultPageSize": 10, 1127 "defaultPageSize": 10,
1128 - "defaultSortOrder": "entityLabel",  
1129 - "displayEntityName": false, 1128 + "defaultSortOrder": "entityName",
  1129 + "displayEntityName": true,
1130 "displayEntityType": false, 1130 "displayEntityType": false,
1131 "enableSelectColumnDisplay": false, 1131 "enableSelectColumnDisplay": false,
1132 "enableStickyHeader": true, 1132 "enableStickyHeader": true,
1133 "enableStickyAction": true, 1133 "enableStickyAction": true,
1134 "entitiesTitle": "Devices", 1134 "entitiesTitle": "Devices",
1135 - "displayEntityLabel": true,  
1136 - "entityLabelColumnTitle": "Device" 1135 + "displayEntityLabel": false,
  1136 + "entityNameColumnTitle": "Device"
1137 }, 1137 },
1138 "title": "New Entities table", 1138 "title": "New Entities table",
1139 "dropShadow": true, 1139 "dropShadow": true,
@@ -1422,15 +1422,15 @@ @@ -1422,15 +1422,15 @@
1422 "enableSearch": true, 1422 "enableSearch": true,
1423 "displayPagination": true, 1423 "displayPagination": true,
1424 "defaultPageSize": 10, 1424 "defaultPageSize": 10,
1425 - "defaultSortOrder": "entityLabel",  
1426 - "displayEntityName": false, 1425 + "defaultSortOrder": "entityName",
  1426 + "displayEntityName": true,
1427 "displayEntityType": false, 1427 "displayEntityType": false,
1428 "enableSelectColumnDisplay": false, 1428 "enableSelectColumnDisplay": false,
1429 "enableStickyHeader": true, 1429 "enableStickyHeader": true,
1430 "enableStickyAction": true, 1430 "enableStickyAction": true,
1431 "entitiesTitle": "Devices", 1431 "entitiesTitle": "Devices",
1432 - "displayEntityLabel": true,  
1433 - "entityLabelColumnTitle": "Device" 1432 + "displayEntityLabel": false,
  1433 + "entityNameColumnTitle": "Device"
1434 }, 1434 },
1435 "title": "New Entities table", 1435 "title": "New Entities table",
1436 "dropShadow": true, 1436 "dropShadow": true,
@@ -1719,15 +1719,15 @@ @@ -1719,15 +1719,15 @@
1719 "enableSearch": true, 1719 "enableSearch": true,
1720 "displayPagination": true, 1720 "displayPagination": true,
1721 "defaultPageSize": 10, 1721 "defaultPageSize": 10,
1722 - "defaultSortOrder": "entityLabel",  
1723 - "displayEntityName": false, 1722 + "defaultSortOrder": "entityName",
  1723 + "displayEntityName": true,
1724 "displayEntityType": false, 1724 "displayEntityType": false,
1725 "enableSelectColumnDisplay": false, 1725 "enableSelectColumnDisplay": false,
1726 "enableStickyHeader": true, 1726 "enableStickyHeader": true,
1727 "enableStickyAction": true, 1727 "enableStickyAction": true,
1728 "entitiesTitle": "Devices", 1728 "entitiesTitle": "Devices",
1729 - "displayEntityLabel": true,  
1730 - "entityLabelColumnTitle": "Device" 1729 + "displayEntityLabel": false,
  1730 + "entityNameColumnTitle": "Device"
1731 }, 1731 },
1732 "title": "New Entities table", 1732 "title": "New Entities table",
1733 "dropShadow": true, 1733 "dropShadow": true,
  1 +{
  2 + "providerId": "Apple",
  3 + "additionalInfo": null,
  4 + "accessTokenUri": "https://appleid.apple.com/auth/token",
  5 + "authorizationUri": "https://appleid.apple.com/auth/authorize?response_mode=form_post",
  6 + "scope": ["email","openid","name"],
  7 + "jwkSetUri": "https://appleid.apple.com/auth/keys",
  8 + "userInfoUri": null,
  9 + "clientAuthenticationMethod": "POST",
  10 + "userNameAttributeName": "email",
  11 + "mapperConfig": {
  12 + "type": "APPLE",
  13 + "basic": {
  14 + "emailAttributeKey": "email",
  15 + "firstNameAttributeKey": "firstName",
  16 + "lastNameAttributeKey": "lastName",
  17 + "tenantNameStrategy": "DOMAIN"
  18 + }
  19 + },
  20 + "comment": null,
  21 + "loginButtonIcon": "apple-logo",
  22 + "loginButtonLabel": "Apple",
  23 + "helpLink": "https://developer.apple.com/sign-in-with-apple/get-started/"
  24 +}
@@ -18,17 +18,18 @@ CREATE OR REPLACE PROCEDURE drop_partitions_by_max_ttl(IN partition_type varchar @@ -18,17 +18,18 @@ CREATE OR REPLACE PROCEDURE drop_partitions_by_max_ttl(IN partition_type varchar
18 LANGUAGE plpgsql AS 18 LANGUAGE plpgsql AS
19 $$ 19 $$
20 DECLARE 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 BEGIN 34 BEGIN
34 SELECT max(attribute_kv.long_v) 35 SELECT max(attribute_kv.long_v)
@@ -45,53 +46,138 @@ BEGIN @@ -45,53 +46,138 @@ BEGIN
45 if max_ttl IS NOT NULL AND max_ttl > 0 THEN 46 if max_ttl IS NOT NULL AND max_ttl > 0 THEN
46 date := to_timestamp(EXTRACT(EPOCH FROM current_timestamp) - max_ttl); 47 date := to_timestamp(EXTRACT(EPOCH FROM current_timestamp) - max_ttl);
47 partition_by_max_ttl_date := get_partition_by_max_ttl_date(partition_type, date); 48 partition_by_max_ttl_date := get_partition_by_max_ttl_date(partition_type, date);
  49 + RAISE NOTICE 'Date by max ttl: %', date;
48 RAISE NOTICE 'Partition by max ttl: %', partition_by_max_ttl_date; 50 RAISE NOTICE 'Partition by max ttl: %', partition_by_max_ttl_date;
49 IF partition_by_max_ttl_date IS NOT NULL THEN 51 IF partition_by_max_ttl_date IS NOT NULL THEN
50 CASE 52 CASE
51 WHEN partition_type = 'DAYS' THEN 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 WHEN partition_type = 'MONTHS' THEN 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 ELSE 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 END CASE; 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 ELSE 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 END IF; 117 END IF;
82 END IF; 118 END IF;
83 END IF; 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 END IF; 175 END IF;
85 END IF; 176 END IF;
86 END IF; 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 END IF; 178 END IF;
94 - END LOOP; 179 + END IF;
  180 + END IF;
95 END IF; 181 END IF;
96 END IF; 182 END IF;
97 END 183 END
@@ -107,8 +193,6 @@ BEGIN @@ -107,8 +193,6 @@ BEGIN
107 partition := 'ts_kv_' || to_char(date, 'yyyy') || '_' || to_char(date, 'MM'); 193 partition := 'ts_kv_' || to_char(date, 'yyyy') || '_' || to_char(date, 'MM');
108 WHEN partition_type = 'YEARS' THEN 194 WHEN partition_type = 'YEARS' THEN
109 partition := 'ts_kv_' || to_char(date, 'yyyy'); 195 partition := 'ts_kv_' || to_char(date, 'yyyy');
110 - WHEN partition_type = 'INDEFINITE' THEN  
111 - partition := NULL;  
112 ELSE 196 ELSE
113 partition := NULL; 197 partition := NULL;
114 END CASE; 198 END CASE;
@@ -92,7 +92,7 @@ CREATE TABLE IF NOT EXISTS oauth2_registration ( @@ -92,7 +92,7 @@ CREATE TABLE IF NOT EXISTS oauth2_registration (
92 created_time bigint NOT NULL, 92 created_time bigint NOT NULL,
93 additional_info varchar, 93 additional_info varchar,
94 client_id varchar(255), 94 client_id varchar(255),
95 - client_secret varchar(255), 95 + client_secret varchar(2048),
96 authorization_uri varchar(255), 96 authorization_uri varchar(255),
97 token_uri varchar(255), 97 token_uri varchar(255),
98 scope varchar(255), 98 scope varchar(255),
@@ -197,3 +197,17 @@ $$; @@ -197,3 +197,17 @@ $$;
197 ALTER TABLE api_usage_state 197 ALTER TABLE api_usage_state
198 ADD COLUMN IF NOT EXISTS alarm_exec VARCHAR(32); 198 ADD COLUMN IF NOT EXISTS alarm_exec VARCHAR(32);
199 UPDATE api_usage_state SET alarm_exec = 'ENABLED' WHERE alarm_exec IS NULL; 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,6 +65,7 @@ import org.thingsboard.server.dao.relation.RelationService;
65 import org.thingsboard.server.dao.resource.ResourceService; 65 import org.thingsboard.server.dao.resource.ResourceService;
66 import org.thingsboard.server.dao.rule.RuleChainService; 66 import org.thingsboard.server.dao.rule.RuleChainService;
67 import org.thingsboard.server.dao.rule.RuleNodeStateService; 67 import org.thingsboard.server.dao.rule.RuleNodeStateService;
  68 +import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
68 import org.thingsboard.server.dao.tenant.TenantProfileService; 69 import org.thingsboard.server.dao.tenant.TenantProfileService;
69 import org.thingsboard.server.dao.tenant.TenantService; 70 import org.thingsboard.server.dao.tenant.TenantService;
70 import org.thingsboard.server.dao.timeseries.TimeseriesService; 71 import org.thingsboard.server.dao.timeseries.TimeseriesService;
@@ -80,9 +81,9 @@ import org.thingsboard.server.service.executors.ExternalCallExecutorService; @@ -80,9 +81,9 @@ import org.thingsboard.server.service.executors.ExternalCallExecutorService;
80 import org.thingsboard.server.service.executors.SharedEventLoopGroupService; 81 import org.thingsboard.server.service.executors.SharedEventLoopGroupService;
81 import org.thingsboard.server.service.mail.MailExecutorService; 82 import org.thingsboard.server.service.mail.MailExecutorService;
82 import org.thingsboard.server.service.profile.TbDeviceProfileCache; 83 import org.thingsboard.server.service.profile.TbDeviceProfileCache;
83 -import org.thingsboard.server.dao.tenant.TbTenantProfileCache;  
84 import org.thingsboard.server.service.queue.TbClusterService; 84 import org.thingsboard.server.service.queue.TbClusterService;
85 import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; 85 import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
  86 +import org.thingsboard.server.service.rpc.TbRpcService;
86 import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; 87 import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService;
87 import org.thingsboard.server.service.script.JsExecutorService; 88 import org.thingsboard.server.service.script.JsExecutorService;
88 import org.thingsboard.server.service.script.JsInvokeService; 89 import org.thingsboard.server.service.script.JsInvokeService;
@@ -303,23 +304,33 @@ public class ActorSystemContext { @@ -303,23 +304,33 @@ public class ActorSystemContext {
303 304
304 @Lazy 305 @Lazy
305 @Autowired(required = false) 306 @Autowired(required = false)
306 - @Getter private EdgeService edgeService; 307 + @Getter
  308 + private EdgeService edgeService;
307 309
308 @Lazy 310 @Lazy
309 @Autowired(required = false) 311 @Autowired(required = false)
310 - @Getter private EdgeEventService edgeEventService; 312 + @Getter
  313 + private EdgeEventService edgeEventService;
311 314
312 @Lazy 315 @Lazy
313 @Autowired(required = false) 316 @Autowired(required = false)
314 - @Getter private EdgeRpcService edgeRpcService; 317 + @Getter
  318 + private EdgeRpcService edgeRpcService;
315 319
316 @Lazy 320 @Lazy
317 @Autowired(required = false) 321 @Autowired(required = false)
318 - @Getter private ResourceService resourceService; 322 + @Getter
  323 + private ResourceService resourceService;
319 324
320 @Lazy 325 @Lazy
321 @Autowired(required = false) 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 @Value("${actors.session.max_concurrent_sessions_per_device:1}") 335 @Value("${actors.session.max_concurrent_sessions_per_device:1}")
325 @Getter 336 @Getter
@@ -46,7 +46,7 @@ public class DeviceActor extends ContextAwareActor { @@ -46,7 +46,7 @@ public class DeviceActor extends ContextAwareActor {
46 super.init(ctx); 46 super.init(ctx);
47 log.debug("[{}][{}] Starting device actor.", processor.tenantId, processor.deviceId); 47 log.debug("[{}][{}] Starting device actor.", processor.tenantId, processor.deviceId);
48 try { 48 try {
49 - processor.initSessionTimeout(ctx); 49 + processor.init(ctx);
50 log.debug("[{}][{}] Device actor started.", processor.tenantId, processor.deviceId); 50 log.debug("[{}][{}] Device actor started.", processor.tenantId, processor.deviceId);
51 } catch (Exception e) { 51 } catch (Exception e) {
52 log.warn("[{}][{}] Unknown failure", processor.tenantId, processor.deviceId, e); 52 log.warn("[{}][{}] Unknown failure", processor.tenantId, processor.deviceId, e);
@@ -23,6 +23,8 @@ import com.google.common.util.concurrent.MoreExecutors; @@ -23,6 +23,8 @@ import com.google.common.util.concurrent.MoreExecutors;
23 import com.google.protobuf.InvalidProtocolBufferException; 23 import com.google.protobuf.InvalidProtocolBufferException;
24 import lombok.extern.slf4j.Slf4j; 24 import lombok.extern.slf4j.Slf4j;
25 import org.apache.commons.collections.CollectionUtils; 25 import org.apache.commons.collections.CollectionUtils;
  26 +import org.thingsboard.common.util.JacksonUtil;
  27 +import org.thingsboard.common.util.LinkedHashMapRemoveEldest;
26 import org.thingsboard.rule.engine.api.RpcError; 28 import org.thingsboard.rule.engine.api.RpcError;
27 import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg; 29 import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg;
28 import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; 30 import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg;
@@ -38,12 +40,17 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType; @@ -38,12 +40,17 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType;
38 import org.thingsboard.server.common.data.edge.EdgeEventType; 40 import org.thingsboard.server.common.data.edge.EdgeEventType;
39 import org.thingsboard.server.common.data.id.DeviceId; 41 import org.thingsboard.server.common.data.id.DeviceId;
40 import org.thingsboard.server.common.data.id.EdgeId; 42 import org.thingsboard.server.common.data.id.EdgeId;
  43 +import org.thingsboard.server.common.data.id.RpcId;
41 import org.thingsboard.server.common.data.id.TenantId; 44 import org.thingsboard.server.common.data.id.TenantId;
42 import org.thingsboard.server.common.data.kv.AttributeKey; 45 import org.thingsboard.server.common.data.kv.AttributeKey;
43 import org.thingsboard.server.common.data.kv.AttributeKvEntry; 46 import org.thingsboard.server.common.data.kv.AttributeKvEntry;
44 import org.thingsboard.server.common.data.kv.KvEntry; 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 import org.thingsboard.server.common.data.relation.EntityRelation; 50 import org.thingsboard.server.common.data.relation.EntityRelation;
46 import org.thingsboard.server.common.data.relation.RelationTypeGroup; 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 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody; 54 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
48 import org.thingsboard.server.common.data.security.DeviceCredentials; 55 import org.thingsboard.server.common.data.security.DeviceCredentials;
49 import org.thingsboard.server.common.data.security.DeviceCredentialsType; 56 import org.thingsboard.server.common.data.security.DeviceCredentialsType;
@@ -52,8 +59,8 @@ import org.thingsboard.server.common.msg.TbMsgMetaData; @@ -52,8 +59,8 @@ import org.thingsboard.server.common.msg.TbMsgMetaData;
52 import org.thingsboard.server.common.msg.queue.TbCallback; 59 import org.thingsboard.server.common.msg.queue.TbCallback;
53 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; 60 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
54 import org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg; 61 import org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg;
55 -import org.thingsboard.server.gen.transport.TransportProtos;  
56 import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg; 62 import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg;
  63 +import org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg;
57 import org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry; 64 import org.thingsboard.server.gen.transport.TransportProtos.DeviceSessionsCacheEntry;
58 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg; 65 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg;
59 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg; 66 import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeResponseMsg;
@@ -68,10 +75,12 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionType; @@ -68,10 +75,12 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionType;
68 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg; 75 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToAttributeUpdatesMsg;
69 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg; 76 import org.thingsboard.server.gen.transport.TransportProtos.SubscribeToRPCMsg;
70 import org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto; 77 import org.thingsboard.server.gen.transport.TransportProtos.SubscriptionInfoProto;
  78 +import org.thingsboard.server.gen.transport.TransportProtos.ToDevicePersistedRpcResponseMsg;
71 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg; 79 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcRequestMsg;
72 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg; 80 import org.thingsboard.server.gen.transport.TransportProtos.ToDeviceRpcResponseMsg;
73 import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg; 81 import org.thingsboard.server.gen.transport.TransportProtos.ToServerRpcResponseMsg;
74 import org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg; 82 import org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg;
  83 +import org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto;
75 import org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg; 84 import org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg;
76 import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto; 85 import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto;
77 import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; 86 import org.thingsboard.server.service.rpc.FromDeviceRpcResponse;
@@ -88,6 +97,8 @@ import java.util.HashSet; @@ -88,6 +97,8 @@ import java.util.HashSet;
88 import java.util.LinkedHashMap; 97 import java.util.LinkedHashMap;
89 import java.util.List; 98 import java.util.List;
90 import java.util.Map; 99 import java.util.Map;
  100 +import java.util.Objects;
  101 +import java.util.Optional;
91 import java.util.Set; 102 import java.util.Set;
92 import java.util.UUID; 103 import java.util.UUID;
93 import java.util.function.Consumer; 104 import java.util.function.Consumer;
@@ -102,7 +113,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -102,7 +113,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
102 113
103 final TenantId tenantId; 114 final TenantId tenantId;
104 final DeviceId deviceId; 115 final DeviceId deviceId;
105 - private final Map<UUID, SessionInfoMetaData> sessions; 116 + final LinkedHashMapRemoveEldest<UUID, SessionInfoMetaData> sessions;
106 private final Map<UUID, SessionInfo> attributeSubscriptions; 117 private final Map<UUID, SessionInfo> attributeSubscriptions;
107 private final Map<UUID, SessionInfo> rpcSubscriptions; 118 private final Map<UUID, SessionInfo> rpcSubscriptions;
108 private final Map<Integer, ToDeviceRpcRequestMetadata> toDeviceRpcPendingMap; 119 private final Map<Integer, ToDeviceRpcRequestMetadata> toDeviceRpcPendingMap;
@@ -117,16 +128,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -117,16 +128,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
117 super(systemContext); 128 super(systemContext);
118 this.tenantId = tenantId; 129 this.tenantId = tenantId;
119 this.deviceId = deviceId; 130 this.deviceId = deviceId;
120 - this.sessions = new LinkedHashMap<>();  
121 this.attributeSubscriptions = new HashMap<>(); 131 this.attributeSubscriptions = new HashMap<>();
122 this.rpcSubscriptions = new HashMap<>(); 132 this.rpcSubscriptions = new HashMap<>();
123 this.toDeviceRpcPendingMap = new HashMap<>(); 133 this.toDeviceRpcPendingMap = new HashMap<>();
  134 + this.sessions = new LinkedHashMapRemoveEldest<>(systemContext.getMaxConcurrentSessionsPerDevice(), this::notifyTransportAboutClosedSessionMaxSessionsLimit);
124 if (initAttributes()) { 135 if (initAttributes()) {
125 restoreSessions(); 136 restoreSessions();
126 } 137 }
127 } 138 }
128 139
129 - private boolean initAttributes() { 140 + boolean initAttributes() {
130 Device device = systemContext.getDeviceService().findDeviceById(tenantId, deviceId); 141 Device device = systemContext.getDeviceService().findDeviceById(tenantId, deviceId);
131 if (device != null) { 142 if (device != null) {
132 this.deviceName = device.getName(); 143 this.deviceName = device.getName();
@@ -162,20 +173,19 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -162,20 +173,19 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
162 173
163 void processRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg) { 174 void processRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg) {
164 ToDeviceRpcRequest request = msg.getMsg(); 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 long timeout = request.getExpirationTime() - System.currentTimeMillis(); 178 long timeout = request.getExpirationTime() - System.currentTimeMillis();
  179 + boolean persisted = request.isPersisted();
  180 +
176 if (timeout <= 0) { 181 if (timeout <= 0) {
177 log.debug("[{}][{}] Ignoring message due to exp time reached, {}", deviceId, request.getId(), request.getExpirationTime()); 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 return; 186 return;
  187 + } else if (persisted) {
  188 + createRpc(request, RpcStatus.QUEUED);
179 } 189 }
180 190
181 boolean sent; 191 boolean sent;
@@ -192,10 +202,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -192,10 +202,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
192 syncSessionSet.add(key); 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 syncSessionSet.forEach(rpcSubscriptions::remove); 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 if (request.isOneway() && sent) { 215 if (request.isOneway() && sent) {
200 log.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId()); 216 log.debug("[{}] Rpc command response sent [{}]!", deviceId, request.getId());
201 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), null, null)); 217 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(msg.getMsg().getId(), null, null));
@@ -209,6 +225,32 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -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 void processRpcResponsesFromEdge(TbActorCtx context, FromDeviceRpcResponseActorMsg responseMsg) { 254 void processRpcResponsesFromEdge(TbActorCtx context, FromDeviceRpcResponseActorMsg responseMsg) {
213 log.debug("[{}] Processing rpc command response from edge session", deviceId); 255 log.debug("[{}] Processing rpc command response from edge session", deviceId);
214 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(responseMsg.getRequestId()); 256 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(responseMsg.getRequestId());
@@ -230,6 +272,9 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -230,6 +272,9 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
230 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(msg.getId()); 272 ToDeviceRpcRequestMetadata requestMd = toDeviceRpcPendingMap.remove(msg.getId());
231 if (requestMd != null) { 273 if (requestMd != null) {
232 log.debug("[{}] RPC request [{}] timeout detected!", deviceId, msg.getId()); 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 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(), 278 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(),
234 null, requestMd.isSent() ? RpcError.TIMEOUT : RpcError.NO_ACTIVE_CONNECTION)); 279 null, requestMd.isSent() ? RpcError.TIMEOUT : RpcError.NO_ACTIVE_CONNECTION));
235 } 280 }
@@ -271,7 +316,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -271,7 +316,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
271 .setExpirationTime(request.getExpirationTime()) 316 .setExpirationTime(request.getExpirationTime())
272 .setRequestIdMSB(request.getId().getMostSignificantBits()) 317 .setRequestIdMSB(request.getId().getMostSignificantBits())
273 .setRequestIdLSB(request.getId().getLeastSignificantBits()) 318 .setRequestIdLSB(request.getId().getLeastSignificantBits())
  319 + .setOneway(request.isOneway())
  320 + .setPersisted(request.isPersisted())
274 .build(); 321 .build();
  322 +
275 sendToTransport(rpcRequest, sessionId, nodeId); 323 sendToTransport(rpcRequest, sessionId, nodeId);
276 }; 324 };
277 } 325 }
@@ -279,31 +327,39 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -279,31 +327,39 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
279 void process(TbActorCtx context, TransportToDeviceActorMsgWrapper wrapper) { 327 void process(TbActorCtx context, TransportToDeviceActorMsgWrapper wrapper) {
280 TransportToDeviceActorMsg msg = wrapper.getMsg(); 328 TransportToDeviceActorMsg msg = wrapper.getMsg();
281 TbCallback callback = wrapper.getCallback(); 329 TbCallback callback = wrapper.getCallback();
  330 + var sessionInfo = msg.getSessionInfo();
  331 +
282 if (msg.hasSessionEvent()) { 332 if (msg.hasSessionEvent()) {
283 - processSessionStateMsgs(msg.getSessionInfo(), msg.getSessionEvent()); 333 + processSessionStateMsgs(sessionInfo, msg.getSessionEvent());
284 } 334 }
285 if (msg.hasSubscribeToAttributes()) { 335 if (msg.hasSubscribeToAttributes()) {
286 - processSubscriptionCommands(context, msg.getSessionInfo(), msg.getSubscribeToAttributes()); 336 + processSubscriptionCommands(context, sessionInfo, msg.getSubscribeToAttributes());
287 } 337 }
288 if (msg.hasSubscribeToRPC()) { 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 if (msg.hasGetAttributes()) { 344 if (msg.hasGetAttributes()) {
292 - handleGetAttributesRequest(context, msg.getSessionInfo(), msg.getGetAttributes()); 345 + handleGetAttributesRequest(context, sessionInfo, msg.getGetAttributes());
293 } 346 }
294 if (msg.hasToDeviceRPCCallResponse()) { 347 if (msg.hasToDeviceRPCCallResponse()) {
295 - processRpcResponses(context, msg.getSessionInfo(), msg.getToDeviceRPCCallResponse()); 348 + processRpcResponses(context, sessionInfo, msg.getToDeviceRPCCallResponse());
296 } 349 }
297 if (msg.hasSubscriptionInfo()) { 350 if (msg.hasSubscriptionInfo()) {
298 - handleSessionActivity(context, msg.getSessionInfo(), msg.getSubscriptionInfo()); 351 + handleSessionActivity(context, sessionInfo, msg.getSubscriptionInfo());
299 } 352 }
300 if (msg.hasClaimDevice()) { 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 callback.onSuccess(); 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 DeviceId deviceId = new DeviceId(new UUID(msg.getDeviceIdMSB(), msg.getDeviceIdLSB())); 363 DeviceId deviceId = new DeviceId(new UUID(msg.getDeviceIdMSB(), msg.getDeviceIdLSB()));
308 systemContext.getClaimDevicesService().registerClaimingInfo(tenantId, deviceId, msg.getSecretKey(), msg.getDurationMs()); 364 systemContext.getClaimDevicesService().registerClaimingInfo(tenantId, deviceId, msg.getSecretKey(), msg.getDurationMs());
309 } 365 }
@@ -442,11 +498,22 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -442,11 +498,22 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
442 if (success) { 498 if (success) {
443 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(), 499 systemContext.getTbCoreDeviceRpcService().processRpcResponseFromDeviceActor(new FromDeviceRpcResponse(requestMd.getMsg().getMsg().getId(),
444 responseMsg.getPayload(), null)); 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 } else { 504 } else {
446 log.debug("[{}] Rpc command response [{}] is stale!", deviceId, responseMsg.getRequestId()); 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 private void processSubscriptionCommands(TbActorCtx context, SessionInfoProto sessionInfo, SubscribeToAttributeUpdatesMsg subscribeCmd) { 517 private void processSubscriptionCommands(TbActorCtx context, SessionInfoProto sessionInfo, SubscribeToAttributeUpdatesMsg subscribeCmd) {
451 UUID sessionId = getSessionId(sessionInfo); 518 UUID sessionId = getSessionId(sessionInfo);
452 if (subscribeCmd.getUnsubscribe()) { 519 if (subscribeCmd.getUnsubscribe()) {
@@ -488,18 +555,14 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -488,18 +555,14 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
488 555
489 private void processSessionStateMsgs(SessionInfoProto sessionInfo, SessionEventMsg msg) { 556 private void processSessionStateMsgs(SessionInfoProto sessionInfo, SessionEventMsg msg) {
490 UUID sessionId = getSessionId(sessionInfo); 557 UUID sessionId = getSessionId(sessionInfo);
  558 + Objects.requireNonNull(sessionId);
491 if (msg.getEvent() == SessionEvent.OPEN) { 559 if (msg.getEvent() == SessionEvent.OPEN) {
492 if (sessions.containsKey(sessionId)) { 560 if (sessions.containsKey(sessionId)) {
493 log.debug("[{}] Received duplicate session open event [{}]", deviceId, sessionId); 561 log.debug("[{}] Received duplicate session open event [{}]", deviceId, sessionId);
494 return; 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 sessions.put(sessionId, new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfo.getNodeId()))); 566 sessions.put(sessionId, new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfo.getNodeId())));
504 if (sessions.size() == 1) { 567 if (sessions.size() == 1) {
505 reportSessionOpen(); 568 reportSessionOpen();
@@ -520,8 +583,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -520,8 +583,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
520 583
521 private void handleSessionActivity(TbActorCtx context, SessionInfoProto sessionInfoProto, SubscriptionInfoProto subscriptionInfo) { 584 private void handleSessionActivity(TbActorCtx context, SessionInfoProto sessionInfoProto, SubscriptionInfoProto subscriptionInfo) {
522 UUID sessionId = getSessionId(sessionInfoProto); 585 UUID sessionId = getSessionId(sessionInfoProto);
  586 + Objects.requireNonNull(sessionId);
  587 +
523 SessionInfoMetaData sessionMD = sessions.computeIfAbsent(sessionId, 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 sessionMD.setLastActivityTime(subscriptionInfo.getLastActivityTime()); 591 sessionMD.setLastActivityTime(subscriptionInfo.getLastActivityTime());
527 sessionMD.setSubscribedToAttributes(subscriptionInfo.getAttributeSubscription()); 592 sessionMD.setSubscribedToAttributes(subscriptionInfo.getAttributeSubscription());
@@ -551,6 +616,11 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -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 private void notifyTransportAboutClosedSession(UUID sessionId, SessionInfoMetaData sessionMd, String message) { 624 private void notifyTransportAboutClosedSession(UUID sessionId, SessionInfoMetaData sessionMd, String message) {
555 SessionCloseNotificationProto sessionCloseNotificationProto = SessionCloseNotificationProto 625 SessionCloseNotificationProto sessionCloseNotificationProto = SessionCloseNotificationProto
556 .newBuilder() 626 .newBuilder()
@@ -565,7 +635,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -565,7 +635,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
565 635
566 void notifyTransportAboutProfileUpdate(UUID sessionId, SessionInfoMetaData sessionMd, DeviceCredentials deviceCredentials) { 636 void notifyTransportAboutProfileUpdate(UUID sessionId, SessionInfoMetaData sessionMd, DeviceCredentials deviceCredentials) {
567 log.info("2) LwM2Mtype: "); 637 log.info("2) LwM2Mtype: ");
568 - TransportProtos.ToTransportUpdateCredentialsProto.Builder notification = TransportProtos.ToTransportUpdateCredentialsProto.newBuilder(); 638 + ToTransportUpdateCredentialsProto.Builder notification = ToTransportUpdateCredentialsProto.newBuilder();
569 notification.addCredentialsId(deviceCredentials.getCredentialsId()); 639 notification.addCredentialsId(deviceCredentials.getCredentialsId());
570 notification.addCredentialsValue(deviceCredentials.getCredentialsValue()); 640 notification.addCredentialsValue(deviceCredentials.getCredentialsValue());
571 ToTransportMsg msg = ToTransportMsg.newBuilder() 641 ToTransportMsg msg = ToTransportMsg.newBuilder()
@@ -640,7 +710,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -640,7 +710,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
640 ListenableFuture<EdgeEvent> future = systemContext.getEdgeEventService().saveAsync(edgeEvent); 710 ListenableFuture<EdgeEvent> future = systemContext.getEdgeEventService().saveAsync(edgeEvent);
641 Futures.addCallback(future, new FutureCallback<EdgeEvent>() { 711 Futures.addCallback(future, new FutureCallback<EdgeEvent>() {
642 @Override 712 @Override
643 - public void onSuccess( EdgeEvent result) { 713 + public void onSuccess(EdgeEvent result) {
644 systemContext.getClusterService().onEdgeEventUpdate(tenantId, edgeId); 714 systemContext.getClusterService().onEdgeEventUpdate(tenantId, edgeId);
645 } 715 }
646 716
@@ -697,7 +767,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -697,7 +767,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
697 return builder.build(); 767 return builder.build();
698 } 768 }
699 769
700 - private void restoreSessions() { 770 + void restoreSessions() {
701 log.debug("[{}] Restoring sessions from cache", deviceId); 771 log.debug("[{}] Restoring sessions from cache", deviceId);
702 DeviceSessionsCacheEntry sessionsDump = null; 772 DeviceSessionsCacheEntry sessionsDump = null;
703 try { 773 try {
@@ -710,6 +780,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -710,6 +780,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
710 log.debug("[{}] No session information found", deviceId); 780 log.debug("[{}] No session information found", deviceId);
711 return; 781 return;
712 } 782 }
  783 + // TODO: Take latest max allowed sessions size from cache
713 for (SessionSubscriptionInfoProto sessionSubscriptionInfoProto : sessionsDump.getSessionsList()) { 784 for (SessionSubscriptionInfoProto sessionSubscriptionInfoProto : sessionsDump.getSessionsList()) {
714 SessionInfoProto sessionInfoProto = sessionSubscriptionInfoProto.getSessionInfo(); 785 SessionInfoProto sessionInfoProto = sessionSubscriptionInfoProto.getSessionInfo();
715 UUID sessionId = getSessionId(sessionInfoProto); 786 UUID sessionId = getSessionId(sessionInfoProto);
@@ -756,11 +827,30 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -756,11 +827,30 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
756 .addAllSessions(sessionsList).build().toByteArray()); 827 .addAllSessions(sessionsList).build().toByteArray());
757 } 828 }
758 829
759 - void initSessionTimeout(TbActorCtx ctx) { 830 + void init(TbActorCtx ctx) {
760 schedulePeriodicMsgWithDelay(ctx, SessionTimeoutCheckMsg.instance(), systemContext.getSessionReportTimeout(), systemContext.getSessionReportTimeout()); 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 void checkSessionsTimeout() { 852 void checkSessionsTimeout() {
  853 + log.debug("[{}] checkSessionsTimeout started. Size before check {}", deviceId, sessions.size());
764 long expTime = System.currentTimeMillis() - systemContext.getSessionInactivityTimeout(); 854 long expTime = System.currentTimeMillis() - systemContext.getSessionInactivityTimeout();
765 Map<UUID, SessionInfoMetaData> sessionsToRemove = sessions.entrySet().stream().filter(kv -> kv.getValue().getLastActivityTime() < expTime).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 855 Map<UUID, SessionInfoMetaData> sessionsToRemove = sessions.entrySet().stream().filter(kv -> kv.getValue().getLastActivityTime() < expTime).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
766 sessionsToRemove.forEach((sessionId, sessionMD) -> { 856 sessionsToRemove.forEach((sessionId, sessionMD) -> {
@@ -772,6 +862,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { @@ -772,6 +862,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor {
772 if (!sessionsToRemove.isEmpty()) { 862 if (!sessionsToRemove.isEmpty()) {
773 dumpSessions(); 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,5 +25,4 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionType;
25 public class SessionInfo { 25 public class SessionInfo {
26 private final SessionType type; 26 private final SessionType type;
27 private final String nodeId; 27 private final String nodeId;
28 - private long lastActivityTime;  
29 } 28 }
@@ -135,7 +135,7 @@ public class AuthController extends BaseController { @@ -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 public ResponseEntity<String> checkActivateToken( 139 public ResponseEntity<String> checkActivateToken(
140 @RequestParam(value = "activateToken") String activateToken) { 140 @RequestParam(value = "activateToken") String activateToken) {
141 HttpHeaders headers = new HttpHeaders(); 141 HttpHeaders headers = new HttpHeaders();
@@ -159,7 +159,7 @@ public class AuthController extends BaseController { @@ -159,7 +159,7 @@ public class AuthController extends BaseController {
159 159
160 @RequestMapping(value = "/noauth/resetPasswordByEmail", method = RequestMethod.POST) 160 @RequestMapping(value = "/noauth/resetPasswordByEmail", method = RequestMethod.POST)
161 @ResponseStatus(value = HttpStatus.OK) 161 @ResponseStatus(value = HttpStatus.OK)
162 - public void requestResetPasswordByEmail ( 162 + public void requestResetPasswordByEmail(
163 @RequestBody JsonNode resetPasswordByEmailRequest, 163 @RequestBody JsonNode resetPasswordByEmailRequest,
164 HttpServletRequest request) throws ThingsboardException { 164 HttpServletRequest request) throws ThingsboardException {
165 try { 165 try {
@@ -170,13 +170,13 @@ public class AuthController extends BaseController { @@ -170,13 +170,13 @@ public class AuthController extends BaseController {
170 String resetUrl = String.format("%s/api/noauth/resetPassword?resetToken=%s", baseUrl, 170 String resetUrl = String.format("%s/api/noauth/resetPassword?resetToken=%s", baseUrl,
171 userCredentials.getResetToken()); 171 userCredentials.getResetToken());
172 172
173 - mailService.sendResetPasswordEmail(resetUrl, email); 173 + mailService.sendResetPasswordEmailAsync(resetUrl, email);
174 } catch (Exception e) { 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 public ResponseEntity<String> checkResetToken( 180 public ResponseEntity<String> checkResetToken(
181 @RequestParam(value = "resetToken") String resetToken) { 181 @RequestParam(value = "resetToken") String resetToken) {
182 HttpHeaders headers = new HttpHeaders(); 182 HttpHeaders headers = new HttpHeaders();
@@ -69,6 +69,7 @@ import org.thingsboard.server.common.data.id.EntityId; @@ -69,6 +69,7 @@ import org.thingsboard.server.common.data.id.EntityId;
69 import org.thingsboard.server.common.data.id.EntityIdFactory; 69 import org.thingsboard.server.common.data.id.EntityIdFactory;
70 import org.thingsboard.server.common.data.id.EntityViewId; 70 import org.thingsboard.server.common.data.id.EntityViewId;
71 import org.thingsboard.server.common.data.id.OtaPackageId; 71 import org.thingsboard.server.common.data.id.OtaPackageId;
  72 +import org.thingsboard.server.common.data.id.RpcId;
72 import org.thingsboard.server.common.data.id.TbResourceId; 73 import org.thingsboard.server.common.data.id.TbResourceId;
73 import org.thingsboard.server.common.data.id.RuleChainId; 74 import org.thingsboard.server.common.data.id.RuleChainId;
74 import org.thingsboard.server.common.data.id.RuleNodeId; 75 import org.thingsboard.server.common.data.id.RuleNodeId;
@@ -83,6 +84,7 @@ import org.thingsboard.server.common.data.page.TimePageLink; @@ -83,6 +84,7 @@ import org.thingsboard.server.common.data.page.TimePageLink;
83 import org.thingsboard.server.common.data.plugin.ComponentDescriptor; 84 import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
84 import org.thingsboard.server.common.data.plugin.ComponentType; 85 import org.thingsboard.server.common.data.plugin.ComponentType;
85 import org.thingsboard.server.common.data.relation.EntityRelation; 86 import org.thingsboard.server.common.data.relation.EntityRelation;
  87 +import org.thingsboard.server.common.data.rpc.Rpc;
86 import org.thingsboard.server.common.data.rule.RuleChain; 88 import org.thingsboard.server.common.data.rule.RuleChain;
87 import org.thingsboard.server.common.data.rule.RuleChainType; 89 import org.thingsboard.server.common.data.rule.RuleChainType;
88 import org.thingsboard.server.common.data.rule.RuleNode; 90 import org.thingsboard.server.common.data.rule.RuleNode;
@@ -106,6 +108,7 @@ import org.thingsboard.server.dao.model.ModelConstants; @@ -106,6 +108,7 @@ import org.thingsboard.server.dao.model.ModelConstants;
106 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService; 108 import org.thingsboard.server.dao.oauth2.OAuth2ConfigTemplateService;
107 import org.thingsboard.server.dao.oauth2.OAuth2Service; 109 import org.thingsboard.server.dao.oauth2.OAuth2Service;
108 import org.thingsboard.server.dao.relation.RelationService; 110 import org.thingsboard.server.dao.relation.RelationService;
  111 +import org.thingsboard.server.dao.rpc.RpcService;
109 import org.thingsboard.server.dao.rule.RuleChainService; 112 import org.thingsboard.server.dao.rule.RuleChainService;
110 import org.thingsboard.server.dao.tenant.TbTenantProfileCache; 113 import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
111 import org.thingsboard.server.dao.tenant.TenantProfileService; 114 import org.thingsboard.server.dao.tenant.TenantProfileService;
@@ -246,6 +249,9 @@ public abstract class BaseController { @@ -246,6 +249,9 @@ public abstract class BaseController {
246 protected OtaPackageStateService otaPackageStateService; 249 protected OtaPackageStateService otaPackageStateService;
247 250
248 @Autowired 251 @Autowired
  252 + protected RpcService rpcService;
  253 +
  254 + @Autowired
249 protected TbQueueProducerProvider producerProvider; 255 protected TbQueueProducerProvider producerProvider;
250 256
251 @Autowired 257 @Autowired
@@ -786,6 +792,18 @@ public abstract class BaseController { @@ -786,6 +792,18 @@ public abstract class BaseController {
786 } 792 }
787 } 793 }
788 794
  795 + Rpc checkRpcId(RpcId rpcId, Operation operation) throws ThingsboardException {
  796 + try {
  797 + validateId(rpcId, "Incorrect rpcId " + rpcId);
  798 + Rpc rpc = rpcService.findById(getCurrentUser().getTenantId(), rpcId);
  799 + checkNotNull(rpc);
  800 + accessControlService.checkPermission(getCurrentUser(), Resource.RPC, operation, rpcId, rpc);
  801 + return rpc;
  802 + } catch (Exception e) {
  803 + throw handleException(e, false);
  804 + }
  805 + }
  806 +
789 @SuppressWarnings("unchecked") 807 @SuppressWarnings("unchecked")
790 protected <I extends EntityId> I emptyId(EntityType entityType) { 808 protected <I extends EntityId> I emptyId(EntityType entityType) {
791 return (I) EntityIdFactory.getByTypeAndUuid(entityType, ModelConstants.NULL_UUID); 809 return (I) EntityIdFactory.getByTypeAndUuid(entityType, ModelConstants.NULL_UUID);
@@ -782,15 +782,17 @@ public class DeviceController extends BaseController { @@ -782,15 +782,17 @@ public class DeviceController extends BaseController {
782 } 782 }
783 783
784 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") 784 @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
785 - @RequestMapping(value = "/devices/count/{otaPackageType}", method = RequestMethod.GET) 785 + @RequestMapping(value = "/devices/count/{otaPackageType}/{deviceProfileId}", method = RequestMethod.GET)
786 @ResponseBody 786 @ResponseBody
787 - public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(@PathVariable("otaPackageType") String otaPackageType,  
788 - @RequestParam String deviceProfileId) throws ThingsboardException { 787 + public Long countByDeviceProfileAndEmptyOtaPackage(@PathVariable("otaPackageType") String otaPackageType,
  788 + @PathVariable("deviceProfileId") String deviceProfileId) throws ThingsboardException {
789 checkParameter("OtaPackageType", otaPackageType); 789 checkParameter("OtaPackageType", otaPackageType);
790 checkParameter("DeviceProfileId", deviceProfileId); 790 checkParameter("DeviceProfileId", deviceProfileId);
791 try { 791 try {
792 return deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage( 792 return deviceService.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(
793 - getCurrentUser().getTenantId(), new DeviceProfileId(UUID.fromString(deviceProfileId)), OtaPackageType.valueOf(otaPackageType)); 793 + getTenantId(),
  794 + new DeviceProfileId(UUID.fromString(deviceProfileId)),
  795 + OtaPackageType.valueOf(otaPackageType));
794 } catch (Exception e) { 796 } catch (Exception e) {
795 throw handleException(e); 797 throw handleException(e);
796 } 798 }
@@ -29,6 +29,7 @@ import org.springframework.web.bind.annotation.PathVariable; @@ -29,6 +29,7 @@ import org.springframework.web.bind.annotation.PathVariable;
29 import org.springframework.web.bind.annotation.RequestBody; 29 import org.springframework.web.bind.annotation.RequestBody;
30 import org.springframework.web.bind.annotation.RequestMapping; 30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod; 31 import org.springframework.web.bind.annotation.RequestMethod;
  32 +import org.springframework.web.bind.annotation.RequestParam;
32 import org.springframework.web.bind.annotation.ResponseBody; 33 import org.springframework.web.bind.annotation.ResponseBody;
33 import org.springframework.web.bind.annotation.RestController; 34 import org.springframework.web.bind.annotation.RestController;
34 import org.springframework.web.context.request.async.DeferredResult; 35 import org.springframework.web.context.request.async.DeferredResult;
@@ -38,8 +39,13 @@ import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; @@ -38,8 +39,13 @@ import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
38 import org.thingsboard.server.common.data.exception.ThingsboardException; 39 import org.thingsboard.server.common.data.exception.ThingsboardException;
39 import org.thingsboard.server.common.data.id.DeviceId; 40 import org.thingsboard.server.common.data.id.DeviceId;
40 import org.thingsboard.server.common.data.id.EntityId; 41 import org.thingsboard.server.common.data.id.EntityId;
  42 +import org.thingsboard.server.common.data.id.RpcId;
41 import org.thingsboard.server.common.data.id.TenantId; 43 import org.thingsboard.server.common.data.id.TenantId;
42 import org.thingsboard.server.common.data.id.UUIDBased; 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 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody; 49 import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody;
44 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; 50 import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
45 import org.thingsboard.server.queue.util.TbCoreComponent; 51 import org.thingsboard.server.queue.util.TbCoreComponent;
@@ -93,6 +99,52 @@ public class RpcController extends BaseController { @@ -93,6 +99,52 @@ public class RpcController extends BaseController {
93 return handleDeviceRPCRequest(false, new DeviceId(UUID.fromString(deviceIdStr)), requestBody); 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 private DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody) throws ThingsboardException { 148 private DeferredResult<ResponseEntity> handleDeviceRPCRequest(boolean oneWay, DeviceId deviceId, String requestBody) throws ThingsboardException {
97 try { 149 try {
98 JsonNode rpcRequestBody = jsonMapper.readTree(requestBody); 150 JsonNode rpcRequestBody = jsonMapper.readTree(requestBody);
@@ -103,6 +155,7 @@ public class RpcController extends BaseController { @@ -103,6 +155,7 @@ public class RpcController extends BaseController {
103 long timeout = rpcRequestBody.has("timeout") ? rpcRequestBody.get("timeout").asLong() : defaultTimeout; 155 long timeout = rpcRequestBody.has("timeout") ? rpcRequestBody.get("timeout").asLong() : defaultTimeout;
104 long expTime = System.currentTimeMillis() + Math.max(minTimeout, timeout); 156 long expTime = System.currentTimeMillis() + Math.max(minTimeout, timeout);
105 UUID rpcRequestUUID = rpcRequestBody.has("requestUUID") ? UUID.fromString(rpcRequestBody.get("requestUUID").asText()) : UUID.randomUUID(); 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 accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<DeferredResult<ResponseEntity>>() { 159 accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<DeferredResult<ResponseEntity>>() {
107 @Override 160 @Override
108 public void onSuccess(@Nullable DeferredResult<ResponseEntity> result) { 161 public void onSuccess(@Nullable DeferredResult<ResponseEntity> result) {
@@ -111,7 +164,8 @@ public class RpcController extends BaseController { @@ -111,7 +164,8 @@ public class RpcController extends BaseController {
111 deviceId, 164 deviceId,
112 oneWay, 165 oneWay,
113 expTime, 166 expTime,
114 - body 167 + body,
  168 + persisted
115 ); 169 );
116 deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse), currentUser); 170 deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse), currentUser);
117 } 171 }
@@ -74,6 +74,7 @@ import java.util.List; @@ -74,6 +74,7 @@ import java.util.List;
74 import java.util.Map; 74 import java.util.Map;
75 import java.util.Set; 75 import java.util.Set;
76 import java.util.concurrent.ConcurrentMap; 76 import java.util.concurrent.ConcurrentMap;
  77 +import java.util.concurrent.TimeUnit;
77 import java.util.stream.Collectors; 78 import java.util.stream.Collectors;
78 79
79 @Slf4j 80 @Slf4j
@@ -86,6 +87,7 @@ public class RuleChainController extends BaseController { @@ -86,6 +87,7 @@ public class RuleChainController extends BaseController {
86 public static final String RULE_NODE_ID = "ruleNodeId"; 87 public static final String RULE_NODE_ID = "ruleNodeId";
87 88
88 private static final ObjectMapper objectMapper = new ObjectMapper(); 89 private static final ObjectMapper objectMapper = new ObjectMapper();
  90 + public static final int TIMEOUT = 20;
89 91
90 @Autowired 92 @Autowired
91 private InstallScripts installScripts; 93 private InstallScripts installScripts;
@@ -388,25 +390,25 @@ public class RuleChainController extends BaseController { @@ -388,25 +390,25 @@ public class RuleChainController extends BaseController {
388 TbMsg inMsg = TbMsg.newMsg(msgType, null, new TbMsgMetaData(metadata), TbMsgDataType.JSON, data); 390 TbMsg inMsg = TbMsg.newMsg(msgType, null, new TbMsgMetaData(metadata), TbMsgDataType.JSON, data);
389 switch (scriptType) { 391 switch (scriptType) {
390 case "update": 392 case "update":
391 - output = msgToOutput(engine.executeUpdate(inMsg)); 393 + output = msgToOutput(engine.executeUpdateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
392 break; 394 break;
393 case "generate": 395 case "generate":
394 - output = msgToOutput(engine.executeGenerate(inMsg)); 396 + output = msgToOutput(engine.executeGenerateAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS));
395 break; 397 break;
396 case "filter": 398 case "filter":
397 - boolean result = engine.executeFilter(inMsg); 399 + boolean result = engine.executeFilterAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
398 output = Boolean.toString(result); 400 output = Boolean.toString(result);
399 break; 401 break;
400 case "switch": 402 case "switch":
401 - Set<String> states = engine.executeSwitch(inMsg); 403 + Set<String> states = engine.executeSwitchAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
402 output = objectMapper.writeValueAsString(states); 404 output = objectMapper.writeValueAsString(states);
403 break; 405 break;
404 case "json": 406 case "json":
405 - JsonNode json = engine.executeJson(inMsg); 407 + JsonNode json = engine.executeJsonAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
406 output = objectMapper.writeValueAsString(json); 408 output = objectMapper.writeValueAsString(json);
407 break; 409 break;
408 case "string": 410 case "string":
409 - output = engine.executeToString(inMsg); 411 + output = engine.executeToStringAsync(inMsg).get(TIMEOUT, TimeUnit.SECONDS);
410 break; 412 break;
411 default: 413 default:
412 throw new IllegalArgumentException("Unsupported script type: " + scriptType); 414 throw new IllegalArgumentException("Unsupported script type: " + scriptType);
@@ -25,9 +25,12 @@ import org.springframework.security.authentication.BadCredentialsException; @@ -25,9 +25,12 @@ import org.springframework.security.authentication.BadCredentialsException;
25 import org.springframework.security.authentication.DisabledException; 25 import org.springframework.security.authentication.DisabledException;
26 import org.springframework.security.authentication.LockedException; 26 import org.springframework.security.authentication.LockedException;
27 import org.springframework.security.core.AuthenticationException; 27 import org.springframework.security.core.AuthenticationException;
  28 +import org.springframework.security.core.userdetails.UsernameNotFoundException;
28 import org.springframework.security.web.access.AccessDeniedHandler; 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 import org.springframework.web.client.HttpClientErrorException; 32 import org.springframework.web.client.HttpClientErrorException;
  33 +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
31 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; 34 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
32 import org.thingsboard.server.common.data.exception.ThingsboardException; 35 import org.thingsboard.server.common.data.exception.ThingsboardException;
33 import org.thingsboard.server.common.msg.tools.TbRateLimitsException; 36 import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
@@ -40,14 +43,15 @@ import javax.servlet.http.HttpServletRequest; @@ -40,14 +43,15 @@ import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse; 43 import javax.servlet.http.HttpServletResponse;
41 import java.io.IOException; 44 import java.io.IOException;
42 45
43 -@Component  
44 @Slf4j 46 @Slf4j
45 -public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { 47 +@RestControllerAdvice
  48 +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler {
46 49
47 @Autowired 50 @Autowired
48 private ObjectMapper mapper; 51 private ObjectMapper mapper;
49 52
50 @Override 53 @Override
  54 + @ExceptionHandler(AccessDeniedException.class)
51 public void handle(HttpServletRequest request, HttpServletResponse response, 55 public void handle(HttpServletRequest request, HttpServletResponse response,
52 AccessDeniedException accessDeniedException) throws IOException, 56 AccessDeniedException accessDeniedException) throws IOException,
53 ServletException { 57 ServletException {
@@ -60,6 +64,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { @@ -60,6 +64,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
60 } 64 }
61 } 65 }
62 66
  67 + @ExceptionHandler(Exception.class)
63 public void handle(Exception exception, HttpServletResponse response) { 68 public void handle(Exception exception, HttpServletResponse response) {
64 log.debug("Processing exception {}", exception.getMessage(), exception); 69 log.debug("Processing exception {}", exception.getMessage(), exception);
65 if (!response.isCommitted()) { 70 if (!response.isCommitted()) {
@@ -148,7 +153,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { @@ -148,7 +153,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
148 153
149 private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException { 154 private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException {
150 response.setStatus(HttpStatus.UNAUTHORIZED.value()); 155 response.setStatus(HttpStatus.UNAUTHORIZED.value());
151 - if (authenticationException instanceof BadCredentialsException) { 156 + if (authenticationException instanceof BadCredentialsException || authenticationException instanceof UsernameNotFoundException) {
152 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Invalid username or password", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); 157 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Invalid username or password", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
153 } else if (authenticationException instanceof DisabledException) { 158 } else if (authenticationException instanceof DisabledException) {
154 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("User account is not active", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); 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,7 +164,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
159 } else if (authenticationException instanceof AuthMethodNotSupportedException) { 164 } else if (authenticationException instanceof AuthMethodNotSupportedException) {
160 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); 165 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
161 } else if (authenticationException instanceof UserPasswordExpiredException) { 166 } else if (authenticationException instanceof UserPasswordExpiredException) {
162 - UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException; 167 + UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException;
163 String resetToken = expiredException.getResetToken(); 168 String resetToken = expiredException.getResetToken();
164 mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); 169 mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken));
165 } else { 170 } else {
@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
28 import org.springframework.stereotype.Service; 28 import org.springframework.stereotype.Service;
29 import org.thingsboard.common.util.ThingsBoardThreadFactory; 29 import org.thingsboard.common.util.ThingsBoardThreadFactory;
30 import org.thingsboard.server.common.data.DataConstants; 30 import org.thingsboard.server.common.data.DataConstants;
  31 +import org.thingsboard.server.common.data.ResourceUtils;
31 import org.thingsboard.server.common.data.Tenant; 32 import org.thingsboard.server.common.data.Tenant;
32 import org.thingsboard.server.common.data.edge.Edge; 33 import org.thingsboard.server.common.data.edge.Edge;
33 import org.thingsboard.server.common.data.id.EdgeId; 34 import org.thingsboard.server.common.data.id.EdgeId;
@@ -48,6 +49,7 @@ import javax.annotation.PostConstruct; @@ -48,6 +49,7 @@ import javax.annotation.PostConstruct;
48 import javax.annotation.PreDestroy; 49 import javax.annotation.PreDestroy;
49 import java.io.File; 50 import java.io.File;
50 import java.io.IOException; 51 import java.io.IOException;
  52 +import java.io.InputStream;
51 import java.util.Collections; 53 import java.util.Collections;
52 import java.util.Map; 54 import java.util.Map;
53 import java.util.UUID; 55 import java.util.UUID;
@@ -103,9 +105,9 @@ public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase i @@ -103,9 +105,9 @@ public class EdgeGrpcService extends EdgeRpcServiceGrpc.EdgeRpcServiceImplBase i
103 .addService(this); 105 .addService(this);
104 if (sslEnabled) { 106 if (sslEnabled) {
105 try { 107 try {
106 - File certFile = new File(Resources.getResource(certFileResource).toURI());  
107 - File privateKeyFile = new File(Resources.getResource(privateKeyResource).toURI());  
108 - builder.useTransportSecurity(certFile, privateKeyFile); 108 + InputStream certFileIs = ResourceUtils.getInputStream(this, certFileResource);
  109 + InputStream privateKeyFileIs = ResourceUtils.getInputStream(this, privateKeyResource);
  110 + builder.useTransportSecurity(certFileIs, privateKeyFileIs);
109 } catch (Exception e) { 111 } catch (Exception e) {
110 log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e); 112 log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e);
111 throw new RuntimeException("Unable to set up SSL context!", e); 113 throw new RuntimeException("Unable to set up SSL context!", e);
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 */ 15 */
16 package org.thingsboard.server.service.install.update; 16 package org.thingsboard.server.service.install.update;
17 17
  18 +import com.fasterxml.jackson.databind.JsonNode;
18 import com.fasterxml.jackson.databind.node.ObjectNode; 19 import com.fasterxml.jackson.databind.node.ObjectNode;
19 import com.google.common.util.concurrent.Futures; 20 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture; 21 import com.google.common.util.concurrent.ListenableFuture;
@@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.Tenant; @@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.Tenant;
31 import org.thingsboard.server.common.data.alarm.Alarm; 32 import org.thingsboard.server.common.data.alarm.Alarm;
32 import org.thingsboard.server.common.data.alarm.AlarmInfo; 33 import org.thingsboard.server.common.data.alarm.AlarmInfo;
33 import org.thingsboard.server.common.data.alarm.AlarmQuery; 34 import org.thingsboard.server.common.data.alarm.AlarmQuery;
  35 +import org.thingsboard.server.common.data.alarm.AlarmSeverity;
34 import org.thingsboard.server.common.data.id.EntityViewId; 36 import org.thingsboard.server.common.data.id.EntityViewId;
35 import org.thingsboard.server.common.data.id.TenantId; 37 import org.thingsboard.server.common.data.id.TenantId;
36 import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery; 38 import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
@@ -41,16 +43,21 @@ import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientsParams; @@ -41,16 +43,21 @@ import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientsParams;
41 import org.thingsboard.server.common.data.page.PageData; 43 import org.thingsboard.server.common.data.page.PageData;
42 import org.thingsboard.server.common.data.page.PageLink; 44 import org.thingsboard.server.common.data.page.PageLink;
43 import org.thingsboard.server.common.data.page.TimePageLink; 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 import org.thingsboard.server.common.data.rule.RuleChain; 48 import org.thingsboard.server.common.data.rule.RuleChain;
45 import org.thingsboard.server.common.data.rule.RuleChainMetaData; 49 import org.thingsboard.server.common.data.rule.RuleChainMetaData;
46 import org.thingsboard.server.common.data.rule.RuleNode; 50 import org.thingsboard.server.common.data.rule.RuleNode;
  51 +import org.thingsboard.server.dao.DaoUtil;
47 import org.thingsboard.server.dao.alarm.AlarmDao; 52 import org.thingsboard.server.dao.alarm.AlarmDao;
48 import org.thingsboard.server.dao.alarm.AlarmService; 53 import org.thingsboard.server.dao.alarm.AlarmService;
49 import org.thingsboard.server.dao.entity.EntityService; 54 import org.thingsboard.server.dao.entity.EntityService;
50 import org.thingsboard.server.dao.entityview.EntityViewService; 55 import org.thingsboard.server.dao.entityview.EntityViewService;
  56 +import org.thingsboard.server.dao.model.sql.DeviceProfileEntity;
51 import org.thingsboard.server.dao.oauth2.OAuth2Service; 57 import org.thingsboard.server.dao.oauth2.OAuth2Service;
52 import org.thingsboard.server.dao.oauth2.OAuth2Utils; 58 import org.thingsboard.server.dao.oauth2.OAuth2Utils;
53 import org.thingsboard.server.dao.rule.RuleChainService; 59 import org.thingsboard.server.dao.rule.RuleChainService;
  60 +import org.thingsboard.server.dao.sql.device.DeviceProfileRepository;
54 import org.thingsboard.server.dao.tenant.TenantService; 61 import org.thingsboard.server.dao.tenant.TenantService;
55 import org.thingsboard.server.dao.timeseries.TimeseriesService; 62 import org.thingsboard.server.dao.timeseries.TimeseriesService;
56 import org.thingsboard.server.service.install.InstallScripts; 63 import org.thingsboard.server.service.install.InstallScripts;
@@ -93,6 +100,9 @@ public class DefaultDataUpdateService implements DataUpdateService { @@ -93,6 +100,9 @@ public class DefaultDataUpdateService implements DataUpdateService {
93 private AlarmDao alarmDao; 100 private AlarmDao alarmDao;
94 101
95 @Autowired 102 @Autowired
  103 + private DeviceProfileRepository deviceProfileRepository;
  104 +
  105 + @Autowired
96 private OAuth2Service oAuth2Service; 106 private OAuth2Service oAuth2Service;
97 107
98 @Override 108 @Override
@@ -114,6 +124,7 @@ public class DefaultDataUpdateService implements DataUpdateService { @@ -114,6 +124,7 @@ public class DefaultDataUpdateService implements DataUpdateService {
114 log.info("Updating data from version 3.2.2 to 3.3.0 ..."); 124 log.info("Updating data from version 3.2.2 to 3.3.0 ...");
115 tenantsDefaultEdgeRuleChainUpdater.updateEntities(null); 125 tenantsDefaultEdgeRuleChainUpdater.updateEntities(null);
116 tenantsAlarmsCustomerUpdater.updateEntities(null); 126 tenantsAlarmsCustomerUpdater.updateEntities(null);
  127 + deviceProfileEntityDynamicConditionsUpdater.updateEntities(null);
117 updateOAuth2Params(); 128 updateOAuth2Params();
118 break; 129 break;
119 default: 130 default:
@@ -121,6 +132,45 @@ public class DefaultDataUpdateService implements DataUpdateService { @@ -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 private final PaginatedUpdater<String, Tenant> tenantsDefaultRuleChainUpdater = 174 private final PaginatedUpdater<String, Tenant> tenantsDefaultRuleChainUpdater =
125 new PaginatedUpdater<>() { 175 new PaginatedUpdater<>() {
126 176
@@ -370,6 +420,33 @@ public class DefaultDataUpdateService implements DataUpdateService { @@ -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 private void updateOAuth2Params() { 450 private void updateOAuth2Params() {
374 try { 451 try {
375 OAuth2ClientsParams oauth2ClientsParams = oAuth2Service.findOAuth2Params(); 452 OAuth2ClientsParams oauth2ClientsParams = oAuth2Service.findOAuth2Params();
@@ -380,9 +457,8 @@ public class DefaultDataUpdateService implements DataUpdateService { @@ -380,9 +457,8 @@ public class DefaultDataUpdateService implements DataUpdateService {
380 oAuth2Service.saveOAuth2Params(new OAuth2ClientsParams(false, Collections.emptyList())); 457 oAuth2Service.saveOAuth2Params(new OAuth2ClientsParams(false, Collections.emptyList()));
381 log.info("Successfully updated OAuth2 parameters!"); 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,7 +22,7 @@ import org.thingsboard.server.common.data.page.PageData;
22 import org.thingsboard.server.common.data.page.PageLink; 22 import org.thingsboard.server.common.data.page.PageLink;
23 23
24 @Slf4j 24 @Slf4j
25 -public abstract class PaginatedUpdater<I, D extends SearchTextBased<? extends UUIDBased>> { 25 +public abstract class PaginatedUpdater<I, D> {
26 26
27 private static final int DEFAULT_LIMIT = 100; 27 private static final int DEFAULT_LIMIT = 100;
28 private int updated = 0; 28 private int updated = 0;
@@ -30,8 +30,12 @@ import java.math.BigInteger; @@ -30,8 +30,12 @@ import java.math.BigInteger;
30 import java.security.AlgorithmParameters; 30 import java.security.AlgorithmParameters;
31 import java.security.GeneralSecurityException; 31 import java.security.GeneralSecurityException;
32 import java.security.KeyFactory; 32 import java.security.KeyFactory;
  33 +import java.security.KeyStore;
33 import java.security.KeyStoreException; 34 import java.security.KeyStoreException;
  35 +import java.security.NoSuchAlgorithmException;
  36 +import java.security.PrivateKey;
34 import java.security.PublicKey; 37 import java.security.PublicKey;
  38 +import java.security.UnrecoverableKeyException;
35 import java.security.cert.CertificateEncodingException; 39 import java.security.cert.CertificateEncodingException;
36 import java.security.cert.X509Certificate; 40 import java.security.cert.X509Certificate;
37 import java.security.spec.ECGenParameterSpec; 41 import java.security.spec.ECGenParameterSpec;
@@ -62,49 +66,23 @@ public class LwM2MServerSecurityInfoRepository { @@ -62,49 +66,23 @@ public class LwM2MServerSecurityInfoRepository {
62 bsServ.setPort(serverConfig.getPort()); 66 bsServ.setPort(serverConfig.getPort());
63 bsServ.setSecurityHost(serverConfig.getSecureHost()); 67 bsServ.setSecurityHost(serverConfig.getSecureHost());
64 bsServ.setSecurityPort(serverConfig.getSecurePort()); 68 bsServ.setSecurityPort(serverConfig.getSecurePort());
65 - bsServ.setServerPublicKey(getPublicKey(serverConfig.getCertificateAlias(), this.serverConfig.getPublicX(), this.serverConfig.getPublicY())); 69 + bsServ.setServerPublicKey(getPublicKey(serverConfig));
66 return bsServ; 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 try { 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,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.context.MessageSource; 24 import org.springframework.context.MessageSource;
25 import org.springframework.context.annotation.Lazy; 25 import org.springframework.context.annotation.Lazy;
26 import org.springframework.core.NestedRuntimeException; 26 import org.springframework.core.NestedRuntimeException;
  27 +import org.springframework.core.io.InputStreamSource;
  28 +import org.springframework.mail.javamail.JavaMailSender;
27 import org.springframework.mail.javamail.JavaMailSenderImpl; 29 import org.springframework.mail.javamail.JavaMailSenderImpl;
28 import org.springframework.mail.javamail.MimeMessageHelper; 30 import org.springframework.mail.javamail.MimeMessageHelper;
29 import org.springframework.stereotype.Service; 31 import org.springframework.stereotype.Service;
@@ -45,8 +47,8 @@ import org.thingsboard.server.queue.usagestats.TbApiUsageClient; @@ -45,8 +47,8 @@ import org.thingsboard.server.queue.usagestats.TbApiUsageClient;
45 import org.thingsboard.server.service.apiusage.TbApiUsageStateService; 47 import org.thingsboard.server.service.apiusage.TbApiUsageStateService;
46 48
47 import javax.annotation.PostConstruct; 49 import javax.annotation.PostConstruct;
48 -import javax.mail.MessagingException;  
49 import javax.mail.internet.MimeMessage; 50 import javax.mail.internet.MimeMessage;
  51 +import java.io.ByteArrayInputStream;
50 import java.util.HashMap; 52 import java.util.HashMap;
51 import java.util.Locale; 53 import java.util.Locale;
52 import java.util.Map; 54 import java.util.Map;
@@ -71,6 +73,9 @@ public class DefaultMailService implements MailService { @@ -71,6 +73,9 @@ public class DefaultMailService implements MailService {
71 @Autowired 73 @Autowired
72 private TbApiUsageStateService apiUsageStateService; 74 private TbApiUsageStateService apiUsageStateService;
73 75
  76 + @Autowired
  77 + private MailExecutorService mailExecutorService;
  78 +
74 private JavaMailSenderImpl mailSender; 79 private JavaMailSenderImpl mailSender;
75 80
76 private String mailFrom; 81 private String mailFrom;
@@ -220,6 +225,17 @@ public class DefaultMailService implements MailService { @@ -220,6 +225,17 @@ public class DefaultMailService implements MailService {
220 } 225 }
221 226
222 @Override 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 public void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException { 239 public void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException {
224 240
225 String subject = messages.getMessage("password.was.reset.subject", null, Locale.US); 241 String subject = messages.getMessage("password.was.reset.subject", null, Locale.US);
@@ -234,22 +250,47 @@ public class DefaultMailService implements MailService { @@ -234,22 +250,47 @@ public class DefaultMailService implements MailService {
234 } 250 }
235 251
236 @Override 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 if (apiUsageStateService.getApiUsageState(tenantId).isEmailSendEnabled()) { 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 } else { 294 } else {
254 throw new RuntimeException("Email sending is disabled due to API limits!"); 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,6 +32,7 @@ import org.thingsboard.server.common.msg.queue.ServiceQueue;
32 import org.thingsboard.server.common.msg.queue.ServiceType; 32 import org.thingsboard.server.common.msg.queue.ServiceType;
33 import org.thingsboard.server.common.msg.queue.TbCallback; 33 import org.thingsboard.server.common.msg.queue.TbCallback;
34 import org.thingsboard.server.common.msg.queue.TbMsgCallback; 34 import org.thingsboard.server.common.msg.queue.TbMsgCallback;
  35 +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
35 import org.thingsboard.server.common.stats.StatsFactory; 36 import org.thingsboard.server.common.stats.StatsFactory;
36 import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; 37 import org.thingsboard.server.common.transport.util.DataDecodingEncodingService;
37 import org.thingsboard.server.gen.transport.TransportProtos; 38 import org.thingsboard.server.gen.transport.TransportProtos;
@@ -64,12 +65,14 @@ import java.util.Collections; @@ -64,12 +65,14 @@ import java.util.Collections;
64 import java.util.HashSet; 65 import java.util.HashSet;
65 import java.util.List; 66 import java.util.List;
66 import java.util.Map; 67 import java.util.Map;
  68 +import java.util.Queue;
67 import java.util.Set; 69 import java.util.Set;
68 import java.util.UUID; 70 import java.util.UUID;
69 import java.util.concurrent.ConcurrentHashMap; 71 import java.util.concurrent.ConcurrentHashMap;
70 import java.util.concurrent.ConcurrentMap; 72 import java.util.concurrent.ConcurrentMap;
71 import java.util.concurrent.ExecutorService; 73 import java.util.concurrent.ExecutorService;
72 import java.util.concurrent.Executors; 74 import java.util.concurrent.Executors;
  75 +import java.util.concurrent.ScheduledExecutorService;
73 import java.util.concurrent.TimeUnit; 76 import java.util.concurrent.TimeUnit;
74 77
75 @Service 78 @Service
@@ -79,12 +82,15 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< @@ -79,12 +82,15 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
79 82
80 public static final String SUCCESSFUL_STATUS = "successful"; 83 public static final String SUCCESSFUL_STATUS = "successful";
81 public static final String FAILED_STATUS = "failed"; 84 public static final String FAILED_STATUS = "failed";
  85 + public static final String THREAD_TOPIC_SEPARATOR = " | ";
82 @Value("${queue.rule-engine.poll-interval}") 86 @Value("${queue.rule-engine.poll-interval}")
83 private long pollDuration; 87 private long pollDuration;
84 @Value("${queue.rule-engine.pack-processing-timeout}") 88 @Value("${queue.rule-engine.pack-processing-timeout}")
85 private long packProcessingTimeout; 89 private long packProcessingTimeout;
86 @Value("${queue.rule-engine.stats.enabled:true}") 90 @Value("${queue.rule-engine.stats.enabled:true}")
87 private boolean statsEnabled; 91 private boolean statsEnabled;
  92 + @Value("${queue.rule-engine.prometheus-stats.enabled:false}")
  93 + boolean prometheusStatsEnabled;
88 94
89 private final StatsFactory statsFactory; 95 private final StatsFactory statsFactory;
90 private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory; 96 private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory;
@@ -96,7 +102,9 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< @@ -96,7 +102,9 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
96 private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>(); 102 private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>();
97 private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>(); 103 private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>();
98 private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>(); 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 public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory, 109 public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory,
102 TbRuleEngineSubmitStrategyFactory submitStrategyFactory, 110 TbRuleEngineSubmitStrategyFactory submitStrategyFactory,
@@ -125,18 +133,19 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< @@ -125,18 +133,19 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
125 super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer"); 133 super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer");
126 for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) { 134 for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) {
127 consumerConfigurations.putIfAbsent(configuration.getName(), configuration); 135 consumerConfigurations.putIfAbsent(configuration.getName(), configuration);
128 - consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration));  
129 consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory)); 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 @PreDestroy 145 @PreDestroy
135 public void stop() { 146 public void stop() {
136 super.destroy(); 147 super.destroy();
137 - if (submitExecutor != null) {  
138 - submitExecutor.shutdownNow();  
139 - } 148 + submitExecutor.shutdownNow();
140 ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config)); 149 ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config));
141 } 150 }
142 151
@@ -145,96 +154,185 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< @@ -145,96 +154,185 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
145 if (event.getServiceType().equals(getServiceType())) { 154 if (event.getServiceType().equals(getServiceType())) {
146 ServiceQueue serviceQueue = event.getServiceQueueKey().getServiceQueue(); 155 ServiceQueue serviceQueue = event.getServiceQueueKey().getServiceQueue();
147 log.info("[{}] Subscribing to partitions: {}", serviceQueue.getQueue(), event.getPartitions()); 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 @Override 231 @Override
153 protected void launchMainConsumers() { 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 @Override 236 @Override
158 protected void stopMainConsumers() { 237 protected void stopMainConsumers() {
159 consumers.values().forEach(TbQueueConsumer::unsubscribe); 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 private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) { 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,7 +86,10 @@ public class DefaultTbResourceService implements TbResourceService {
86 } else { 86 } else {
87 throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText())); 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 throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL); 93 throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL);
91 } 94 }
92 if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource, true) == null) { 95 if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource, true) == null) {
@@ -194,8 +197,7 @@ public class DefaultTbResourceService implements TbResourceService { @@ -194,8 +197,7 @@ public class DefaultTbResourceService implements TbResourceService {
194 if (isSave) { 197 if (isSave) {
195 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false); 198 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
196 resources.add(lwM2MResourceObserve); 199 resources.add(lwM2MResourceObserve);
197 - }  
198 - else if (v.operations.isReadable()) { 200 + } else if (v.operations.isReadable()) {
199 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false); 201 LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
200 resources.add(lwM2MResourceObserve); 202 resources.add(lwM2MResourceObserve);
201 } 203 }
@@ -204,8 +206,7 @@ public class DefaultTbResourceService implements TbResourceService { @@ -204,8 +206,7 @@ public class DefaultTbResourceService implements TbResourceService {
204 instance.setResources(resources.toArray(LwM2mResourceObserve[]::new)); 206 instance.setResources(resources.toArray(LwM2mResourceObserve[]::new));
205 lwM2mObject.setInstances(new LwM2mInstance[]{instance}); 207 lwM2mObject.setInstances(new LwM2mInstance[]{instance});
206 return lwM2mObject; 208 return lwM2mObject;
207 - }  
208 - else { 209 + } else {
209 return null; 210 return null;
210 } 211 }
211 } 212 }
@@ -157,6 +157,7 @@ public class DefaultTbCoreDeviceRpcService implements TbCoreDeviceRpcService { @@ -157,6 +157,7 @@ public class DefaultTbCoreDeviceRpcService implements TbCoreDeviceRpcService {
157 metaData.putValue("originServiceId", serviceId); 157 metaData.putValue("originServiceId", serviceId);
158 metaData.putValue("expirationTime", Long.toString(msg.getExpirationTime())); 158 metaData.putValue("expirationTime", Long.toString(msg.getExpirationTime()));
159 metaData.putValue("oneway", Boolean.toString(msg.isOneway())); 159 metaData.putValue("oneway", Boolean.toString(msg.isOneway()));
  160 + metaData.putValue("persisted", Boolean.toString(msg.isPersisted()));
160 161
161 Device device = deviceService.findDeviceById(msg.getTenantId(), msg.getDeviceId()); 162 Device device = deviceService.findDeviceById(msg.getTenantId(), msg.getDeviceId());
162 if (device != null) { 163 if (device != null) {
@@ -100,7 +100,7 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi @@ -100,7 +100,7 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi
100 @Override 100 @Override
101 public void sendRpcRequestToDevice(RuleEngineDeviceRpcRequest src, Consumer<RuleEngineDeviceRpcResponse> consumer) { 101 public void sendRpcRequestToDevice(RuleEngineDeviceRpcRequest src, Consumer<RuleEngineDeviceRpcResponse> consumer) {
102 ToDeviceRpcRequest request = new ToDeviceRpcRequest(src.getRequestUUID(), src.getTenantId(), src.getDeviceId(), 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 forwardRpcRequestToDeviceActor(request, response -> { 104 forwardRpcRequestToDeviceActor(request, response -> {
105 if (src.isRestApiCall()) { 105 if (src.isRestApiCall()) {
106 sendRpcResponseToTbCore(src.getOriginServiceId(), response); 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 +}
@@ -30,6 +30,7 @@ import java.util.UUID; @@ -30,6 +30,7 @@ import java.util.UUID;
30 import java.util.concurrent.ConcurrentHashMap; 30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.Executors; 31 import java.util.concurrent.Executors;
32 import java.util.concurrent.ScheduledExecutorService; 32 import java.util.concurrent.ScheduledExecutorService;
  33 +import java.util.concurrent.TimeoutException;
33 import java.util.concurrent.atomic.AtomicInteger; 34 import java.util.concurrent.atomic.AtomicInteger;
34 35
35 /** 36 /**
@@ -84,8 +85,10 @@ public abstract class AbstractJsInvokeService implements JsInvokeService { @@ -84,8 +85,10 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
84 apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1); 85 apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1);
85 return doInvokeFunction(scriptId, functionName, args); 86 return doInvokeFunction(scriptId, functionName, args);
86 } else { 87 } else {
87 - return Futures.immediateFailedFuture(  
88 - new RuntimeException("Script invocation is blocked due to maximum error count " + getMaxErrors() + "!")); 88 + String message = "Script invocation is blocked due to maximum error count "
  89 + + getMaxErrors() + ", scriptId " + scriptId + "!";
  90 + log.warn(message);
  91 + return Futures.immediateFailedFuture(new RuntimeException(message));
89 } 92 }
90 } else { 93 } else {
91 return Futures.immediateFailedFuture(new RuntimeException("JS Execution is disabled due to API limits!")); 94 return Futures.immediateFailedFuture(new RuntimeException("JS Execution is disabled due to API limits!"));
@@ -117,8 +120,11 @@ public abstract class AbstractJsInvokeService implements JsInvokeService { @@ -117,8 +120,11 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
117 120
118 protected abstract long getMaxBlacklistDuration(); 121 protected abstract long getMaxBlacklistDuration();
119 122
120 - protected void onScriptExecutionError(UUID scriptId) {  
121 - disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo()).incrementAndGet(); 123 + protected void onScriptExecutionError(UUID scriptId, Throwable t, String scriptBody) {
  124 + DisableListInfo disableListInfo = disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo());
  125 + log.warn("Script has exception and will increment counter {} on disabledFunctions for id {}, exception {}, cause {}, scriptBody {}",
  126 + disableListInfo.get(), scriptId, t, t.getCause(), scriptBody);
  127 + disableListInfo.incrementAndGet();
122 } 128 }
123 129
124 private String generateJsScript(JsScriptType scriptType, String functionName, String scriptBody, String... argNames) { 130 private String generateJsScript(JsScriptType scriptType, String functionName, String scriptBody, String... argNames) {
@@ -160,7 +160,7 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer @@ -160,7 +160,7 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
160 return ((Invocable) engine).invokeFunction(functionName, args); 160 return ((Invocable) engine).invokeFunction(functionName, args);
161 } 161 }
162 } catch (Exception e) { 162 } catch (Exception e) {
163 - onScriptExecutionError(scriptId); 163 + onScriptExecutionError(scriptId, e, functionName);
164 throw new ExecutionException(e); 164 throw new ExecutionException(e);
165 } 165 }
166 }); 166 });
@@ -18,7 +18,6 @@ package org.thingsboard.server.service.script; @@ -18,7 +18,6 @@ package org.thingsboard.server.service.script;
18 import com.google.common.util.concurrent.FutureCallback; 18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.Futures; 19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture; 20 import com.google.common.util.concurrent.ListenableFuture;
21 -import com.google.common.util.concurrent.MoreExecutors;  
22 import lombok.Getter; 21 import lombok.Getter;
23 import lombok.extern.slf4j.Slf4j; 22 import lombok.extern.slf4j.Slf4j;
24 import org.springframework.beans.factory.annotation.Autowired; 23 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,6 +25,7 @@ import org.springframework.beans.factory.annotation.Value; @@ -26,6 +25,7 @@ import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 25 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
27 import org.springframework.scheduling.annotation.Scheduled; 26 import org.springframework.scheduling.annotation.Scheduled;
28 import org.springframework.stereotype.Service; 27 import org.springframework.stereotype.Service;
  28 +import org.springframework.util.StopWatch;
29 import org.thingsboard.common.util.ThingsBoardThreadFactory; 29 import org.thingsboard.common.util.ThingsBoardThreadFactory;
30 import org.thingsboard.server.gen.js.JsInvokeProtos; 30 import org.thingsboard.server.gen.js.JsInvokeProtos;
31 import org.thingsboard.server.queue.TbQueueRequestTemplate; 31 import org.thingsboard.server.queue.TbQueueRequestTemplate;
@@ -161,7 +161,8 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService { @@ -161,7 +161,8 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
161 161
162 @Override 162 @Override
163 protected ListenableFuture<Object> doInvokeFunction(UUID scriptId, String functionName, Object[] args) { 163 protected ListenableFuture<Object> doInvokeFunction(UUID scriptId, String functionName, Object[] args) {
164 - String scriptBody = scriptIdToBodysMap.get(scriptId); 164 + log.trace("doInvokeFunction js-request for uuid {} with timeout {}ms", scriptId, maxRequestsTimeout);
  165 + final String scriptBody = scriptIdToBodysMap.get(scriptId);
165 if (scriptBody == null) { 166 if (scriptBody == null) {
166 return Futures.immediateFailedFuture(new RuntimeException("No script body found for scriptId: [" + scriptId + "]!")); 167 return Futures.immediateFailedFuture(new RuntimeException("No script body found for scriptId: [" + scriptId + "]!"));
167 } 168 }
@@ -170,7 +171,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService { @@ -170,7 +171,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
170 .setScriptIdLSB(scriptId.getLeastSignificantBits()) 171 .setScriptIdLSB(scriptId.getLeastSignificantBits())
171 .setFunctionName(functionName) 172 .setFunctionName(functionName)
172 .setTimeout((int) maxRequestsTimeout) 173 .setTimeout((int) maxRequestsTimeout)
173 - .setScriptBody(scriptIdToBodysMap.get(scriptId)); 174 + .setScriptBody(scriptBody);
174 175
175 for (Object arg : args) { 176 for (Object arg : args) {
176 jsRequestBuilder.addArgs(arg.toString()); 177 jsRequestBuilder.addArgs(arg.toString());
@@ -180,6 +181,9 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService { @@ -180,6 +181,9 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
180 .setInvokeRequest(jsRequestBuilder.build()) 181 .setInvokeRequest(jsRequestBuilder.build())
181 .build(); 182 .build();
182 183
  184 + StopWatch stopWatch = new StopWatch();
  185 + stopWatch.start();
  186 +
183 ListenableFuture<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> future = requestTemplate.send(new TbProtoJsQueueMsg<>(UUID.randomUUID(), jsRequestWrapper)); 187 ListenableFuture<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> future = requestTemplate.send(new TbProtoJsQueueMsg<>(UUID.randomUUID(), jsRequestWrapper));
184 if (maxRequestsTimeout > 0) { 188 if (maxRequestsTimeout > 0) {
185 future = Futures.withTimeout(future, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService); 189 future = Futures.withTimeout(future, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService);
@@ -193,7 +197,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService { @@ -193,7 +197,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
193 197
194 @Override 198 @Override
195 public void onFailure(Throwable t) { 199 public void onFailure(Throwable t) {
196 - onScriptExecutionError(scriptId); 200 + onScriptExecutionError(scriptId, t, scriptBody);
197 if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) { 201 if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) {
198 queueTimeoutMsgs.incrementAndGet(); 202 queueTimeoutMsgs.incrementAndGet();
199 } 203 }
@@ -201,13 +205,16 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService { @@ -201,13 +205,16 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
201 } 205 }
202 }, callbackExecutor); 206 }, callbackExecutor);
203 return Futures.transform(future, response -> { 207 return Futures.transform(future, response -> {
  208 + stopWatch.stop();
  209 + log.trace("doInvokeFunction js-response took {}ms for uuid {}", stopWatch.getTotalTimeMillis(), response.getKey());
204 JsInvokeProtos.JsInvokeResponse invokeResult = response.getValue().getInvokeResponse(); 210 JsInvokeProtos.JsInvokeResponse invokeResult = response.getValue().getInvokeResponse();
205 if (invokeResult.getSuccess()) { 211 if (invokeResult.getSuccess()) {
206 return invokeResult.getResult(); 212 return invokeResult.getResult();
207 } else { 213 } else {
208 - onScriptExecutionError(scriptId); 214 + final RuntimeException e = new RuntimeException(invokeResult.getErrorDetails());
  215 + onScriptExecutionError(scriptId, e, scriptBody);
209 log.debug("[{}] Failed to compile script due to [{}]: {}", scriptId, invokeResult.getErrorCode().name(), invokeResult.getErrorDetails()); 216 log.debug("[{}] Failed to compile script due to [{}]: {}", scriptId, invokeResult.getErrorCode().name(), invokeResult.getErrorDetails());
210 - throw new RuntimeException(invokeResult.getErrorDetails()); 217 + throw e;
211 } 218 }
212 }, callbackExecutor); 219 }, callbackExecutor);
213 } 220 }
@@ -18,12 +18,12 @@ package org.thingsboard.server.service.script; @@ -18,12 +18,12 @@ package org.thingsboard.server.service.script;
18 import com.fasterxml.jackson.core.type.TypeReference; 18 import com.fasterxml.jackson.core.type.TypeReference;
19 import com.fasterxml.jackson.databind.JsonNode; 19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.ObjectMapper; 20 import com.fasterxml.jackson.databind.ObjectMapper;
21 -import com.google.common.collect.Sets;  
22 import com.google.common.util.concurrent.Futures; 21 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture; 22 import com.google.common.util.concurrent.ListenableFuture;
24 import com.google.common.util.concurrent.MoreExecutors; 23 import com.google.common.util.concurrent.MoreExecutors;
25 import lombok.extern.slf4j.Slf4j; 24 import lombok.extern.slf4j.Slf4j;
26 import org.apache.commons.lang3.StringUtils; 25 import org.apache.commons.lang3.StringUtils;
  26 +import org.thingsboard.server.common.data.id.CustomerId;
27 import org.thingsboard.server.common.data.id.EntityId; 27 import org.thingsboard.server.common.data.id.EntityId;
28 import org.thingsboard.server.common.data.id.TenantId; 28 import org.thingsboard.server.common.data.id.TenantId;
29 import org.thingsboard.server.common.msg.TbMsg; 29 import org.thingsboard.server.common.msg.TbMsg;
@@ -32,6 +32,7 @@ import org.thingsboard.server.common.msg.TbMsgMetaData; @@ -32,6 +32,7 @@ import org.thingsboard.server.common.msg.TbMsgMetaData;
32 import javax.script.ScriptException; 32 import javax.script.ScriptException;
33 import java.util.ArrayList; 33 import java.util.ArrayList;
34 import java.util.Collections; 34 import java.util.Collections;
  35 +import java.util.HashSet;
35 import java.util.List; 36 import java.util.List;
36 import java.util.Map; 37 import java.util.Map;
37 import java.util.Set; 38 import java.util.Set;
@@ -102,140 +103,115 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S @@ -102,140 +103,115 @@ public class RuleNodeJsScriptEngine implements org.thingsboard.rule.engine.api.S
102 String newMessageType = !StringUtils.isEmpty(messageType) ? messageType : msg.getType(); 103 String newMessageType = !StringUtils.isEmpty(messageType) ? messageType : msg.getType();
103 return TbMsg.transformMsg(msg, newMessageType, msg.getOriginator(), newMetadata, newData); 104 return TbMsg.transformMsg(msg, newMessageType, msg.getOriginator(), newMetadata, newData);
104 } catch (Throwable th) { 105 } catch (Throwable th) {
105 - th.printStackTrace();  
106 throw new RuntimeException("Failed to unbind message data from javascript result", th); 106 throw new RuntimeException("Failed to unbind message data from javascript result", th);
107 } 107 }
108 } 108 }
109 109
110 @Override 110 @Override
111 - public List<TbMsg> executeUpdate(TbMsg msg) throws ScriptException {  
112 - JsonNode result = executeScript(msg);  
113 - if (result.isObject()) {  
114 - return Collections.singletonList(unbindMsg(result, msg));  
115 - } else if (result.isArray()){  
116 - List<TbMsg> res = new ArrayList<>(result.size());  
117 - result.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));  
118 - return res;  
119 - } else {  
120 - log.warn("Wrong result type: {}", result.getNodeType());  
121 - throw new ScriptException("Wrong result type: " + result.getNodeType()); 111 + public ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg) {
  112 + ListenableFuture<JsonNode> result = executeScriptAsync(msg);
  113 + return Futures.transformAsync(result,
  114 + json -> executeUpdateTransform(msg, json),
  115 + MoreExecutors.directExecutor());
  116 + }
  117 +
  118 + ListenableFuture<List<TbMsg>> executeUpdateTransform(TbMsg msg, JsonNode json) {
  119 + if (json.isObject()) {
  120 + return Futures.immediateFuture(Collections.singletonList(unbindMsg(json, msg)));
  121 + } else if (json.isArray()) {
  122 + List<TbMsg> res = new ArrayList<>(json.size());
  123 + json.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));
  124 + return Futures.immediateFuture(res);
122 } 125 }
  126 + log.warn("Wrong result type: {}", json.getNodeType());
  127 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
123 } 128 }
124 129
125 @Override 130 @Override
126 - public ListenableFuture<List<TbMsg>> executeUpdateAsync(TbMsg msg) {  
127 - ListenableFuture<JsonNode> result = executeScriptAsync(msg);  
128 - return Futures.transformAsync(result, json -> {  
129 - if (json.isObject()) {  
130 - return Futures.immediateFuture(Collections.singletonList(unbindMsg(json, msg)));  
131 - } else if (json.isArray()){  
132 - List<TbMsg> res = new ArrayList<>(json.size());  
133 - json.forEach(jsonObject -> res.add(unbindMsg(jsonObject, msg)));  
134 - return Futures.immediateFuture(res);  
135 - }  
136 - else{  
137 - log.warn("Wrong result type: {}", json.getNodeType());  
138 - return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));  
139 - }  
140 - }, MoreExecutors.directExecutor()); 131 + public ListenableFuture<TbMsg> executeGenerateAsync(TbMsg prevMsg) {
  132 + return Futures.transformAsync(executeScriptAsync(prevMsg),
  133 + result -> executeGenerateTransform(prevMsg, result),
  134 + MoreExecutors.directExecutor());
141 } 135 }
142 136
143 - @Override  
144 - public TbMsg executeGenerate(TbMsg prevMsg) throws ScriptException {  
145 - JsonNode result = executeScript(prevMsg); 137 + ListenableFuture<TbMsg> executeGenerateTransform(TbMsg prevMsg, JsonNode result) {
146 if (!result.isObject()) { 138 if (!result.isObject()) {
147 log.warn("Wrong result type: {}", result.getNodeType()); 139 log.warn("Wrong result type: {}", result.getNodeType());
148 - throw new ScriptException("Wrong result type: " + result.getNodeType()); 140 + Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
149 } 141 }
150 - return unbindMsg(result, prevMsg); 142 + return Futures.immediateFuture(unbindMsg(result, prevMsg));
151 } 143 }
152 144
153 @Override 145 @Override
154 - public JsonNode executeJson(TbMsg msg) throws ScriptException {  
155 - return executeScript(msg);  
156 - }  
157 -  
158 - @Override  
159 - public ListenableFuture<JsonNode> executeJsonAsync(TbMsg msg) throws ScriptException { 146 + public ListenableFuture<JsonNode> executeJsonAsync(TbMsg msg) {
160 return executeScriptAsync(msg); 147 return executeScriptAsync(msg);
161 } 148 }
162 149
163 @Override 150 @Override
164 - public String executeToString(TbMsg msg) throws ScriptException {  
165 - JsonNode result = executeScript(msg);  
166 - if (!result.isTextual()) {  
167 - log.warn("Wrong result type: {}", result.getNodeType());  
168 - throw new ScriptException("Wrong result type: " + result.getNodeType());  
169 - }  
170 - return result.asText(); 151 + public ListenableFuture<String> executeToStringAsync(TbMsg msg) {
  152 + return Futures.transformAsync(executeScriptAsync(msg),
  153 + this::executeToStringTransform,
  154 + MoreExecutors.directExecutor());
171 } 155 }
172 156
173 - @Override  
174 - public boolean executeFilter(TbMsg msg) throws ScriptException {  
175 - JsonNode result = executeScript(msg);  
176 - if (!result.isBoolean()) {  
177 - log.warn("Wrong result type: {}", result.getNodeType());  
178 - throw new ScriptException("Wrong result type: " + result.getNodeType()); 157 + ListenableFuture<String> executeToStringTransform(JsonNode result) {
  158 + if (result.isTextual()) {
  159 + return Futures.immediateFuture(result.asText());
179 } 160 }
180 - return result.asBoolean(); 161 + log.warn("Wrong result type: {}", result.getNodeType());
  162 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
181 } 163 }
182 164
183 @Override 165 @Override
184 public ListenableFuture<Boolean> executeFilterAsync(TbMsg msg) { 166 public ListenableFuture<Boolean> executeFilterAsync(TbMsg msg) {
185 - ListenableFuture<JsonNode> result = executeScriptAsync(msg);  
186 - return Futures.transformAsync(result, json -> {  
187 - if (!json.isBoolean()) {  
188 - log.warn("Wrong result type: {}", json.getNodeType());  
189 - return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));  
190 - } else {  
191 - return Futures.immediateFuture(json.asBoolean());  
192 - }  
193 - }, MoreExecutors.directExecutor()); 167 + return Futures.transformAsync(executeScriptAsync(msg),
  168 + this::executeFilterTransform,
  169 + MoreExecutors.directExecutor());
194 } 170 }
195 171
196 - @Override  
197 - public Set<String> executeSwitch(TbMsg msg) throws ScriptException {  
198 - JsonNode result = executeScript(msg); 172 + ListenableFuture<Boolean> executeFilterTransform(JsonNode json) {
  173 + if (json.isBoolean()) {
  174 + return Futures.immediateFuture(json.asBoolean());
  175 + }
  176 + log.warn("Wrong result type: {}", json.getNodeType());
  177 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + json.getNodeType()));
  178 + }
  179 +
  180 + ListenableFuture<Set<String>> executeSwitchTransform(JsonNode result) {
199 if (result.isTextual()) { 181 if (result.isTextual()) {
200 - return Collections.singleton(result.asText());  
201 - } else if (result.isArray()) {  
202 - Set<String> nextStates = Sets.newHashSet(); 182 + return Futures.immediateFuture(Collections.singleton(result.asText()));
  183 + }
  184 + if (result.isArray()) {
  185 + Set<String> nextStates = new HashSet<>();
203 for (JsonNode val : result) { 186 for (JsonNode val : result) {
204 if (!val.isTextual()) { 187 if (!val.isTextual()) {
205 log.warn("Wrong result type: {}", val.getNodeType()); 188 log.warn("Wrong result type: {}", val.getNodeType());
206 - throw new ScriptException("Wrong result type: " + val.getNodeType()); 189 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + val.getNodeType()));
207 } else { 190 } else {
208 nextStates.add(val.asText()); 191 nextStates.add(val.asText());
209 } 192 }
210 } 193 }
211 - return nextStates;  
212 - } else {  
213 - log.warn("Wrong result type: {}", result.getNodeType());  
214 - throw new ScriptException("Wrong result type: " + result.getNodeType()); 194 + return Futures.immediateFuture(nextStates);
215 } 195 }
  196 + log.warn("Wrong result type: {}", result.getNodeType());
  197 + return Futures.immediateFailedFuture(new ScriptException("Wrong result type: " + result.getNodeType()));
216 } 198 }
217 199
218 - private JsonNode executeScript(TbMsg msg) throws ScriptException {  
219 - try {  
220 - String[] inArgs = prepareArgs(msg);  
221 - String eval = sandboxService.invokeFunction(tenantId, msg.getCustomerId(), this.scriptId, inArgs[0], inArgs[1], inArgs[2]).get().toString();  
222 - return mapper.readTree(eval);  
223 - } catch (ExecutionException e) {  
224 - if (e.getCause() instanceof ScriptException) {  
225 - throw (ScriptException) e.getCause();  
226 - } else if (e.getCause() instanceof RuntimeException) {  
227 - throw new ScriptException(e.getCause().getMessage());  
228 - } else {  
229 - throw new ScriptException(e);  
230 - }  
231 - } catch (Exception e) {  
232 - throw new ScriptException(e);  
233 - } 200 + @Override
  201 + public ListenableFuture<Set<String>> executeSwitchAsync(TbMsg msg) {
  202 + return Futures.transformAsync(executeScriptAsync(msg),
  203 + this::executeSwitchTransform,
  204 + MoreExecutors.directExecutor()); //usually runs in a callbackExecutor
234 } 205 }
235 206
236 - private ListenableFuture<JsonNode> executeScriptAsync(TbMsg msg) { 207 + ListenableFuture<JsonNode> executeScriptAsync(TbMsg msg) {
  208 + log.trace("execute script async, msg {}", msg);
237 String[] inArgs = prepareArgs(msg); 209 String[] inArgs = prepareArgs(msg);
238 - return Futures.transformAsync(sandboxService.invokeFunction(tenantId, msg.getCustomerId(), this.scriptId, inArgs[0], inArgs[1], inArgs[2]), 210 + return executeScriptAsync(msg.getCustomerId(), inArgs[0], inArgs[1], inArgs[2]);
  211 + }
  212 +
  213 + ListenableFuture<JsonNode> executeScriptAsync(CustomerId customerId, Object... args) {
  214 + return Futures.transformAsync(sandboxService.invokeFunction(tenantId, customerId, this.scriptId, args),
239 o -> { 215 o -> {
240 try { 216 try {
241 return Futures.immediateFuture(mapper.readTree(o.toString())); 217 return Futures.immediateFuture(mapper.readTree(o.toString()));
@@ -47,11 +47,13 @@ import org.thingsboard.server.common.data.id.EntityId; @@ -47,11 +47,13 @@ import org.thingsboard.server.common.data.id.EntityId;
47 import org.thingsboard.server.common.data.id.EntityIdFactory; 47 import org.thingsboard.server.common.data.id.EntityIdFactory;
48 import org.thingsboard.server.common.data.id.EntityViewId; 48 import org.thingsboard.server.common.data.id.EntityViewId;
49 import org.thingsboard.server.common.data.id.OtaPackageId; 49 import org.thingsboard.server.common.data.id.OtaPackageId;
  50 +import org.thingsboard.server.common.data.id.RpcId;
50 import org.thingsboard.server.common.data.id.RuleChainId; 51 import org.thingsboard.server.common.data.id.RuleChainId;
51 import org.thingsboard.server.common.data.id.RuleNodeId; 52 import org.thingsboard.server.common.data.id.RuleNodeId;
52 import org.thingsboard.server.common.data.id.TbResourceId; 53 import org.thingsboard.server.common.data.id.TbResourceId;
53 import org.thingsboard.server.common.data.id.TenantId; 54 import org.thingsboard.server.common.data.id.TenantId;
54 import org.thingsboard.server.common.data.id.UserId; 55 import org.thingsboard.server.common.data.id.UserId;
  56 +import org.thingsboard.server.common.data.rpc.Rpc;
55 import org.thingsboard.server.common.data.rule.RuleChain; 57 import org.thingsboard.server.common.data.rule.RuleChain;
56 import org.thingsboard.server.common.data.rule.RuleNode; 58 import org.thingsboard.server.common.data.rule.RuleNode;
57 import org.thingsboard.server.controller.HttpValidationCallback; 59 import org.thingsboard.server.controller.HttpValidationCallback;
@@ -65,6 +67,7 @@ import org.thingsboard.server.dao.entityview.EntityViewService; @@ -65,6 +67,7 @@ import org.thingsboard.server.dao.entityview.EntityViewService;
65 import org.thingsboard.server.dao.exception.IncorrectParameterException; 67 import org.thingsboard.server.dao.exception.IncorrectParameterException;
66 import org.thingsboard.server.dao.ota.OtaPackageService; 68 import org.thingsboard.server.dao.ota.OtaPackageService;
67 import org.thingsboard.server.dao.resource.ResourceService; 69 import org.thingsboard.server.dao.resource.ResourceService;
  70 +import org.thingsboard.server.dao.rpc.RpcService;
68 import org.thingsboard.server.dao.rule.RuleChainService; 71 import org.thingsboard.server.dao.rule.RuleChainService;
69 import org.thingsboard.server.dao.tenant.TenantService; 72 import org.thingsboard.server.dao.tenant.TenantService;
70 import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; 73 import org.thingsboard.server.dao.usagerecord.ApiUsageStateService;
@@ -137,6 +140,9 @@ public class AccessValidator { @@ -137,6 +140,9 @@ public class AccessValidator {
137 @Autowired 140 @Autowired
138 protected OtaPackageService otaPackageService; 141 protected OtaPackageService otaPackageService;
139 142
  143 + @Autowired
  144 + protected RpcService rpcService;
  145 +
140 private ExecutorService executor; 146 private ExecutorService executor;
141 147
142 @PostConstruct 148 @PostConstruct
@@ -235,6 +241,9 @@ public class AccessValidator { @@ -235,6 +241,9 @@ public class AccessValidator {
235 case OTA_PACKAGE: 241 case OTA_PACKAGE:
236 validateOtaPackage(currentUser, operation, entityId, callback); 242 validateOtaPackage(currentUser, operation, entityId, callback);
237 return; 243 return;
  244 + case RPC:
  245 + validateRpc(currentUser, operation, entityId, callback);
  246 + return;
238 default: 247 default:
239 //TODO: add support of other entities 248 //TODO: add support of other entities
240 throw new IllegalStateException("Not Implemented!"); 249 throw new IllegalStateException("Not Implemented!");
@@ -261,6 +270,22 @@ public class AccessValidator { @@ -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 private void validateDeviceProfile(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) { 289 private void validateDeviceProfile(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
265 if (currentUser.isSystemAdmin()) { 290 if (currentUser.isSystemAdmin()) {
266 callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); 291 callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION));
@@ -206,11 +206,7 @@ public abstract class AbstractOAuth2ClientMapper { @@ -206,11 +206,7 @@ public abstract class AbstractOAuth2ClientMapper {
206 } 206 }
207 207
208 private Optional<DashboardId> getDashboardId(TenantId tenantId, String dashboardName) { 208 private Optional<DashboardId> getDashboardId(TenantId tenantId, String dashboardName) {
209 - PageLink searchTextLink = new PageLink(1, 0, dashboardName);  
210 - PageData<DashboardInfo> dashboardsPage = dashboardService.findDashboardsByTenantId(tenantId, searchTextLink);  
211 - return dashboardsPage.getData().stream()  
212 - .findAny()  
213 - .map(IdBased::getId); 209 + return Optional.ofNullable(dashboardService.findFirstDashboardInfoByTenantIdAndName(tenantId, dashboardName)).map(IdBased::getId);
214 } 210 }
215 211
216 private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) { 212 private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) {
  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.security.auth.oauth2;
  17 +
  18 +import com.fasterxml.jackson.databind.JsonNode;
  19 +import lombok.extern.slf4j.Slf4j;
  20 +import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
  21 +import org.springframework.stereotype.Service;
  22 +import org.springframework.util.LinkedMultiValueMap;
  23 +import org.springframework.util.MultiValueMap;
  24 +import org.springframework.util.StringUtils;
  25 +import org.thingsboard.common.util.JacksonUtil;
  26 +import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig;
  27 +import org.thingsboard.server.common.data.oauth2.OAuth2Registration;
  28 +import org.thingsboard.server.dao.oauth2.OAuth2User;
  29 +import org.thingsboard.server.service.security.model.SecurityUser;
  30 +
  31 +import javax.servlet.http.HttpServletRequest;
  32 +import java.util.HashMap;
  33 +import java.util.Map;
  34 +
  35 +@Service(value = "appleOAuth2ClientMapper")
  36 +@Slf4j
  37 +public class AppleOAuth2ClientMapper extends AbstractOAuth2ClientMapper implements OAuth2ClientMapper {
  38 +
  39 + private static final String USER = "user";
  40 + private static final String NAME = "name";
  41 + private static final String FIRST_NAME = "firstName";
  42 + private static final String LAST_NAME = "lastName";
  43 + private static final String EMAIL = "email";
  44 +
  45 + @Override
  46 + public SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) {
  47 + OAuth2MapperConfig config = registration.getMapperConfig();
  48 + Map<String, Object> attributes = updateAttributesFromRequestParams(request, token.getPrincipal().getAttributes());
  49 + String email = BasicMapperUtils.getStringAttributeByKey(attributes, config.getBasic().getEmailAttributeKey());
  50 + OAuth2User oauth2User = BasicMapperUtils.getOAuth2User(email, attributes, config);
  51 +
  52 + return getOrCreateSecurityUserFromOAuth2User(oauth2User, registration);
  53 + }
  54 +
  55 + private static Map<String, Object> updateAttributesFromRequestParams(HttpServletRequest request, Map<String, Object> attributes) {
  56 + Map<String, Object> updated = attributes;
  57 + MultiValueMap<String, String> params = toMultiMap(request.getParameterMap());
  58 + String userValue = params.getFirst(USER);
  59 + if (StringUtils.hasText(userValue)) {
  60 + JsonNode user = null;
  61 + try {
  62 + user = JacksonUtil.toJsonNode(userValue);
  63 + } catch (Exception e) {}
  64 + if (user != null) {
  65 + updated = new HashMap<>(attributes);
  66 + if (user.has(NAME)) {
  67 + JsonNode name = user.get(NAME);
  68 + if (name.isObject()) {
  69 + JsonNode firstName = name.get(FIRST_NAME);
  70 + if (firstName != null && firstName.isTextual()) {
  71 + updated.put(FIRST_NAME, firstName.asText());
  72 + }
  73 + JsonNode lastName = name.get(LAST_NAME);
  74 + if (lastName != null && lastName.isTextual()) {
  75 + updated.put(LAST_NAME, lastName.asText());
  76 + }
  77 + }
  78 + }
  79 + if (user.has(EMAIL)) {
  80 + JsonNode email = user.get(EMAIL);
  81 + if (email != null && email.isTextual()) {
  82 + updated.put(EMAIL, email.asText());
  83 + }
  84 + }
  85 + }
  86 + }
  87 + return updated;
  88 + }
  89 +
  90 + private static MultiValueMap<String, String> toMultiMap(Map<String, String[]> map) {
  91 + MultiValueMap<String, String> params = new LinkedMultiValueMap<>(map.size());
  92 + map.forEach((key, values) -> {
  93 + if (values.length > 0) {
  94 + for (String value : values) {
  95 + params.add(key, value);
  96 + }
  97 + }
  98 + });
  99 + return params;
  100 + }
  101 +}
@@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration; @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration;
23 import org.thingsboard.server.dao.oauth2.OAuth2User; 23 import org.thingsboard.server.dao.oauth2.OAuth2User;
24 import org.thingsboard.server.service.security.model.SecurityUser; 24 import org.thingsboard.server.service.security.model.SecurityUser;
25 25
  26 +import javax.servlet.http.HttpServletRequest;
26 import java.util.Map; 27 import java.util.Map;
27 28
28 @Service(value = "basicOAuth2ClientMapper") 29 @Service(value = "basicOAuth2ClientMapper")
@@ -30,7 +31,7 @@ import java.util.Map; @@ -30,7 +31,7 @@ import java.util.Map;
30 public class BasicOAuth2ClientMapper extends AbstractOAuth2ClientMapper implements OAuth2ClientMapper { 31 public class BasicOAuth2ClientMapper extends AbstractOAuth2ClientMapper implements OAuth2ClientMapper {
31 32
32 @Override 33 @Override
33 - public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) { 34 + public SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) {
34 OAuth2MapperConfig config = registration.getMapperConfig(); 35 OAuth2MapperConfig config = registration.getMapperConfig();
35 Map<String, Object> attributes = token.getPrincipal().getAttributes(); 36 Map<String, Object> attributes = token.getPrincipal().getAttributes();
36 String email = BasicMapperUtils.getStringAttributeByKey(attributes, config.getBasic().getEmailAttributeKey()); 37 String email = BasicMapperUtils.getStringAttributeByKey(attributes, config.getBasic().getEmailAttributeKey());
@@ -29,6 +29,8 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration; @@ -29,6 +29,8 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration;
29 import org.thingsboard.server.dao.oauth2.OAuth2User; 29 import org.thingsboard.server.dao.oauth2.OAuth2User;
30 import org.thingsboard.server.service.security.model.SecurityUser; 30 import org.thingsboard.server.service.security.model.SecurityUser;
31 31
  32 +import javax.servlet.http.HttpServletRequest;
  33 +
32 @Service(value = "customOAuth2ClientMapper") 34 @Service(value = "customOAuth2ClientMapper")
33 @Slf4j 35 @Slf4j
34 public class CustomOAuth2ClientMapper extends AbstractOAuth2ClientMapper implements OAuth2ClientMapper { 36 public class CustomOAuth2ClientMapper extends AbstractOAuth2ClientMapper implements OAuth2ClientMapper {
@@ -39,7 +41,7 @@ public class CustomOAuth2ClientMapper extends AbstractOAuth2ClientMapper impleme @@ -39,7 +41,7 @@ public class CustomOAuth2ClientMapper extends AbstractOAuth2ClientMapper impleme
39 private RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); 41 private RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
40 42
41 @Override 43 @Override
42 - public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) { 44 + public SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) {
43 OAuth2MapperConfig config = registration.getMapperConfig(); 45 OAuth2MapperConfig config = registration.getMapperConfig();
44 OAuth2User oauth2User = getOAuth2User(token, providerAccessToken, config.getCustom()); 46 OAuth2User oauth2User = getOAuth2User(token, providerAccessToken, config.getCustom());
45 return getOrCreateSecurityUserFromOAuth2User(oauth2User, registration); 47 return getOrCreateSecurityUserFromOAuth2User(oauth2User, registration);
@@ -29,6 +29,7 @@ import org.thingsboard.server.dao.oauth2.OAuth2Configuration; @@ -29,6 +29,7 @@ import org.thingsboard.server.dao.oauth2.OAuth2Configuration;
29 import org.thingsboard.server.dao.oauth2.OAuth2User; 29 import org.thingsboard.server.dao.oauth2.OAuth2User;
30 import org.thingsboard.server.service.security.model.SecurityUser; 30 import org.thingsboard.server.service.security.model.SecurityUser;
31 31
  32 +import javax.servlet.http.HttpServletRequest;
32 import java.util.ArrayList; 33 import java.util.ArrayList;
33 import java.util.Map; 34 import java.util.Map;
34 import java.util.Optional; 35 import java.util.Optional;
@@ -46,7 +47,7 @@ public class GithubOAuth2ClientMapper extends AbstractOAuth2ClientMapper impleme @@ -46,7 +47,7 @@ public class GithubOAuth2ClientMapper extends AbstractOAuth2ClientMapper impleme
46 private OAuth2Configuration oAuth2Configuration; 47 private OAuth2Configuration oAuth2Configuration;
47 48
48 @Override 49 @Override
49 - public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) { 50 + public SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) {
50 OAuth2MapperConfig config = registration.getMapperConfig(); 51 OAuth2MapperConfig config = registration.getMapperConfig();
51 Map<String, String> githubMapperConfig = oAuth2Configuration.getGithubMapper(); 52 Map<String, String> githubMapperConfig = oAuth2Configuration.getGithubMapper();
52 String email = getEmail(githubMapperConfig.get(EMAIL_URL_KEY), providerAccessToken); 53 String email = getEmail(githubMapperConfig.get(EMAIL_URL_KEY), providerAccessToken);
@@ -20,6 +20,8 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration; @@ -20,6 +20,8 @@ import org.thingsboard.server.common.data.oauth2.OAuth2Registration;
20 import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientRegistrationInfo; 20 import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientRegistrationInfo;
21 import org.thingsboard.server.service.security.model.SecurityUser; 21 import org.thingsboard.server.service.security.model.SecurityUser;
22 22
  23 +import javax.servlet.http.HttpServletRequest;
  24 +
23 public interface OAuth2ClientMapper { 25 public interface OAuth2ClientMapper {
24 - SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration); 26 + SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration);
25 } 27 }
@@ -37,6 +37,10 @@ public class OAuth2ClientMapperProvider { @@ -37,6 +37,10 @@ public class OAuth2ClientMapperProvider {
37 @Qualifier("githubOAuth2ClientMapper") 37 @Qualifier("githubOAuth2ClientMapper")
38 private OAuth2ClientMapper githubOAuth2ClientMapper; 38 private OAuth2ClientMapper githubOAuth2ClientMapper;
39 39
  40 + @Autowired
  41 + @Qualifier("appleOAuth2ClientMapper")
  42 + private OAuth2ClientMapper appleOAuth2ClientMapper;
  43 +
40 public OAuth2ClientMapper getOAuth2ClientMapperByType(MapperType oauth2MapperType) { 44 public OAuth2ClientMapper getOAuth2ClientMapperByType(MapperType oauth2MapperType) {
41 switch (oauth2MapperType) { 45 switch (oauth2MapperType) {
42 case CUSTOM: 46 case CUSTOM:
@@ -45,6 +49,8 @@ public class OAuth2ClientMapperProvider { @@ -45,6 +49,8 @@ public class OAuth2ClientMapperProvider {
45 return basicOAuth2ClientMapper; 49 return basicOAuth2ClientMapper;
46 case GITHUB: 50 case GITHUB:
47 return githubOAuth2ClientMapper; 51 return githubOAuth2ClientMapper;
  52 + case APPLE:
  53 + return appleOAuth2ClientMapper;
48 default: 54 default:
49 throw new RuntimeException("OAuth2ClientRegistrationMapper with type " + oauth2MapperType + " is not supported!"); 55 throw new RuntimeException("OAuth2ClientRegistrationMapper with type " + oauth2MapperType + " is not supported!");
50 } 56 }
@@ -36,7 +36,6 @@ import java.net.URLEncoder; @@ -36,7 +36,6 @@ import java.net.URLEncoder;
36 import java.nio.charset.StandardCharsets; 36 import java.nio.charset.StandardCharsets;
37 37
38 @Component(value = "oauth2AuthenticationFailureHandler") 38 @Component(value = "oauth2AuthenticationFailureHandler")
39 -@ConditionalOnProperty(prefix = "security.oauth2", value = "enabled", havingValue = "true")  
40 public class Oauth2AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { 39 public class Oauth2AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
41 40
42 private final HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository; 41 private final HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
@@ -90,7 +90,7 @@ public class Oauth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS @@ -90,7 +90,7 @@ public class Oauth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
90 token.getAuthorizedClientRegistrationId(), 90 token.getAuthorizedClientRegistrationId(),
91 token.getPrincipal().getName()); 91 token.getPrincipal().getName());
92 OAuth2ClientMapper mapper = oauth2ClientMapperProvider.getOAuth2ClientMapperByType(registration.getMapperConfig().getType()); 92 OAuth2ClientMapper mapper = oauth2ClientMapperProvider.getOAuth2ClientMapperByType(registration.getMapperConfig().getType());
93 - SecurityUser securityUser = mapper.getOrCreateUserByClientPrincipal(token, oAuth2AuthorizedClient.getAccessToken().getTokenValue(), 93 + SecurityUser securityUser = mapper.getOrCreateUserByClientPrincipal(request, token, oAuth2AuthorizedClient.getAccessToken().getTokenValue(),
94 registration); 94 registration);
95 95
96 JwtToken accessToken = tokenFactory.createAccessJwtToken(securityUser); 96 JwtToken accessToken = tokenFactory.createAccessJwtToken(securityUser);
@@ -41,6 +41,7 @@ public class CustomerUserPermissions extends AbstractPermissions { @@ -41,6 +41,7 @@ public class CustomerUserPermissions extends AbstractPermissions {
41 put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker); 41 put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker);
42 put(Resource.WIDGET_TYPE, widgetsPermissionChecker); 42 put(Resource.WIDGET_TYPE, widgetsPermissionChecker);
43 put(Resource.EDGE, customerEntityPermissionChecker); 43 put(Resource.EDGE, customerEntityPermissionChecker);
  44 + put(Resource.RPC, rpcPermissionChecker);
44 } 45 }
45 46
46 private static final PermissionChecker customerEntityPermissionChecker = 47 private static final PermissionChecker customerEntityPermissionChecker =
@@ -138,4 +139,22 @@ public class CustomerUserPermissions extends AbstractPermissions { @@ -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,7 +39,8 @@ public enum Resource {
39 API_USAGE_STATE(EntityType.API_USAGE_STATE), 39 API_USAGE_STATE(EntityType.API_USAGE_STATE),
40 TB_RESOURCE(EntityType.TB_RESOURCE), 40 TB_RESOURCE(EntityType.TB_RESOURCE),
41 OTA_PACKAGE(EntityType.OTA_PACKAGE), 41 OTA_PACKAGE(EntityType.OTA_PACKAGE),
42 - EDGE(EntityType.EDGE); 42 + EDGE(EntityType.EDGE),
  43 + RPC(EntityType.RPC);
43 44
44 private final EntityType entityType; 45 private final EntityType entityType;
45 46
@@ -44,6 +44,7 @@ public class TenantAdminPermissions extends AbstractPermissions { @@ -44,6 +44,7 @@ public class TenantAdminPermissions extends AbstractPermissions {
44 put(Resource.TB_RESOURCE, tbResourcePermissionChecker); 44 put(Resource.TB_RESOURCE, tbResourcePermissionChecker);
45 put(Resource.OTA_PACKAGE, tenantEntityPermissionChecker); 45 put(Resource.OTA_PACKAGE, tenantEntityPermissionChecker);
46 put(Resource.EDGE, tenantEntityPermissionChecker); 46 put(Resource.EDGE, tenantEntityPermissionChecker);
  47 + put(Resource.RPC, tenantEntityPermissionChecker);
47 } 48 }
48 49
49 public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() { 50 public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() {
@@ -22,6 +22,7 @@ import com.google.common.util.concurrent.Futures; @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture; 22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors; 23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.protobuf.ByteString; 24 import com.google.protobuf.ByteString;
  25 +import lombok.RequiredArgsConstructor;
25 import lombok.extern.slf4j.Slf4j; 26 import lombok.extern.slf4j.Slf4j;
26 import org.springframework.stereotype.Service; 27 import org.springframework.stereotype.Service;
27 import org.springframework.util.StringUtils; 28 import org.springframework.util.StringUtils;
@@ -40,14 +41,16 @@ import org.thingsboard.server.common.data.TbResource; @@ -40,14 +41,16 @@ import org.thingsboard.server.common.data.TbResource;
40 import org.thingsboard.server.common.data.TenantProfile; 41 import org.thingsboard.server.common.data.TenantProfile;
41 import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; 42 import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
42 import org.thingsboard.server.common.data.device.credentials.ProvisionDeviceCredentialsData; 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 import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials; 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 import org.thingsboard.server.common.data.id.CustomerId; 47 import org.thingsboard.server.common.data.id.CustomerId;
47 import org.thingsboard.server.common.data.id.DeviceId; 48 import org.thingsboard.server.common.data.id.DeviceId;
48 import org.thingsboard.server.common.data.id.DeviceProfileId; 49 import org.thingsboard.server.common.data.id.DeviceProfileId;
49 import org.thingsboard.server.common.data.id.OtaPackageId; 50 import org.thingsboard.server.common.data.id.OtaPackageId;
50 import org.thingsboard.server.common.data.id.TenantId; 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 import org.thingsboard.server.common.data.page.PageData; 54 import org.thingsboard.server.common.data.page.PageData;
52 import org.thingsboard.server.common.data.page.PageLink; 55 import org.thingsboard.server.common.data.page.PageLink;
53 import org.thingsboard.server.common.data.relation.EntityRelation; 56 import org.thingsboard.server.common.data.relation.EntityRelation;
@@ -108,6 +111,7 @@ import java.util.stream.Collectors; @@ -108,6 +111,7 @@ import java.util.stream.Collectors;
108 @Slf4j 111 @Slf4j
109 @Service 112 @Service
110 @TbCoreComponent 113 @TbCoreComponent
  114 +@RequiredArgsConstructor
111 public class DefaultTransportApiService implements TransportApiService { 115 public class DefaultTransportApiService implements TransportApiService {
112 116
113 private static final ObjectMapper mapper = new ObjectMapper(); 117 private static final ObjectMapper mapper = new ObjectMapper();
@@ -129,28 +133,6 @@ public class DefaultTransportApiService implements TransportApiService { @@ -129,28 +133,6 @@ public class DefaultTransportApiService implements TransportApiService {
129 133
130 private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>(); 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 @Override 136 @Override
155 public ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> handle(TbProtoQueueMsg<TransportApiRequestMsg> tbProtoQueueMsg) { 137 public ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> handle(TbProtoQueueMsg<TransportApiRequestMsg> tbProtoQueueMsg) {
156 TransportApiRequestMsg transportApiRequestMsg = tbProtoQueueMsg.getValue(); 138 TransportApiRequestMsg transportApiRequestMsg = tbProtoQueueMsg.getValue();
@@ -479,7 +461,14 @@ public class DefaultTransportApiService implements TransportApiService { @@ -479,7 +461,14 @@ public class DefaultTransportApiService implements TransportApiService {
479 } 461 }
480 462
481 private DeviceInfoProto getDeviceInfoProto(Device device) throws JsonProcessingException { 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 .setTenantIdMSB(device.getTenantId().getId().getMostSignificantBits()) 472 .setTenantIdMSB(device.getTenantId().getId().getMostSignificantBits())
484 .setTenantIdLSB(device.getTenantId().getId().getLeastSignificantBits()) 473 .setTenantIdLSB(device.getTenantId().getId().getLeastSignificantBits())
485 .setCustomerIdMSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getMostSignificantBits()).orElse(0L)) 474 .setCustomerIdMSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getMostSignificantBits()).orElse(0L))
@@ -490,8 +479,11 @@ public class DefaultTransportApiService implements TransportApiService { @@ -490,8 +479,11 @@ public class DefaultTransportApiService implements TransportApiService {
490 .setDeviceType(device.getType()) 479 .setDeviceType(device.getType())
491 .setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits()) 480 .setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits())
492 .setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits()) 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 private ListenableFuture<TransportApiResponseMsg> getEmptyTransportApiResponseFuture() { 489 private ListenableFuture<TransportApiResponseMsg> getEmptyTransportApiResponseFuture() {
@@ -15,8 +15,13 @@ @@ -15,8 +15,13 @@
15 */ 15 */
16 package org.thingsboard.server.service.ttl; 16 package org.thingsboard.server.service.ttl;
17 17
  18 +import lombok.RequiredArgsConstructor;
18 import lombok.extern.slf4j.Slf4j; 19 import lombok.extern.slf4j.Slf4j;
  20 +import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.beans.factory.annotation.Value; 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 import java.sql.Connection; 26 import java.sql.Connection;
22 import java.sql.DriverManager; 27 import java.sql.DriverManager;
@@ -27,43 +32,12 @@ import java.sql.Statement; @@ -27,43 +32,12 @@ import java.sql.Statement;
27 32
28 33
29 @Slf4j 34 @Slf4j
  35 +@RequiredArgsConstructor
30 public abstract class AbstractCleanUpService { 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,7 +13,7 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 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 import lombok.RequiredArgsConstructor; 18 import lombok.RequiredArgsConstructor;
19 import lombok.extern.slf4j.Slf4j; 19 import lombok.extern.slf4j.Slf4j;
@@ -52,6 +52,7 @@ import java.util.concurrent.TimeUnit; @@ -52,6 +52,7 @@ import java.util.concurrent.TimeUnit;
52 @Slf4j 52 @Slf4j
53 @RequiredArgsConstructor 53 @RequiredArgsConstructor
54 public class AlarmsCleanUpService { 54 public class AlarmsCleanUpService {
  55 +
55 @Value("${sql.ttl.alarms.removal_batch_size}") 56 @Value("${sql.ttl.alarms.removal_batch_size}")
56 private Integer removalBatchSize; 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,18 @@ @@ -13,20 +13,18 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 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 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
19 import org.springframework.beans.factory.annotation.Value; 19 import org.springframework.beans.factory.annotation.Value;
20 import org.springframework.scheduling.annotation.Scheduled; 20 import org.springframework.scheduling.annotation.Scheduled;
21 import org.springframework.stereotype.Service; 21 import org.springframework.stereotype.Service;
22 -import org.thingsboard.server.dao.util.PsqlDao; 22 +import org.thingsboard.server.dao.edge.EdgeService;
  23 +import org.thingsboard.server.queue.discovery.PartitionService;
  24 +import org.thingsboard.server.queue.util.TbCoreComponent;
23 import org.thingsboard.server.service.ttl.AbstractCleanUpService; 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 @Slf4j 28 @Slf4j
31 @Service 29 @Service
32 public class EdgeEventsCleanUpService extends AbstractCleanUpService { 30 public class EdgeEventsCleanUpService extends AbstractCleanUpService {
@@ -37,20 +35,18 @@ public class EdgeEventsCleanUpService extends AbstractCleanUpService { @@ -37,20 +35,18 @@ public class EdgeEventsCleanUpService extends AbstractCleanUpService {
37 @Value("${sql.ttl.edge_events.enabled}") 35 @Value("${sql.ttl.edge_events.enabled}")
38 private boolean ttlTaskExecutionEnabled; 36 private boolean ttlTaskExecutionEnabled;
39 37
  38 + private final EdgeService edgeService;
  39 +
  40 + public EdgeEventsCleanUpService(PartitionService partitionService, EdgeService edgeService) {
  41 + super(partitionService);
  42 + this.edgeService = edgeService;
  43 + }
  44 +
40 @Scheduled(initialDelayString = "${sql.ttl.edge_events.execution_interval_ms}", fixedDelayString = "${sql.ttl.edge_events.execution_interval_ms}") 45 @Scheduled(initialDelayString = "${sql.ttl.edge_events.execution_interval_ms}", fixedDelayString = "${sql.ttl.edge_events.execution_interval_ms}")
41 public void cleanUp() { 46 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 - } 47 + if (ttlTaskExecutionEnabled && isSystemTenantPartitionMine()) {
  48 + edgeService.cleanupEvents(ttl);
48 } 49 }
49 } 50 }
50 51
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 } 52 }
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,20 +13,18 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 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 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
19 import org.springframework.beans.factory.annotation.Value; 19 import org.springframework.beans.factory.annotation.Value;
20 import org.springframework.scheduling.annotation.Scheduled; 20 import org.springframework.scheduling.annotation.Scheduled;
21 import org.springframework.stereotype.Service; 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 import org.thingsboard.server.service.ttl.AbstractCleanUpService; 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 @Slf4j 28 @Slf4j
31 @Service 29 @Service
32 public class EventsCleanUpService extends AbstractCleanUpService { 30 public class EventsCleanUpService extends AbstractCleanUpService {
@@ -40,20 +38,18 @@ public class EventsCleanUpService extends AbstractCleanUpService { @@ -40,20 +38,18 @@ public class EventsCleanUpService extends AbstractCleanUpService {
40 @Value("${sql.ttl.events.enabled}") 38 @Value("${sql.ttl.events.enabled}")
41 private boolean ttlTaskExecutionEnabled; 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 @Scheduled(initialDelayString = "${sql.ttl.events.execution_interval_ms}", fixedDelayString = "${sql.ttl.events.execution_interval_ms}") 48 @Scheduled(initialDelayString = "${sql.ttl.events.execution_interval_ms}", fixedDelayString = "${sql.ttl.events.execution_interval_ms}")
44 public void cleanUp() { 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 }
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,19 +13,21 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 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 import lombok.extern.slf4j.Slf4j; 18 import lombok.extern.slf4j.Slf4j;
19 import org.springframework.beans.factory.annotation.Value; 19 import org.springframework.beans.factory.annotation.Value;
20 import org.springframework.scheduling.annotation.Scheduled; 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 import org.thingsboard.server.service.ttl.AbstractCleanUpService; 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 @Slf4j 28 @Slf4j
28 -public abstract class AbstractTimeseriesCleanUpService extends AbstractCleanUpService { 29 +@Service
  30 +public class TimeseriesCleanUpService extends AbstractCleanUpService {
29 31
30 @Value("${sql.ttl.ts.ts_key_value_ttl}") 32 @Value("${sql.ttl.ts.ts_key_value_ttl}")
31 protected long systemTtl; 33 protected long systemTtl;
@@ -33,14 +35,17 @@ public abstract class AbstractTimeseriesCleanUpService extends AbstractCleanUpSe @@ -33,14 +35,17 @@ public abstract class AbstractTimeseriesCleanUpService extends AbstractCleanUpSe
33 @Value("${sql.ttl.ts.enabled}") 35 @Value("${sql.ttl.ts.enabled}")
34 private boolean ttlTaskExecutionEnabled; 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 @Scheduled(initialDelayString = "${sql.ttl.ts.execution_interval_ms}", fixedDelayString = "${sql.ttl.ts.execution_interval_ms}") 45 @Scheduled(initialDelayString = "${sql.ttl.ts.execution_interval_ms}", fixedDelayString = "${sql.ttl.ts.execution_interval_ms}")
37 public void cleanUp() { 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,7 +26,6 @@
26 </appender> 26 </appender>
27 27
28 <logger name="org.thingsboard.server" level="INFO" /> 28 <logger name="org.thingsboard.server" level="INFO" />
29 - <logger name="org.thingsboard.server.transport.snmp" level="DEBUG" />  
30 29
31 <!-- <logger name="org.thingsboard.server.service.queue" level="TRACE" />--> 30 <!-- <logger name="org.thingsboard.server.service.queue" level="TRACE" />-->
32 <!-- <logger name="org.thingsboard.server.service.transport" level="TRACE" />--> 31 <!-- <logger name="org.thingsboard.server.service.transport" level="TRACE" />-->
@@ -276,6 +276,9 @@ sql: @@ -276,6 +276,9 @@ sql:
276 alarms: 276 alarms:
277 checking_interval: "${SQL_ALARMS_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours 277 checking_interval: "${SQL_ALARMS_TTL_CHECKING_INTERVAL:7200000}" # Number of milliseconds. The current value corresponds to two hours
278 removal_batch_size: "${SQL_ALARMS_TTL_REMOVAL_BATCH_SIZE:3000}" # To delete outdated alarms not all at once but in batches 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 # Actor system parameters 283 # Actor system parameters
281 actors: 284 actors:
@@ -332,6 +335,7 @@ actors: @@ -332,6 +335,7 @@ actors:
332 cache: 335 cache:
333 # caffeine or redis 336 # caffeine or redis
334 type: "${CACHE_TYPE:caffeine}" 337 type: "${CACHE_TYPE:caffeine}"
  338 + maximumPoolSize: "${CACHE_MAXIMUM_POOL_SIZE:16}" # max pool size to process futures that calls the external cache
335 attributes: 339 attributes:
336 # make sure that if cache.type is 'redis' and cache.attributes.enabled is 'true' that you change 'maxmemory-policy' Redis config property to 'allkeys-lru', 'allkeys-lfu' or 'allkeys-random' 340 # make sure that if cache.type is 'redis' and cache.attributes.enabled is 'true' that you change 'maxmemory-policy' Redis config property to 'allkeys-lru', 'allkeys-lfu' or 'allkeys-random'
337 enabled: "${CACHE_ATTRIBUTES_ENABLED:true}" 341 enabled: "${CACHE_ATTRIBUTES_ENABLED:true}"
@@ -514,7 +518,7 @@ audit-log: @@ -514,7 +518,7 @@ audit-log:
514 index_pattern: "${AUDIT_LOG_SINK_INDEX_PATTERN:@{TENANT}_AUDIT_LOG_@{DATE}}" 518 index_pattern: "${AUDIT_LOG_SINK_INDEX_PATTERN:@{TENANT}_AUDIT_LOG_@{DATE}}"
515 # Date format. Details of the pattern could be found here: 519 # Date format. Details of the pattern could be found here:
516 # https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html 520 # https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
517 - date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.DD}" 521 + date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.dd}"
518 scheme_name: "${AUDIT_LOG_SINK_SCHEME_NAME:http}" # http or https 522 scheme_name: "${AUDIT_LOG_SINK_SCHEME_NAME:http}" # http or https
519 host: "${AUDIT_LOG_SINK_HOST:localhost}" 523 host: "${AUDIT_LOG_SINK_HOST:localhost}"
520 port: "${AUDIT_LOG_SINK_PORT:9200}" 524 port: "${AUDIT_LOG_SINK_PORT:9200}"
@@ -591,6 +595,10 @@ transport: @@ -591,6 +595,10 @@ transport:
591 ssl: 595 ssl:
592 # Enable/disable SSL support 596 # Enable/disable SSL support
593 enabled: "${MQTT_SSL_ENABLED:false}" 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}"
594 # SSL protocol: See http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext 602 # SSL protocol: See http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext
595 protocol: "${MQTT_SSL_PROTOCOL:TLSv1.2}" 603 protocol: "${MQTT_SSL_PROTOCOL:TLSv1.2}"
596 # Path to the key store that holds the SSL certificate 604 # Path to the key store that holds the SSL certificate
@@ -617,8 +625,6 @@ transport: @@ -617,8 +625,6 @@ transport:
617 bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}" 625 bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}"
618 # CoAP DTLS bind port 626 # CoAP DTLS bind port
619 bind_port: "${COAP_DTLS_BIND_PORT:5684}" 627 bind_port: "${COAP_DTLS_BIND_PORT:5684}"
620 - # Secure mode. Allowed values: NO_AUTH, X509  
621 - mode: "${COAP_DTLS_SECURE_MODE:NO_AUTH}"  
622 # Path to the key store that holds the certificate 628 # Path to the key store that holds the certificate
623 key_store: "${COAP_DTLS_KEY_STORE:coapserver.jks}" 629 key_store: "${COAP_DTLS_KEY_STORE:coapserver.jks}"
624 # Password used to access the key store 630 # Password used to access the key store
@@ -641,49 +647,40 @@ transport: @@ -641,49 +647,40 @@ transport:
641 bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}" 647 bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}"
642 bind_port: "${LWM2M_BIND_PORT:5685}" 648 bind_port: "${LWM2M_BIND_PORT:5685}"
643 security: 649 security:
644 - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY:0.0.0.0}"  
645 - bind_port: "${LWM2M_BIND_PORT_SECURITY:5686}"  
646 - # Only for RPK: Public & Private Key. If the keystore file is missing or not working  
647 - public_x: "${LWM2M_SERVER_PUBLIC_X:05064b9e6762dd8d8b8a52355d7b4d8b9a3d64e6d2ee277d76c248861353f358}"  
648 - public_y: "${LWM2M_SERVER_PUBLIC_Y:5eeb1838e4f9e37b31fa347aef5ce3431eb54e0a2506910c5e0298817445721b}"  
649 - 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}"
650 # Only Certificate_x509: 652 # Only Certificate_x509:
651 - alias: "${LWM2M_KEYSTORE_ALIAS_SERVER:server}" 653 + key_alias: "${LWM2M_SERVER_KEY_ALIAS:server}"
  654 + key_password: "${LWM2M_SERVER_KEY_PASSWORD:server_ks_password}"
652 skip_validity_check_for_client_cert: "${TB_LWM2M_SERVER_SECURITY_SKIP_VALIDITY_CHECK_FOR_CLIENT_CERT:false}" 655 skip_validity_check_for_client_cert: "${TB_LWM2M_SERVER_SECURITY_SKIP_VALIDITY_CHECK_FOR_CLIENT_CERT:false}"
653 bootstrap: 656 bootstrap:
654 enable: "${LWM2M_ENABLED_BS:true}" 657 enable: "${LWM2M_ENABLED_BS:true}"
655 id: "${LWM2M_SERVER_ID_BS:111}" 658 id: "${LWM2M_SERVER_ID_BS:111}"
656 - bind_address: "${LWM2M_BIND_ADDRESS_BS:0.0.0.0}"  
657 - 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}"
658 security: 661 security:
659 - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY_BS:0.0.0.0}"  
660 - bind_port: "${LWM2M_BIND_PORT_SECURITY_BS:5688}"  
661 - # Only for RPK: Public & Private Key. If the keystore file is missing or not working  
662 - public_x: "${LWM2M_SERVER_PUBLIC_X_BS:5017c87a1c1768264656b3b355434b0def6edb8b9bf166a4762d9930cd730f91}"  
663 - public_y: "${LWM2M_SERVER_PUBLIC_Y_BS:3fc4e61bcd8901ec27c424114c3e887ed372497f0c2cf85839b8443e76988b34}"  
664 - 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}"
665 # Only Certificate_x509: 664 # Only Certificate_x509:
666 - alias: "${LWM2M_KEYSTORE_ALIAS_BS:bootstrap}" 665 + key_alias: "${LWM2M_BS_KEY_ALIAS:bootstrap}"
  666 + key_password: "${LWM2M_BS_KEY_PASSWORD:server_ks_password}"
667 security: 667 security:
668 # Certificate_x509: 668 # Certificate_x509:
669 # To get helps about files format and how to generate it, see: https://github.com/eclipse/leshan/wiki/Credential-files-format 669 # To get helps about files format and how to generate it, see: https://github.com/eclipse/leshan/wiki/Credential-files-format
670 # Create new X509 Certificates: common/transport/lwm2m/src/main/resources/credentials/shell/lwM2M_credentials.sh 670 # Create new X509 Certificates: common/transport/lwm2m/src/main/resources/credentials/shell/lwM2M_credentials.sh
671 key_store_type: "${LWM2M_KEYSTORE_TYPE:JKS}" 671 key_store_type: "${LWM2M_KEYSTORE_TYPE:JKS}"
672 # key_store_path_file: "${KEY_STORE_PATH_FILE:/common/transport/lwm2m/src/main/resources/credentials/serverKeyStore.jks" 672 # key_store_path_file: "${KEY_STORE_PATH_FILE:/common/transport/lwm2m/src/main/resources/credentials/serverKeyStore.jks"
673 - key_store: "${LWM2M_KEY_STORE:lwm2mserver.jks}"  
674 - key_store_password: "${LWM2M_KEY_STORE_PASSWORD:server_ks_password}"  
675 - root_alias: "${LWM2M_SERVER_ROOT_CA:rootca}"  
676 - 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}"
677 timeout: "${LWM2M_TIMEOUT:120000}" 678 timeout: "${LWM2M_TIMEOUT:120000}"
678 - recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}"  
679 - recommended_supported_groups: "${LWM2M_RECOMMENDED_SUPPORTED_GROUPS:true}"  
680 - response_pool_size: "${LWM2M_RESPONSE_POOL_SIZE:100}"  
681 - registered_pool_size: "${LWM2M_REGISTERED_POOL_SIZE:10}"  
682 - registration_store_pool_size: "${LWM2M_REGISTRATION_STORE_POOL_SIZE:100}" 679 + uplink_pool_size: "${LWM2M_UPLINK_POOL_SIZE:10}"
  680 + downlink_pool_size: "${LWM2M_DOWNLINK_POOL_SIZE:10}"
  681 + ota_pool_size: "${LWM2M_OTA_POOL_SIZE:10}"
683 clean_period_in_sec: "${LWM2M_CLEAN_PERIOD_IN_SEC:2}" 682 clean_period_in_sec: "${LWM2M_CLEAN_PERIOD_IN_SEC:2}"
684 - update_registered_pool_size: "${LWM2M_UPDATE_REGISTERED_POOL_SIZE:10}"  
685 - un_registered_pool_size: "${LWM2M_UN_REGISTERED_POOL_SIZE:10}"  
686 - log_max_length: "${LWM2M_LOG_MAX_LENGTH:100}" 683 + log_max_length: "${LWM2M_LOG_MAX_LENGTH:1024}"
687 # Use redis for Security and Registration stores 684 # Use redis for Security and Registration stores
688 redis.enabled: "${LWM2M_REDIS_ENABLED:false}" 685 redis.enabled: "${LWM2M_REDIS_ENABLED:false}"
689 snmp: 686 snmp:
@@ -869,6 +866,7 @@ queue: @@ -869,6 +866,7 @@ queue:
869 topic: "${TB_QUEUE_RE_MAIN_TOPIC:tb_rule_engine.main}" 866 topic: "${TB_QUEUE_RE_MAIN_TOPIC:tb_rule_engine.main}"
870 poll-interval: "${TB_QUEUE_RE_MAIN_POLL_INTERVAL_MS:25}" 867 poll-interval: "${TB_QUEUE_RE_MAIN_POLL_INTERVAL_MS:25}"
871 partitions: "${TB_QUEUE_RE_MAIN_PARTITIONS:10}" 868 partitions: "${TB_QUEUE_RE_MAIN_PARTITIONS:10}"
  869 + consumer-per-partition: "${TB_QUEUE_RE_MAIN_CONSUMER_PER_PARTITION:true}"
872 pack-processing-timeout: "${TB_QUEUE_RE_MAIN_PACK_PROCESSING_TIMEOUT_MS:2000}" 870 pack-processing-timeout: "${TB_QUEUE_RE_MAIN_PACK_PROCESSING_TIMEOUT_MS:2000}"
873 submit-strategy: 871 submit-strategy:
874 type: "${TB_QUEUE_RE_MAIN_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL 872 type: "${TB_QUEUE_RE_MAIN_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL
@@ -885,6 +883,7 @@ queue: @@ -885,6 +883,7 @@ queue:
885 topic: "${TB_QUEUE_RE_HP_TOPIC:tb_rule_engine.hp}" 883 topic: "${TB_QUEUE_RE_HP_TOPIC:tb_rule_engine.hp}"
886 poll-interval: "${TB_QUEUE_RE_HP_POLL_INTERVAL_MS:25}" 884 poll-interval: "${TB_QUEUE_RE_HP_POLL_INTERVAL_MS:25}"
887 partitions: "${TB_QUEUE_RE_HP_PARTITIONS:10}" 885 partitions: "${TB_QUEUE_RE_HP_PARTITIONS:10}"
  886 + consumer-per-partition: "${TB_QUEUE_RE_HP_CONSUMER_PER_PARTITION:true}"
888 pack-processing-timeout: "${TB_QUEUE_RE_HP_PACK_PROCESSING_TIMEOUT_MS:2000}" 887 pack-processing-timeout: "${TB_QUEUE_RE_HP_PACK_PROCESSING_TIMEOUT_MS:2000}"
889 submit-strategy: 888 submit-strategy:
890 type: "${TB_QUEUE_RE_HP_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL 889 type: "${TB_QUEUE_RE_HP_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL
@@ -901,6 +900,7 @@ queue: @@ -901,6 +900,7 @@ queue:
901 topic: "${TB_QUEUE_RE_SQ_TOPIC:tb_rule_engine.sq}" 900 topic: "${TB_QUEUE_RE_SQ_TOPIC:tb_rule_engine.sq}"
902 poll-interval: "${TB_QUEUE_RE_SQ_POLL_INTERVAL_MS:25}" 901 poll-interval: "${TB_QUEUE_RE_SQ_POLL_INTERVAL_MS:25}"
903 partitions: "${TB_QUEUE_RE_SQ_PARTITIONS:10}" 902 partitions: "${TB_QUEUE_RE_SQ_PARTITIONS:10}"
  903 + consumer-per-partition: "${TB_QUEUE_RE_SQ_CONSUMER_PER_PARTITION:true}"
904 pack-processing-timeout: "${TB_QUEUE_RE_SQ_PACK_PROCESSING_TIMEOUT_MS:2000}" 904 pack-processing-timeout: "${TB_QUEUE_RE_SQ_PACK_PROCESSING_TIMEOUT_MS:2000}"
905 submit-strategy: 905 submit-strategy:
906 type: "${TB_QUEUE_RE_SQ_SUBMIT_STRATEGY_TYPE:SEQUENTIAL_BY_ORIGINATOR}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL 906 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 +}
@@ -155,6 +155,7 @@ public abstract class BaseUserControllerTest extends AbstractControllerTest { @@ -155,6 +155,7 @@ public abstract class BaseUserControllerTest extends AbstractControllerTest {
155 155
156 doPost("/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest) 156 doPost("/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest)
157 .andExpect(status().isOk()); 157 .andExpect(status().isOk());
  158 + Thread.sleep(1000);
158 doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken) 159 doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken)
159 .andExpect(status().isSeeOther()) 160 .andExpect(status().isSeeOther())
160 .andExpect(header().string(HttpHeaders.LOCATION, "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken)); 161 .andExpect(header().string(HttpHeaders.LOCATION, "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken));
@@ -51,7 +51,7 @@ public class TestMailService { @@ -51,7 +51,7 @@ public class TestMailService {
51 currentResetPasswordToken = passwordResetLink.split("=")[1]; 51 currentResetPasswordToken = passwordResetLink.split("=")[1];
52 return null; 52 return null;
53 } 53 }
54 - }).when(mailService).sendResetPasswordEmail(Mockito.anyString(), Mockito.anyString()); 54 + }).when(mailService).sendResetPasswordEmailAsync(Mockito.anyString(), Mockito.anyString());
55 return mailService; 55 return mailService;
56 } 56 }
57 57
@@ -17,24 +17,46 @@ package org.thingsboard.server.transport.lwm2m; @@ -17,24 +17,46 @@ package org.thingsboard.server.transport.lwm2m;
17 17
18 import com.fasterxml.jackson.core.type.TypeReference; 18 import com.fasterxml.jackson.core.type.TypeReference;
19 import org.apache.commons.io.IOUtils; 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 import org.eclipse.leshan.core.util.Hex; 22 import org.eclipse.leshan.core.util.Hex;
21 import org.junit.After; 23 import org.junit.After;
22 import org.junit.Assert; 24 import org.junit.Assert;
23 import org.junit.Before; 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 import org.thingsboard.common.util.JacksonUtil; 29 import org.thingsboard.common.util.JacksonUtil;
  30 +import org.thingsboard.server.common.data.Device;
25 import org.thingsboard.server.common.data.DeviceProfile; 31 import org.thingsboard.server.common.data.DeviceProfile;
26 import org.thingsboard.server.common.data.DeviceProfileProvisionType; 32 import org.thingsboard.server.common.data.DeviceProfileProvisionType;
27 import org.thingsboard.server.common.data.DeviceProfileType; 33 import org.thingsboard.server.common.data.DeviceProfileType;
28 import org.thingsboard.server.common.data.DeviceTransportType; 34 import org.thingsboard.server.common.data.DeviceTransportType;
  35 +import org.thingsboard.server.common.data.OtaPackageInfo;
29 import org.thingsboard.server.common.data.ResourceType; 36 import org.thingsboard.server.common.data.ResourceType;
30 import org.thingsboard.server.common.data.TbResource; 37 import org.thingsboard.server.common.data.TbResource;
  38 +import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredentials;
31 import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; 39 import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
32 import org.thingsboard.server.common.data.device.profile.DeviceProfileData; 40 import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
33 import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration; 41 import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration;
34 import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration; 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 import org.thingsboard.server.controller.AbstractWebsocketTest; 51 import org.thingsboard.server.controller.AbstractWebsocketTest;
36 import org.thingsboard.server.controller.TbTestWebSocketClient; 52 import org.thingsboard.server.controller.TbTestWebSocketClient;
37 import org.thingsboard.server.dao.service.DaoSqlTest; 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 import java.io.IOException; 61 import java.io.IOException;
40 import java.io.InputStream; 62 import java.io.InputStream;
@@ -54,9 +76,16 @@ import java.security.spec.ECPrivateKeySpec; @@ -54,9 +76,16 @@ import java.security.spec.ECPrivateKeySpec;
54 import java.security.spec.ECPublicKeySpec; 76 import java.security.spec.ECPublicKeySpec;
55 import java.security.spec.KeySpec; 77 import java.security.spec.KeySpec;
56 import java.util.Base64; 78 import java.util.Base64;
  79 +import java.util.Collections;
  80 +import java.util.List;
57 import java.util.concurrent.Executors; 81 import java.util.concurrent.Executors;
58 import java.util.concurrent.ScheduledExecutorService; 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 @DaoSqlTest 89 @DaoSqlTest
61 public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { 90 public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
62 91
@@ -103,7 +132,9 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -103,7 +132,9 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
103 " }\n" + 132 " }\n" +
104 " },\n" + 133 " },\n" +
105 " \"clientLwM2mSettings\": {\n" + 134 " \"clientLwM2mSettings\": {\n" +
106 - " \"clientOnlyObserveAfterConnect\": 1\n" + 135 + " \"clientOnlyObserveAfterConnect\": 1,\n" +
  136 + " \"fwUpdateStrategy\": 1,\n" +
  137 + " \"swUpdateStrategy\": 1\n" +
107 " }\n" + 138 " }\n" +
108 "}"; 139 "}";
109 140
@@ -137,6 +168,15 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -137,6 +168,15 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
137 // certificates trustedby the server (should contain rootCA) 168 // certificates trustedby the server (should contain rootCA)
138 protected final Certificate[] trustedCertificates = new Certificate[1]; 169 protected final Certificate[] trustedCertificates = new Certificate[1];
139 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 +
140 public AbstractLwM2MIntegrationTest() { 180 public AbstractLwM2MIntegrationTest() {
141 // create client credentials 181 // create client credentials
142 try { 182 try {
@@ -224,7 +264,7 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -224,7 +264,7 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
224 executor = Executors.newScheduledThreadPool(10); 264 executor = Executors.newScheduledThreadPool(10);
225 loginTenantAdmin(); 265 loginTenantAdmin();
226 266
227 - 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"};
228 for (String resourceName : resources) { 268 for (String resourceName : resources) {
229 TbResource lwModel = new TbResource(); 269 TbResource lwModel = new TbResource();
230 lwModel.setResourceType(ResourceType.LWM2M_MODEL); 270 lwModel.setResourceType(ResourceType.LWM2M_MODEL);
@@ -260,10 +300,110 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -260,10 +300,110 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
260 Assert.assertNotNull(deviceProfile); 300 Assert.assertNotNull(deviceProfile);
261 } 301 }
262 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 +
263 @After 365 @After
264 public void after() { 366 public void after() {
265 executor.shutdownNow(); 367 executor.shutdownNow();
266 wsClient.close(); 368 wsClient.close();
267 } 369 }
268 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 + }
269 } 409 }
@@ -15,75 +15,151 @@ @@ -15,75 +15,151 @@
15 */ 15 */
16 package org.thingsboard.server.transport.lwm2m; 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;  
20 -import org.jetbrains.annotations.NotNull; 18 +import com.fasterxml.jackson.core.type.TypeReference;
21 import org.junit.Assert; 19 import org.junit.Assert;
22 import org.junit.Test; 20 import org.junit.Test;
23 -import org.thingsboard.common.util.JacksonUtil;  
24 import org.thingsboard.server.common.data.Device; 21 import org.thingsboard.server.common.data.Device;
  22 +import org.thingsboard.server.common.data.OtaPackageInfo;
25 import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials; 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;
26 import org.thingsboard.server.common.data.query.EntityData; 27 import org.thingsboard.server.common.data.query.EntityData;
27 import org.thingsboard.server.common.data.query.EntityDataPageLink; 28 import org.thingsboard.server.common.data.query.EntityDataPageLink;
28 import org.thingsboard.server.common.data.query.EntityDataQuery; 29 import org.thingsboard.server.common.data.query.EntityDataQuery;
29 import org.thingsboard.server.common.data.query.EntityKey; 30 import org.thingsboard.server.common.data.query.EntityKey;
30 import org.thingsboard.server.common.data.query.EntityKeyType; 31 import org.thingsboard.server.common.data.query.EntityKeyType;
31 import org.thingsboard.server.common.data.query.SingleEntityFilter; 32 import org.thingsboard.server.common.data.query.SingleEntityFilter;
32 -import org.thingsboard.server.common.data.security.DeviceCredentials;  
33 -import org.thingsboard.server.common.data.security.DeviceCredentialsType;  
34 import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper; 33 import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;
35 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd; 34 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;
36 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate; 35 import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;
37 import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd; 36 import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;
38 import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient; 37 import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;
39 -import org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MCredentials;  
40 38
  39 +import java.util.Arrays;
41 import java.util.Collections; 40 import java.util.Collections;
42 import java.util.List; 41 import java.util.List;
  42 +import java.util.stream.Collectors;
43 43
44 -import static org.eclipse.leshan.client.object.Security.noSec;  
45 -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 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;
46 52
47 public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { 53 public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
48 54
49 - private final int PORT = 5685;  
50 - private final Security SECURITY = noSec("coap://localhost:" + PORT, 123);  
51 - private final NetworkConfig COAP_CONFIG = new NetworkConfig().setString("COAP_PORT", Integer.toString(PORT));  
52 - private final String ENDPOINT = "deviceAEndpoint";  
53 -  
54 - @NotNull  
55 - private Device createDevice() throws Exception {  
56 - Device device = new Device();  
57 - device.setName("Device A");  
58 - device.setDeviceProfileId(deviceProfile.getId());  
59 - device.setTenantId(tenantId);  
60 - device = doPost("/api/device", device, Device.class);  
61 - Assert.assertNotNull(device);  
62 -  
63 - DeviceCredentials deviceCredentials =  
64 - doGet("/api/device/" + device.getId().getId().toString() + "/credentials", DeviceCredentials.class);  
65 - Assert.assertEquals(device.getId(), deviceCredentials.getDeviceId());  
66 - deviceCredentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS); 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 + "}";
67 131
68 - LwM2MCredentials noSecCredentials = new LwM2MCredentials(); 132 + @Test
  133 + public void testConnectAndObserveTelemetry() throws Exception {
69 NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); 134 NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
70 clientCredentials.setEndpoint(ENDPOINT); 135 clientCredentials.setEndpoint(ENDPOINT);
71 - noSecCredentials.setClient(clientCredentials);  
72 - deviceCredentials.setCredentialsValue(JacksonUtil.toString(noSecCredentials));  
73 - doPost("/api/device/credentials", deviceCredentials).andExpect(status().isOk());  
74 - return device; 136 + super.basicTestConnectionObserveTelemetry(SECURITY, clientCredentials, COAP_CONFIG, ENDPOINT);
75 } 137 }
76 138
77 @Test 139 @Test
78 - public void testConnectAndObserveTelemetry() throws Exception { 140 + public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception {
79 createDeviceProfile(TRANSPORT_CONFIGURATION); 141 createDeviceProfile(TRANSPORT_CONFIGURATION);
  142 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  143 + clientCredentials.setEndpoint(ENDPOINT);
  144 + Device device = createDevice(clientCredentials);
80 145
81 - Device device = createDevice(); 146 + OtaPackageInfo firmware = createFirmware();
  147 +
  148 + LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);
  149 + client.init(SECURITY, COAP_CONFIG);
  150 +
  151 + Thread.sleep(1000);
  152 +
  153 + device.setFirmwareId(firmware.getId());
  154 +
  155 + device = doPost("/api/device", device, Device.class);
  156 +
  157 + Thread.sleep(1000);
82 158
83 SingleEntityFilter sef = new SingleEntityFilter(); 159 SingleEntityFilter sef = new SingleEntityFilter();
84 sef.setSingleEntity(device.getId()); 160 sef.setSingleEntity(device.getId());
85 LatestValueCmd latestCmd = new LatestValueCmd(); 161 LatestValueCmd latestCmd = new LatestValueCmd();
86 - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel"))); 162 + latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "fw_state")));
87 EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null), 163 EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
88 Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); 164 Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
89 165
@@ -95,8 +171,7 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { @@ -95,8 +171,7 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
95 wsClient.waitForReply(); 171 wsClient.waitForReply();
96 172
97 wsClient.registerWaitForUpdate(); 173 wsClient.registerWaitForUpdate();
98 - LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);  
99 - client.init(SECURITY, COAP_CONFIG); 174 +
100 String msg = wsClient.waitForUpdate(); 175 String msg = wsClient.waitForUpdate();
101 176
102 EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class); 177 EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
@@ -106,8 +181,72 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { @@ -106,8 +181,72 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
106 Assert.assertEquals(1, eData.size()); 181 Assert.assertEquals(1, eData.size());
107 Assert.assertEquals(device.getId(), eData.get(0).getEntityId()); 182 Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
108 Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES)); 183 Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
109 - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");  
110 - 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());
  186 + client.destroy();
  187 + }
  188 +
  189 + @Test
  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);
  195 +
  196 + OtaPackageInfo firmware = createFirmware();
  197 +
  198 + LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
  199 + client.init(SECURITY, COAP_CONFIG);
  200 +
  201 + Thread.sleep(1000);
  202 +
  203 + device.setFirmwareId(firmware.getId());
  204 +
  205 + device = doPost("/api/device", device, Device.class);
  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 +
  233 + Thread.sleep(1000);
  234 +
  235 + device.setSoftwareId(software.getId());
  236 +
  237 + device = doPost("/api/device", device, Device.class);
  238 +
  239 + Thread.sleep(4000);
  240 +
  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 + }));
  243 +
  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);
  249 +
111 client.destroy(); 250 client.destroy();
112 } 251 }
113 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,145 +15,40 @@ @@ -15,145 +15,40 @@
15 */ 15 */
16 package org.thingsboard.server.transport.lwm2m; 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 org.eclipse.leshan.client.object.Security;
20 -import org.jetbrains.annotations.NotNull;  
21 -import org.junit.Assert; 19 +import org.junit.Ignore;
22 import org.junit.Test; 20 import org.junit.Test;
23 -import org.thingsboard.common.util.JacksonUtil;  
24 -import org.thingsboard.server.common.data.Device;  
25 import org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredentials; 21 import org.thingsboard.server.common.data.device.credentials.lwm2m.X509ClientCredentials;
26 -import org.thingsboard.server.common.data.query.EntityData;  
27 -import org.thingsboard.server.common.data.query.EntityDataPageLink;  
28 -import org.thingsboard.server.common.data.query.EntityDataQuery;  
29 -import org.thingsboard.server.common.data.query.EntityKey;  
30 -import org.thingsboard.server.common.data.query.EntityKeyType;  
31 -import org.thingsboard.server.common.data.query.SingleEntityFilter;  
32 -import org.thingsboard.server.common.data.security.DeviceCredentials;  
33 -import org.thingsboard.server.common.data.security.DeviceCredentialsType;  
34 import org.thingsboard.server.common.transport.util.SslUtil; 22 import org.thingsboard.server.common.transport.util.SslUtil;
35 -import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;  
36 -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;  
37 -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;  
38 -import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;  
39 -import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;  
40 -import org.thingsboard.server.transport.lwm2m.secure.credentials.LwM2MCredentials;  
41 -  
42 -import java.util.Collections;  
43 -import java.util.List;  
44 23
45 import static org.eclipse.leshan.client.object.Security.x509; 24 import static org.eclipse.leshan.client.object.Security.x509;
46 -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;  
47 25
48 public class X509LwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { 26 public class X509LwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
49 27
50 - private final int port = 5686;  
51 - private final NetworkConfig coapConfig = new NetworkConfig().setString("COAP_SECURE_PORT", Integer.toString(port));  
52 - private final String endpoint = "deviceAEndpoint";  
53 - private final String serverUri = "coaps://localhost:" + port;  
54 -  
55 - @NotNull  
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 @Test 28 @Test
79 public void testConnectAndObserveTelemetry() throws Exception { 29 public void testConnectAndObserveTelemetry() throws Exception {
80 - createDeviceProfile(TRANSPORT_CONFIGURATION);  
81 X509ClientCredentials credentials = new X509ClientCredentials(); 30 X509ClientCredentials credentials = new X509ClientCredentials();
82 - credentials.setEndpoint(endpoint);  
83 - Device device = createDevice(credentials);  
84 -  
85 - SingleEntityFilter sef = new SingleEntityFilter();  
86 - sef.setSingleEntity(device.getId());  
87 - LatestValueCmd latestCmd = new LatestValueCmd();  
88 - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));  
89 - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),  
90 - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());  
91 -  
92 - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);  
93 - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();  
94 - wrapper.setEntityDataCmds(Collections.singletonList(cmd));  
95 -  
96 - wsClient.send(mapper.writeValueAsString(wrapper));  
97 - wsClient.waitForReply();  
98 -  
99 - wsClient.registerWaitForUpdate();  
100 - LwM2MTestClient client = new LwM2MTestClient(executor, endpoint);  
101 - Security security = x509(serverUri, 123, clientX509Cert.getEncoded(), clientPrivateKeyFromCert.getEncoded(), serverX509Cert.getEncoded());  
102 - client.init(security, coapConfig);  
103 - String msg = wsClient.waitForUpdate();  
104 -  
105 - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);  
106 - Assert.assertEquals(1, update.getCmdId());  
107 - List<EntityData> eData = update.getUpdate();  
108 - Assert.assertNotNull(eData);  
109 - Assert.assertEquals(1, eData.size());  
110 - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());  
111 - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));  
112 - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");  
113 - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));  
114 - 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);
115 } 38 }
116 39
  40 + @Ignore //See LwM2mClientContextImpl.unregister
117 @Test 41 @Test
118 public void testConnectWithCertAndObserveTelemetry() throws Exception { 42 public void testConnectWithCertAndObserveTelemetry() throws Exception {
119 - createDeviceProfile(TRANSPORT_CONFIGURATION);  
120 X509ClientCredentials credentials = new X509ClientCredentials(); 43 X509ClientCredentials credentials = new X509ClientCredentials();
121 - credentials.setEndpoint(endpoint); 44 + credentials.setEndpoint(ENDPOINT);
122 credentials.setCert(SslUtil.getCertificateString(clientX509CertNotTrusted)); 45 credentials.setCert(SslUtil.getCertificateString(clientX509CertNotTrusted));
123 - Device device = createDevice(credentials);  
124 -  
125 - SingleEntityFilter sef = new SingleEntityFilter();  
126 - sef.setSingleEntity(device.getId());  
127 - LatestValueCmd latestCmd = new LatestValueCmd();  
128 - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));  
129 - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),  
130 - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());  
131 -  
132 - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);  
133 - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();  
134 - wrapper.setEntityDataCmds(Collections.singletonList(cmd));  
135 -  
136 - wsClient.send(mapper.writeValueAsString(wrapper));  
137 - wsClient.waitForReply();  
138 -  
139 - wsClient.registerWaitForUpdate();  
140 - LwM2MTestClient client = new LwM2MTestClient(executor, endpoint);  
141 -  
142 - Security security = x509(serverUri, 123, clientX509CertNotTrusted.getEncoded(), clientPrivateKeyFromCert.getEncoded(), serverX509Cert.getEncoded());  
143 -  
144 - client.init(security, coapConfig);  
145 - String msg = wsClient.waitForUpdate();  
146 -  
147 - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);  
148 - Assert.assertEquals(1, update.getCmdId());  
149 - List<EntityData> eData = update.getUpdate();  
150 - Assert.assertNotNull(eData);  
151 - Assert.assertEquals(1, eData.size());  
152 - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());  
153 - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));  
154 - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");  
155 - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));  
156 - 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);
157 } 52 }
158 53
159 } 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,8 +53,10 @@ import java.util.List;
53 import java.util.concurrent.ScheduledExecutorService; 53 import java.util.concurrent.ScheduledExecutorService;
54 54
55 import static org.eclipse.leshan.core.LwM2mId.DEVICE; 55 import static org.eclipse.leshan.core.LwM2mId.DEVICE;
  56 +import static org.eclipse.leshan.core.LwM2mId.FIRMWARE;
56 import static org.eclipse.leshan.core.LwM2mId.SECURITY; 57 import static org.eclipse.leshan.core.LwM2mId.SECURITY;
57 import static org.eclipse.leshan.core.LwM2mId.SERVER; 58 import static org.eclipse.leshan.core.LwM2mId.SERVER;
  59 +import static org.eclipse.leshan.core.LwM2mId.SOFTWARE_MANAGEMENT;
58 60
59 @Slf4j 61 @Slf4j
60 @Data 62 @Data
@@ -65,7 +67,7 @@ public class LwM2MTestClient { @@ -65,7 +67,7 @@ public class LwM2MTestClient {
65 private LeshanClient client; 67 private LeshanClient client;
66 68
67 public void init(Security security, NetworkConfig coapConfig) throws InvalidDDFFileException, IOException { 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 List<ObjectModel> models = new ArrayList<>(); 71 List<ObjectModel> models = new ArrayList<>();
70 for (String resourceName : resources) { 72 for (String resourceName : resources) {
71 models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName)); 73 models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName));
@@ -75,6 +77,8 @@ public class LwM2MTestClient { @@ -75,6 +77,8 @@ public class LwM2MTestClient {
75 initializer.setInstancesForObject(SECURITY, security); 77 initializer.setInstancesForObject(SECURITY, security);
76 initializer.setInstancesForObject(SERVER, new Server(123, 300)); 78 initializer.setInstancesForObject(SERVER, new Server(123, 300));
77 initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice()); 79 initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice());
  80 + initializer.setInstancesForObject(FIRMWARE, new FwLwM2MDevice());
  81 + initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, new SwLwM2MDevice());
78 initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class); 82 initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class);
79 83
80 DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(); 84 DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
@@ -45,74 +45,74 @@ public class SimpleLwM2MDevice extends BaseInstanceEnabler implements Destroyabl @@ -45,74 +45,74 @@ public class SimpleLwM2MDevice extends BaseInstanceEnabler implements Destroyabl
45 ); 45 );
46 46
47 @Override 47 @Override
48 - public ReadResponse read(ServerIdentity identity, int resourceid) { 48 + public ReadResponse read(ServerIdentity identity, int resourceId) {
49 if (!identity.isSystem()) 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 case 0: 52 case 0:
53 - return ReadResponse.success(resourceid, getManufacturer()); 53 + return ReadResponse.success(resourceId, getManufacturer());
54 case 1: 54 case 1:
55 - return ReadResponse.success(resourceid, getModelNumber()); 55 + return ReadResponse.success(resourceId, getModelNumber());
56 case 2: 56 case 2:
57 - return ReadResponse.success(resourceid, getSerialNumber()); 57 + return ReadResponse.success(resourceId, getSerialNumber());
58 case 3: 58 case 3:
59 - return ReadResponse.success(resourceid, getFirmwareVersion()); 59 + return ReadResponse.success(resourceId, getFirmwareVersion());
60 case 9: 60 case 9:
61 - return ReadResponse.success(resourceid, getBatteryLevel()); 61 + return ReadResponse.success(resourceId, getBatteryLevel());
62 case 10: 62 case 10:
63 - return ReadResponse.success(resourceid, getMemoryFree()); 63 + return ReadResponse.success(resourceId, getMemoryFree());
64 case 11: 64 case 11:
65 Map<Integer, Long> errorCodes = new HashMap<>(); 65 Map<Integer, Long> errorCodes = new HashMap<>();
66 errorCodes.put(0, getErrorCode()); 66 errorCodes.put(0, getErrorCode());
67 - return ReadResponse.success(resourceid, errorCodes, ResourceModel.Type.INTEGER); 67 + return ReadResponse.success(resourceId, errorCodes, ResourceModel.Type.INTEGER);
68 case 14: 68 case 14:
69 - return ReadResponse.success(resourceid, getUtcOffset()); 69 + return ReadResponse.success(resourceId, getUtcOffset());
70 case 15: 70 case 15:
71 - return ReadResponse.success(resourceid, getTimezone()); 71 + return ReadResponse.success(resourceId, getTimezone());
72 case 16: 72 case 16:
73 - return ReadResponse.success(resourceid, getSupportedBinding()); 73 + return ReadResponse.success(resourceId, getSupportedBinding());
74 case 17: 74 case 17:
75 - return ReadResponse.success(resourceid, getDeviceType()); 75 + return ReadResponse.success(resourceId, getDeviceType());
76 case 18: 76 case 18:
77 - return ReadResponse.success(resourceid, getHardwareVersion()); 77 + return ReadResponse.success(resourceId, getHardwareVersion());
78 case 19: 78 case 19:
79 - return ReadResponse.success(resourceid, getSoftwareVersion()); 79 + return ReadResponse.success(resourceId, getSoftwareVersion());
80 case 20: 80 case 20:
81 - return ReadResponse.success(resourceid, getBatteryStatus()); 81 + return ReadResponse.success(resourceId, getBatteryStatus());
82 case 21: 82 case 21:
83 - return ReadResponse.success(resourceid, getMemoryTotal()); 83 + return ReadResponse.success(resourceId, getMemoryTotal());
84 default: 84 default:
85 - return super.read(identity, resourceid); 85 + return super.read(identity, resourceId);
86 } 86 }
87 } 87 }
88 88
89 @Override 89 @Override
90 - public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) { 90 + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) {
91 String withParams = null; 91 String withParams = null;
92 if (params != null && params.length() != 0) { 92 if (params != null && params.length() != 0) {
93 withParams = " with params " + params; 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 return ExecuteResponse.success(); 96 return ExecuteResponse.success();
97 } 97 }
98 98
99 @Override 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 case 13: 104 case 13:
105 return WriteResponse.notFound(); 105 return WriteResponse.notFound();
106 case 14: 106 case 14:
107 setUtcOffset((String) value.getValue()); 107 setUtcOffset((String) value.getValue());
108 - fireResourcesChange(resourceid); 108 + fireResourcesChange(resourceId);
109 return WriteResponse.success(); 109 return WriteResponse.success();
110 case 15: 110 case 15:
111 setTimezone((String) value.getValue()); 111 setTimezone((String) value.getValue());
112 - fireResourcesChange(resourceid); 112 + fireResourcesChange(resourceId);
113 return WriteResponse.success(); 113 return WriteResponse.success();
114 default: 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 +}
1 transport.lwm2m.security.key_store=lwm2m/credentials/serverKeyStore.jks 1 transport.lwm2m.security.key_store=lwm2m/credentials/serverKeyStore.jks
2 transport.lwm2m.security.key_store_password=server 2 transport.lwm2m.security.key_store_password=server
3 edges.enabled=true 3 edges.enabled=true
4 -transport.lwm2m.bootstrap.security.alias=server  
  4 +transport.lwm2m.server.security.key_alias=server
  5 +transport.lwm2m.server.security.key_password=server
  6 +transport.lwm2m.bootstrap.security.key_alias=server
  7 +transport.lwm2m.bootstrap.security.key_password=server
@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 <logger name="org.springframework.boot.test" level="WARN"/> 14 <logger name="org.springframework.boot.test" level="WARN"/>
15 <logger name="org.apache.cassandra" level="WARN"/> 15 <logger name="org.apache.cassandra" level="WARN"/>
16 <logger name="org.cassandraunit" level="INFO"/> 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 <root level="WARN"> 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>