Browse Source

Moved provisionDeviceKey as regular column

pull/3518/head
zbeacon 6 years ago
parent
commit
2704696f2b
  1. 3
      application/src/main/data/upgrade/3.1.2/schema_update_before.sql
  2. 32
      application/src/main/java/org/thingsboard/server/service/device/DeviceProvisionServiceImpl.java
  3. 5
      application/src/main/java/org/thingsboard/server/service/transport/DefaultTransportApiService.java
  4. 1
      application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java
  5. 12
      application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java
  6. 5
      common/dao-api/src/main/java/org/thingsboard/server/dao/device/provision/ProvisionRequest.java
  7. 3
      common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java
  8. 8
      common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileProvisionType.java
  9. 3
      common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileType.java
  10. 3
      common/data/src/main/java/org/thingsboard/server/common/data/device/data/DeviceConfiguration.java
  11. 31
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AllowCreateNewDevicesDeviceProfileProvisionConfiguration.java
  12. 31
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.java
  13. 3
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileConfiguration.java
  14. 1
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileData.java
  15. 39
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileProvisionConfiguration.java
  16. 31
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DisabledDeviceProfileProvisionConfiguration.java
  17. 57
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileConfiguration.java
  18. 5
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileCredentials.java
  19. 5
      common/queue/src/main/proto/queue.proto
  20. 1
      common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java
  21. 2
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileDao.java
  22. 7
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java
  23. 2
      dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java
  24. 12
      dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceProfileEntity.java
  25. 14
      dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceProfileRepository.java
  26. 4
      dao/src/main/java/org/thingsboard/server/dao/sql/device/JpaDeviceProfileDao.java
  27. 3
      dao/src/main/resources/sql/schema-entities-hsql.sql
  28. 3
      dao/src/main/resources/sql/schema-entities.sql

3
application/src/main/data/upgrade/3.1.2/schema_update_before.sql

@ -20,13 +20,16 @@ CREATE TABLE IF NOT EXISTS device_profile (
name varchar(255), name varchar(255),
type varchar(255), type varchar(255),
transport_type varchar(255), transport_type varchar(255),
provision_type varchar(255),
profile_data jsonb, profile_data jsonb,
description varchar, description varchar,
search_text varchar(255), search_text varchar(255),
is_default boolean, is_default boolean,
tenant_id uuid, tenant_id uuid,
default_rule_chain_id uuid, default_rule_chain_id uuid,
provision_device_key varchar,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
); );

32
application/src/main/java/org/thingsboard/server/service/device/DeviceProvisionServiceImpl.java

@ -27,10 +27,8 @@ import org.springframework.util.StringUtils;
import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.DeviceProfileType; import org.thingsboard.server.common.data.DeviceProfileProvisionType;
import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration;
import org.thingsboard.server.common.data.device.profile.ProvisionRequestValidationStrategyType;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
@ -117,21 +115,18 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
} }
DeviceProfile targetProfile = deviceProfileDao.findProfileByProfileNameAndProfileDataProvisionConfigurationPair( DeviceProfile targetProfile = deviceProfileDao.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionRequestKey, provisionRequestSecret);
provisionRequest.getDeviceType(),
provisionRequestKey,
provisionRequestSecret);
if (targetProfile == null || targetProfile.getProfileData().getConfiguration().getType() != DeviceProfileType.PROVISION) { if (targetProfile == null ||
!(targetProfile.getProvisionType() != DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES ||
targetProfile.getProvisionType() != DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES)) {
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
} }
ProvisionRequestValidationStrategyType validationStrategy = getStrategy(targetProfile);
Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null); Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null);
switch(validationStrategy) { switch(targetProfile.getProvisionType()) {
case CHECK_NEW_DEVICE: case ALLOW_CREATE_NEW_DEVICES:
if (targetDevice != null) { if (targetDevice != null) {
log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName()); log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false); notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
@ -139,15 +134,15 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
} else { } else {
return createDevice(provisionRequest, targetProfile); return createDevice(provisionRequest, targetProfile);
} }
case CHECK_PRE_PROVISIONED_DEVICE: case CHECK_PRE_PROVISIONED_DEVICES:
if (targetDevice != null){ if (targetDevice != null && targetDevice.getDeviceProfileId().equals(targetProfile.getId())){
return processProvision(targetDevice, provisionRequest); return processProvision(targetDevice, provisionRequest);
} else { } else {
log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName()); log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE)); return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
} }
default: default:
throw new RuntimeException("Strategy is not supported - " + validationStrategy.name()); throw new RuntimeException("Strategy is not supported - " + targetProfile.getProvisionType().name());
} }
} }
@ -193,11 +188,6 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
logAction(device.getTenantId(), device.getCustomerId(), device, success, provisionRequest); logAction(device.getTenantId(), device.getCustomerId(), device, success, provisionRequest);
} }
private ProvisionRequestValidationStrategyType getStrategy(DeviceProfile profile) {
return ((ProvisionDeviceProfileConfiguration) profile.getProfileData().getConfiguration()).getStrategy();
}
private ListenableFuture<ProvisionResponse> processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) { private ListenableFuture<ProvisionResponse> processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName()); Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName());
if (device == null) { if (device == null) {
@ -226,7 +216,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
private Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) { private Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
Device device = new Device(); Device device = new Device();
device.setName(provisionRequest.getDeviceName()); device.setName(provisionRequest.getDeviceName());
device.setType(provisionRequest.getDeviceType()); device.setType(profile.getName());
device.setTenantId(profile.getTenantId()); device.setTenantId(profile.getTenantId());
return deviceService.saveDevice(device); return deviceService.saveDevice(device);
} }

5
application/src/main/java/org/thingsboard/server/service/transport/DefaultTransportApiService.java

@ -30,7 +30,7 @@ import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.TenantProfile;
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId;
@ -279,9 +279,8 @@ public class DefaultTransportApiService implements TransportApiService {
provisionResponseFuture = deviceProvisionService.provisionDevice( provisionResponseFuture = deviceProvisionService.provisionDevice(
new ProvisionRequest( new ProvisionRequest(
requestMsg.getDeviceName(), requestMsg.getDeviceName(),
requestMsg.getDeviceType(),
requestMsg.getX509CertPubKey(), requestMsg.getX509CertPubKey(),
new ProvisionDeviceProfileConfiguration( new ProvisionDeviceProfileCredentials(
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceKey(), requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceKey(),
requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret()))); requestMsg.getProvisionDeviceCredentialsMsg().getProvisionDeviceSecret())));
return Futures.transform(provisionResponseFuture, provisionResponse -> { return Futures.transform(provisionResponseFuture, provisionResponse -> {

1
application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java

@ -68,6 +68,7 @@ import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
import org.thingsboard.server.common.data.id.HasId; import org.thingsboard.server.common.data.id.HasId;
import org.thingsboard.server.common.data.id.RuleChainId; import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;

12
application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java

@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.DeviceProfileType;
import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.Authority;
@ -153,6 +154,17 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController
.andExpect(statusReason(containsString("Device profile with such name already exists"))); .andExpect(statusReason(containsString("Device profile with such name already exists")));
} }
@Test
public void testSaveDeviceProfileWithSameProvisionDeviceKey() throws Exception {
DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile");
deviceProfile.setProvisionDeviceKey("testProvisionDeviceKey");
doPost("/api/deviceProfile", deviceProfile).andExpect(status().isOk());
DeviceProfile deviceProfile2 = this.createDeviceProfile("Device Profile 2");
deviceProfile2.setProvisionDeviceKey("testProvisionDeviceKey");
doPost("/api/deviceProfile", deviceProfile2).andExpect(status().isBadRequest())
.andExpect(statusReason(containsString("Device profile with such provision device key already exists")));
}
@Ignore @Ignore
@Test @Test
public void testChangeDeviceProfileTypeWithExistingDevices() throws Exception { public void testChangeDeviceProfileTypeWithExistingDevices() throws Exception {

5
common/dao-api/src/main/java/org/thingsboard/server/dao/device/provision/ProvisionRequest.java

@ -17,13 +17,12 @@ package org.thingsboard.server.dao.device.provision;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public class ProvisionRequest { public class ProvisionRequest {
private String deviceName; private String deviceName;
private String deviceType;
private String x509CertPubKey; private String x509CertPubKey;
private ProvisionDeviceProfileConfiguration credentials; private ProvisionDeviceProfileCredentials credentials;
} }

3
common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfile.java

@ -41,10 +41,12 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H
private boolean isDefault; private boolean isDefault;
private DeviceProfileType type; private DeviceProfileType type;
private DeviceTransportType transportType; private DeviceTransportType transportType;
private DeviceProfileProvisionType provisionType;
private RuleChainId defaultRuleChainId; private RuleChainId defaultRuleChainId;
private transient DeviceProfileData profileData; private transient DeviceProfileData profileData;
@JsonIgnore @JsonIgnore
private byte[] profileDataBytes; private byte[] profileDataBytes;
private String provisionDeviceKey;
public DeviceProfile() { public DeviceProfile() {
super(); super();
@ -62,6 +64,7 @@ public class DeviceProfile extends SearchTextBased<DeviceProfileId> implements H
this.isDefault = deviceProfile.isDefault(); this.isDefault = deviceProfile.isDefault();
this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId(); this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId();
this.setProfileData(deviceProfile.getProfileData()); this.setProfileData(deviceProfile.getProfileData());
this.provisionDeviceKey = deviceProfile.getProvisionDeviceKey();
} }
@Override @Override

8
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionRequestValidationStrategyType.java → common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileProvisionType.java

@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.common.data.device.profile; package org.thingsboard.server.common.data;
public enum ProvisionRequestValidationStrategyType { public enum DeviceProfileProvisionType {
CHECK_NEW_DEVICE, CHECK_PRE_PROVISIONED_DEVICE DISABLED,
ALLOW_CREATE_NEW_DEVICES,
CHECK_PRE_PROVISIONED_DEVICES
} }

3
common/data/src/main/java/org/thingsboard/server/common/data/DeviceProfileType.java

@ -16,6 +16,5 @@
package org.thingsboard.server.common.data; package org.thingsboard.server.common.data;
public enum DeviceProfileType { public enum DeviceProfileType {
DEFAULT, DEFAULT
PROVISION
} }

3
common/data/src/main/java/org/thingsboard/server/common/data/device/data/DeviceConfiguration.java

@ -27,8 +27,7 @@ import org.thingsboard.server.common.data.DeviceProfileType;
include = JsonTypeInfo.As.PROPERTY, include = JsonTypeInfo.As.PROPERTY,
property = "type") property = "type")
@JsonSubTypes({ @JsonSubTypes({
@JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "DEFAULT"), @JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "DEFAULT")})
@JsonSubTypes.Type(value = DefaultDeviceConfiguration.class, name = "PROVISION")})
public interface DeviceConfiguration { public interface DeviceConfiguration {
@JsonIgnore @JsonIgnore

31
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AllowCreateNewDevicesDeviceProfileProvisionConfiguration.java

@ -0,0 +1,31 @@
/**
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.device.profile;
import lombok.Data;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
@Data
public class AllowCreateNewDevicesDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration {
private final String provisionDeviceSecret;
@Override
public DeviceProfileProvisionType getType() {
return DeviceProfileProvisionType.ALLOW_CREATE_NEW_DEVICES;
}
}

31
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.java

@ -0,0 +1,31 @@
/**
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.device.profile;
import lombok.Data;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
@Data
public class CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration {
private final String provisionDeviceSecret;
@Override
public DeviceProfileProvisionType getType() {
return DeviceProfileProvisionType.CHECK_PRE_PROVISIONED_DEVICES;
}
}

3
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileConfiguration.java

@ -27,8 +27,7 @@ import org.thingsboard.server.common.data.DeviceProfileType;
include = JsonTypeInfo.As.PROPERTY, include = JsonTypeInfo.As.PROPERTY,
property = "type") property = "type")
@JsonSubTypes({ @JsonSubTypes({
@JsonSubTypes.Type(value = DefaultDeviceProfileConfiguration.class, name = "DEFAULT"), @JsonSubTypes.Type(value = DefaultDeviceProfileConfiguration.class, name = "DEFAULT")})
@JsonSubTypes.Type(value = ProvisionDeviceProfileConfiguration.class, name = "PROVISION")})
public interface DeviceProfileConfiguration { public interface DeviceProfileConfiguration {
@JsonIgnore @JsonIgnore

1
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileData.java

@ -24,6 +24,7 @@ public class DeviceProfileData {
private DeviceProfileConfiguration configuration; private DeviceProfileConfiguration configuration;
private DeviceProfileTransportConfiguration transportConfiguration; private DeviceProfileTransportConfiguration transportConfiguration;
private DeviceProfileProvisionConfiguration provisionConfiguration;
private List<DeviceProfileAlarm> alarms; private List<DeviceProfileAlarm> alarms;
} }

39
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DeviceProfileProvisionConfiguration.java

@ -0,0 +1,39 @@
/**
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.device.profile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = DisabledDeviceProfileProvisionConfiguration.class, name = "DISABLED"),
@JsonSubTypes.Type(value = AllowCreateNewDevicesDeviceProfileProvisionConfiguration.class, name = "ALLOW_CREATE_NEW_DEVICES"),
@JsonSubTypes.Type(value = CheckPreProvisionedDevicesDeviceProfileProvisionConfiguration.class, name = "CHECK_PRE_PROVISIONED_DEVICES")})
public interface DeviceProfileProvisionConfiguration {
@JsonIgnore
DeviceProfileProvisionType getType();
}

31
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/DisabledDeviceProfileProvisionConfiguration.java

@ -0,0 +1,31 @@
/**
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.device.profile;
import lombok.Data;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
@Data
public class DisabledDeviceProfileProvisionConfiguration implements DeviceProfileProvisionConfiguration {
private final String provisionDeviceSecret;
@Override
public DeviceProfileProvisionType getType() {
return DeviceProfileProvisionType.DISABLED;
}
}

57
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileConfiguration.java

@ -1,57 +0,0 @@
/**
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.common.data.device.profile;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.thingsboard.server.common.data.DeviceProfileType;
import java.util.Objects;
@Data
public class ProvisionDeviceProfileConfiguration implements DeviceProfileConfiguration {
private String provisionDeviceKey;
private String provisionDeviceSecret;
private ProvisionRequestValidationStrategyType strategy;
@Override
public DeviceProfileType getType() {
return DeviceProfileType.PROVISION;
}
@JsonCreator
public ProvisionDeviceProfileConfiguration(@JsonProperty("provisionDeviceKey") String provisionProfileKey, @JsonProperty("provisionDeviceSecret") String provisionProfileSecret) {
this.provisionDeviceKey = provisionProfileKey;
this.provisionDeviceSecret = provisionProfileSecret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProvisionDeviceProfileConfiguration that = (ProvisionDeviceProfileConfiguration) o;
return provisionDeviceKey.equals(that.provisionDeviceKey) &&
provisionDeviceSecret.equals(that.provisionDeviceSecret);
}
@Override
public int hashCode() {
return Objects.hash(provisionDeviceKey, provisionDeviceSecret);
}
}

5
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionRequestValidationStrategy.java → common/data/src/main/java/org/thingsboard/server/common/data/device/profile/ProvisionDeviceProfileCredentials.java

@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile;
import lombok.Data; import lombok.Data;
@Data @Data
public class ProvisionRequestValidationStrategy { public class ProvisionDeviceProfileCredentials {
private final ProvisionRequestValidationStrategyType validationStrategyType; private final String provisionDeviceKey;
private final String provisionDeviceSecret;
} }

5
common/queue/src/main/proto/queue.proto

@ -256,9 +256,8 @@ message DeviceCredentialsProto {
message ProvisionDeviceRequestMsg { message ProvisionDeviceRequestMsg {
string deviceName = 1; string deviceName = 1;
string deviceType = 2; string x509CertPubKey = 2;
string x509CertPubKey = 3; ProvisionDeviceCredentialsMsg provisionDeviceCredentialsMsg = 3;
ProvisionDeviceCredentialsMsg provisionDeviceCredentialsMsg = 4;
} }
message ProvisionDeviceCredentialsMsg { message ProvisionDeviceCredentialsMsg {

1
common/transport/transport-api/src/main/java/org/thingsboard/server/common/transport/adaptor/JsonConverter.java

@ -543,7 +543,6 @@ public class JsonConverter {
private static TransportProtos.ProvisionDeviceRequestMsg buildProvisionRequestMsg(JsonObject jo) { private static TransportProtos.ProvisionDeviceRequestMsg buildProvisionRequestMsg(JsonObject jo) {
return TransportProtos.ProvisionDeviceRequestMsg.newBuilder() return TransportProtos.ProvisionDeviceRequestMsg.newBuilder()
.setDeviceName(getStrValue(jo, DataConstants.DEVICE_NAME, true)) .setDeviceName(getStrValue(jo, DataConstants.DEVICE_NAME, true))
.setDeviceType(getStrValue(jo, DataConstants.DEVICE_TYPE, true))
.setX509CertPubKey(getStrValue(jo, DataConstants.CERT_PUB_KEY, false)) .setX509CertPubKey(getStrValue(jo, DataConstants.CERT_PUB_KEY, false))
.setProvisionDeviceCredentialsMsg(buildProvisionDeviceCredentialsMsg( .setProvisionDeviceCredentialsMsg(buildProvisionDeviceCredentialsMsg(
getStrValue(jo, DataConstants.PROVISION_KEY, true), getStrValue(jo, DataConstants.PROVISION_KEY, true),

2
dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileDao.java

@ -38,7 +38,7 @@ public interface DeviceProfileDao extends Dao<DeviceProfile> {
DeviceProfileInfo findDefaultDeviceProfileInfo(TenantId tenantId); DeviceProfileInfo findDefaultDeviceProfileInfo(TenantId tenantId);
DeviceProfile findProfileByProfileNameAndProfileDataProvisionConfigurationPair(String profileName, String provisionDeviceKey, String provisionDeviceSecret); DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret);
DeviceProfile findByName(TenantId tenantId, String profileName); DeviceProfile findByName(TenantId tenantId, String profileName);
} }

7
dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java

@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.DeviceProfileInfo; import org.thingsboard.server.common.data.DeviceProfileInfo;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
import org.thingsboard.server.common.data.DeviceProfileType; import org.thingsboard.server.common.data.DeviceProfileType;
import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntitySubtype;
@ -33,6 +34,7 @@ import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration;
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration;
import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
@ -112,6 +114,8 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) { if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_profile_name_unq_key")) {
throw new DataValidationException("Device profile with such name already exists!"); throw new DataValidationException("Device profile with such name already exists!");
} else if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_provision_key_unq_key")) {
throw new DataValidationException("Device profile with such provision device key already exists!");
} else { } else {
throw t; throw t;
} }
@ -210,12 +214,15 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
deviceProfile.setName(profileName); deviceProfile.setName(profileName);
deviceProfile.setType(DeviceProfileType.DEFAULT); deviceProfile.setType(DeviceProfileType.DEFAULT);
deviceProfile.setTransportType(DeviceTransportType.DEFAULT); deviceProfile.setTransportType(DeviceTransportType.DEFAULT);
deviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
deviceProfile.setDescription("Default device profile"); deviceProfile.setDescription("Default device profile");
DeviceProfileData deviceProfileData = new DeviceProfileData(); DeviceProfileData deviceProfileData = new DeviceProfileData();
DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration(); DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration(); DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration();
DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null);
deviceProfileData.setConfiguration(configuration); deviceProfileData.setConfiguration(configuration);
deviceProfileData.setTransportConfiguration(transportConfiguration); deviceProfileData.setTransportConfiguration(transportConfiguration);
deviceProfileData.setProvisionConfiguration(provisionConfiguration);
deviceProfile.setProfileData(deviceProfileData); deviceProfile.setProfileData(deviceProfileData);
return saveDeviceProfile(deviceProfile); return saveDeviceProfile(deviceProfile);
} }

2
dao/src/main/java/org/thingsboard/server/dao/model/ModelConstants.java

@ -169,10 +169,12 @@ public class ModelConstants {
public static final String DEVICE_PROFILE_NAME_PROPERTY = "name"; public static final String DEVICE_PROFILE_NAME_PROPERTY = "name";
public static final String DEVICE_PROFILE_TYPE_PROPERTY = "type"; public static final String DEVICE_PROFILE_TYPE_PROPERTY = "type";
public static final String DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY = "transport_type"; public static final String DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY = "transport_type";
public static final String DEVICE_PROFILE_PROVISION_TYPE_PROPERTY = "provision_type";
public static final String DEVICE_PROFILE_PROFILE_DATA_PROPERTY = "profile_data"; public static final String DEVICE_PROFILE_PROFILE_DATA_PROPERTY = "profile_data";
public static final String DEVICE_PROFILE_DESCRIPTION_PROPERTY = "description"; public static final String DEVICE_PROFILE_DESCRIPTION_PROPERTY = "description";
public static final String DEVICE_PROFILE_IS_DEFAULT_PROPERTY = "is_default"; public static final String DEVICE_PROFILE_IS_DEFAULT_PROPERTY = "is_default";
public static final String DEVICE_PROFILE_DEFAULT_RULE_CHAIN_ID_PROPERTY = "default_rule_chain_id"; public static final String DEVICE_PROFILE_DEFAULT_RULE_CHAIN_ID_PROPERTY = "default_rule_chain_id";
public static final String DEVICE_PROFILE_PROVISION_DEVICE_KEY = "provision_device_key";
/** /**
* Cassandra entityView constants. * Cassandra entityView constants.

12
dao/src/main/java/org/thingsboard/server/dao/model/sql/DeviceProfileEntity.java

@ -23,6 +23,7 @@ import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDef;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.DeviceProfileType; import org.thingsboard.server.common.data.DeviceProfileType;
import org.thingsboard.server.common.data.DeviceProfileProvisionType;
import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.DeviceTransportType;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId;
@ -62,6 +63,10 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
@Column(name = ModelConstants.DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY) @Column(name = ModelConstants.DEVICE_PROFILE_TRANSPORT_TYPE_PROPERTY)
private DeviceTransportType transportType; private DeviceTransportType transportType;
@Enumerated(EnumType.STRING)
@Column(name = ModelConstants.DEVICE_PROFILE_PROVISION_TYPE_PROPERTY)
private DeviceProfileProvisionType provisionType;
@Column(name = ModelConstants.DEVICE_PROFILE_DESCRIPTION_PROPERTY) @Column(name = ModelConstants.DEVICE_PROFILE_DESCRIPTION_PROPERTY)
private String description; private String description;
@ -78,6 +83,9 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
@Column(name = ModelConstants.DEVICE_PROFILE_PROFILE_DATA_PROPERTY, columnDefinition = "jsonb") @Column(name = ModelConstants.DEVICE_PROFILE_PROFILE_DATA_PROPERTY, columnDefinition = "jsonb")
private JsonNode profileData; private JsonNode profileData;
@Column(name=ModelConstants.DEVICE_PROFILE_PROVISION_DEVICE_KEY)
private String provisionDeviceKey;
public DeviceProfileEntity() { public DeviceProfileEntity() {
super(); super();
} }
@ -93,12 +101,14 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
this.name = deviceProfile.getName(); this.name = deviceProfile.getName();
this.type = deviceProfile.getType(); this.type = deviceProfile.getType();
this.transportType = deviceProfile.getTransportType(); this.transportType = deviceProfile.getTransportType();
this.provisionType = deviceProfile.getProvisionType();
this.description = deviceProfile.getDescription(); this.description = deviceProfile.getDescription();
this.isDefault = deviceProfile.isDefault(); this.isDefault = deviceProfile.isDefault();
this.profileData = JacksonUtil.convertValue(deviceProfile.getProfileData(), ObjectNode.class); this.profileData = JacksonUtil.convertValue(deviceProfile.getProfileData(), ObjectNode.class);
if (deviceProfile.getDefaultRuleChainId() != null) { if (deviceProfile.getDefaultRuleChainId() != null) {
this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId().getId(); this.defaultRuleChainId = deviceProfile.getDefaultRuleChainId().getId();
} }
this.provisionDeviceKey = deviceProfile.getProvisionDeviceKey();
} }
@Override @Override
@ -125,12 +135,14 @@ public final class DeviceProfileEntity extends BaseSqlEntity<DeviceProfile> impl
deviceProfile.setName(name); deviceProfile.setName(name);
deviceProfile.setType(type); deviceProfile.setType(type);
deviceProfile.setTransportType(transportType); deviceProfile.setTransportType(transportType);
deviceProfile.setProvisionType(provisionType);
deviceProfile.setDescription(description); deviceProfile.setDescription(description);
deviceProfile.setDefault(isDefault); deviceProfile.setDefault(isDefault);
deviceProfile.setProfileData(JacksonUtil.convertValue(profileData, DeviceProfileData.class)); deviceProfile.setProfileData(JacksonUtil.convertValue(profileData, DeviceProfileData.class));
if (defaultRuleChainId != null) { if (defaultRuleChainId != null) {
deviceProfile.setDefaultRuleChainId(new RuleChainId(defaultRuleChainId)); deviceProfile.setDefaultRuleChainId(new RuleChainId(defaultRuleChainId));
} }
deviceProfile.setProvisionDeviceKey(provisionDeviceKey);
return deviceProfile; return deviceProfile;
} }
} }

14
dao/src/main/java/org/thingsboard/server/dao/sql/device/DeviceProfileRepository.java

@ -56,14 +56,10 @@ public interface DeviceProfileRepository extends PagingAndSortingRepository<Devi
DeviceProfileEntity findByTenantIdAndName(UUID id, String profileName); DeviceProfileEntity findByTenantIdAndName(UUID id, String profileName);
@Query(value = "SELECT d.* FROM device_profile as d " + @Query(value = "SELECT d.* from device_profile d " +
"WHERE d.name = :profileName " + "WHERE d.provision_device_key = :provisionDeviceKey " +
"AND d.profile_data->'configuration'->>'provisionDeviceKey' IS NOT NULL " + "AND d.profile_data->'provisionConfiguration'->>'provisionDeviceSecret' = :provisionDeviceSecret",
"AND d.profile_data->'configuration'->>'provisionDeviceSecret' IS NOT NULL " +
"AND d.profile_data->'configuration'->>'provisionDeviceKey' = :provisionDeviceKey " +
"AND d.profile_data->'configuration'->>'provisionDeviceSecret' = :provisionDeviceSecret",
nativeQuery = true) nativeQuery = true)
DeviceProfileEntity findProfileByProfileNameAndProfileDataProvisionConfigurationPair(@Param("profileName") String profileName, DeviceProfileEntity findByProvisionDeviceKeyAndProvisionDeviceSecret(@Param("provisionDeviceKey") String provisionDeviceKey,
@Param("provisionDeviceKey") String provisionDeviceKey, @Param("provisionDeviceSecret") String provisionDeviceSecret);
@Param("provisionDeviceSecret") String provisionDeviceSecret);
} }

4
dao/src/main/java/org/thingsboard/server/dao/sql/device/JpaDeviceProfileDao.java

@ -81,8 +81,8 @@ public class JpaDeviceProfileDao extends JpaAbstractSearchTextDao<DeviceProfileE
} }
@Override @Override
public DeviceProfile findProfileByProfileNameAndProfileDataProvisionConfigurationPair(String profileName, String provisionDeviceKey, String provisionDeviceSecret) { public DeviceProfile findByProvisionDeviceKeyAndProvisionDeviceSecret(String provisionDeviceKey, String provisionDeviceSecret) {
return DaoUtil.getData(deviceProfileRepository.findProfileByProfileNameAndProfileDataProvisionConfigurationPair(profileName, provisionDeviceKey, provisionDeviceSecret)); return DaoUtil.getData(deviceProfileRepository.findByProvisionDeviceKeyAndProvisionDeviceSecret(provisionDeviceKey, provisionDeviceSecret));
} }
@Override @Override

3
dao/src/main/resources/sql/schema-entities-hsql.sql

@ -152,13 +152,16 @@ CREATE TABLE IF NOT EXISTS device_profile (
name varchar(255), name varchar(255),
type varchar(255), type varchar(255),
transport_type varchar(255), transport_type varchar(255),
provision_type varchar(255),
profile_data jsonb, profile_data jsonb,
description varchar, description varchar,
search_text varchar(255), search_text varchar(255),
is_default boolean, is_default boolean,
tenant_id uuid, tenant_id uuid,
default_rule_chain_id uuid, default_rule_chain_id uuid,
provision_device_key varchar,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
); );

3
dao/src/main/resources/sql/schema-entities.sql

@ -170,13 +170,16 @@ CREATE TABLE IF NOT EXISTS device_profile (
name varchar(255), name varchar(255),
type varchar(255), type varchar(255),
transport_type varchar(255), transport_type varchar(255),
provision_type varchar(255),
profile_data jsonb, profile_data jsonb,
description varchar, description varchar,
search_text varchar(255), search_text varchar(255),
is_default boolean, is_default boolean,
tenant_id uuid, tenant_id uuid,
default_rule_chain_id uuid, default_rule_chain_id uuid,
provision_device_key varchar,
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name), CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name),
CONSTRAINT device_provision_key_unq_key UNIQUE (provision_device_key),
CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id) CONSTRAINT fk_default_rule_chain_device_profile FOREIGN KEY (default_rule_chain_id) REFERENCES rule_chain(id)
); );

Loading…
Cancel
Save