/** * Constructor. * * @param defaultEventInterval * default timer value to use for interval between events. * This puts an upper bound for the timer value * @param minimumEventInterval * for loading {@link DistributionSet#getModules()}. This * puts a lower bound to the timer value * @param timerUnit * representing the unit of time to be used for timer. */ EventTimer(final String defaultEventInterval, final String minimumEventInterval, final TemporalUnit timeUnit) { this.defaultEventInterval = defaultEventInterval; this.defaultEventIntervalDuration = MaintenanceScheduleHelper.convertToISODuration(defaultEventInterval); this.minimumEventInterval = minimumEventInterval; this.minimumEventIntervalDuration = MaintenanceScheduleHelper.convertToISODuration(minimumEventInterval); this.timeUnit = timeUnit; }
if (allNotEmpty(cronSchedule, duration, timezone)) { validateCronSchedule(cronSchedule); validateDuration(duration); if (!getNextMaintenanceWindow(cronSchedule, duration, timezone).isPresent()) { throw new InvalidMaintenanceScheduleException( "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.");
/** * 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. * * @return {@link Duration} in ISO format. * * @throws DateTimeParseException * if the text cannot be converted to ISO format. */ public static Duration convertToISODuration(final String timeInterval) { return Duration.between(LocalTime.MIN, convertDurationToLocalTime(timeInterval)); }
final String timezone) { try { final ExecutionTime scheduleExecutor = ExecutionTime.forCron(getCronFromExpression(cronSchedule)); final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.of(timezone)); final ZonedDateTime after = now.minus(convertToISODuration(duration)); final ZonedDateTime next = scheduleExecutor.nextExecution(after); return Optional.of(next);
@Override public ResponseEntity<MgmtTargetAssignmentResponseBody> createAssignedTarget( @PathVariable("distributionSetId") final Long distributionSetId, @RequestBody final List<MgmtTargetAssignmentRequestBody> assignments, @RequestParam(value = "offline", required = false) final boolean offline) { if (offline) { return ResponseEntity.ok(MgmtDistributionSetMapper .toResponse(this.deployManagament.offlineAssignedDistributionSet(distributionSetId, assignments .stream().map(MgmtTargetAssignmentRequestBody::getId).collect(Collectors.toList())))); } final DistributionSetAssignmentResult assignDistributionSet = this.deployManagament .assignDistributionSet(distributionSetId, assignments.stream().map(t -> { final MgmtMaintenanceWindowRequestBody maintenanceWindow = t.getMaintenanceWindow(); if (maintenanceWindow == null) { return new TargetWithActionType(t.getId(), MgmtRestModelMapper.convertActionType(t.getType()), t.getForcetime()); } final String cronSchedule = maintenanceWindow.getSchedule(); final String duration = maintenanceWindow.getDuration(); final String timezone = maintenanceWindow.getTimezone(); MaintenanceScheduleHelper.validateMaintenanceSchedule(cronSchedule, duration, timezone); return new TargetWithActionType(t.getId(), MgmtRestModelMapper.convertActionType(t.getType()), t.getForcetime(), cronSchedule, duration, timezone); }).collect(Collectors.toList())); return ResponseEntity.ok(MgmtDistributionSetMapper.toResponse(assignDistributionSet)); }
/** * 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". * * @throws InvalidMaintenanceScheduleException * if the cron expression doesn't have a valid quartz format. */ public static void validateCronSchedule(final String cronSchedule) { try { if (StringUtils.hasText(cronSchedule)) { getCronFromExpression(cronSchedule); } } catch (final IllegalArgumentException e) { throw new InvalidMaintenanceScheduleException(e.getMessage(), e); } } }
@Override public Optional<ZonedDateTime> getMaintenanceWindowStartTime() { return MaintenanceScheduleHelper.getNextMaintenanceWindow(maintenanceWindowSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone); }
@SuppressWarnings("squid:S1166") @Override public void validate(final Object value) { try { MaintenanceScheduleHelper.validateDuration((String) value); } catch (final InvalidMaintenanceScheduleException e) { throw new InvalidValueException(i18n.getMessage("message.maintenancewindow.duration.validation.error", e.getDurationErrorIndex())); } } }
@SuppressWarnings("squid:S1166") @Override public void validate(final Object value) { try { MaintenanceScheduleHelper.validateCronSchedule((String) value); } catch (final InvalidMaintenanceScheduleException e) { throw new InvalidValueException(i18n.getMessage(CRON_VALIDATION_ERROR) + ": " + e.getMessage()); } } }
private boolean isMaintenanceWindowValid() { if (maintenanceWindowLayout.isEnabled()) { try { MaintenanceScheduleHelper.validateMaintenanceSchedule(maintenanceWindowLayout.getMaintenanceSchedule(), maintenanceWindowLayout.getMaintenanceDuration(), maintenanceWindowLayout.getMaintenanceTimeZone()); } catch (final InvalidMaintenanceScheduleException e) { LOG.error("Maintenance window is not valid", e); getNotification().displayValidationError(e.getMessage()); return false; } } return true; }
@SuppressWarnings("squid:S1166") private String translateCron(final String cronExpression) { try { return cronDescriptor.describe(MaintenanceScheduleHelper.getCronFromExpression(cronExpression)); } catch (final IllegalArgumentException ex) { return i18n.getMessage(CRON_VALIDATION_ERROR); } }
@Override public Optional<ZonedDateTime> getMaintenanceWindowStartTime() { return MaintenanceScheduleHelper.getNextMaintenanceWindow(maintenanceWindowSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone); }
@SuppressWarnings("squid:S1166") @Override public void validate(final Object value) { try { MaintenanceScheduleHelper.validateDuration((String) value); } catch (final InvalidMaintenanceScheduleException e) { throw new InvalidValueException(i18n.getMessage("message.maintenancewindow.duration.validation.error", e.getDurationErrorIndex())); } } }
@SuppressWarnings("squid:S1166") @Override public void validate(final Object value) { try { MaintenanceScheduleHelper.validateCronSchedule((String) value); } catch (final InvalidMaintenanceScheduleException e) { throw new InvalidValueException(i18n.getMessage(CRON_VALIDATION_ERROR) + ": " + e.getMessage()); } } }
private boolean isMaintenanceWindowValid() { if (maintenanceWindowLayout.isEnabled()) { try { MaintenanceScheduleHelper.validateMaintenanceSchedule(maintenanceWindowLayout.getMaintenanceSchedule(), maintenanceWindowLayout.getMaintenanceDuration(), maintenanceWindowLayout.getMaintenanceTimeZone()); } catch (final InvalidMaintenanceScheduleException e) { LOG.error("Maintenance window is not valid", e); getNotification().displayValidationError(e.getMessage()); return false; } } return true; }
/** * Constructor. * * @param defaultEventInterval * default timer value to use for interval between events. * This puts an upper bound for the timer value * @param minimumEventInterval * for loading {@link DistributionSet#getModules()}. This * puts a lower bound to the timer value * @param timerUnit * representing the unit of time to be used for timer. */ EventTimer(final String defaultEventInterval, final String minimumEventInterval, final TemporalUnit timeUnit) { this.defaultEventInterval = defaultEventInterval; this.defaultEventIntervalDuration = MaintenanceScheduleHelper.convertToISODuration(defaultEventInterval); this.minimumEventInterval = minimumEventInterval; this.minimumEventIntervalDuration = MaintenanceScheduleHelper.convertToISODuration(minimumEventInterval); this.timeUnit = timeUnit; }
@SuppressWarnings("squid:S1166") private String translateCron(final String cronExpression) { try { return cronDescriptor.describe(MaintenanceScheduleHelper.getCronFromExpression(cronExpression)); } catch (final IllegalArgumentException ex) { return i18n.getMessage(CRON_VALIDATION_ERROR); } }
/** * 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. */ public static void validateDuration(final String duration) { try { if (StringUtils.hasText(duration)) { convertDurationToLocalTime(duration); } } catch (final DateTimeParseException e) { throw new InvalidMaintenanceScheduleException("Provided duration is not valid", e, e.getErrorIndex()); } }
private boolean isMaintenanceWindowValid() { if (maintenanceWindowLayout.isEnabled()) { try { MaintenanceScheduleHelper.validateMaintenanceSchedule(maintenanceWindowLayout.getMaintenanceSchedule(), maintenanceWindowLayout.getMaintenanceDuration(), maintenanceWindowLayout.getMaintenanceTimeZone()); } catch (final InvalidMaintenanceScheduleException e) { LOG.error("Maintenance window is not valid", e); getNotification().displayValidationError(e.getMessage()); return false; } } return true; }
/** * Returns the end time of next available or active maintenance window for * the {@link Action} as {@link ZonedDateTime}. If a maintenance window is * already active, the end time of currently active window is returned. * * @return the end time of window as { @link Optional<ZonedDateTime>}. */ private Optional<ZonedDateTime> getMaintenanceWindowEndTime() { return getMaintenanceWindowStartTime() .map(start -> start.plus(MaintenanceScheduleHelper.convertToISODuration(maintenanceWindowDuration))); }