public static void validateRootLocation(
      final UnitConfig newRootLocation,
      final ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder> entryMap,
      final ConsistencyHandler consistencyHandler)
      throws CouldNotPerformException, EntryModification {
    try {
      boolean modified = false;
      // detect root location
      IdentifiableMessage<String, UnitConfig, UnitConfig.Builder> detectedRootLocationConfigEntry =
          entryMap.get(newRootLocation.getId());
      UnitConfig.Builder detectedRootLocationConfigBuilder =
          detectedRootLocationConfigEntry.getMessage().toBuilder();

      // verify if root flag is set.
      if (!detectedRootLocationConfigBuilder.getLocationConfig().hasRoot()
          || !detectedRootLocationConfigBuilder.getLocationConfig().getRoot()) {
        detectedRootLocationConfigBuilder.getLocationConfigBuilder().setRoot(true);
        modified = true;
      }

      // verify if placement field is set.
      if (!detectedRootLocationConfigBuilder.hasPlacementConfig()) {
        detectedRootLocationConfigBuilder.setPlacementConfig(
            PlacementConfigType.PlacementConfig.newBuilder());
        modified = true;
      }

      // verify if placement location id is set.
      if (!detectedRootLocationConfigBuilder.getPlacementConfig().hasLocationId()
          || !detectedRootLocationConfigBuilder
              .getPlacementConfig()
              .getLocationId()
              .equals(detectedRootLocationConfigBuilder.getId())) {
        detectedRootLocationConfigBuilder
            .getPlacementConfigBuilder()
            .setLocationId(detectedRootLocationConfigBuilder.getId());
        modified = true;
      }

      if (modified) {
        throw new EntryModification(
            detectedRootLocationConfigEntry.setMessage(detectedRootLocationConfigBuilder),
            consistencyHandler);
      }
    } catch (CouldNotPerformException ex) {
      throw new CouldNotPerformException("Could not validate root location!", ex);
    }
  }
  public static UnitConfig computeNewRootLocation(
      final UnitConfig currentLocationConfig,
      ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder> entryMap)
      throws CouldNotPerformException {
    try {
      HashMap<String, UnitConfig> rootLocationConfigList = new HashMap<>();
      for (UnitConfig locationConfig : entryMap.getMessages()) {
        rootLocationConfigList.put(locationConfig.getId(), locationConfig);
      }

      rootLocationConfigList.put(currentLocationConfig.getId(), currentLocationConfig);

      if (rootLocationConfigList.size() == 1) {
        return rootLocationConfigList.values().stream().findFirst().get();
      }

      for (UnitConfig locationConfig : new ArrayList<>(rootLocationConfigList.values())) {
        if (!locationConfig.hasPlacementConfig()) {
        } else if (!locationConfig.getPlacementConfig().hasLocationId()) {
        } else if (locationConfig.getPlacementConfig().getLocationId().isEmpty()) {
        } else if (locationConfig
            .getPlacementConfig()
            .getLocationId()
            .equals(locationConfig.getId())) {
          return locationConfig;
        } else {
          rootLocationConfigList.remove(locationConfig.getId());
        }
      }

      if (rootLocationConfigList.isEmpty()) {
        throw new NotAvailableException("root candidate");
      } else if (rootLocationConfigList.size() == 1) {
        return rootLocationConfigList.values().stream().findFirst().get();
      }

      throw new InvalidStateException("To many potential root locations detected!");

    } catch (CouldNotPerformException ex) {
      throw new CouldNotPerformException("Could not compute root location!", ex);
    }
  }
 public static UnitConfig getRootLocation(
     final ProtoBufMessageMap<String, UnitConfig, UnitConfig.Builder> entryMap)
     throws NotAvailableException, CouldNotPerformException {
   return getRootLocation(entryMap.getMessages());
 }