use of software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest in project aws-greengrass-nucleus by aws-greengrass.
the class ShadowDeploymentE2ETest method GIVEN_device_deployment_WHEN_shadow_update_messages_gets_delivered_out_of_order_THEN_shadow_updated_with_latest_deployment_status.
@Test
void GIVEN_device_deployment_WHEN_shadow_update_messages_gets_delivered_out_of_order_THEN_shadow_updated_with_latest_deployment_status() throws Exception {
CreateDeploymentRequest createDeploymentRequest = CreateDeploymentRequest.builder().targetArn(thingInfo.getThingArn()).components(Utils.immutableMap("CustomerApp", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build(), "SomeService", ComponentDeploymentSpecification.builder().componentVersion("1.0.0").build())).build();
draftAndCreateDeployment(createDeploymentRequest);
assertThat(kernel.getMain()::getState, eventuallyEval(is(State.FINISHED)));
IotShadowClient shadowClient = new IotShadowClient(new WrapperMqttClientConnection(kernel.getContext().get(MqttClient.class)));
UpdateNamedShadowSubscriptionRequest req = new UpdateNamedShadowSubscriptionRequest();
req.shadowName = DEPLOYMENT_SHADOW_NAME;
req.thingName = thingInfo.getThingName();
CountDownLatch reportSucceededCdl = new CountDownLatch(1);
CountDownLatch deviceSyncedStateToSucceededCdl = new CountDownLatch(1);
AtomicReference<HashMap<String, Object>> reportedSection = new AtomicReference<>();
AtomicReference<Integer> shadowVersionWhenDeviceFirstReportedSuccess = new AtomicReference<>();
AtomicReference<Integer> shadowVersionWhenDeviceReportedInProgress = new AtomicReference<>();
shadowClient.SubscribeToUpdateNamedShadowAccepted(req, QualityOfService.AT_LEAST_ONCE, (response) -> {
try {
logger.info("Got shadow update: {}", new ObjectMapper().writeValueAsString(response));
} catch (JsonProcessingException e) {
// ignore
}
if (response.state.reported == null) {
return;
}
String reportedStatus = (String) response.state.reported.get(STATUS_KEY);
if (JobStatus.IN_PROGRESS.toString().equals(reportedStatus)) {
reportedSection.set(response.state.reported);
shadowVersionWhenDeviceReportedInProgress.set(response.version);
} else if (JobStatus.SUCCEEDED.toString().equals(reportedStatus)) {
// state to SUCCESS second time the shadow version
if (reportSucceededCdl.getCount() == 0 && response.version > shadowVersionWhenDeviceFirstReportedSuccess.get()) {
deviceSyncedStateToSucceededCdl.countDown();
}
shadowVersionWhenDeviceFirstReportedSuccess.set(response.version);
reportSucceededCdl.countDown();
}
});
// waiting for the device to report success
assertTrue(reportSucceededCdl.await(60, TimeUnit.SECONDS));
// Updating the shadow with deployment status IN_PROGRESS to simulate out-of-order update of shadow
ShadowState shadowState = new ShadowState();
shadowState.reported = reportedSection.get();
UpdateNamedShadowRequest updateNamedShadowRequest = new UpdateNamedShadowRequest();
updateNamedShadowRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
updateNamedShadowRequest.thingName = thingInfo.getThingName();
updateNamedShadowRequest.state = shadowState;
shadowClient.PublishUpdateNamedShadow(updateNamedShadowRequest, QualityOfService.AT_LEAST_ONCE).get(30, TimeUnit.SECONDS);
// verify that the device updates shadow state to SUCCEEDED
assertTrue(deviceSyncedStateToSucceededCdl.await(60, TimeUnit.SECONDS));
// Updating the shadow with a lower version number to trigger a message to /update/rejected event
shadowState = new ShadowState();
shadowState.reported = reportedSection.get();
updateNamedShadowRequest = new UpdateNamedShadowRequest();
updateNamedShadowRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
updateNamedShadowRequest.thingName = thingInfo.getThingName();
updateNamedShadowRequest.state = shadowState;
updateNamedShadowRequest.version = shadowVersionWhenDeviceReportedInProgress.get();
shadowClient.PublishUpdateNamedShadow(updateNamedShadowRequest, QualityOfService.AT_LEAST_ONCE).get(30, TimeUnit.SECONDS);
CountDownLatch deviceRetrievedShadowCdl = new CountDownLatch(1);
GetNamedShadowSubscriptionRequest getNamedShadowSubscriptionRequest = new GetNamedShadowSubscriptionRequest();
getNamedShadowSubscriptionRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
getNamedShadowSubscriptionRequest.thingName = thingInfo.getThingName();
shadowClient.SubscribeToGetNamedShadowAccepted(getNamedShadowSubscriptionRequest, QualityOfService.AT_MOST_ONCE, getShadowResponse -> {
deviceRetrievedShadowCdl.countDown();
}).get(30, TimeUnit.SECONDS);
// verify that the device retrieved the shadow when an update operation was rejected.
assertTrue(deviceRetrievedShadowCdl.await(60, TimeUnit.SECONDS));
}
use of software.amazon.awssdk.iot.iotshadow.model.UpdateNamedShadowRequest in project aws-greengrass-nucleus by aws-greengrass.
the class ShadowDeploymentListener method updateReportedSectionOfShadowWithDeploymentStatus.
private boolean updateReportedSectionOfShadowWithDeploymentStatus() {
Map<String, Object> deploymentDetails = lastDeploymentStatus.get();
try {
ShadowState shadowState = new ShadowState();
shadowState.reported = populateReportedSectionOfShadow(deploymentDetails);
UpdateNamedShadowRequest updateNamedShadowRequest = new UpdateNamedShadowRequest();
updateNamedShadowRequest.shadowName = DEPLOYMENT_SHADOW_NAME;
updateNamedShadowRequest.thingName = thingName;
updateNamedShadowRequest.state = shadowState;
updateNamedShadowRequest.version = lastVersion.get();
iotShadowClient.PublishUpdateNamedShadow(updateNamedShadowRequest, QualityOfService.AT_LEAST_ONCE).get(TIMEOUT_FOR_PUBLISHING_TO_TOPICS_SECONDS, TimeUnit.SECONDS);
logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, deploymentDetails.get(DEPLOYMENT_ID_KEY_NAME)).kv(STATUS_KEY, shadowState.reported.get(STATUS_KEY)).log("Updated reported state for deployment");
return true;
} catch (InterruptedException e) {
// Since this method can run as runnable cannot throw exception so handling exceptions here
logger.atWarn().log("Interrupted while publishing reported state");
} catch (ExecutionException e) {
logger.atError().setCause(e).log("Caught exception while publishing reported state");
} catch (TimeoutException e) {
logger.atWarn().setCause(e).log("Publish reported state timed out, will retry shortly");
}
return false;
}
Aggregations