diff --git a/application/src/main/java/org/thingsboard/server/service/device/DeviceBulkImportService.java b/application/src/main/java/org/thingsboard/server/service/device/DeviceBulkImportService.java index 509e0fdbef..a37e03de1b 100644 --- a/application/src/main/java/org/thingsboard/server/service/device/DeviceBulkImportService.java +++ b/application/src/main/java/org/thingsboard/server/service/device/DeviceBulkImportService.java @@ -33,7 +33,10 @@ import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MClientCredential; import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MSecurityMode; +import org.thingsboard.server.common.data.device.data.DefaultDeviceConfiguration; +import org.thingsboard.server.common.data.device.data.DeviceData; import org.thingsboard.server.common.data.device.data.PowerMode; +import org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration; import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration; @@ -45,6 +48,7 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.security.DeviceCredentials; import org.thingsboard.server.common.data.security.DeviceCredentialsType; import org.thingsboard.server.common.data.sync.ie.importing.csv.BulkImportColumnType; +import org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion; import org.thingsboard.server.dao.device.DeviceCredentialsService; import org.thingsboard.server.dao.device.DeviceProfileService; import org.thingsboard.server.dao.device.DeviceService; @@ -76,18 +80,18 @@ public class DeviceBulkImportService extends AbstractBulkImportService { private final Lock findOrCreateDeviceProfileLock = new ReentrantLock(); @Override - protected void setEntityFields(Device entity, Map fields) { - ObjectNode additionalInfo = getOrCreateAdditionalInfoObj(entity); + protected void setEntityFields(Device device, Map fields) { + ObjectNode additionalInfo = getOrCreateAdditionalInfoObj(device); fields.forEach((columnType, value) -> { switch (columnType) { case NAME: - entity.setName(value); + device.setName(value); break; case TYPE: - entity.setType(value); + device.setType(value); break; case LABEL: - entity.setLabel(value); + device.setLabel(value); break; case DESCRIPTION: additionalInfo.set("description", new TextNode(value)); @@ -96,16 +100,17 @@ public class DeviceBulkImportService extends AbstractBulkImportService { additionalInfo.set("gateway", BooleanNode.valueOf(Boolean.parseBoolean(value))); break; } - entity.setAdditionalInfo(additionalInfo); + device.setAdditionalInfo(additionalInfo); }); + setUpDeviceConfiguration(device, fields); } @Override @SneakyThrows - protected Device saveEntity(SecurityUser user, Device entity, Map fields) { + protected Device saveEntity(SecurityUser user, Device device, Map fields) { DeviceCredentials deviceCredentials; try { - deviceCredentials = createDeviceCredentials(entity.getTenantId(), entity.getId(), fields); + deviceCredentials = createDeviceCredentials(device.getTenantId(), device.getId(), fields); deviceCredentialsService.formatCredentials(deviceCredentials); } catch (Exception e) { throw new DeviceCredentialsValidationException("Invalid device credentials: " + e.getMessage()); @@ -113,15 +118,15 @@ public class DeviceBulkImportService extends AbstractBulkImportService { DeviceProfile deviceProfile; if (deviceCredentials.getCredentialsType() == DeviceCredentialsType.LWM2M_CREDENTIALS) { - deviceProfile = setUpLwM2mDeviceProfile(entity.getTenantId(), entity); - } else if (StringUtils.isNotEmpty(entity.getType())) { - deviceProfile = deviceProfileService.findOrCreateDeviceProfile(entity.getTenantId(), entity.getType()); + deviceProfile = setUpLwM2mDeviceProfile(device.getTenantId(), device); + } else if (StringUtils.isNotEmpty(device.getType())) { + deviceProfile = deviceProfileService.findOrCreateDeviceProfile(device.getTenantId(), device.getType()); } else { - deviceProfile = deviceProfileService.findDefaultDeviceProfile(entity.getTenantId()); + deviceProfile = deviceProfileService.findDefaultDeviceProfile(device.getTenantId()); } - entity.setDeviceProfileId(deviceProfile.getId()); + device.setDeviceProfileId(deviceProfile.getId()); - return tbDeviceService.saveDeviceWithCredentials(entity, deviceCredentials, user); + return tbDeviceService.saveDeviceWithCredentials(device, deviceCredentials, user); } @Override @@ -136,6 +141,22 @@ public class DeviceBulkImportService extends AbstractBulkImportService { entity.setCustomerId(user.getCustomerId()); } + private void setUpDeviceConfiguration(Device device, Map fields) { + if (fields.containsKey(BulkImportColumnType.SNMP_HOST)) { + SnmpDeviceTransportConfiguration transportConfiguration = new SnmpDeviceTransportConfiguration(); + transportConfiguration.setHost(fields.get(BulkImportColumnType.SNMP_HOST)); + transportConfiguration.setPort(Optional.ofNullable(fields.get(BulkImportColumnType.SNMP_PORT)) + .map(Integer::parseInt).orElse(161)); + transportConfiguration.setProtocolVersion(Optional.ofNullable(fields.get(BulkImportColumnType.SNMP_VERSION)) + .map(version -> SnmpProtocolVersion.valueOf(version.toUpperCase())).orElse(SnmpProtocolVersion.V2C)); + transportConfiguration.setCommunity(fields.getOrDefault(BulkImportColumnType.SNMP_COMMUNITY_STRING, "public")); + + DeviceData deviceData = new DeviceData(); + deviceData.setTransportConfiguration(transportConfiguration); + device.setDeviceData(deviceData); + } + } + @SneakyThrows private DeviceCredentials createDeviceCredentials(TenantId tenantId, DeviceId deviceId, Map fields) { DeviceCredentials credentials = new DeviceCredentials(); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/importing/csv/BulkImportColumnType.java b/common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/importing/csv/BulkImportColumnType.java index c004187876..87582b4d6d 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/importing/csv/BulkImportColumnType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/sync/ie/importing/csv/BulkImportColumnType.java @@ -43,6 +43,10 @@ public enum BulkImportColumnType { LWM2M_SERVER_SECURITY_MODE("securityMode", LwM2MSecurityMode.NO_SEC.name()), LWM2M_SERVER_CLIENT_PUBLIC_KEY_OR_ID("clientPublicKeyOrId"), LWM2M_SERVER_CLIENT_SECRET_KEY("clientSecretKey"), + SNMP_HOST, + SNMP_PORT, + SNMP_VERSION, + SNMP_COMMUNITY_STRING, IS_GATEWAY, DESCRIPTION, ROUTING_KEY, diff --git a/ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts b/ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts index fddfa80d91..5e70c50394 100644 --- a/ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts +++ b/ui-ngx/src/app/modules/home/components/import-export/import-export.models.ts @@ -64,6 +64,10 @@ export enum ImportEntityColumnType { lwm2mServerSecurityMode = 'LWM2M_SERVER_SECURITY_MODE', lwm2mServerClientPublicKeyOrId = 'LWM2M_SERVER_CLIENT_PUBLIC_KEY_OR_ID', lwm2mServerClientSecretKey = 'LWM2M_SERVER_CLIENT_SECRET_KEY', + snmpHost = 'SNMP_HOST', + snmpPort = 'SNMP_PORT', + snmpVersion = 'SNMP_VERSION', + snmpCommunityString = 'SNMP_COMMUNITY_STRING', isGateway = 'IS_GATEWAY', description = 'DESCRIPTION', routingKey = 'ROUTING_KEY', @@ -95,6 +99,10 @@ export const importEntityColumnTypeTranslations = new Map