Search in sources :

Example 1 with ProvisioningDeviceHubException

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceHubException 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 2 with ProvisioningDeviceHubException

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

the class ContractAPIAmqp method getRegistrationStatus.

/**
 * Gets the registration status over AMQP
 * @param requestData A non {@code null} value with all the request data
 * @param responseCallback A non {@code null} value for the callback
 * @param callbackContext An object for context. Can be {@code null}
 * @throws ProvisioningDeviceClientException If any of the parameters are invalid ({@code null} or empty)
 * @throws ProvisioningDeviceTransportException If any of the API calls to transport fail
 * @throws ProvisioningDeviceHubException If hub responds back with an invalid status
 */
public synchronized void getRegistrationStatus(RequestData requestData, ResponseCallback responseCallback, Object callbackContext) throws ProvisioningDeviceClientException {
    // SRS_ContractAPIAmqp_07_009: [If requestData is null this method shall throw ProvisioningDeviceClientException.]
    if (requestData == null) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("requestData cannot be null"));
    }
    // SRS_ContractAPIAmqp_07_010: [If requestData.getOperationId() is null or empty, this method shall throw ProvisioningDeviceClientException.]
    String operationId = requestData.getOperationId();
    if (operationId == null || operationId.isEmpty()) {
        throw new ProvisioningDeviceClientException(new IllegalArgumentException("operationId cannot be null or empty"));
    }
    // SRS_ContractAPIAmqp_07_011: [If responseCallback is null, this method shall throw ProvisioningDeviceClientException.]
    if (responseCallback == null) {
        throw new ProvisioningDeviceClientException("responseCallback cannot be null");
    }
    // SRS_ContractAPIAmqp_07_012: [If amqpConnection is null or not connected, this method shall throw ProvisioningDeviceConnectionException.]
    try {
        if (!this.provisioningAmqpOperations.isAmqpConnected()) {
            throw new ProvisioningDeviceConnectionException("Amqp is not connected");
        }
    } catch (Exception e) {
        throw new ProvisioningDeviceClientException(e);
    }
    // SRS_ContractAPIAmqp_07_013: [This method shall send an AMQP message with the property of iotdps-get-operationstatus and the OperationId.]
    this.provisioningAmqpOperations.sendStatusMessage(operationId, responseCallback, callbackContext);
}
Also used : ProvisioningDeviceConnectionException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceConnectionException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException) ProvisioningDeviceConnectionException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceConnectionException) ProvisioningDeviceTransportException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceTransportException) ProvisioningDeviceHubException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceHubException) IOException(java.io.IOException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Aggregations

ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)2 ProvisioningDeviceHubException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceHubException)2 ProvisioningDeviceConnectionException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceConnectionException)1 ProvisioningDeviceTransportException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceTransportException)1 DeviceRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationResultParser)1 RegistrationOperationStatusParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser)1 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)1 IOException (java.io.IOException)1