Search in sources :

Example 56 with Device

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;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) SSLContext(javax.net.ssl.SSLContext)

Example 57 with Device

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);
}
Also used : AuthenticationParser(com.microsoft.azure.sdk.iot.deps.serializer.AuthenticationParser) SymmetricKeyParser(com.microsoft.azure.sdk.iot.deps.serializer.SymmetricKeyParser) Device(com.microsoft.azure.sdk.iot.service.Device) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser) ArrayList(java.util.ArrayList) X509ThumbprintParser(com.microsoft.azure.sdk.iot.deps.serializer.X509ThumbprintParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

Example 58 with Device

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);
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device)

Example 59 with Device

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);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 60 with Device

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());
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceCapabilities(com.microsoft.azure.sdk.iot.deps.twin.DeviceCapabilities) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Aggregations

Device (com.microsoft.azure.sdk.iot.service.Device)68 Test (org.junit.Test)46 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)17 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)15 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)12 IOException (java.io.IOException)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)11 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)10 ArrayList (java.util.ArrayList)10 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)7 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)5 AzureSasCredential (com.azure.core.credential.AzureSasCredential)4 DeviceCapabilities (com.microsoft.azure.sdk.iot.deps.twin.DeviceCapabilities)4 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)4 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)4 Module (com.microsoft.azure.sdk.iot.service.Module)4