|
|
|
|
@@ -21,46 +21,35 @@ import com.cronutils.model.CronType;
|
|
|
|
|
import com.cronutils.model.definition.CronDefinitionBuilder;
|
|
|
|
|
import com.cronutils.model.time.ExecutionTime;
|
|
|
|
|
import com.cronutils.parser.CronParser;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import org.eclipse.hawkbit.repository.exception.InvalidMaintenanceScheduleException;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class to check validity of maintenance schedule definition and manage
|
|
|
|
|
* scheduling of maintenance window using a cron expression based scheduler. It
|
|
|
|
|
* also provides a helper method for conversion of duration specified in
|
|
|
|
|
* HH:mm:ss format to ISO format.
|
|
|
|
|
* Helper class to check validity of maintenance schedule definition and manage scheduling of maintenance window using
|
|
|
|
|
* a cron expression based scheduler. It also provides a helper method for conversion of duration specified in HH:mm:ss format to ISO format.
|
|
|
|
|
*/
|
|
|
|
|
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
|
|
|
|
|
public final class MaintenanceScheduleHelper {
|
|
|
|
|
|
|
|
|
|
private static final CronParser cronParser = new CronParser(
|
|
|
|
|
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
|
|
|
|
|
|
|
|
|
|
private MaintenanceScheduleHelper() {
|
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
|
}
|
|
|
|
|
private static final CronParser CRON_PARSER = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the next available maintenance window.
|
|
|
|
|
*
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last
|
|
|
|
|
* optional field: "second minute hour dayofmonth month weekday
|
|
|
|
|
* year".
|
|
|
|
|
* @param duration in HH:mm:ss format specifying the duration of a maintenance
|
|
|
|
|
* window, for example 00:30:00 for 30 minutes.
|
|
|
|
|
* @param timezone is the time zone specified as +/-hh:mm offset from UTC. For
|
|
|
|
|
* example +02:00 for CET summer time and +00:00 for UTC. The
|
|
|
|
|
* start time of a maintenance window calculated based on the
|
|
|
|
|
* cron expression is relative to this time zone.
|
|
|
|
|
* @return { @link Optional<ZonedDateTime>} of the next available window. In
|
|
|
|
|
* case there is none, or there are maintenance window validation
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last optional field: "second minute hour day-of-month month
|
|
|
|
|
* weekday year".
|
|
|
|
|
* @param duration in HH:mm:ss format specifying the duration of a maintenance window, for example 00:30:00 for 30 minutes.
|
|
|
|
|
* @param timezone is the time zone specified as +/-hh:mm offset from UTC. For example +02:00 for CET summer time and +00:00 for UTC. The
|
|
|
|
|
* start time of a maintenance window calculated based on the cron expression is relative to this time zone.
|
|
|
|
|
* @return { @link Optional<ZonedDateTime>} of the next available window. In case there is none, or there are maintenance window validation
|
|
|
|
|
* errors, returns empty value.
|
|
|
|
|
*/
|
|
|
|
|
// Exception squid:S1166 - if there are validation error(format of cron
|
|
|
|
|
// expression or duration is wrong), we simply return empty value
|
|
|
|
|
@SuppressWarnings("squid:S1166")
|
|
|
|
|
public static Optional<ZonedDateTime> getNextMaintenanceWindow(final String cronSchedule, final String duration,
|
|
|
|
|
final String timezone) {
|
|
|
|
|
public static Optional<ZonedDateTime> getNextMaintenanceWindow(final String cronSchedule, final String duration, final String timezone) {
|
|
|
|
|
try {
|
|
|
|
|
final ExecutionTime scheduleExecutor = ExecutionTime.forCron(getCronFromExpression(cronSchedule));
|
|
|
|
|
final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.of(timezone));
|
|
|
|
|
@@ -74,36 +63,28 @@ public final class MaintenanceScheduleHelper {
|
|
|
|
|
/**
|
|
|
|
|
* Parse the given cron expression with quartz parser.
|
|
|
|
|
*
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last
|
|
|
|
|
* optional field: "second minute hour dayofmonth month weekday
|
|
|
|
|
* year".
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last optional field: "second minute hour day-of-month
|
|
|
|
|
* month weekday year".
|
|
|
|
|
* @return {@link Cron} object, that corresponds to the expression.
|
|
|
|
|
* @throws IllegalArgumentException if the cron expression doesn't have a valid format.
|
|
|
|
|
*/
|
|
|
|
|
public static Cron getCronFromExpression(final String cronSchedule) {
|
|
|
|
|
return cronParser.parse(cronSchedule);
|
|
|
|
|
return CRON_PARSER.parse(cronSchedule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the maintenance schedule definition is valid in terms of
|
|
|
|
|
* validity of cron expression, duration and availability of at least one
|
|
|
|
|
* valid maintenance window. Further a maintenance schedule is valid if
|
|
|
|
|
* either all the parameters: schedule, duration and time zone are valid or
|
|
|
|
|
* are null.
|
|
|
|
|
* Check if the maintenance schedule definition is valid in terms of validity of cron expression, duration and availability of at least one
|
|
|
|
|
* valid maintenance window. Further a maintenance schedule is valid if either all the parameters: schedule, duration and time zone are
|
|
|
|
|
* valid or are null.
|
|
|
|
|
*
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last
|
|
|
|
|
* optional field: "second minute hour dayofmonth month weekday
|
|
|
|
|
* year".
|
|
|
|
|
* @param duration in HH:mm:ss format specifying the duration of a maintenance
|
|
|
|
|
* window, for example 00:30:00 for 30 minutes.
|
|
|
|
|
* @param timezone is the time zone specified as +/-hh:mm offset from UTC. For
|
|
|
|
|
* example +02:00 for CET summer time and +00:00 for UTC. The
|
|
|
|
|
* start time of a maintenance window calculated based on the
|
|
|
|
|
* cron expression is relative to this time zone.
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last optional field: "second minute hour day-of-month month
|
|
|
|
|
* weekday year".
|
|
|
|
|
* @param duration in HH:mm:ss format specifying the duration of a maintenance window, for example 00:30:00 for 30 minutes.
|
|
|
|
|
* @param timezone is the time zone specified as +/-hh:mm offset from UTC. For example +02:00 for CET summer time and +00:00 for UTC. The
|
|
|
|
|
* start time of a maintenance window calculated based on the cron expression is relative to this time zone.
|
|
|
|
|
* @throws InvalidMaintenanceScheduleException if the defined schedule fails the validity criteria.
|
|
|
|
|
*/
|
|
|
|
|
public static void validateMaintenanceSchedule(final String cronSchedule, final String duration,
|
|
|
|
|
final String timezone) {
|
|
|
|
|
public static void validateMaintenanceSchedule(final String cronSchedule, final String duration, final String timezone) {
|
|
|
|
|
if (allNotEmpty(cronSchedule, duration, timezone)) {
|
|
|
|
|
validateCronSchedule(cronSchedule);
|
|
|
|
|
validateDuration(duration);
|
|
|
|
|
@@ -114,18 +95,15 @@ public final class MaintenanceScheduleHelper {
|
|
|
|
|
"No valid maintenance window available after current time");
|
|
|
|
|
}
|
|
|
|
|
} else if (atLeastOneNotEmpty(cronSchedule, duration, timezone)) {
|
|
|
|
|
throw new InvalidMaintenanceScheduleException(
|
|
|
|
|
"All of schedule, duration and timezone should either be null or non empty.");
|
|
|
|
|
throw new InvalidMaintenanceScheduleException("All of schedule, duration and timezone should either be null or non empty.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert the time interval or duration specified in "HH:mm:ss" format to
|
|
|
|
|
* ISO format.
|
|
|
|
|
* Convert the time interval or duration specified in "HH:mm:ss" format to ISO format.
|
|
|
|
|
*
|
|
|
|
|
* @param timeInterval in "HH:mm:ss" string format. This format is popularly used but
|
|
|
|
|
* can be confused with time of the day, hence conversion to ISO
|
|
|
|
|
* specified format for time duration is required.
|
|
|
|
|
* @param timeInterval in "HH:mm:ss" string format. This format is popularly used but can be confused with time of the day,
|
|
|
|
|
* hence conversion to ISO specified format for time duration is required.
|
|
|
|
|
* @return {@link Duration} in ISO format.
|
|
|
|
|
* @throws DateTimeParseException if the text cannot be converted to ISO format.
|
|
|
|
|
*/
|
|
|
|
|
@@ -136,11 +114,9 @@ public final class MaintenanceScheduleHelper {
|
|
|
|
|
/**
|
|
|
|
|
* Validates the format of the maintenance window duration
|
|
|
|
|
*
|
|
|
|
|
* @param duration in "HH:mm:ss" string format. This format is popularly used but
|
|
|
|
|
* can be confused with time of the day, hence conversion to ISO
|
|
|
|
|
* specified format for time duration is required.
|
|
|
|
|
* @throws InvalidMaintenanceScheduleException if the duration doesn't have a valid format to be converted
|
|
|
|
|
* to ISO.
|
|
|
|
|
* @param duration in "HH:mm:ss" string format. This format is popularly used but can be confused with time of the day, hence conversion
|
|
|
|
|
* to ISO specified format for time duration is required.
|
|
|
|
|
* @throws InvalidMaintenanceScheduleException if the duration doesn't have a valid format to be converted to ISO.
|
|
|
|
|
*/
|
|
|
|
|
public static void validateDuration(final String duration) {
|
|
|
|
|
try {
|
|
|
|
|
@@ -155,9 +131,8 @@ public final class MaintenanceScheduleHelper {
|
|
|
|
|
/**
|
|
|
|
|
* Validates the format of the maintenance window cron expression
|
|
|
|
|
*
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last
|
|
|
|
|
* optional field: "second minute hour dayofmonth month weekday
|
|
|
|
|
* year".
|
|
|
|
|
* @param cronSchedule is a cron expression with 6 mandatory fields and 1 last optional field: "second minute hour day-of-month month
|
|
|
|
|
* weekday year".
|
|
|
|
|
* @throws InvalidMaintenanceScheduleException if the cron expression doesn't have a valid quartz format.
|
|
|
|
|
*/
|
|
|
|
|
public static void validateCronSchedule(final String cronSchedule) {
|
|
|
|
|
@@ -181,4 +156,4 @@ public final class MaintenanceScheduleHelper {
|
|
|
|
|
private static LocalTime convertDurationToLocalTime(final String timeInterval) {
|
|
|
|
|
return LocalTime.parse(timeInterval.strip());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|