use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class Tools method getX509TestDevice.
private static TestDeviceIdentity getX509TestDevice(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, GeneralSecurityException {
// and the subsequent callers just used one of those devices
synchronized (testX509DeviceQueueLock) {
TestDeviceIdentity testDeviceIdentity;
if (!needCleanTwin && testX509DeviceWithTwinQueue.size() > 0) {
log.debug("Acquiring test device from testX509DeviceWithTwinQueue");
testDeviceIdentity = testX509DeviceWithTwinQueue.remove();
} else {
if (testX509DeviceQueue.size() < 1) {
// No cached devices to return, so create a new set of devices to cache, and return one of the newly created devices
log.debug("Proactively adding another {} devices to the X509 test device queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
List<Device> devicesToAdd = new ArrayList<>();
for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
Device deviceToAdd = Device.createDevice("test-device-" + UUID.randomUUID().toString(), AuthenticationType.SELF_SIGNED);
String x509Thumbprint = IntegrationTest.x509CertificateGenerator.getX509Thumbprint();
deviceToAdd.setThumbprintFinal(x509Thumbprint, x509Thumbprint);
devicesToAdd.add(deviceToAdd);
}
addDevices(devicesToAdd, iotHubConnectionString);
for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
testX509DeviceQueue.add(new TestDeviceIdentity(null, devicesToAdd.get(i)));
}
}
log.debug("Acquiring test device from testX509DeviceQueue");
testDeviceIdentity = testX509DeviceQueue.remove();
}
SSLContext sslContext = SSLContextBuilder.buildSSLContext(IntegrationTest.x509CertificateGenerator.getPublicCertificate(), IntegrationTest.x509CertificateGenerator.getPrivateKey());
DeviceClient client = new DeviceClient(getRegistyManager(iotHubConnectionString).getDeviceConnectionString(testDeviceIdentity.getDevice()), protocol, sslContext);
testDeviceIdentity.setDeviceClient(client);
return testDeviceIdentity;
}
}
use of com.microsoft.azure.sdk.iot.service.Device 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.service.Device in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientComponentTests method createDeviceClient.
private DeviceClient createDeviceClient(IotHubClientProtocol protocol) throws IOException, IotHubException, URISyntaxException {
ClientOptions options = new ClientOptions();
options.setModelId(E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
this.deviceId = DEVICE_ID_PREFIX.concat(UUID.randomUUID().toString());
Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
Device registeredDevice = registryManager.addDevice(device);
String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
return new DeviceClient(deviceConnectionString, protocol, options);
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class ServiceClientTests method serviceClientTokenRenewalWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
RegistryManager registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
Device device = testDeviceIdentity.getDevice();
ServiceClient serviceClient;
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
serviceClient = new ServiceClient(iotHubConnectionStringObj.getHostName(), sasCredential, testInstance.protocol);
serviceClient.open();
Message message = new Message(SMALL_PAYLOAD);
serviceClient.send(device.getDeviceId(), message);
// deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
serviceClient.send(device.getDeviceId(), message);
fail("Expected sending cloud to device message to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (IOException e) {
// For service client, the unauthorized exception is wrapped by an IOException, so we need to unwrap it here
if (e.getCause() instanceof IotHubUnathorizedException) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
} else {
throw e;
}
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
sasCredential.update(serviceSasToken.toString());
// The final c2d send should succeed since the shared access signature has been renewed
serviceClient.send(device.getDeviceId(), message);
serviceClient.close();
registryManager.close();
Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class RegistryManagerTests method deviceCreationWithDeviceScope.
@StandardTierHubOnlyTest
@Test
@ContinuousIntegrationTest
public void deviceCreationWithDeviceScope() throws IOException, InterruptedException, IotHubException, URISyntaxException {
// Arrange
this.testInstance = new RegistryManagerTestInstance();
String edge1Id = deviceIdPrefix + UUID.randomUUID().toString();
String edge2Id = deviceIdPrefix + UUID.randomUUID().toString();
String deviceId = this.testInstance.deviceId;
// -Create-//
Device edgeDevice1 = Device.createFromId(edge1Id, DeviceStatus.Enabled, null);
DeviceCapabilities capabilities = new DeviceCapabilities();
capabilities.setIotEdge(true);
edgeDevice1.setCapabilities(capabilities);
edgeDevice1 = Tools.addDeviceWithRetry(this.testInstance.registryManager, edgeDevice1);
Device edgeDevice2 = Device.createFromId(edge2Id, DeviceStatus.Enabled, null);
capabilities.setIotEdge(true);
edgeDevice2.setCapabilities(capabilities);
// set edge1 as parent
edgeDevice2.getParentScopes().add(edgeDevice1.getScope());
edgeDevice2 = Tools.addDeviceWithRetry(this.testInstance.registryManager, edgeDevice2);
Device leafDevice = Device.createFromId(deviceId, DeviceStatus.Enabled, null);
assertNotNull(edgeDevice1.getScope());
leafDevice.setScope(edgeDevice1.getScope());
Tools.addDeviceWithRetry(this.testInstance.registryManager, leafDevice);
// -Read-//
Device deviceRetrieved = this.testInstance.registryManager.getDevice(deviceId);
// -Delete-//
this.testInstance.registryManager.removeDevice(edge1Id);
this.testInstance.registryManager.removeDevice(edge2Id);
this.testInstance.registryManager.removeDevice(deviceId);
// Assert
assertEquals(buildExceptionMessage("Edge parent scope did not match parent's device scope", hostName), edgeDevice2.getParentScopes().get(0), edgeDevice1.getScope());
assertNotEquals(buildExceptionMessage("Child edge device scope should be it's own", hostName), edgeDevice2.getScope(), edgeDevice1.getScope());
assertEquals(buildExceptionMessage("Registered device Id is not correct", hostName), deviceId, leafDevice.getDeviceId());
assertEquals(buildExceptionMessage("Device scopes did not match", hostName), deviceRetrieved.getScope(), edgeDevice1.getScope());
assertEquals(buildExceptionMessage("Device's first parent scope did not match device scope", hostName), deviceRetrieved.getParentScopes().get(0), deviceRetrieved.getScope());
}
Aggregations