use of software.amazon.awssdk.services.greengrassv2data.model.GreengrassV2DataException in project aws-greengrass-nucleus by aws-greengrass.
the class DefaultDeploymentTask method getNonTargetGroupToRootPackagesMap.
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private Map<String, Set<ComponentRequirementIdentifier>> getNonTargetGroupToRootPackagesMap(DeploymentDocument deploymentDocument) throws DeploymentTaskFailureException, InterruptedException {
// Don't block local deployments due to device being offline by using finite retries for getting the
// hierarchy and fall back to hierarchy stored previously in worst case. For cloud deployment, use infinite
// retries by default similar/to all other cloud interactions.
boolean isLocalDeployment = Deployment.DeploymentType.LOCAL.equals(deployment.getDeploymentType());
// SDK already retries with RetryMode.STANDARD. For local deployment we don't retry on top of that
int retryCount = isLocalDeployment ? 1 : INFINITE_RETRY_COUNT;
Optional<Set<String>> groupsForDeviceOpt;
try {
groupsForDeviceOpt = thingGroupHelper.listThingGroupsForDevice(retryCount);
} catch (GreengrassV2DataException e) {
if (e.statusCode() == HttpStatusCode.FORBIDDEN) {
// Getting group hierarchy requires permission to call the ListThingGroupsForCoreDevice API which
// may not be configured on existing IoT Thing policy in use for current device, log a warning in
// that case and move on.
logger.atWarn().setCause(e).log("Failed to get thing group hierarchy. Deployment will proceed. " + "To automatically clean up unused components, please add " + "greengrass:ListThingGroupsForCoreDevice permission to your IoT Thing policy.");
groupsForDeviceOpt = getPersistedMembershipInfo();
} else {
throw new DeploymentTaskFailureException("Error fetching thing group information", e);
}
} catch (Exception e) {
if (isLocalDeployment && ThingGroupHelper.DEVICE_OFFLINE_INDICATIVE_EXCEPTIONS.contains(e.getClass())) {
logger.atWarn().setCause(e).log("Failed to get thing group hierarchy, local deployment will proceed");
groupsForDeviceOpt = getPersistedMembershipInfo();
} else {
throw new DeploymentTaskFailureException("Error fetching thing group information", e);
}
}
Set<String> groupsForDevice = groupsForDeviceOpt.isPresent() ? groupsForDeviceOpt.get() : Collections.emptySet();
Map<String, Set<ComponentRequirementIdentifier>> nonTargetGroupsToRootPackagesMap = new HashMap<>();
Topics groupsToRootPackages = deploymentServiceConfig.lookupTopics(DeploymentService.GROUP_TO_ROOT_COMPONENTS_TOPICS);
groupsToRootPackages.iterator().forEachRemaining(node -> {
Topics groupTopics = (Topics) node;
// skip root packages if device does not belong to that group anymore
if (!groupTopics.getName().equals(deploymentDocument.getGroupName()) && (groupTopics.getName().startsWith(DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX) || groupTopics.getName().equals(LOCAL_DEPLOYMENT_GROUP_NAME) || groupsForDevice.contains(groupTopics.getName()))) {
groupTopics.forEach(pkgNode -> {
Topics pkgTopics = (Topics) pkgNode;
Requirement versionReq = Requirement.buildNPM(Coerce.toString(pkgTopics.lookup(GROUP_TO_ROOT_COMPONENTS_VERSION_KEY)));
nonTargetGroupsToRootPackagesMap.putIfAbsent(groupTopics.getName(), new HashSet<>());
nonTargetGroupsToRootPackagesMap.get(groupTopics.getName()).add(new ComponentRequirementIdentifier(pkgTopics.getName(), versionReq));
});
}
});
deploymentServiceConfig.lookupTopics(DeploymentService.GROUP_MEMBERSHIP_TOPICS).remove();
Topics groupMembership = deploymentServiceConfig.lookupTopics(DeploymentService.GROUP_MEMBERSHIP_TOPICS);
groupsForDevice.forEach(groupMembership::createLeafChild);
return nonTargetGroupsToRootPackagesMap;
}
Aggregations