Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import google.registry.model.common.FeatureFlag.FeatureStatus;
import google.registry.model.common.TimedTransitionProperty;
import google.registry.model.domain.token.AllocationToken;
import google.registry.model.tld.Tld;
import google.registry.model.tld.Tld.ExpiryAccessPeriodMode;
import google.registry.model.tld.Tld.TldState;
import google.registry.persistence.VKey;
Expand Down Expand Up @@ -340,7 +341,7 @@ public TimedTransitionProperty<TldState> deserialize(
JsonParser jp, DeserializationContext context) throws IOException {
SortedMap<String, String> valueMap =
jp.readValueAs(new TypeReference<SortedMap<String, String>>() {});
return TimedTransitionProperty.fromValueMap(
return Tld.createTldStateTransitions(
valueMap.keySet().stream()
.collect(
toImmutableSortedMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ private TimedTransitionProperty(ImmutableSortedMap<Instant, V> backingMap) {
this.backingMap = ImmutableSortedMap.copyOfSorted(backingMap);
}

/** Returns an empty {@link TimedTransitionProperty}. */
public static <V extends Serializable> TimedTransitionProperty<V> forEmptyMap() {
return new TimedTransitionProperty<>(ImmutableSortedMap.of());
}

/**
* Returns a {@link TimedTransitionProperty} that starts with the given value at {@code
* START_INSTANT}.
Expand All @@ -99,10 +94,23 @@ public static <V extends Serializable> TimedTransitionProperty<V> fromValueMap(

/**
* Returns a {@link TimedTransitionProperty} that contains the transition values and times defined
* in the given map.
* in the given map, validated against a set of allowed transitions.
*
* <p>The map must start at {@code START_INSTANT}.
*/
public static <V extends Serializable> TimedTransitionProperty<V> make(
ImmutableSortedMap<Instant, V> valueMap,
ImmutableMultimap<V, V> allowedTransitions,
String mapName) {
validateTimedTransitionMap(valueMap, allowedTransitions, mapName);
return fromValueMap(valueMap);
}

/**
* Returns a {@link TimedTransitionProperty} that contains the transition values and times defined
* in the given map, validated against a set of allowed transitions and a required initial value.
*
* <p>The map must contain a value for {@code START_INSTANT}. The map is also validated against a
* set of allowed transitions.
* <p>The map must start at {@code START_INSTANT} with {@code initialValue}.
*/
public static <V extends Serializable> TimedTransitionProperty<V> make(
ImmutableSortedMap<Instant, V> valueMap,
Expand All @@ -124,7 +132,9 @@ public static <V extends Serializable> void validateTimedTransitionMap(
Ordering.natural().equals(valueMap.comparator()),
"Timed transition value map must have transition time keys in chronological order");
checkArgument(
valueMap.firstKey().equals(START_INSTANT), "%s map must start at START_INSTANT.", mapName);
!valueMap.isEmpty() && valueMap.firstKey().equals(START_INSTANT),
"%s map must start at START_INSTANT.",
mapName);

V lastValue = null;
for (V value : valueMap.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,22 @@ public class AllocationToken extends UpdateAutoTimestampEntity implements Builda
private static final ImmutableMap<String, TokenBehavior> STATIC_TOKEN_BEHAVIORS =
ImmutableMap.of(REMOVE_BULK_PRICING, TokenBehavior.REMOVE_BULK_PRICING);

// Promotions should only move forward, and ENDED / CANCELLED are terminal states.
private static final ImmutableMultimap<TokenStatus, TokenStatus> VALID_TOKEN_STATUS_TRANSITIONS =
/**
* Valid transitions between {@link TokenStatus} values. Promotions should only move forward, and
* {@link TokenStatus#ENDED} and {@link TokenStatus#CANCELLED} are terminal states.
*/
public static final ImmutableMultimap<TokenStatus, TokenStatus> VALID_TOKEN_STATUS_TRANSITIONS =
ImmutableMultimap.<TokenStatus, TokenStatus>builder()
.putAll(NOT_STARTED, VALID, CANCELLED)
.putAll(VALID, ENDED, CANCELLED)
.build();

/** Returns true if {@code status} is a terminal status with no valid outgoing transitions. */
public static boolean isTerminalStatus(TokenStatus status) {
checkArgumentNotNull(status, "TokenStatus cannot be null");
return VALID_TOKEN_STATUS_TRANSITIONS.get(status).isEmpty();
}

private static final ImmutableMap<String, AllocationToken> BEHAVIORAL_TOKENS =
ImmutableMap.of(
REMOVE_BULK_PRICING,
Expand Down Expand Up @@ -491,7 +500,7 @@ public AllocationToken build() {
getInstance().renewalPriceBehavior.equals(RenewalPriceBehavior.SPECIFIED),
"BULK_PRICING tokens must have renewalPriceBehavior set to SPECIFIED");
checkArgument(
getInstance().renewalPrice.getAmount().intValue() == 0,
getInstance().renewalPrice.isZero(),
"BULK_PRICING tokens must have a renewal price of 0");
checkArgument(
ImmutableSet.of(CommandName.CREATE).equals(getInstance().allowedEppActions),
Expand Down
147 changes: 112 additions & 35 deletions core/src/main/java/google/registry/model/tld/Tld.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.net.InternetDomainName;
import google.registry.model.Buildable;
Expand Down Expand Up @@ -96,7 +96,6 @@
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.hibernate.annotations.Type;
import org.joda.money.CurrencyUnit;
Expand Down Expand Up @@ -170,8 +169,8 @@ public enum TldType {
}

/**
* The states a TLD can be in at any given point in time. The ordering below is the required
* sequence of states (ignoring {@link #PDT} which is a pseudo-state).
* The states a TLD can be in at any given point in time. Valid state transitions are enforced by
* {@link #VALID_TLD_STATE_TRANSITIONS}.
*/
public enum TldState {

Expand All @@ -186,8 +185,9 @@ public enum TldState {

/**
* A state in which no domain operations are permitted. Generally used between sunrise and
* general availability. This state is special in that it has no ordering constraints and can
* appear after any phase.
* general availability. This state may be entered from any phase and exit into
* non-predelegation phases, but cannot repeat consecutively or be used to transition backward
* across phases.
*/
QUIET_PERIOD,

Expand All @@ -201,6 +201,108 @@ public enum TldState {
PDT
}

/**
* Valid direct transitions between {@link TldState} values.
*
* <p>TLD launch phases progress strictly forward ({@code PREDELEGATION -> PDT ->
* START_DATE_SUNRISE -> GENERAL_AVAILABILITY}, skipping optional intermediate phases). {@link
* TldState#QUIET_PERIOD} may be entered from any phase and exit into non-predelegation phases,
* but cannot repeat consecutively or be used to transition backward across phases.
*/
public static final ImmutableMultimap<TldState, TldState> VALID_TLD_STATE_TRANSITIONS =
ImmutableMultimap.<TldState, TldState>builder()
.putAll(
TldState.PREDELEGATION,
TldState.PDT,
TldState.QUIET_PERIOD,
TldState.START_DATE_SUNRISE,
TldState.GENERAL_AVAILABILITY)
.putAll(
TldState.PDT,
TldState.QUIET_PERIOD,
TldState.START_DATE_SUNRISE,
TldState.GENERAL_AVAILABILITY)
.putAll(TldState.START_DATE_SUNRISE, TldState.QUIET_PERIOD, TldState.GENERAL_AVAILABILITY)
.putAll(
TldState.QUIET_PERIOD,
TldState.PDT,
TldState.START_DATE_SUNRISE,
TldState.GENERAL_AVAILABILITY)
.putAll(TldState.GENERAL_AVAILABILITY, TldState.QUIET_PERIOD)
.build();

/**
* Creates a validated {@link TimedTransitionProperty} for {@link TldState} transitions, enforcing
* {@link #VALID_TLD_STATE_TRANSITIONS} on both adjacent transitions and phase progression across
* intervening {@link TldState#QUIET_PERIOD} entries.
*/
public static TimedTransitionProperty<TldState> createTldStateTransitions(
ImmutableSortedMap<Instant, TldState> tldStatesMap) {
checkNotNull(tldStatesMap, "TLD states map cannot be null");
TimedTransitionProperty<TldState> transitions =
TimedTransitionProperty.make(
tldStatesMap, VALID_TLD_STATE_TRANSITIONS, "tldStateTransitions");
validateNonQuietPeriodProgression(tldStatesMap);
return transitions;
}

private static void validateTldStateTransitions(
ImmutableSortedMap<Instant, TldState> tldStatesMap) {
TimedTransitionProperty.validateTimedTransitionMap(
tldStatesMap, VALID_TLD_STATE_TRANSITIONS, "tldStateTransitions");
validateNonQuietPeriodProgression(tldStatesMap);
}

private static void validateNonQuietPeriodProgression(
ImmutableSortedMap<Instant, TldState> tldStatesMap) {
TldState previousPhase = null;
for (TldState state : tldStatesMap.values()) {
if (TldState.QUIET_PERIOD.equals(state)) {
continue;
}
if (previousPhase != null
&& !VALID_TLD_STATE_TRANSITIONS.containsEntry(previousPhase, state)) {
throw new IllegalArgumentException(
String.format(
"tldStateTransitions map cannot transition from %s to %s (even via QUIET_PERIOD).",
previousPhase, state));
}
previousPhase = state;
}
}

/**
* Validates that every token in {@code promoTokens} exists in the database, has type {@link
* TokenType#DEFAULT_PROMO}, and is allowed on {@code tldStr}.
*/
public static void validateDefaultPromoTokens(
String tldStr, @Nullable List<VKey<AllocationToken>> promoTokens) {
if (promoTokens == null || promoTokens.isEmpty()) {
return;
}
tm().reTransact(
() -> {
Map<VKey<? extends AllocationToken>, AllocationToken> tokensByKey =
tm().loadByKeysIfPresent(promoTokens);
for (VKey<AllocationToken> tokenKey : promoTokens) {
AllocationToken token = tokensByKey.get(tokenKey);
checkArgument(
token != null, "Default promo token %s does not exist", tokenKey.getKey());
checkArgument(
token.getTokenType().equals(TokenType.DEFAULT_PROMO),
"Token %s has an invalid token type of %s. DefaultPromoTokens must be of"
+ " the type DEFAULT_PROMO",
token.getToken(),
token.getTokenType());
checkArgument(
token.getAllowedTlds().isEmpty() || token.getAllowedTlds().contains(tldStr),
"The token %s is not valid for this TLD. The valid TLDs for it are %s",
token.getToken(),
token.getAllowedTlds());
}
});
}

/** The modes an Expiry Access Period (XAP) can be in at any given point in time. */
public enum ExpiryAccessPeriodMode {
DISABLED,
Expand Down Expand Up @@ -858,6 +960,7 @@ public void validateState() {
// here to catch cases where we loaded an invalid TimedTransitionProperty from the database
// and cloned it into a new builder, to block re-building a Tld in an invalid state.
tldStateTransitions.checkValidity();
validateTldStateTransitions(tldStateTransitions.toValueMap());
createBillingCostTransitions.checkValidity();
renewBillingCostTransitions.checkValidity();
eapFeeSchedule.checkValidity();
Expand Down Expand Up @@ -915,17 +1018,7 @@ public Builder setInvoicingEnabled(boolean invoicingEnabled) {

/** Sets the TLD state to transition to the specified states at the specified times. */
public Builder setTldStateTransitions(ImmutableSortedMap<Instant, TldState> tldStatesMap) {
checkNotNull(tldStatesMap, "TLD states map cannot be null");
// Filter out any entries with QUIET_PERIOD as the value before checking for ordering, since
// that phase is allowed to appear anywhere.
checkArgument(
Ordering.natural()
.isStrictlyOrdered(
tldStatesMap.values().stream()
.filter(state -> !TldState.QUIET_PERIOD.equals(state))
.collect(Collectors.toList())),
"The TLD states are chronologically out of order");
getInstance().tldStateTransitions = TimedTransitionProperty.fromValueMap(tldStatesMap);
getInstance().tldStateTransitions = createTldStateTransitions(tldStatesMap);
return this;
}

Expand Down Expand Up @@ -1152,24 +1245,8 @@ public Builder setAllowedFullyQualifiedHostNames(
}

public Builder setDefaultPromoTokens(ImmutableList<VKey<AllocationToken>> promoTokens) {
tm().transact(
() -> {
for (VKey<AllocationToken> tokenKey : promoTokens) {
AllocationToken token = tm().loadByKey(tokenKey);
checkArgument(
token.getTokenType().equals(TokenType.DEFAULT_PROMO),
"Token %s has an invalid token type of %s. DefaultPromoTokens must be of"
+ " the type DEFAULT_PROMO",
token.getToken(),
token.getTokenType());
checkArgument(
token.getAllowedTlds().contains(getInstance().tldStr),
"The token %s is not valid for this TLD. The valid TLDs for it are %s",
token.getToken(),
token.getAllowedTlds());
}
getInstance().defaultPromoTokens = promoTokens;
});
validateDefaultPromoTokens(getInstance().tldStr, promoTokens);
getInstance().defaultPromoTokens = promoTokens;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ protected void init() throws Exception {
checkPremiumList(newTld);
checkDnsWriters(newTld);
checkCurrency(newTld);
checkDefaultPromoTokens(newTld);
// bsaEnrollStartTime only exists in DB. Need to carry it over to the updated copy. See Tld.java
// for more information.
Optional<Instant> bsaEnrollTime =
Expand Down Expand Up @@ -248,6 +249,10 @@ private void checkDnsWriters(Tld newTld) {
invalidDnsWriters.isEmpty(), "Invalid DNS writer name(s) specified: %s", invalidDnsWriters);
}

private void checkDefaultPromoTokens(Tld newTld) {
Tld.validateDefaultPromoTokens(newTld.getTldStr(), newTld.getDefaultPromoTokens());
}

private void checkCurrency(Tld newTld) {
CurrencyUnit currencyUnit = newTld.getCurrency();
checkArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ private void verifyInput() {
// TODO(b/261763205): Create a cleaner way to handle ending bulk pricing packages once we
// actually have customers using them
boolean hasEnding =
tokenStatusTransitions.containsValue(TokenStatus.ENDED)
|| tokenStatusTransitions.containsValue(TokenStatus.CANCELLED);
tokenStatusTransitions.values().stream().anyMatch(AllocationToken::isTerminalStatus);
checkArgument(
!(BULK_PRICING.equals(tokenType) && hasEnding),
"BULK_PRICING tokens should not be generated with ENDED or CANCELLED in their transition"
Expand Down
Loading
Loading