Commit 2704696f2bc6a1abdf55176ddc8183c4655df8c2
1 parent
950907d3
Moved provisionDeviceKey as regular column
Showing
27 changed files
with
189 additions
and
85 deletions
... | ... | @@ -20,13 +20,16 @@ CREATE TABLE IF NOT EXISTS device_profile ( |
20 | 20 | name varchar(255), |
21 | 21 | type varchar(255), |
22 | 22 | transport_type varchar(255), |
23 | + provision_type varchar(255), | |
23 | 24 | profile_data jsonb, |
24 | 25 | description varchar, |
25 | 26 | search_text varchar(255), |
26 | 27 | is_default boolean, |
27 | 28 | tenant_id uuid, |
28 | 29 | default_rule_chain_id uuid, |
30 | + provision_device_key varchar, | |
29 | 31 | CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), |
32 | + CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key), | |
30 | 33 | CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) |
31 | 34 | ); |
32 | 35 | ... | ... |
... | ... | @@ -27,10 +27,8 @@ import org.springframework.util.StringUtils; |
27 | 27 | import org.thingsboard.server.common.data.DataConstants; |
28 | 28 | import org.thingsboard.server.common.data.Device; |
29 | 29 | import org.thingsboard.server.common.data.DeviceProfile; |
30 | -import org.thingsboard.server.common.data.DeviceProfileType; | |
30 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
31 | 31 | import org.thingsboard.server.common.data.audit.ActionType; |
32 | -import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; | |
33 | -import org.thingsboard.server.common.data.device.profile.ProvisionRequestValidationStrategyType; | |
34 | 32 | import org.thingsboard.server.common.data.id.CustomerId; |
35 | 33 | import org.thingsboard.server.common.data.id.TenantId; |
36 | 34 | import org.thingsboard.server.common.data.id.UserId; |
... | ... | @@ -117,21 +115,18 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
117 | 115 | return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); |
118 | 116 | } |
119 | 117 | |
120 | - DeviceProfile targetProfile = deviceProfileDao.findProfileByProfileNameAndProfileDataProvisionConfigurationPair( | |
121 | - provisionRequest.getDeviceType(), | |
122 | - provisionRequestKey, | |
123 | - provisionRequestSecret); | |
118 | + DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionRequestKey, provisionRequestSecret); | |
124 | 119 | |
125 | - if (targetProfile == null || targetProfile.getProfileData().getConfiguration().getType() != DeviceProfileType.PROVISION) { | |
120 | + if (targetProfile == null || | |
121 | + !(targetProfile.getProvisionType() != DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES || | |
122 | + targetProfile.getProvisionType() != DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES)) { | |
126 | 123 | return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); |
127 | 124 | } |
128 | 125 | |
129 | - ProvisionRequestValidationStrategyType validationStrategy = getStrategy(targetProfile); | |
130 | - | |
131 | 126 | Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null); |
132 | 127 | |
133 | - switch(validationStrategy) { | |
134 | - case CHECK_NEW_DEVICE: | |
128 | + switch(targetProfile.getProvisionType()) { | |
129 | + case ALLOW_CREATE_NEW_DEVICES: | |
135 | 130 | if (targetDevice != null) { |
136 | 131 | log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName()); |
137 | 132 | notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false); |
... | ... | @@ -139,15 +134,15 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
139 | 134 | } else { |
140 | 135 | return createDevice(provisionRequest, targetProfile); |
141 | 136 | } |
142 | - case CHECK_PRE_PROVISIONED_DEVICE: | |
143 | - if (targetDevice != null){ | |
137 | + case CHECK_PRE_PROVISIONED_DEVICES: | |
138 | + if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())){ | |
144 | 139 | return processProvision(targetDevice, provisionRequest); |
145 | 140 | } else { |
146 | 141 | log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName()); |
147 | 142 | return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE)); |
148 | 143 | } |
149 | 144 | default: |
150 | - throw new RuntimeException("Strategy is not supported - " + validationStrategy.name()); | |
145 | + throw new RuntimeException("Strategy is not supported - " + targetProfile.getProvisionType().name()); | |
151 | 146 | } |
152 | 147 | } |
153 | 148 | |
... | ... | @@ -193,11 +188,6 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
193 | 188 | logAction(device.getTenantId(), device.getCustomerId(), device, success, provisionRequest); |
194 | 189 | } |
195 | 190 | |
196 | - private ProvisionRequestValidationStrategyType getStrategy(DeviceProfile profile) { | |
197 | - return ((ProvisionDeviceProfileConfiguration) profile.getProfileData().getConfiguration()).getStrategy(); | |
198 | - | |
199 | - } | |
200 | - | |
201 | 191 | private ListenableFuture<ProvisionResponse> processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) { |
202 | 192 | Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName()); |
203 | 193 | if (device == null) { |
... | ... | @@ -226,7 +216,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
226 | 216 | private Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) { |
227 | 217 | Device device = new Device(); |
228 | 218 | device.setName(provisionRequest.getDeviceName()); |
229 | - device.setType(provisionRequest.getDeviceType()); | |
219 | + device.setType(profile.getName()); | |
230 | 220 | device.setTenantId(profile.getTenantId()); |
231 | 221 | return deviceService.saveDevice(device); |
232 | 222 | } | ... | ... |
... | ... | @@ -30,7 +30,7 @@ import org.thingsboard.server.common.data.Device; |
30 | 30 | import org.thingsboard.server.common.data.DeviceProfile; |
31 | 31 | import org.thingsboard.server.common.data.TenantProfile; |
32 | 32 | import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; |
33 | -import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; | |
33 | +import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials; | |
34 | 34 | import org.thingsboard.server.common.data.id.CustomerId; |
35 | 35 | import org.thingsboard.server.common.data.id.DeviceId; |
36 | 36 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
... | ... | @@ -279,9 +279,8 @@ public class DefaultTransportApiService implements TransportApiService { |
279 | 279 | provisionResponseFuture = deviceProvisionService.provisionDevice( |
280 | 280 | new ProvisionRequest( |
281 | 281 | requestMsg.getDeviceName(), |
282 | - requestMsg.getDeviceType(), | |
283 | 282 | requestMsg.getX509CertPubKey(), |
284 | - new ProvisionDeviceProfileConfiguration( | |
283 | + new ProvisionDeviceProfileCredentials( | |
285 | 284 | requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceKey(), |
286 | 285 | requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret()))); |
287 | 286 | return Futures.transform(provisionResponseFuture, provisionResponse -> { | ... | ... |
... | ... | @@ -68,6 +68,7 @@ import org.thingsboard.server.common.data.User; |
68 | 68 | import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
69 | 69 | import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; |
70 | 70 | import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
71 | +import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials; | |
71 | 72 | import org.thingsboard.server.common.data.id.HasId; |
72 | 73 | import org.thingsboard.server.common.data.id.RuleChainId; |
73 | 74 | import org.thingsboard.server.common.data.id.TenantId; | ... | ... |
... | ... | @@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.DeviceProfileType; |
28 | 28 | import org.thingsboard.server.common.data.DeviceTransportType; |
29 | 29 | import org.thingsboard.server.common.data.Tenant; |
30 | 30 | import org.thingsboard.server.common.data.User; |
31 | +import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials; | |
31 | 32 | import org.thingsboard.server.common.data.page.PageData; |
32 | 33 | import org.thingsboard.server.common.data.page.PageLink; |
33 | 34 | import org.thingsboard.server.common.data.security.Authority; |
... | ... | @@ -153,6 +154,17 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController |
153 | 154 | .andExpect(statusReason(containsString("Device profile with such name already exists"))); |
154 | 155 | } |
155 | 156 | |
157 | + @Test | |
158 | + public void testSaveDeviceProfileWithSameProvisionDeviceKey() throws Exception { | |
159 | + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); | |
160 | + deviceProfile.setProvisionDeviceKey("testProvisionDeviceKey"); | |
161 | + doPost("/api/deviceProfile", deviceProfile).andExpect(status().isOk()); | |
162 | + DeviceProfile deviceProfile2 = this.createDeviceProfile("Device Profile 2"); | |
163 | + deviceProfile2.setProvisionDeviceKey("testProvisionDeviceKey"); | |
164 | + doPost("/api/deviceProfile", deviceProfile2).andExpect(status().isBadRequest()) | |
165 | + .andExpect(statusReason(containsString("Device profile with such provision device key already exists"))); | |
166 | + } | |
167 | + | |
156 | 168 | @Ignore |
157 | 169 | @Test |
158 | 170 | public void testChangeDeviceProfileTypeWithExistingDevices() throws Exception { | ... | ... |
... | ... | @@ -17,13 +17,12 @@ package org.thingsboard.server.dao.device.provision; |
17 | 17 | |
18 | 18 | import lombok.AllArgsConstructor; |
19 | 19 | import lombok.Data; |
20 | -import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; | |
20 | +import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials; | |
21 | 21 | |
22 | 22 | @Data |
23 | 23 | @AllArgsConstructor |
24 | 24 | public class ProvisionRequest { |
25 | 25 | private String deviceName; |
26 | - private String deviceType; | |
27 | 26 | private String x509CertPubKey; |
28 | - private ProvisionDeviceProfileConfiguration credentials; | |
27 | + private ProvisionDeviceProfileCredentials credentials; | |
29 | 28 | } | ... | ... |
... | ... | @@ -41,10 +41,12 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H |
41 | 41 | private boolean isDefault; |
42 | 42 | private DeviceProfileType type; |
43 | 43 | private DeviceTransportType transportType; |
44 | + private DeviceProfileProvisionType provisionType; | |
44 | 45 | private RuleChainId defaultRuleChainId; |
45 | 46 | private transient DeviceProfileData profileData; |
46 | 47 | @JsonIgnore |
47 | 48 | private byte[] profileDataBytes; |
49 | + private String provisionDeviceKey; | |
48 | 50 | |
49 | 51 | public DeviceProfile() { |
50 | 52 | super(); |
... | ... | @@ -62,6 +64,7 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H |
62 | 64 | this.isDefault = deviceProfile.isDefault(); |
63 | 65 | this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId(); |
64 | 66 | this.setProfileData(deviceProfile.getProfileData()); |
67 | + this.provisionDeviceKey = deviceProfile.getProvisionDeviceKey(); | |
65 | 68 | } |
66 | 69 | |
67 | 70 | @Override | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileProvisionType.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionRequestValidationStrategyType.java
... | ... | @@ -13,8 +13,10 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.server.common.data.device.profile; | |
16 | +package org.thingsboard.server.common.data; | |
17 | 17 | |
18 | -public enum ProvisionRequestValidationStrategyType { | |
19 | - CHECK_NEW_DEVICE, CHECK_PRE_PROVISIONED_DEVICE | |
18 | +public enum DeviceProfileProvisionType { | |
19 | + DISABLED, | |
20 | + ALLOW_CREATE_NEW_DEVICES, | |
21 | + CHECK_PRE_PROVISIONED_DEVICES | |
20 | 22 | } | ... | ... |
... | ... | @@ -27,8 +27,7 @@ import org.thingsboard.server.common.data.DeviceProfileType; |
27 | 27 | include = JsonTypeInfo.As.PROPERTY, |
28 | 28 | property = "type") |
29 | 29 | @JsonSubTypes({ |
30 | - @JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "DEFAULT"), | |
31 | - @JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "PROVISION")}) | |
30 | + @JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "DEFAULT")}) | |
32 | 31 | public interface DeviceConfiguration { |
33 | 32 | |
34 | 33 | @JsonIgnore | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.data.device.profile; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
20 | + | |
21 | +@Data | |
22 | +public class AllowCreateNewDevicesDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration { | |
23 | + | |
24 | + private final String provisionDeviceSecret; | |
25 | + | |
26 | + @Override | |
27 | + public DeviceProfileProvisionType getType() { | |
28 | + return DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES; | |
29 | + } | |
30 | + | |
31 | +} | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileConfiguration.java
... | ... | @@ -15,43 +15,17 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.data.device.profile; |
17 | 17 | |
18 | -import com.fasterxml.jackson.annotation.JsonCreator; | |
19 | -import com.fasterxml.jackson.annotation.JsonProperty; | |
20 | 18 | import lombok.Data; |
21 | -import org.thingsboard.server.common.data.DeviceProfileType; | |
22 | - | |
23 | -import java.util.Objects; | |
19 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
24 | 20 | |
25 | 21 | @Data |
26 | -public class ProvisionDeviceProfileConfiguration implements DeviceProfileConfiguration { | |
27 | - | |
28 | - private String provisionDeviceKey; | |
29 | - private String provisionDeviceSecret; | |
22 | +public class CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration { | |
30 | 23 | |
31 | - private ProvisionRequestValidationStrategyType strategy; | |
24 | + private final String provisionDeviceSecret; | |
32 | 25 | |
33 | 26 | @Override |
34 | - public DeviceProfileType getType() { | |
35 | - return DeviceProfileType.PROVISION; | |
27 | + public DeviceProfileProvisionType getType() { | |
28 | + return DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES; | |
36 | 29 | } |
37 | 30 | |
38 | - @JsonCreator | |
39 | - public ProvisionDeviceProfileConfiguration(@JsonProperty("provisionDeviceKey") String provisionProfileKey, @JsonProperty("provisionDeviceSecret") String provisionProfileSecret) { | |
40 | - this.provisionDeviceKey = provisionProfileKey; | |
41 | - this.provisionDeviceSecret = provisionProfileSecret; | |
42 | - } | |
43 | - | |
44 | - @Override | |
45 | - public boolean equals(Object o) { | |
46 | - if (this == o) return true; | |
47 | - if (o == null || getClass() != o.getClass()) return false; | |
48 | - ProvisionDeviceProfileConfiguration that = (ProvisionDeviceProfileConfiguration) o; | |
49 | - return provisionDeviceKey.equals(that.provisionDeviceKey) && | |
50 | - provisionDeviceSecret.equals(that.provisionDeviceSecret); | |
51 | - } | |
52 | - | |
53 | - @Override | |
54 | - public int hashCode() { | |
55 | - return Objects.hash(provisionDeviceKey, provisionDeviceSecret); | |
56 | - } | |
57 | 31 | } | ... | ... |
... | ... | @@ -27,8 +27,7 @@ import org.thingsboard.server.common.data.DeviceProfileType; |
27 | 27 | include = JsonTypeInfo.As.PROPERTY, |
28 | 28 | property = "type") |
29 | 29 | @JsonSubTypes({ |
30 | - @JsonSubTypes.Type(value = DefaultDeviceProfileConfiguration.class, name = "DEFAULT"), | |
31 | - @JsonSubTypes.Type(value = ProvisionDeviceProfileConfiguration.class, name = "PROVISION")}) | |
30 | + @JsonSubTypes.Type(value = DefaultDeviceProfileConfiguration.class, name = "DEFAULT")}) | |
32 | 31 | public interface DeviceProfileConfiguration { |
33 | 32 | |
34 | 33 | @JsonIgnore | ... | ... |
... | ... | @@ -24,6 +24,7 @@ public class DeviceProfileData { |
24 | 24 | |
25 | 25 | private DeviceProfileConfiguration configuration; |
26 | 26 | private DeviceProfileTransportConfiguration transportConfiguration; |
27 | + private DeviceProfileProvisionConfiguration provisionConfiguration; | |
27 | 28 | private List<DeviceProfileAlarm> alarms; |
28 | 29 | |
29 | 30 | } | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.data.device.profile; | |
17 | + | |
18 | +import com.fasterxml.jackson.annotation.JsonIgnore; | |
19 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
20 | +import com.fasterxml.jackson.annotation.JsonSubTypes; | |
21 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
22 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
23 | + | |
24 | + | |
25 | +@JsonIgnoreProperties(ignoreUnknown = true) | |
26 | +@JsonTypeInfo( | |
27 | + use = JsonTypeInfo.Id.NAME, | |
28 | + include = JsonTypeInfo.As.PROPERTY, | |
29 | + property = "type") | |
30 | +@JsonSubTypes({ | |
31 | + @JsonSubTypes.Type(value = DisabledDeviceProfileProvisionConfiguration.class, name = "DISABLED"), | |
32 | + @JsonSubTypes.Type(value = AllowCreateNewDevicesDeviceProfileProvisionConfiguration.class, name = "ALLOW_CREATE_NEW_DEVICES"), | |
33 | + @JsonSubTypes.Type(value = CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.class, name = "CHECK_PRE_PROVISIONED_DEVICES")}) | |
34 | +public interface DeviceProfileProvisionConfiguration { | |
35 | + | |
36 | + @JsonIgnore | |
37 | + DeviceProfileProvisionType getType(); | |
38 | + | |
39 | +} | |
\ No newline at end of file | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.data.device.profile; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
20 | + | |
21 | +@Data | |
22 | +public class DisabledDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration { | |
23 | + | |
24 | + private final String provisionDeviceSecret; | |
25 | + | |
26 | + @Override | |
27 | + public DeviceProfileProvisionType getType() { | |
28 | + return DeviceProfileProvisionType.DISABLED; | |
29 | + } | |
30 | + | |
31 | +} | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileCredentials.java
renamed from
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionRequestValidationStrategy.java
... | ... | @@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile; |
18 | 18 | import lombok.Data; |
19 | 19 | |
20 | 20 | @Data |
21 | -public class ProvisionRequestValidationStrategy { | |
22 | - private final ProvisionRequestValidationStrategyType validationStrategyType; | |
21 | +public class ProvisionDeviceProfileCredentials { | |
22 | + private final String provisionDeviceKey; | |
23 | + private final String provisionDeviceSecret; | |
23 | 24 | } | ... | ... |
... | ... | @@ -256,9 +256,8 @@ message DeviceCredentialsProto { |
256 | 256 | |
257 | 257 | message ProvisionDeviceRequestMsg { |
258 | 258 | string deviceName = 1; |
259 | - string deviceType = 2; | |
260 | - string x509CertPubKey = 3; | |
261 | - ProvisionDeviceCredentialsMsg provisionDeviceCredentialsMsg = 4; | |
259 | + string x509CertPubKey = 2; | |
260 | + ProvisionDeviceCredentialsMsg provisionDeviceCredentialsMsg = 3; | |
262 | 261 | } |
263 | 262 | |
264 | 263 | message ProvisionDeviceCredentialsMsg { | ... | ... |
... | ... | @@ -543,7 +543,6 @@ public class JsonConverter { |
543 | 543 | private static TransportProtos.ProvisionDeviceRequestMsg buildProvisionRequestMsg(JsonObject jo) { |
544 | 544 | return TransportProtos.ProvisionDeviceRequestMsg.newBuilder() |
545 | 545 | .setDeviceName(getStrValue(jo, DataConstants.DEVICE_NAME, true)) |
546 | - .setDeviceType(getStrValue(jo, DataConstants.DEVICE_TYPE, true)) | |
547 | 546 | .setX509CertPubKey(getStrValue(jo, DataConstants.CERT_PUB_KEY, false)) |
548 | 547 | .setProvisionDeviceCredentialsMsg(buildProvisionDeviceCredentialsMsg( |
549 | 548 | getStrValue(jo, DataConstants.PROVISION_KEY, true), | ... | ... |
... | ... | @@ -38,7 +38,7 @@ public interface DeviceProfileDao extends Dao<DeviceProfile> { |
38 | 38 | |
39 | 39 | DeviceProfileInfo findDefaultDeviceProfileInfo(TenantId tenantId); |
40 | 40 | |
41 | - DeviceProfile findProfileByProfileNameAndProfileDataProvisionConfigurationPair(String profileName, String provisionDeviceKey, String provisionDeviceSecret); | |
41 | + DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret); | |
42 | 42 | |
43 | 43 | DeviceProfile findByName(TenantId tenantId, String profileName); |
44 | 44 | } | ... | ... |
... | ... | @@ -26,6 +26,7 @@ import org.springframework.stereotype.Service; |
26 | 26 | import org.thingsboard.server.common.data.Device; |
27 | 27 | import org.thingsboard.server.common.data.DeviceProfile; |
28 | 28 | import org.thingsboard.server.common.data.DeviceProfileInfo; |
29 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
29 | 30 | import org.thingsboard.server.common.data.DeviceProfileType; |
30 | 31 | import org.thingsboard.server.common.data.DeviceTransportType; |
31 | 32 | import org.thingsboard.server.common.data.EntitySubtype; |
... | ... | @@ -33,6 +34,7 @@ import org.thingsboard.server.common.data.Tenant; |
33 | 34 | import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
34 | 35 | import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; |
35 | 36 | import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
37 | +import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration; | |
36 | 38 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
37 | 39 | import org.thingsboard.server.common.data.id.TenantId; |
38 | 40 | import org.thingsboard.server.common.data.page.PageData; |
... | ... | @@ -112,6 +114,8 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
112 | 114 | ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
113 | 115 | if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) { |
114 | 116 | throw new DataValidationException("Device profile with such name already exists!"); |
117 | + } else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_provision_key_unq_key")) { | |
118 | + throw new DataValidationException("Device profile with such provision device key already exists!"); | |
115 | 119 | } else { |
116 | 120 | throw t; |
117 | 121 | } |
... | ... | @@ -210,12 +214,15 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D |
210 | 214 | deviceProfile.setName(profileName); |
211 | 215 | deviceProfile.setType(DeviceProfileType.DEFAULT); |
212 | 216 | deviceProfile.setTransportType(DeviceTransportType.DEFAULT); |
217 | + deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED); | |
213 | 218 | deviceProfile.setDescription("Default device profile"); |
214 | 219 | DeviceProfileData deviceProfileData = new DeviceProfileData(); |
215 | 220 | DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration(); |
216 | 221 | DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration(); |
222 | + DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null); | |
217 | 223 | deviceProfileData.setConfiguration(configuration); |
218 | 224 | deviceProfileData.setTransportConfiguration(transportConfiguration); |
225 | + deviceProfileData.setProvisionConfiguration(provisionConfiguration); | |
219 | 226 | deviceProfile.setProfileData(deviceProfileData); |
220 | 227 | return saveDeviceProfile(deviceProfile); |
221 | 228 | } | ... | ... |
... | ... | @@ -169,10 +169,12 @@ public class ModelConstants { |
169 | 169 | public static final String DEVICE_PROFILE_NAME_PROPERTY = "name"; |
170 | 170 | public static final String DEVICE_PROFILE_TYPE_PROPERTY = "type"; |
171 | 171 | public static final String DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY = "transport_type"; |
172 | + public static final String DEVICE_PROFILE_PROVISION_TYPE_PROPERTY = "provision_type"; | |
172 | 173 | public static final String DEVICE_PROFILE_PROFILE_DATA_PROPERTY = "profile_data"; |
173 | 174 | public static final String DEVICE_PROFILE_DESCRIPTION_PROPERTY = "description"; |
174 | 175 | public static final String DEVICE_PROFILE_IS_DEFAULT_PROPERTY = "is_default"; |
175 | 176 | public static final String DEVICE_PROFILE_DEFAULT_RULE_CHAIN_ID_PROPERTY = "default_rule_chain_id"; |
177 | + public static final String DEVICE_PROFILE_PROVISION_DEVICE_KEY = "provision_device_key"; | |
176 | 178 | |
177 | 179 | /** |
178 | 180 | * Cassandra entityView constants. | ... | ... |
... | ... | @@ -23,6 +23,7 @@ import org.hibernate.annotations.Type; |
23 | 23 | import org.hibernate.annotations.TypeDef; |
24 | 24 | import org.thingsboard.server.common.data.DeviceProfile; |
25 | 25 | import org.thingsboard.server.common.data.DeviceProfileType; |
26 | +import org.thingsboard.server.common.data.DeviceProfileProvisionType; | |
26 | 27 | import org.thingsboard.server.common.data.DeviceTransportType; |
27 | 28 | import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
28 | 29 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
... | ... | @@ -62,6 +63,10 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl |
62 | 63 | @Column(name = ModelConstants.DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY) |
63 | 64 | private DeviceTransportType transportType; |
64 | 65 | |
66 | + @Enumerated(EnumType.STRING) | |
67 | + @Column(name = ModelConstants.DEVICE_PROFILE_PROVISION_TYPE_PROPERTY) | |
68 | + private DeviceProfileProvisionType provisionType; | |
69 | + | |
65 | 70 | @Column(name = ModelConstants.DEVICE_PROFILE_DESCRIPTION_PROPERTY) |
66 | 71 | private String description; |
67 | 72 | |
... | ... | @@ -78,6 +83,9 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl |
78 | 83 | @Column(name = ModelConstants.DEVICE_PROFILE_PROFILE_DATA_PROPERTY, columnDefinition = "jsonb") |
79 | 84 | private JsonNode profileData; |
80 | 85 | |
86 | + @Column(name=ModelConstants.DEVICE_PROFILE_PROVISION_DEVICE_KEY) | |
87 | + private String provisionDeviceKey; | |
88 | + | |
81 | 89 | public DeviceProfileEntity() { |
82 | 90 | super(); |
83 | 91 | } |
... | ... | @@ -93,12 +101,14 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl |
93 | 101 | this.name = deviceProfile.getName(); |
94 | 102 | this.type = deviceProfile.getType(); |
95 | 103 | this.transportType = deviceProfile.getTransportType(); |
104 | + this.provisionType = deviceProfile.getProvisionType(); | |
96 | 105 | this.description = deviceProfile.getDescription(); |
97 | 106 | this.isDefault = deviceProfile.isDefault(); |
98 | 107 | this.profileData = JacksonUtil.convertValue(deviceProfile.getProfileData(), ObjectNode.class); |
99 | 108 | if (deviceProfile.getDefaultRuleChainId() != null) { |
100 | 109 | this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId().getId(); |
101 | 110 | } |
111 | + this.provisionDeviceKey = deviceProfile.getProvisionDeviceKey(); | |
102 | 112 | } |
103 | 113 | |
104 | 114 | @Override |
... | ... | @@ -125,12 +135,14 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl |
125 | 135 | deviceProfile.setName(name); |
126 | 136 | deviceProfile.setType(type); |
127 | 137 | deviceProfile.setTransportType(transportType); |
138 | + deviceProfile.setProvisionType(provisionType); | |
128 | 139 | deviceProfile.setDescription(description); |
129 | 140 | deviceProfile.setDefault(isDefault); |
130 | 141 | deviceProfile.setProfileData(JacksonUtil.convertValue(profileData, DeviceProfileData.class)); |
131 | 142 | if (defaultRuleChainId != null) { |
132 | 143 | deviceProfile.setDefaultRuleChainId(new RuleChainId(defaultRuleChainId)); |
133 | 144 | } |
145 | + deviceProfile.setProvisionDeviceKey(provisionDeviceKey); | |
134 | 146 | return deviceProfile; |
135 | 147 | } |
136 | 148 | } | ... | ... |
... | ... | @@ -56,14 +56,10 @@ public interface DeviceProfileRepository extends PagingAndSortingRepository<Devi |
56 | 56 | |
57 | 57 | DeviceProfileEntity findByTenantIdAndName(UUID id, String profileName); |
58 | 58 | |
59 | - @Query(value = "SELECT d.* FROM device_profile as d " + | |
60 | - "WHERE d.name = :profileName " + | |
61 | - "AND d.profile_data->'configuration'->>'provisionDeviceKey' IS NOT NULL " + | |
62 | - "AND d.profile_data->'configuration'->>'provisionDeviceSecret' IS NOT NULL " + | |
63 | - "AND d.profile_data->'configuration'->>'provisionDeviceKey' = :provisionDeviceKey " + | |
64 | - "AND d.profile_data->'configuration'->>'provisionDeviceSecret' = :provisionDeviceSecret", | |
59 | + @Query(value = "SELECT d.* from device_profile d " + | |
60 | + "WHERE d.provision_device_key = :provisionDeviceKey " + | |
61 | + "AND d.profile_data->'provisionConfiguration'->>'provisionDeviceSecret' = :provisionDeviceSecret", | |
65 | 62 | nativeQuery = true) |
66 | - DeviceProfileEntity findProfileByProfileNameAndProfileDataProvisionConfigurationPair(@Param("profileName") String profileName, | |
67 | - @Param("provisionDeviceKey") String provisionDeviceKey, | |
68 | - @Param("provisionDeviceSecret") String provisionDeviceSecret); | |
63 | + DeviceProfileEntity findByProvisionDeviceKeyAndProvisionDeviceSecret(@Param("provisionDeviceKey") String provisionDeviceKey, | |
64 | + @Param("provisionDeviceSecret") String provisionDeviceSecret); | |
69 | 65 | } | ... | ... |
... | ... | @@ -81,8 +81,8 @@ public class JpaDeviceProfileDao extends JpaAbstractSearchTextDao<DeviceProfileE |
81 | 81 | } |
82 | 82 | |
83 | 83 | @Override |
84 | - public DeviceProfile findProfileByProfileNameAndProfileDataProvisionConfigurationPair(String profileName, String provisionDeviceKey, String provisionDeviceSecret) { | |
85 | - return DaoUtil.getData(deviceProfileRepository.findProfileByProfileNameAndProfileDataProvisionConfigurationPair(profileName, provisionDeviceKey, provisionDeviceSecret)); | |
84 | + public DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret) { | |
85 | + return DaoUtil.getData(deviceProfileRepository.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionDeviceKey, provisionDeviceSecret)); | |
86 | 86 | } |
87 | 87 | |
88 | 88 | @Override | ... | ... |
... | ... | @@ -152,13 +152,16 @@ CREATE TABLE IF NOT EXISTS device_profile ( |
152 | 152 | name varchar(255), |
153 | 153 | type varchar(255), |
154 | 154 | transport_type varchar(255), |
155 | + provision_type varchar(255), | |
155 | 156 | profile_data jsonb, |
156 | 157 | description varchar, |
157 | 158 | search_text varchar(255), |
158 | 159 | is_default boolean, |
159 | 160 | tenant_id uuid, |
160 | 161 | default_rule_chain_id uuid, |
162 | + provision_device_key varchar, | |
161 | 163 | CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), |
164 | + CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key), | |
162 | 165 | CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) |
163 | 166 | ); |
164 | 167 | ... | ... |
... | ... | @@ -170,13 +170,16 @@ CREATE TABLE IF NOT EXISTS device_profile ( |
170 | 170 | name varchar(255), |
171 | 171 | type varchar(255), |
172 | 172 | transport_type varchar(255), |
173 | + provision_type varchar(255), | |
173 | 174 | profile_data jsonb, |
174 | 175 | description varchar, |
175 | 176 | search_text varchar(255), |
176 | 177 | is_default boolean, |
177 | 178 | tenant_id uuid, |
178 | 179 | default_rule_chain_id uuid, |
180 | + provision_device_key varchar, | |
179 | 181 | CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), |
182 | + CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key), | |
180 | 183 | CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) |
181 | 184 | ); |
182 | 185 | ... | ... |