use of com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser in project azure-iot-sdk-java by Azure.
the class ExportImportTests method runExportJob.
private static List<ExportImportDevice> runExportJob(Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
Boolean excludeKeys = false;
String containerSasUri = getContainerSasUri(exportContainer);
boolean exportJobScheduled = false;
JobProperties exportJob = null;
while (!exportJobScheduled) {
try {
if (storageAuthenticationType.isPresent()) {
JobProperties exportJobProperties = JobProperties.createForExportJob(containerSasUri, excludeKeys, storageAuthenticationType.get());
exportJob = registryManager.exportDevices(exportJobProperties);
} else {
exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
}
exportJobScheduled = true;
} catch (IotHubTooManyDevicesException e) {
// test is being throttled, wait a while and try again
Thread.sleep(10 * 1000);
}
}
JobProperties.JobStatus jobStatus;
long startTime = System.currentTimeMillis();
while (true) {
exportJob = registryManager.getJob(exportJob.getJobId());
jobStatus = exportJob.getStatus();
if (jobStatus == JobProperties.JobStatus.COMPLETED || jobStatus == JobProperties.JobStatus.FAILED) {
break;
}
if (System.currentTimeMillis() - startTime > EXPORT_JOB_TIMEOUT_MILLISECONDS) {
fail("Timed out waiting for the export job to complete");
}
Thread.sleep(100);
}
String exportedDevicesJson = "";
for (BlobItem blobItem : exportContainer.listBlobs()) {
BlobInputStream stream = exportContainer.getBlobClient(blobItem.getName()).openInputStream();
try (Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name())) {
exportedDevicesJson = scanner.next();
}
}
List<ExportImportDevice> result = new ArrayList<>();
Scanner scanner = new Scanner(exportedDevicesJson);
while (scanner.hasNextLine()) {
String exportImportDeviceJson = scanner.nextLine();
ExportImportDeviceParser parser = new ExportImportDeviceParser(exportImportDeviceJson);
ExportImportDevice device = Deencapsulation.newInstance(ExportImportDevice.class, new Class[] { ExportImportDeviceParser.class }, parser);
device.setImportMode(ImportMode.CreateOrUpdate);
result.add(device);
}
scanner.close();
if (jobStatus != JobProperties.JobStatus.COMPLETED) {
Assert.fail("The export job was not completed successfully");
}
return result;
}
use of com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser in project azure-iot-sdk-java by Azure.
the class Tools method addModules.
private static void addModules(Iterable<Module> modules, String connectionString) throws IOException, IotHubException {
if (modules == null) {
throw new IllegalArgumentException("modules cannot be null");
}
IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
URL url = getBulkDeviceAddUrl(iotHubConnectionString);
List<ExportImportDeviceParser> parsers = new ArrayList<>();
for (Module module : modules) {
ExportImportDeviceParser exportImportDevice = new ExportImportDeviceParser();
exportImportDevice.setId(module.getDeviceId());
exportImportDevice.setModuleId(module.getId());
AuthenticationParser authenticationParser = new AuthenticationParser();
if (module.getAuthenticationType() == AuthenticationType.SAS) {
authenticationParser.setType(AuthenticationTypeParser.SAS);
authenticationParser.setSymmetricKey(new SymmetricKeyParser(module.getSymmetricKey().getPrimaryKey(), module.getSymmetricKey().getSecondaryKey()));
} else {
authenticationParser.setType(AuthenticationTypeParser.SELF_SIGNED);
authenticationParser.setThumbprint(new X509ThumbprintParser(module.getPrimaryThumbprint(), module.getSecondaryThumbprint()));
}
exportImportDevice.setAuthentication(authenticationParser);
exportImportDevice.setImportMode(IMPORT_MODE_CREATE);
parsers.add(exportImportDevice);
}
ExportImportDevicesParser body = new ExportImportDevicesParser();
body.setExportImportDevices(parsers);
bulkRegistryOperation(body.toJson(), url, connectionString);
}
use of com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser in project azure-iot-sdk-java by Azure.
the class Tools method removeDevices.
private static void removeDevices(Iterable<Device> devices, String connectionString) throws IOException, IotHubException {
if (devices == null) {
throw new IllegalArgumentException("devices cannot be null");
}
IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
URL url = getBulkDeviceAddUrl(iotHubConnectionString);
List<ExportImportDeviceParser> parsers = new ArrayList<>();
for (Device device : devices) {
ExportImportDeviceParser exportImportDevice = new ExportImportDeviceParser();
exportImportDevice.setId(device.getDeviceId());
exportImportDevice.setImportMode(IMPORT_MODE_DELETE);
parsers.add(exportImportDevice);
}
ExportImportDevicesParser body = new ExportImportDevicesParser();
body.setExportImportDevices(parsers);
bulkRegistryOperation(body.toJson(), url, connectionString);
}
use of com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser in project azure-iot-sdk-java by Azure.
the class Tools method addDevices.
private static void addDevices(Iterable<Device> devices, String connectionString) throws IOException, IotHubException {
if (devices == null) {
throw new IllegalArgumentException("devices cannot be null");
}
IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
URL url = getBulkDeviceAddUrl(iotHubConnectionString);
List<ExportImportDeviceParser> parsers = new ArrayList<>();
for (Device device : devices) {
ExportImportDeviceParser exportImportDevice = new ExportImportDeviceParser();
exportImportDevice.setId(device.getDeviceId());
AuthenticationParser authenticationParser = new AuthenticationParser();
if (device.getAuthenticationType() == AuthenticationType.SAS) {
authenticationParser.setType(AuthenticationTypeParser.SAS);
authenticationParser.setSymmetricKey(new SymmetricKeyParser(device.getSymmetricKey().getPrimaryKey(), device.getSymmetricKey().getSecondaryKey()));
} else {
authenticationParser.setType(AuthenticationTypeParser.SELF_SIGNED);
authenticationParser.setThumbprint(new X509ThumbprintParser(device.getPrimaryThumbprint(), device.getSecondaryThumbprint()));
}
exportImportDevice.setAuthentication(authenticationParser);
exportImportDevice.setImportMode(IMPORT_MODE_CREATE);
parsers.add(exportImportDevice);
}
ExportImportDevicesParser body = new ExportImportDevicesParser();
body.setExportImportDevices(parsers);
bulkRegistryOperation(body.toJson(), url, connectionString);
}
use of com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser in project azure-iot-sdk-java by Azure.
the class ExportImportTests method runImportJob.
private static void runImportJob(List<ExportImportDevice> devices, ImportMode importMode, Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
// Creating the json string to be submitted for import using the specified importMode
StringBuilder devicesToAdd = new StringBuilder();
for (int i = 0; i < devices.size(); i++) {
devices.get(i).setImportMode(importMode);
ExportImportDeviceParser parser = Deencapsulation.invoke(devices.get(i), "toExportImportDeviceParser");
devicesToAdd.append(parser.toJson());
if (i < devices.size() - 1) {
devicesToAdd.append("\r\n");
}
}
byte[] blobToImport = devicesToAdd.toString().getBytes(StandardCharsets.UTF_8);
// Creating the Azure storage blob and uploading the serialized string of devices
InputStream stream = new ByteArrayInputStream(blobToImport);
String importBlobName = "devices.txt";
BlockBlobClient importBlob = importContainer.getBlobClient(importBlobName).getBlockBlobClient();
importBlob.delete();
importBlob.upload(stream, blobToImport.length);
// Starting the import job
boolean importJobScheduled = false;
JobProperties importJob = null;
while (!importJobScheduled) {
try {
if (storageAuthenticationType.isPresent()) {
// For a given StorageAuthenticationType, create JobProperties and pass it
JobProperties importJobProperties = JobProperties.createForImportJob(getContainerSasUri(importContainer), getContainerSasUri(importContainer), storageAuthenticationType.get());
importJob = registryManager.importDevices(importJobProperties);
} else {
importJob = registryManager.importDevices(getContainerSasUri(importContainer), getContainerSasUri(importContainer));
}
importJobScheduled = true;
} catch (IotHubTooManyDevicesException e) {
// test is being throttled, wait a while and try again
Thread.sleep(10 * 1000);
}
}
// Waiting for the import job to complete
long startTime = System.currentTimeMillis();
while (true) {
importJob = registryManager.getJob(importJob.getJobId());
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
break;
}
if (System.currentTimeMillis() - startTime > IMPORT_JOB_TIMEOUT_MILLISECONDS) {
fail("Timed out waiting for the import job to complete");
}
Thread.sleep(100);
}
// Checking the result of the import job
if (importJob.getStatus() != JobProperties.JobStatus.COMPLETED) {
Assert.fail("The import job was not completed successfully for " + importMode + " operation.");
}
}
Aggregations