Search in sources :

Example 11 with Module

use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.

the class Tools method addModuleWithRetry.

public static Module addModuleWithRetry(RegistryManager registryManager, Module module) throws IotHubException, IOException, InterruptedException {
    long startTime = System.currentTimeMillis();
    Module ret = null;
    while (System.currentTimeMillis() - startTime < RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
        try {
            log.debug("Attempting to add module {} to registry", module.getId());
            ret = registryManager.addModule(module);
            log.debug("Successfully added module {} to registry", module.getId());
            break;
        } catch (UnknownHostException | SocketException | SocketTimeoutException e) {
            log.warn("Failed to add module " + module.getId());
            e.printStackTrace();
            Thread.sleep(WAIT_FOR_RETRY);
            if (System.currentTimeMillis() - startTime >= RETRY_TIMEOUT_ON_NETWORK_FAILURE_MILLISECONDS) {
                throw e;
            }
        }
    }
    return ret;
}
Also used : SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) Module(com.microsoft.azure.sdk.iot.service.Module)

Example 12 with Module

use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.

the class Tools method getX509TestModule.

private static TestModuleIdentity getX509TestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testX509ModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testX509ModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testX509ModuleWithTwinQueue");
            testModuleIdentity = testX509ModuleWithTwinQueue.remove();
        } else {
            if (testX509ModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SELF_SIGNED, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    Module module = Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SELF_SIGNED);
                    String x509Thumbprint = IntegrationTest.x509CertificateGenerator.getX509Thumbprint();
                    module.setThumbprintFinal(x509Thumbprint, x509Thumbprint);
                    modulesToAdd.add(module);
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testX509ModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testX509ModuleQueue");
            testModuleIdentity = testX509ModuleQueue.remove();
        }
        SSLContext sslContext = SSLContextBuilder.buildSSLContext(IntegrationTest.x509CertificateGenerator.getPublicCertificate(), IntegrationTest.x509CertificateGenerator.getPrivateKey());
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol, sslContext);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) SSLContext(javax.net.ssl.SSLContext) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Example 13 with Module

use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.

the class ModuleTest method createModule_success.

// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_005: [The function shall create a new instance of Module using the given
// moduleId for the device with deviceId and return it]
@Test
public void createModule_success() {
    // Arrange
    String deviceId = "device-xxx";
    String moduleId = "module-xxx";
    // Act
    Module module = Module.createModule(deviceId, moduleId, AuthenticationType.SAS);
    // Assert
    assertNotNull(module);
    assertEquals(deviceId, module.getDeviceId());
    assertEquals(moduleId, module.getId());
    assertNotNull(module.getSymmetricKey());
}
Also used : Module(com.microsoft.azure.sdk.iot.service.Module) Test(org.junit.Test)

Example 14 with Module

use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.

the class ModuleTest method module_get_all_properties.

// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_001: [The Module class shall have the following properties: id, deviceId,
// generationId, Etag, ConnectionState, ConnectionStateUpdatedTime, LastActivityTime, cloudToDeviceMessageCount,
// authentication, managedBy.]
// Assert
@Test
public void module_get_all_properties() {
    // Arrange
    String deviceId = "xxx-device";
    String moduleId = "xxx-module";
    SymmetricKey expectedSymmetricKey = new SymmetricKey();
    String expectedPrimaryThumbprint = "0000000000000000000000000000000000000000";
    String expectedSecondaryThumbprint = "1111111111111111111111111111111111111111";
    // Act
    Module module = Module.createFromId(deviceId, moduleId, null);
    module.setSymmetricKey(expectedSymmetricKey);
    assertEquals(expectedSymmetricKey, module.getSymmetricKey());
    module.setThumbprintFinal(expectedPrimaryThumbprint, expectedSecondaryThumbprint);
    assertEquals(expectedPrimaryThumbprint, module.getPrimaryThumbprint());
    assertEquals(expectedSecondaryThumbprint, module.getSecondaryThumbprint());
    module.getId();
    module.getManagedBy();
    module.getPrimaryThumbprint();
    module.getSecondaryThumbprint();
    module.getDeviceId();
    module.getGenerationId();
    module.getPrimaryKey();
    module.getSecondaryKey();
    module.geteTag();
    module.getConnectionState();
    module.getConnectionStateUpdatedTime();
    module.getLastActivityTime();
    module.getCloudToDeviceMessageCount();
}
Also used : SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) Module(com.microsoft.azure.sdk.iot.service.Module) Test(org.junit.Test)

Example 15 with Module

use of com.microsoft.azure.sdk.iot.service.Module in project azure-iot-sdk-java by Azure.

the class ModuleTest method createFromId_success.

// Tests_SRS_SERVICE_SDK_JAVA_MODULE_28_003: [The function shall create a new instance
// of Module using the given moduleId for device with deviceId and return it]
// Assert
@Test
public void createFromId_success() {
    String deviceName = "device-xxx";
    String moduleName = "module-xxx";
    // Act
    Module module = Module.createFromId(deviceName, moduleName, null);
    // Assert
    assertNotNull(module);
    assertEquals(deviceName, module.getDeviceId());
    assertEquals(moduleName, module.getId());
    assertNotNull(module.getSymmetricKey());
}
Also used : Module(com.microsoft.azure.sdk.iot.service.Module) Test(org.junit.Test)

Aggregations

Module (com.microsoft.azure.sdk.iot.service.Module)23 Test (org.junit.Test)14 Device (com.microsoft.azure.sdk.iot.service.Device)5 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)3 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)3 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)3 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)3 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)2 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)2 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)2 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 AuthenticationParser (com.microsoft.azure.sdk.iot.deps.serializer.AuthenticationParser)1 ExportImportDeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)1 SymmetricKeyParser (com.microsoft.azure.sdk.iot.deps.serializer.SymmetricKeyParser)1 X509ThumbprintParser (com.microsoft.azure.sdk.iot.deps.serializer.X509ThumbprintParser)1