Search in sources :

Example 1 with GreengrassV2DataException

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) ComponentRequirementIdentifier(com.aws.greengrass.componentmanager.models.ComponentRequirementIdentifier) GreengrassV2DataException(software.amazon.awssdk.services.greengrassv2data.model.GreengrassV2DataException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) GreengrassV2DataException(software.amazon.awssdk.services.greengrassv2data.model.GreengrassV2DataException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DeploymentTaskFailureException(com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException) Requirement(com.vdurmont.semver4j.Requirement) DeploymentTaskFailureException(com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException)

Aggregations

PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentRequirementIdentifier (com.aws.greengrass.componentmanager.models.ComponentRequirementIdentifier)1 Topics (com.aws.greengrass.config.Topics)1 DeploymentTaskFailureException (com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException)1 Requirement (com.vdurmont.semver4j.Requirement)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 GreengrassV2DataException (software.amazon.awssdk.services.greengrassv2data.model.GreengrassV2DataException)1