class PackageVersionChecker {

    public CheckResult compareImageAndInstancesMandatoryPackageVersion(StatedImage newImage, Set<InstanceMetaData> instanceMetaDataSet) {
        Map<String, String> packageVersions = newImage.getImage().getPackageVersions();
        List<String> packagesToCompare;
        packagesToCompare = newImage.getImage().isPrewarmed()
                ? instanceMetadataUpdater.getPackages().stream().map(Package::getName).collect(Collectors.toList())
                : instanceMetadataUpdater.getPackages().stream().filter(pkg -> !pkg.isPrewarmed()).map(Package::getName).collect(Collectors.toList());

        List<String> missingPackageVersion = new ArrayList<>();
        List<String> differentPackageVersion = new ArrayList<>();
        Map<String, String> instancePackageVersions;
        try {
            instancePackageVersions = instanceMetaDataSet.stream()
                    .findFirst().orElseThrow(() -> new NoSuchElementException("Package version is missing"))
                    .getImage().get(Image.class).getPackageVersions();
        } catch (IOException e) {
            LOGGER.debug("Could not get image", e);
            return CheckResult.failed("Could not get image");
        }

        for (String packageToCompare : packagesToCompare) {
            if (!SALT_BOOTSTRAP.equals(packageToCompare) && !SALT.equals(packageToCompare)) {
                String packageVersionInImage = packageVersions.get(packageToCompare);
                if (StringUtils.isBlank(packageVersionInImage)) {
                    LOGGER.debug("Missing package in image: " + packageToCompare);
                    missingPackageVersion.add(packageToCompare);
                } else if (!instanceMetadataUpdater.isPackagesVersionEqual(packageVersionInImage, instancePackageVersions.get(packageToCompare))) {
                    LOGGER.debug(String.format("Different package [%s] version on image [%s] and on instance [%s]",
                            packageToCompare, packageVersionInImage, instancePackageVersions.get(packageToCompare)));
                    differentPackageVersion.add(packageToCompare);
                }
            }
        }

        if (!missingPackageVersion.isEmpty() || !differentPackageVersion.isEmpty()) {
            String message = messagesService.getMessage(Msg.PACKAGES_ARE_DIFFERENT_IN_IMAGE.code(),
                    Lists.newArrayList(StringUtils.join(missingPackageVersion, ","), StringUtils.join(differentPackageVersion, ",")));
            return CheckResult.failed(message);
        }
        return CheckResult.ok();
    }

}