Search in sources :

Example 16 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 constructorThrowsOnNullRegistrationId.

// SRS_RegistrationOperationStatusParser_25_005: [ This method shall throw IllegalArgumentException if Registration Id cannot be parsed. ]
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnNullRegistrationId() throws IllegalArgumentException, JsonParseException {
    final String json = "{\"operationId\":\"" + TEST_OPERATION_ID + "\"," + "\"status\":\"assigning\"," + "\"registrationState\":" + "{" + "\"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 17 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 constructorWithoutRegistrationStateSucceed.

@Test
public void constructorWithoutRegistrationStateSucceed() throws IllegalArgumentException, JsonParseException {
    final String json = "{\"operationId\":\"" + TEST_OPERATION_ID + "\"," + "\"status\":\"assigning\"}";
    RegistrationOperationStatusParser operationsRegistrationOperationStatusParser = RegistrationOperationStatusParser.createFromJson(json);
    assertNotNull(operationsRegistrationOperationStatusParser.getOperationId());
    assertNotNull(operationsRegistrationOperationStatusParser.getStatus());
    assertNull(operationsRegistrationOperationStatusParser.getRegistrationState());
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) Test(org.junit.Test)

Example 18 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 executeStateMachineForStatus.

private void executeStateMachineForStatus(RegistrationOperationStatusParser registrationOperationStatusParser) throws TimeoutException, InterruptedException, ExecutionException, ProvisioningDeviceClientException, SecurityProviderException {
    boolean isContinue = false;
    RegistrationOperationStatusParser statusRegistrationOperationStatusParser = registrationOperationStatusParser;
    ProvisioningStatus nextStatus = ProvisioningStatus.fromString(registrationOperationStatusParser.getStatus());
    log.info("Current provisioning status: {}", nextStatus);
    // continue invoking for status until a terminal state is reached
    do {
        if (nextStatus == null) {
            throw new ProvisioningDeviceClientException("Did not receive a valid status");
        }
        switch(nextStatus) {
            case UNASSIGNED:
            // intended fall through
            case ASSIGNING:
                log.trace("Polling device provisioning service for status of registration...");
                statusRegistrationOperationStatusParser = this.invokeStatus(registrationOperationStatusParser.getOperationId());
                nextStatus = ProvisioningStatus.fromString(statusRegistrationOperationStatusParser.getStatus());
                isContinue = true;
                break;
            case ASSIGNED:
                this.dpsStatus = PROVISIONING_DEVICE_STATUS_ASSIGNED;
                DeviceRegistrationResultParser registrationStatus = statusRegistrationOperationStatusParser.getRegistrationState();
                if (registrationStatus == null || registrationStatus.getAssignedHub() == null || registrationStatus.getAssignedHub().isEmpty() || registrationStatus.getDeviceId() == null || registrationStatus.getDeviceId().isEmpty()) {
                    // Codes_SRS_ProvisioningTask_34_018: [Upon reaching the terminal state ASSIGNED, if the registration status json is missing an assigned hub or device id, this function shall throw a ProvisioningDeviceClientException.]
                    throw new ProvisioningDeviceClientException("Could not retrieve Assigned Hub or Device ID and status changed to Assigned");
                }
                RegistrationResult registrationInfo = new RegistrationResult(registrationStatus.getAssignedHub(), registrationStatus.getDeviceId(), registrationStatus.getPayload(), PROVISIONING_DEVICE_STATUS_ASSIGNED);
                registrationInfo.setRegistrationId(registrationStatus.getRegistrationId());
                registrationInfo.setStatus(registrationStatus.getStatus());
                registrationInfo.setSubstatus(ProvisioningDeviceClientSubstatus.fromString(registrationStatus.getSubstatus()));
                registrationInfo.setCreatedDateTimeUtc(registrationStatus.getCreatedDateTimeUtc());
                registrationInfo.setLastUpdatesDateTimeUtc(registrationStatus.getLastUpdatesDateTimeUtc());
                registrationInfo.setETag(registrationStatus.getEtag());
                if (this.securityProvider instanceof SecurityProviderTpm) {
                    if (registrationStatus.getTpm() == null || registrationStatus.getTpm().getAuthenticationKey() == null || registrationStatus.getTpm().getAuthenticationKey().isEmpty()) {
                        // Codes_SRS_ProvisioningTask_34_017: [Upon reaching the terminal state ASSIGNED, if the saved security client is an instance of SecurityClientTpm and if the registration status json does not contain an authentication key, this function shall throw a ProvisioningDeviceClientException.]
                        throw new ProvisioningDeviceClientException("Could not retrieve Authentication key when status was assigned");
                    }
                    // Codes_SRS_ProvisioningTask_34_016: [Upon reaching the terminal state ASSIGNED, if the saved security client is an instance of SecurityClientTpm, the security client shall decrypt and store the authentication key from the statusResponseParser.]
                    String authenticationKey = registrationStatus.getTpm().getAuthenticationKey();
                    ((SecurityProviderTpm) this.securityProvider).activateIdentityKey(decodeBase64(authenticationKey.getBytes(StandardCharsets.UTF_8)));
                }
                log.info("Device provisioning service assigned the device successfully");
                this.invokeRegistrationCallback(registrationInfo, null);
                isContinue = false;
                break;
            case FAILED:
                this.dpsStatus = PROVISIONING_DEVICE_STATUS_FAILED;
                String errorMessage = statusRegistrationOperationStatusParser.getRegistrationState().getErrorMessage();
                ProvisioningDeviceHubException dpsHubException = new ProvisioningDeviceHubException(errorMessage);
                dpsHubException.setErrorCode(registrationOperationStatusParser.getRegistrationState().getErrorCode());
                registrationInfo = new RegistrationResult(null, null, null, PROVISIONING_DEVICE_STATUS_FAILED);
                log.error("Device provisioning service failed to provision the device, finished with status FAILED: {}", errorMessage);
                this.invokeRegistrationCallback(registrationInfo, dpsHubException);
                isContinue = false;
                break;
            case DISABLED:
                this.dpsStatus = PROVISIONING_DEVICE_STATUS_DISABLED;
                String disabledErrorMessage = statusRegistrationOperationStatusParser.getRegistrationState().getErrorMessage();
                dpsHubException = new ProvisioningDeviceHubException(disabledErrorMessage);
                dpsHubException.setErrorCode(registrationOperationStatusParser.getRegistrationState().getErrorCode());
                registrationInfo = new RegistrationResult(null, null, null, PROVISIONING_DEVICE_STATUS_DISABLED);
                log.error("Device provisioning service failed to provision the device, finished with status DISABLED: {}", disabledErrorMessage);
                this.invokeRegistrationCallback(registrationInfo, dpsHubException);
                isContinue = false;
                break;
        }
    } while (isContinue);
}
Also used : ProvisioningDeviceHubException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceHubException) RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) SecurityProviderTpm(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm) DeviceRegistrationResultParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationResultParser) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 19 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 invokeRegister.

private RegistrationOperationStatusParser invokeRegister() throws InterruptedException, ExecutionException, TimeoutException, ProvisioningDeviceClientException {
    RegisterTask registerTask = new RegisterTask(this.provisioningDeviceClientConfig, securityProvider, provisioningDeviceClientContract, authorization);
    FutureTask<RegistrationOperationStatusParser> futureRegisterTask = new FutureTask<>(registerTask);
    executor.submit(futureRegisterTask);
    RegistrationOperationStatusParser registrationOperationStatusParser = futureRegisterTask.get(MAX_TIME_TO_WAIT_FOR_REGISTRATION, TimeUnit.MILLISECONDS);
    if (registrationOperationStatusParser == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Registration response could not be retrieved, " + "authentication failure");
    }
    ProvisioningStatus status = ProvisioningStatus.fromString(registrationOperationStatusParser.getStatus());
    if (status == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Received null status for registration, " + "authentication failure");
    }
    if (registrationOperationStatusParser.getOperationId() == null) {
        throw new ProvisioningDeviceClientAuthenticationException("operation id could not be retrieved, " + "authentication failure");
    }
    return registrationOperationStatusParser;
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)

Example 20 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 invokeStatus.

private RegistrationOperationStatusParser invokeStatus(String operationId) throws TimeoutException, InterruptedException, ExecutionException, ProvisioningDeviceClientException {
    Thread.sleep(provisioningDeviceClientContract.getRetryValue());
    StatusTask statusTask = new StatusTask(securityProvider, provisioningDeviceClientContract, provisioningDeviceClientConfig, operationId, this.authorization);
    FutureTask<RegistrationOperationStatusParser> futureStatusTask = new FutureTask<>(statusTask);
    executor.submit(futureStatusTask);
    RegistrationOperationStatusParser statusRegistrationOperationStatusParser = futureStatusTask.get(MAX_TIME_TO_WAIT_FOR_STATUS_UPDATE, TimeUnit.MILLISECONDS);
    if (statusRegistrationOperationStatusParser == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status response could not be retrieved, " + "authentication failure");
    }
    if (statusRegistrationOperationStatusParser.getStatus() == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status could not be retrieved, " + "authentication failure");
    }
    if (ProvisioningStatus.fromString(statusRegistrationOperationStatusParser.getStatus()) == null) {
        this.dpsStatus = PROVISIONING_DEVICE_STATUS_ERROR;
        throw new ProvisioningDeviceClientAuthenticationException("Status could not be retrieved, " + "authentication failure");
    }
    return statusRegistrationOperationStatusParser;
}
Also used : RegistrationOperationStatusParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser) ProvisioningDeviceClientAuthenticationException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientAuthenticationException)

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