class LocalBaseImageSteps {

  @VisibleForTesting
  static Optional<LocalImage> getCachedDockerImage(
      Cache cache, DockerImageDetails dockerImageDetails)
      throws DigestException, IOException, CacheCorruptedException {
    // Get config
    Optional<ContainerConfigurationTemplate> cachedConfig =
        cache.retrieveLocalConfig(dockerImageDetails.getImageId());
    if (!cachedConfig.isPresent()) {
      return Optional.empty();
    }

    // Get layers
    List<Future<PreparedLayer>> cachedLayers = new ArrayList<>();
    for (DescriptorDigest diffId : dockerImageDetails.getDiffIds()) {
      Optional<CachedLayer> cachedLayer = cache.retrieveTarLayer(diffId);
      if (!cachedLayer.isPresent()) {
        return Optional.empty();
      }
      CachedLayer layer = cachedLayer.get();
      cachedLayers.add(Futures.immediateFuture(new PreparedLayer.Builder(layer).build()));
    }

    return Optional.of(new LocalImage(cachedLayers, cachedConfig.get()));
  }

}