use of com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationResultParser 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);
}
Aggregations