package com.mesosphere.sdk.config.validate;

import com.mesosphere.sdk.specification.ServiceSpec;

import java.util.*;

/**
 * Configuration validator which validates that a {@link com.mesosphere.sdk.specification.ServiceSpec}'s region
 * cannot change between deployments.
 *
 * This is to prevent a pod deployed in one region from being deployed in another region if it is replaced, since we
 * do not support multi-region deployments. Since region awareness is implemented with placement constraints,
 * a pod replace would otherwise break this assumption.
 */
public class RegionCannotChange implements ConfigValidator<ServiceSpec> {

    @Override
    public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
        if (!oldConfig.isPresent()) {
            return Collections.emptyList();
        }

        List<ConfigValidationError> errors = new ArrayList<>();
        boolean regionWasAdded = oldConfig.isPresent() && !oldConfig.get().getRegion().isPresent() &&
                newConfig.getRegion().isPresent();
        boolean regionWasUnset = oldConfig.isPresent() && oldConfig.get().getRegion().isPresent() &&
                !newConfig.getRegion().isPresent();
        boolean regionWasChanged = oldConfig.isPresent() && oldConfig.get().getRegion().isPresent() &&
                newConfig.getRegion().isPresent() &&
                !newConfig.getRegion().get().equals(oldConfig.get().getRegion().get());
        if (regionWasAdded || regionWasUnset || regionWasChanged) {
            errors.add(ConfigValidationError.transitionError(
                    "region",
                    oldConfig.get().getUser(),
                    newConfig.getUser(),
                    "Region for old service must remain the same across deployments."
            ));
        }

        return errors;
    }
}


@@ -927,504 +927,38 @@
     
-boolean regionWasAdded = oldConfig.isPresent() && !oldConfig.get().getRegion().isPresent() &&%0A                newConfig.getRegion().isPresent();%0A        boolean regionWasUnset = oldConfig.isPresent() && oldConfig.get().getRegion().isPresent() &&%0A                !newConfig.getRegion().isPresent();%0A        boolean regionWasChanged = oldConfig.isPresent() && oldConfig.get().getRegion().isPresent() &&%0A                newConfig.getRegion().isPresent() &&%0A                !newConfig.getRegion().get
+if (!oldConfig.get().getRegion
 ().e
@@ -963,32 +963,26 @@
 .equals(
-old
+new
 Config.
-get().
 getRegio
@@ -988,81 +988,9 @@
 on()
-.get());%0A        if (regionWasAdded %7C%7C regionWasUnset %7C%7C regionWasChanged
+)
 ) %7B%0A
@@ -1116,28 +1116,41 @@
 ig.get().get
-User
+Region().toString
 (),%0A        
@@ -1178,12 +1178,25 @@
 .get
-User
+Region().toString
 (),%0A

Clean up logic in region matching and report region mismatch correctly (#2399)