Remove UNRESERVED as a reservation type

This is a follow-up to Lai's refactoring of the get reservation types
code to return a set rather than a single type. Since we're always
returning a set now, the more natural way to represent a label that is
not reserved is to return an empty set rather than a set containing
UNRESERVED.

Also fixes some minor style issues I ran across regarding static
importing and test method naming that I ran across (no logic
implications).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=151132116
This commit is contained in:
mcilwain 2017-03-24 09:09:06 -07:00 committed by Ben McIlwain
parent 4260fb573f
commit b03bd3b525
12 changed files with 83 additions and 90 deletions

View file

@ -14,8 +14,6 @@
package google.registry.export;
import static google.registry.model.registry.label.ReservationType.UNRESERVED;
import com.google.common.base.Joiner;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig.Config;
@ -45,9 +43,7 @@ public final class ExportUtils {
ReservedList reservedList = ReservedList.load(key).get();
if (reservedList.getShouldPublish()) {
for (ReservedListEntry entry : reservedList.getReservedListEntries().values()) {
if (entry.getValue() != UNRESERVED) {
reservedTerms.add(entry.getLabel());
}
reservedTerms.add(entry.getLabel());
}
}
}

View file

@ -24,10 +24,10 @@ import static google.registry.flows.domain.DomainFlowUtils.validateDomainNameWit
import static google.registry.flows.domain.DomainFlowUtils.verifyNotInPredelegation;
import static google.registry.model.EppResourceUtils.checkResourcesExist;
import static google.registry.model.index.DomainApplicationIndex.loadActiveApplicationsByDomainName;
import static google.registry.model.registry.label.ReservationType.UNRESERVED;
import static google.registry.model.registry.label.ReservationType.getTypeOfHighestSeverity;
import static google.registry.pricing.PricingEngineProxy.isDomainPremium;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
@ -150,8 +150,8 @@ public final class DomainCheckFlow implements Flow {
Set<String> existingIds = checkResourcesExist(DomainResource.class, targetIds, now);
ImmutableList.Builder<DomainCheck> checks = new ImmutableList.Builder<>();
for (String targetId : targetIds) {
String message = getMessageForCheck(domainNames.get(targetId), existingIds, now);
checks.add(DomainCheck.create(message == null, targetId, message));
Optional<String> message = getMessageForCheck(domainNames.get(targetId), existingIds, now);
checks.add(DomainCheck.create(!message.isPresent(), targetId, message.orNull()));
}
BeforeResponseReturnData responseData =
customLogic.beforeResponse(
@ -166,10 +166,10 @@ public final class DomainCheckFlow implements Flow {
.build();
}
private String getMessageForCheck(
private Optional<String> getMessageForCheck(
InternetDomainName domainName, Set<String> existingIds, DateTime now) {
if (existingIds.contains(domainName.toString())) {
return "In use";
return Optional.of("In use");
}
Registry registry = Registry.get(domainName.parent().toString());
if (PENDING_ALLOCATION_TLD_STATES.contains(registry.getTldState(now))
@ -179,17 +179,19 @@ public final class DomainCheckFlow implements Flow {
public boolean apply(DomainApplication input) {
return !input.getApplicationStatus().isFinalStatus();
}})) {
return "Pending allocation";
return Optional.of("Pending allocation");
}
ImmutableSet<ReservationType> reservationTypes = getReservationTypes(domainName);
if (reservationTypes.equals(ImmutableSet.of(UNRESERVED))
if (reservationTypes.isEmpty()
&& isDomainPremium(domainName.toString(), now)
&& registry.getPremiumPriceAckRequired()
&& eppInput.getSingleExtension(FeeCheckCommandExtension.class) == null) {
return "Premium names require EPP ext.";
return Optional.of("Premium names require EPP ext.");
}
return getTypeOfHighestSeverity(reservationTypes).getMessageForCheck();
return reservationTypes.isEmpty()
? Optional.<String>absent()
: Optional.of(getTypeOfHighestSeverity(reservationTypes).getMessageForCheck());
}
/** Handle the fee check extension. */

View file

@ -26,7 +26,9 @@ import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.domain.DomainResource.MAX_REGISTRATION_YEARS;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED;
import static google.registry.model.registry.label.ReservationType.NAMESERVER_RESTRICTED;
import static google.registry.model.registry.label.ReservationType.RESERVED_FOR_ANCHOR_TENANT;
import static google.registry.model.registry.label.ReservedList.getAllowedNameservers;
import static google.registry.pricing.PricingEngineProxy.isDomainPremium;
import static google.registry.tldconfig.idn.IdnLabelValidator.findValidIdnTableForTld;
@ -391,8 +393,8 @@ public class DomainFlowUtils {
private static boolean isReserved(InternetDomainName domainName, boolean isSunrise) {
ImmutableSet<ReservationType> types = getReservationTypes(domainName);
return types.contains(ReservationType.FULLY_BLOCKED)
|| types.contains(ReservationType.RESERVED_FOR_ANCHOR_TENANT)
return types.contains(FULLY_BLOCKED)
|| types.contains(RESERVED_FOR_ANCHOR_TENANT)
|| !(isSunrise || intersection(TYPES_ALLOWED_FOR_CREATE_ONLY_IN_SUNRISE, types).isEmpty());
}

View file

@ -17,6 +17,7 @@ package google.registry.model.eppoutput;
import com.google.common.collect.ImmutableList;
import google.registry.model.ImmutableObject;
import google.registry.model.eppoutput.EppResponse.ResponseData;
import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
@ -124,7 +125,7 @@ public abstract class CheckData extends ImmutableObject implements ResponseData
/** A version with domain namespacing. */
@XmlType(namespace = "urn:ietf:params:xml:ns:domain-1.0")
public static class DomainCheck extends Check {
public static DomainCheck create(boolean avail, String name, String reason) {
public static DomainCheck create(boolean avail, String name, @Nullable String reason) {
return init(new DomainCheck(), CheckName.create(avail, name), reason);
}

View file

@ -166,8 +166,7 @@ class DomainLabelMetrics {
String mostSevereReservedList =
matches.isEmpty() ? "(none)" : mostSevereMatch.reservedListName();
String mostSevereReservationType =
(matches.isEmpty() ? ReservationType.UNRESERVED : mostSevereMatch.reservationType())
.toString();
(matches.isEmpty() ? "(none)" : mostSevereMatch.reservationType()).toString();
reservedListChecks.increment(
tld, matchCount, mostSevereReservedList, mostSevereReservationType);
reservedListProcessingTime.record(

View file

@ -14,8 +14,11 @@
package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.collect.Ordering;
import java.util.Set;
import javax.annotation.Nullable;
@ -27,13 +30,12 @@ public enum ReservationType {
// label has multiple reservation types, its message is the that of the one with the highest
// severity.
UNRESERVED(null, 0),
NAMESERVER_RESTRICTED("Nameserver restricted", 1),
ALLOWED_IN_SUNRISE("Reserved for non-sunrise", 2),
MISTAKEN_PREMIUM("Reserved", 3),
RESERVED_FOR_ANCHOR_TENANT("Reserved", 4),
NAME_COLLISION("Cannot be delegated", 5),
FULLY_BLOCKED("Reserved", 6);
NAMESERVER_RESTRICTED("Nameserver restricted", 0),
ALLOWED_IN_SUNRISE("Reserved for non-sunrise", 1),
MISTAKEN_PREMIUM("Reserved", 2),
RESERVED_FOR_ANCHOR_TENANT("Reserved", 3),
NAME_COLLISION("Cannot be delegated", 4),
FULLY_BLOCKED("Reserved", 5);
@Nullable
private final String messageForCheck;
@ -48,20 +50,23 @@ public enum ReservationType {
return messageForCheck;
}
private static final Ordering<ReservationType> ORDERING = new Ordering<ReservationType>() {
@Override
public int compare(ReservationType left, ReservationType right) {
return Integer.compare(left.ordinal(), right.ordinal());
}
};
/**
* Returns the {@code ReservationType} with the highest severity, used when a label has multiple
* reservation types and a reservation message is needed.
*
* @param types the set of reservation types that a label is associated with.
* @return the reservation type with the highest severity.
*/
public static ReservationType getTypeOfHighestSeverity(Set<ReservationType> types) {
ReservationType mostSevereType = UNRESERVED;
for (ReservationType type : types) {
if (type.compareTo(mostSevereType) > 0) {
mostSevereType = type;
}
}
return mostSevereType;
checkArgumentNotNull(types, "types must not be null");
checkArgument(!types.isEmpty(), "types must not be empty");
return ORDERING.max(types);
}
}

View file

@ -25,7 +25,6 @@ import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION;
import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED;
import static google.registry.model.registry.label.ReservationType.NAMESERVER_RESTRICTED;
import static google.registry.model.registry.label.ReservationType.RESERVED_FOR_ANCHOR_TENANT;
import static google.registry.model.registry.label.ReservationType.UNRESERVED;
import static google.registry.util.CollectionUtils.nullToEmpty;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.time.DateTimeZone.UTC;
@ -37,9 +36,9 @@ import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.net.InternetDomainName;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.googlecode.objectify.Key;
@ -211,27 +210,25 @@ public final class ReservedList
}
/**
* Queries the set of all reserved lists associated with the specified tld and returns the
* reservation types of the label. If the label is in none of the lists, it returns a set that
* contains UNRESERVED.
* Queries the set of all reserved lists associated with the specified TLD and returns the
* reservation types of the label.
*
* <p>If the label is in none of the lists, it returns an empty set.
*/
public static ImmutableSet<ReservationType> getReservationTypes(String label, String tld) {
checkNotNull(label, "label");
if (label.length() == 0) {
return ImmutableSet.of(FULLY_BLOCKED);
}
ImmutableSet<ReservedListEntry> entries = getReservedListEntries(label, tld);
return entries.isEmpty()
? ImmutableSet.of(UNRESERVED)
: ImmutableSet.copyOf(
Iterables.transform(
entries,
new Function<ReservedListEntry, ReservationType>() {
@Override
public ReservationType apply(ReservedListEntry reservedListEntry) {
return reservedListEntry.reservationType;
}
}));
return FluentIterable.from(getReservedListEntries(label, tld))
.transform(
new Function<ReservedListEntry, ReservationType>() {
@Override
public ReservationType apply(ReservedListEntry reservedListEntry) {
return reservedListEntry.reservationType;
}
})
.toSet();
}
/**