Search in sources :

Example 21 with RegistrationOperationStatusParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser in project azure-iot-sdk-java by Azure.

the class ProvisioningTask method call.

// this thread will continue to run until DPS status is assigned and registered or exit on error
// DPS State machine
/**
 * This method executes the State machine with the device goes through during registration.
 * @return Returns {@code null}
 * @throws Exception This exception is thrown if any of the exception during execution is not handled.
 */
@Override
public Object call() throws Exception {
    // The thread doesn't have any opened connections associated to it yet.
    String threadName = this.provisioningDeviceClientContract.getHostName() + "-" + this.provisioningDeviceClientConfig.getUniqueIdentifier() + "-Cxn" + "PendingConnectionId" + "-" + THREAD_NAME;
    Thread.currentThread().setName(threadName);
    try {
        // SRS_ProvisioningTask_25_015: [ This method shall invoke open call on the contract.]
        log.info("Opening the connection to device provisioning service...");
        provisioningDeviceClientContract.open(new RequestData(securityProvider.getRegistrationId(), securityProvider.getSSLContext(), securityProvider instanceof SecurityProviderX509, provisioningDeviceClientConfig.getPayload()));
        // SRS_ProvisioningTask_25_007: [ This method shall invoke Register task and status task to execute the state machine of the service as per below rules.]
        /*
            Service State Machine Rules

            SRS_ProvisioningTask_25_008: [ This method shall invoke register task and wait for it to complete.]
            SRS_ProvisioningTask_25_009: [ This method shall invoke status callback with status PROVISIONING_DEVICE_STATUS_AUTHENTICATED if register task completes successfully.]
            SRS_ProvisioningTask_25_010: [ This method shall invoke status task to get the current state of the device registration and wait until a terminal state is reached.]
            SRS_ProvisioningTask_25_011: [ Upon reaching one of the terminal state i.e ASSIGNED, this method shall invoke registration callback with the information retrieved from service for IotHub Uri and DeviceId. Also if status callback is defined then it shall be invoked with status PROVISIONING_DEVICE_STATUS_ASSIGNED.]
            SRS_ProvisioningTask_25_012: [ Upon reaching one of the terminal states i.e FAILED or DISABLED, this method shall invoke registration callback with error message received from service. Also if status callback is defined then it shall be invoked with status PROVISIONING_DEVICE_STATUS_ERROR.]
            SRS_ProvisioningTask_25_013: [ Upon reaching intermediate state i.e UNASSIGNED or ASSIGNING, this method shall continue to query for status until a terminal state is reached. Also if status callback is defined then it shall be invoked with status PROVISIONING_DEVICE_STATUS_ASSIGNING.]
            State diagram :

            One of the following states can be reached from register or status task - (A) Unassigned (B) Assigning (C) Assigned (D) Fail (E) Disable

                Return-State	A	            B	        C	        D	        E
                Register-State	B, C, D, E	    C, D, E	    terminal	terminal	terminal
                Status-State	B, C, D, E	    C, D, E	    terminal	terminal	terminal
             */
        String connectionId = this.provisioningDeviceClientConfig.getUniqueIdentifier();
        if (connectionId == null) {
            // For Symetric Key authentication, connection is not open until the registration is invoked.
            connectionId = "PendingConnectionId";
        }
        threadName = this.provisioningDeviceClientContract.getHostName() + "-" + this.provisioningDeviceClientConfig.getUniqueIdentifier() + "-Cxn" + connectionId + "-" + THREAD_NAME;
        Thread.currentThread().setName(threadName);
        log.info("Connection to device provisioning service opened successfully, sending initial device registration message");
        RegistrationOperationStatusParser registrationOperationStatusParser = this.invokeRegister();
        log.info("Waiting for device provisioning service to provision this device...");
        this.executeStateMachineForStatus(registrationOperationStatusParser);
        this.close();
    } catch (ExecutionException | TimeoutException | ProvisioningDeviceClientException | SecurityProviderException e) {
        // SRS_ProvisioningTask_25_006: [ This method shall invoke the status callback, if any of the task fail or throw any exception. ]
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        invokeRegistrationCallback(new RegistrationResult(null, null, null, PROVISIONING_DEVICE_STATUS_ERROR), e);
        // SRS_ProvisioningTask_25_015: [ This method shall invoke close call on the contract and close the threads started.]
        this.close();
    }
    return null;
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) SecurityProviderX509(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderX509) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 22 with RegistrationOperationStatusParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorWithX509JsonThrowsOnNullSubjectNameSigningCert.

// SRS_RegistrationOperationStatusParser_25_016: [ This method shall throw IllegalArgumentException if Subject Name from X509 Signing Certificate Info cannot be parsed. ]
@Test(expected = IllegalArgumentException.class)
public void constructorWithX509JsonThrowsOnNullSubjectNameSigningCert() throws IllegalArgumentException, JsonParseException {
    final String json = "{\"operationId\":\"" + TEST_OPERATION_ID + "\"," + "\"status\":\"assigned\"," + "\"registrationState\":" + "{\"x509\":" + "{\"certificateInfo\":" + "{\"subjectName\":\"" + TEST_SUBJECT_NAME + "\"," + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"serialNumber\":\"" + TEST_SERIAL_NUMBER + "\"," + "\"version\":" + TEST_VERSION + "}," + "\"enrollmentGroupId\":\"" + TEST_ENROLLMENT_GROUP_ID + "\"," + "\"signingCertificateInfo\":" + "{" + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"serialNumber\":\"" + TEST_SERIAL_NUMBER + "\"," + "\"version\":" + TEST_VERSION + "}" + "}," + "\"registrationId\":\"" + TEST_REGISTRATION_ID + "\"," + "\"createdDateTimeUtc\":\"2017-07-21T20:56:19.3109747Z\"," + "\"assignedHub\":\"" + TEST_ASSIGNED_HUB + "\"," + "\"deviceId\":\"" + TEST_DEVICE_ID + "\"," + "\"status\":\"assigned\"," + "\"etag\":\"172d963d-ec26-41ba-84c4-afe5f6c93ef4\"," + "\"lastUpdatedDateTimeUtc\":\"2017-07-21T20:56:19.7978138Z\"}}\n";
    RegistrationOperationStatusParser operationsRegistrationOperationStatusParser = RegistrationOperationStatusParser.createFromJson(json);
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) Test(org.junit.Test)

Example 23 with RegistrationOperationStatusParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorWithX509JsonThrowsOnNullSerialNumberSigningCert.

// SRS_RegistrationOperationStatusParser_25_021: [ This method shall throw IllegalArgumentException if Serial Number from X509 Signing Certificate Info cannot be parsed. ]
@Test(expected = IllegalArgumentException.class)
public void constructorWithX509JsonThrowsOnNullSerialNumberSigningCert() throws IllegalArgumentException, JsonParseException {
    final String json = "{\"operationId\":\"" + TEST_OPERATION_ID + "\"," + "\"status\":\"assigned\"," + "\"registrationState\":" + "{\"x509\":" + "{\"certificateInfo\":" + "{\"subjectName\":\"" + TEST_SUBJECT_NAME + "\"," + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"serialNumber\":\"" + TEST_SERIAL_NUMBER + "\"," + "\"version\":" + TEST_VERSION + "}," + "\"enrollmentGroupId\":\"" + TEST_ENROLLMENT_GROUP_ID + "\"," + "\"signingCertificateInfo\":" + "{\"subjectName\":\"" + TEST_SUBJECT_NAME + "\"," + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"version\":" + TEST_VERSION + "}" + "}," + "\"registrationId\":\"" + TEST_REGISTRATION_ID + "\"," + "\"createdDateTimeUtc\":\"2017-07-21T20:56:19.3109747Z\"," + "\"assignedHub\":\"" + TEST_ASSIGNED_HUB + "\"," + "\"deviceId\":\"" + TEST_DEVICE_ID + "\"," + "\"status\":\"assigned\"," + "\"etag\":\"172d963d-ec26-41ba-84c4-afe5f6c93ef4\"," + "\"lastUpdatedDateTimeUtc\":\"2017-07-21T20:56:19.7978138Z\"}}\n";
    RegistrationOperationStatusParser operationsRegistrationOperationStatusParser = RegistrationOperationStatusParser.createFromJson(json);
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) Test(org.junit.Test)

Example 24 with RegistrationOperationStatusParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorThrowsOnNullOperationId.

// SRS_RegistrationOperationStatusParser_25_004: [ This method shall throw IllegalArgumentException if operationId cannot be parsed. ]
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnNullOperationId() throws IllegalArgumentException, JsonParseException {
    final String json = "{" + "\"status\":\"assigning\"}";
    RegistrationOperationStatusParser operationsRegistrationOperationStatusParser = RegistrationOperationStatusParser.createFromJson(json);
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) Test(org.junit.Test)

Example 25 with RegistrationOperationStatusParser

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser in project azure-iot-sdk-java by Azure.

the class RegistrationOperationStatusParserTest method constructorWithX509JsonSucceed.

// SRS_X509RegistrationResultParser_25_001: [ This method shall return the parsed Subject name. ]
// SRS_X509RegistrationResultParser_25_001: [ This method shall return the parsed Subject name. ]
// SRS_X509RegistrationResultParser_25_003: [ This method shall return the parsed sha256Thumbprint. ]
// SRS_X509RegistrationResultParser_25_004: [ This method shall return the parsed issuerName. ]
// SRS_X509RegistrationResultParser_25_005: [ This method shall return the parsed notBeforeUtc time. ]
// SRS_X509RegistrationResultParser_25_006: [ This method shall return the parsed notAfterUtc time. ]
// SRS_X509RegistrationResultParser_25_007: [ This method shall return the parsed serialNumber. ]
// SRS_X509RegistrationResultParser_25_008: [ This method shall return the parsed version. ]
// SRS_X509RegistrationResultParser_25_009: [ This method shall return the parsed certificateInfo. ]
// SRS_X509RegistrationResultParser_25_010: [ This method shall return the parsed signingCertificateInfo. ]
// SRS_X509RegistrationResultParser_25_011: [ This method shall return the parsed enrollmentGroupId. ]
// SRS_DeviceRegistrationResultParser_25_001: [ This method shall return the parsed registrationId. ]
// SRS_DeviceRegistrationResultParser_25_002: [ This method shall return the parsed createdDateTimeUtc. ]
// SRS_DeviceRegistrationResultParser_25_003: [ This method shall return the parsed assignedHub. ]
// SRS_DeviceRegistrationResultParser_25_004: [ This method shall return the parsed deviceId. ]
// SRS_DeviceRegistrationResultParser_25_005: [ This method shall return the parsed status. ]
// SRS_DeviceRegistrationResultParser_25_006: [ This method shall return the parsed eTag. ]
// SRS_DeviceRegistrationResultParser_25_007: [ This method shall return the parsed lastUpdatesDateTimeUtc. ]
// SRS_DeviceRegistrationResultParser_25_009: [ This method shall return the parsed X509RegistrationResultParser object. ]
@Test
public void constructorWithX509JsonSucceed() throws IllegalArgumentException, JsonParseException {
    final String json = "{\"operationId\":\"" + TEST_OPERATION_ID + "\"," + "\"status\":\"assigned\"," + "\"registrationState\":" + "{\"x509\":" + "{\"certificateInfo\":" + "{\"subjectName\":\"" + TEST_SUBJECT_NAME + "\"," + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"serialNumber\":\"" + TEST_SERIAL_NUMBER + "\"," + "\"version\":" + TEST_VERSION + "}," + "\"enrollmentGroupId\":\"" + TEST_ENROLLMENT_GROUP_ID + "\"," + "\"signingCertificateInfo\":" + "{\"subjectName\":\"" + TEST_SUBJECT_NAME + "\"," + "\"sha1Thumbprint\":\"" + TEST_SHA1_THUMBPRINT + "\"," + "\"sha256Thumbprint\":\"" + TEST_SHA256_THUMBPRINT + "\"," + "\"issuerName\":\"" + TEST_ISSUER_NAME + "\"," + "\"notBeforeUtc\":\"2017-01-01T00:00:00Z\"," + "\"notAfterUtc\":\"2037-01-01T00:00:00Z\"," + "\"serialNumber\":\"" + TEST_SERIAL_NUMBER + "\"," + "\"version\":" + TEST_VERSION + "}" + "}," + "\"registrationId\":\"" + TEST_REGISTRATION_ID + "\"," + "\"createdDateTimeUtc\":\"2017-07-21T20:56:19.3109747Z\"," + "\"assignedHub\":\"" + TEST_ASSIGNED_HUB + "\"," + "\"deviceId\":\"" + TEST_DEVICE_ID + "\"," + "\"status\":\"assigned\"," + "\"etag\":\"172d963d-ec26-41ba-84c4-afe5f6c93ef4\"," + "\"lastUpdatedDateTimeUtc\":\"2017-07-21T20:56:19.7978138Z\"}}\n";
    RegistrationOperationStatusParser operationsRegistrationOperationStatusParser = RegistrationOperationStatusParser.createFromJson(json);
    assertNotNull(operationsRegistrationOperationStatusParser.getOperationId());
    assertEquals(TEST_OPERATION_ID, operationsRegistrationOperationStatusParser.getOperationId());
    assertNotNull(operationsRegistrationOperationStatusParser.getStatus());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getRegistrationId());
    assertEquals(TEST_REGISTRATION_ID, operationsRegistrationOperationStatusParser.getRegistrationState().getRegistrationId());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getCreatedDateTimeUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getAssignedHub());
    assertEquals(TEST_ASSIGNED_HUB, operationsRegistrationOperationStatusParser.getRegistrationState().getAssignedHub());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getDeviceId());
    assertEquals(TEST_DEVICE_ID, operationsRegistrationOperationStatusParser.getRegistrationState().getDeviceId());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getStatus());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getEtag());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getLastUpdatesDateTimeUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSubjectName());
    assertEquals(TEST_SUBJECT_NAME, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSubjectName());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSha1Thumbprint());
    assertEquals(TEST_SHA1_THUMBPRINT, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSha1Thumbprint());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSha256Thumbprint());
    assertEquals(TEST_SHA256_THUMBPRINT, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSha256Thumbprint());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getIssuerName());
    assertEquals(TEST_ISSUER_NAME, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getIssuerName());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getNotBeforeUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getNotAfterUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSerialNumber());
    assertEquals(TEST_SERIAL_NUMBER, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getCertificateInfo().getSerialNumber());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getEnrollmentGroupId());
    assertEquals(TEST_ENROLLMENT_GROUP_ID, operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getEnrollmentGroupId());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getSubjectName());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getSha1Thumbprint());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getSha256Thumbprint());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getIssuerName());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getNotBeforeUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getNotAfterUtc());
    assertNotNull(operationsRegistrationOperationStatusParser.getRegistrationState().getX509().getSigningCertificateInfo().getSerialNumber());
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) Test(org.junit.Test)

Aggregations

RegistrationOperationStatusParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser)31 Test (org.junit.Test)27 ProvisioningDeviceClientAuthenticationException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)2 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)2 ProvisioningDeviceHubException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceHubException)1 DeviceRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationResultParser)1 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)1 SecurityProviderX509 (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderX509)1 SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)1