Search in sources :

Example 6 with ExecutionWindowHelper

use of com.emc.storageos.db.client.util.ExecutionWindowHelper in project coprhd-controller by CoprHD.

the class ExecutionWindowHelperTest method testMonthly.

@Test
public void testMonthly() {
    // Monthly on the 15th at 1:15am
    ExecutionWindow window = createMonthlyWindow(15, 1, 15);
    setLengthInHours(window, 1);
    ExecutionWindowHelper helper = new ExecutionWindowHelper(window);
    // Just before window
    Calendar beforeTime = getDateTimeUTC(2013, Calendar.MAY, 15, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    // Just after the window
    Calendar afterTime = getDateTimeUTC(2013, Calendar.APRIL, 16, 1, 10, 0);
    assertDateTime(helper.calculateNext(afterTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(afterTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    // Start of the window
    Calendar duringTime = getDateTimeUTC(2013, Calendar.MAY, 15, 1, 15, 0);
    assertDateTime(helper.calculateNext(duringTime), 2013, Calendar.JUNE, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(duringTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    Assert.assertTrue(helper.isActive(duringTime));
    // One minute after start
    duringTime = getDateTimeUTC(2013, Calendar.MAY, 15, 1, 16, 0);
    assertDateTime(helper.calculateNext(duringTime), 2013, Calendar.JUNE, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(duringTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    Assert.assertTrue(helper.isActive(duringTime));
    // One minute before end
    duringTime = getDateTimeUTC(2013, Calendar.MAY, 15, 2, 14, 0);
    assertDateTime(helper.calculateNext(duringTime), 2013, Calendar.JUNE, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(duringTime), 2013, Calendar.MAY, 15, 1, 15, 0);
    Assert.assertTrue(helper.isActive(duringTime));
    // End of the window
    duringTime = getDateTimeUTC(2013, Calendar.MAY, 15, 2, 15, 0);
    assertDateTime(helper.calculateNext(duringTime), 2013, Calendar.JUNE, 15, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(duringTime), 2013, Calendar.JUNE, 15, 1, 15, 0);
    Assert.assertFalse(helper.isActive(duringTime));
}
Also used : ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) ExecutionWindow(com.emc.storageos.db.client.model.uimodels.ExecutionWindow) Calendar(java.util.Calendar) Test(org.junit.Test)

Example 7 with ExecutionWindowHelper

use of com.emc.storageos.db.client.util.ExecutionWindowHelper in project coprhd-controller by CoprHD.

the class ExecutionWindowHelperTest method testWeeklyBeforeTime.

@Test
public void testWeeklyBeforeTime() {
    // Weekly on Sunday at 1:15am
    ExecutionWindow window = createWeeklyWindow(Calendar.SUNDAY, 1, 15);
    setLengthInHours(window, 1);
    ExecutionWindowHelper helper = new ExecutionWindowHelper(window);
    Calendar beforeTime = getDateTimeUTC(2013, Calendar.MAY, 5, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    beforeTime = getDateTimeUTC(2013, Calendar.MAY, 4, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    beforeTime = getDateTimeUTC(2013, Calendar.MAY, 3, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    beforeTime = getDateTimeUTC(2013, Calendar.MAY, 2, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    beforeTime = getDateTimeUTC(2013, Calendar.MAY, 1, 1, 10, 0);
    assertDateTime(helper.calculateNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
    assertDateTime(helper.calculateCurrentOrNext(beforeTime), 2013, Calendar.MAY, 5, 1, 15, 0);
}
Also used : ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) ExecutionWindow(com.emc.storageos.db.client.model.uimodels.ExecutionWindow) Calendar(java.util.Calendar) Test(org.junit.Test)

Example 8 with ExecutionWindowHelper

use of com.emc.storageos.db.client.util.ExecutionWindowHelper in project coprhd-controller by CoprHD.

the class OrderService method scheduleReoccurenceOrders.

private void scheduleReoccurenceOrders() throws Exception {
    lock = coordinatorClient.getLock(LOCK_NAME);
    try {
        lock.acquire();
        List<ScheduledEvent> scheduledEvents = dataManager.getAllReoccurrenceEvents();
        for (ScheduledEvent event : scheduledEvents) {
            if (event.getEventStatus() != ScheduledEventStatus.APPROVED) {
                log.debug("Skipping event {} which is not in APPROVED status.", event.getId());
                continue;
            }
            URI orderId = event.getLatestOrderId();
            Order order = getOrderById(orderId, false);
            if (!(OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.SUCCESS) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.PARTIAL_SUCCESS) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.ERROR) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.CANCELLED))) {
                log.debug("Skipping event {} whose latest order {} is not finished yet.", event.getId(), order.getId());
                continue;
            }
            log.info("Trying to schedule a new order for event {} : {}", event.getId(), ScheduleInfo.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getScheduleInfo().getBytes(UTF_8))).toString());
            StorageOSUser user = StorageOSUser.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getStorageOSUser().getBytes(UTF_8)));
            OrderCreateParam createParam = OrderCreateParam.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getOrderCreationParam().getBytes(UTF_8)));
            ScheduleInfo scheduleInfo = ScheduleInfo.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getScheduleInfo().getBytes(UTF_8)));
            Calendar nextScheduledTime = ScheduleTimeHelper.getNextScheduledTime(order.getScheduledTime(), scheduleInfo);
            int retry = 0;
            if (order.getExecutionWindowId() != null && !order.getExecutionWindowId().getURI().equals(ExecutionWindow.NEXT)) {
                ExecutionWindow window = client.executionWindows().findById(order.getExecutionWindowId().getURI());
                if (window != null) {
                    ExecutionWindowHelper helper = new ExecutionWindowHelper(window);
                    if (nextScheduledTime != null && !helper.isActive(nextScheduledTime)) {
                        log.warn("Execution window {} might be changed after the event is scheduled.", order.getExecutionWindowId().getURI());
                        log.warn("Otherwise it is a HOURLY scheduled event");
                        do {
                            nextScheduledTime = ScheduleTimeHelper.getNextScheduledTime(nextScheduledTime, scheduleInfo);
                            retry++;
                        } while (nextScheduledTime != null && !helper.isActive(nextScheduledTime) && retry < ScheduleTimeHelper.SCHEDULE_TIME_RETRY_THRESHOLD);
                        if (retry == ScheduleTimeHelper.SCHEDULE_TIME_RETRY_THRESHOLD) {
                            log.error("Failed to find next scheduled time that match with {}", order.getExecutionWindowId().getURI());
                            nextScheduledTime = null;
                        }
                    }
                } else {
                    log.error("Execution window {} does not exist.", order.getExecutionWindowId().getURI());
                }
            }
            if (nextScheduledTime == null) {
                log.info("Scheduled event {} should be set finished.", event.getId());
                event.setEventStatus(ScheduledEventStatus.FINISHED);
            } else {
                createParam.setScheduledTime(ScheduleTimeHelper.convertCalendarToStr(nextScheduledTime));
                order = createNewOrder(user, uri(order.getTenant()), createParam);
                orderManager.processOrder(order);
                event.setLatestOrderId(order.getId());
                log.info("Scheduled an new order {} for event {} ...", order.getId(), event.getId());
            }
            client.save(event);
        }
    } catch (Exception e) {
        log.error("Failed to schedule next orders", e);
    } finally {
        try {
            lock.release();
        } catch (Exception e) {
            log.error("Error releasing order scheduler lock", e);
        }
    }
}
Also used : Order(com.emc.storageos.db.client.model.uimodels.Order) ScheduledEvent(com.emc.storageos.db.client.model.uimodels.ScheduledEvent) ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) ExecutionWindow(com.emc.storageos.db.client.model.uimodels.ExecutionWindow) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) Calendar(java.util.Calendar) OrderCreateParam(com.emc.vipr.model.catalog.OrderCreateParam) URI(java.net.URI) ScheduleInfo(com.emc.vipr.model.catalog.ScheduleInfo) TimeSeriesConstraint(com.emc.storageos.db.client.constraint.TimeSeriesConstraint) InvalidParameterException(java.security.InvalidParameterException) WebApplicationException(javax.ws.rs.WebApplicationException) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException)

Example 9 with ExecutionWindowHelper

use of com.emc.storageos.db.client.util.ExecutionWindowHelper in project coprhd-controller by CoprHD.

the class ScheduledEventService method match.

/**
 * Check if schedule time info is matched with the desired execution window set by admin.
 * @param scheduleInfo  schedule time info
 * @param window         desired execution window set by admin
 * @return                empty for matching, otherwise including detail unmatched reason.
 */
private String match(ScheduleInfo scheduleInfo, ExecutionWindow window) {
    String msg = "";
    ExecutionWindowHelper windowHelper = new ExecutionWindowHelper(window);
    if (!windowHelper.inHourMinWindow(scheduleInfo.getHourOfDay(), scheduleInfo.getMinuteOfHour())) {
        msg = "Schedule hour/minute info does not match with execution window.";
        return msg;
    }
    if (scheduleInfo.getReoccurrence() == 1)
        return msg;
    switch(scheduleInfo.getCycleType()) {
        case MINUTELY:
        case HOURLY:
            log.warn("Not all of the orders would be scheduled due to schedule cycle type {}", scheduleInfo.getCycleType());
            break;
        case DAILY:
            if (!window.getExecutionWindowType().equals(ExecutionWindowType.DAILY.name())) {
                msg = "Schedule cycle type has conflicts with execution window.";
            }
            break;
        case WEEKLY:
            if (window.getExecutionWindowType().equals(ExecutionWindowType.MONTHLY.name())) {
                msg = "Schedule cycle type has conflicts with execution window.";
            } else if (window.getExecutionWindowType().equals(ExecutionWindowType.WEEKLY.name())) {
                if (window.getDayOfWeek() != Integer.valueOf(scheduleInfo.getSectionsInCycle().get(0))) {
                    msg = "Scheduled date has conflicts with execution window.";
                }
            }
            break;
        case MONTHLY:
            if (window.getExecutionWindowType().equals(ExecutionWindowType.WEEKLY.name())) {
                msg = "Schedule cycle type has conflicts with execution window.";
            } else if (window.getExecutionWindowType().equals(ExecutionWindowType.MONTHLY.name())) {
                if (window.getDayOfMonth() != Integer.valueOf(scheduleInfo.getSectionsInCycle().get(0))) {
                    msg = "Scheduled date has conflicts with execution window.";
                }
            }
            break;
        default:
            log.error("not expected schedule cycle.");
    }
    return msg;
}
Also used : ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) URIUtil.asString(com.emc.storageos.db.client.URIUtil.asString)

Example 10 with ExecutionWindowHelper

use of com.emc.storageos.db.client.util.ExecutionWindowHelper in project coprhd-controller by CoprHD.

the class OrderManagerImpl method createOrder.

public Order createOrder(Order order, List<OrderParameter> orderParameters, StorageOSUser user) {
    CatalogService catalogService = catalogServiceManager.getCatalogServiceById(order.getCatalogServiceId());
    ServiceDescriptor serviceDescriptor = ServiceDescriptorUtil.getServiceDescriptorByName(serviceDescriptors, workflowServiceDescriptor, catalogService.getBaseService());
    order.setOrderNumber(getNextOrderNumber());
    order.setSummary(catalogService.getTitle());
    if (order.getScheduledEventId() == null) {
        if (catalogService.getExecutionWindowRequired()) {
            if (catalogService.getDefaultExecutionWindowId() == null || catalogService.getDefaultExecutionWindowId().getURI().equals(ExecutionWindow.NEXT)) {
                // For default execution window, null is deemed as NEXT window as well.
                // But we always need to set order execution window to NEXT explicitly to different it
                // with INFINITE window in new scheduler framework.
                // Set schedule time to latest updated time.  It would still be scheduled in executed window
                Calendar scheduleTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                scheduleTime.setTime(new Date());
                order.setScheduledTime(scheduleTime);
                order.setExecutionWindowId(new NamedURI(ExecutionWindow.NEXT, "NEXT"));
            } else {
                // Set schedule time to
                // either 1) the next execution window starting time
                // or     2) the current time if it is in current execution window
                ExecutionWindow executionWindow = client.findById(catalogService.getDefaultExecutionWindowId().getURI());
                ExecutionWindowHelper helper = new ExecutionWindowHelper(executionWindow);
                order.setScheduledTime(helper.getScheduledTime());
                order.setExecutionWindowId(catalogService.getDefaultExecutionWindowId());
            }
        } else {
            // If no execution window is indicated, order will be submitted to DQ immediately.
            ;
        }
    } else {
        // ExecutionWindow and ScheduleTime are already set via Parameter
        ;
    }
    order.setMessage("");
    order.setSubmittedByUserId(user.getUserName());
    order.setOrderStatus(OrderStatus.PENDING.name());
    createExecutionState(order, user);
    client.save(order);
    Map<String, String> assetOptions = getAssetValues(serviceDescriptor, orderParameters);
    for (OrderParameter orderParameter : orderParameters) {
        ServiceField serviceField = findServiceField(serviceDescriptor, orderParameter.getLabel());
        String friendlyLabel = serviceField.getLabel();
        StringBuilder friendlyValue = new StringBuilder();
        List<String> values = TextUtils.parseCSV(orderParameter.getValue());
        for (String value : values) {
            if (friendlyValue.length() > 0) {
                friendlyValue.append(",");
            }
            friendlyValue.append(getFriendlyValue(serviceField, value, assetOptions, user));
        }
        orderParameter.setFriendlyLabel(friendlyLabel);
        orderParameter.setFriendlyValue(friendlyValue.toString());
        createOrderParameter(orderParameter);
    }
    return order;
}
Also used : ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) ServiceDescriptor(com.emc.sa.descriptor.ServiceDescriptor) ServiceField(com.emc.sa.descriptor.ServiceField)

Aggregations

ExecutionWindowHelper (com.emc.storageos.db.client.util.ExecutionWindowHelper)15 ExecutionWindow (com.emc.storageos.db.client.model.uimodels.ExecutionWindow)11 Calendar (java.util.Calendar)10 Test (org.junit.Test)9 URI (java.net.URI)3 NamedURI (com.emc.storageos.db.client.model.NamedURI)2 Order (com.emc.storageos.db.client.model.uimodels.Order)2 ServiceDescriptor (com.emc.sa.descriptor.ServiceDescriptor)1 ServiceField (com.emc.sa.descriptor.ServiceField)1 DbClient (com.emc.storageos.db.client.DbClient)1 URIUtil.asString (com.emc.storageos.db.client.URIUtil.asString)1 TimeSeriesConstraint (com.emc.storageos.db.client.constraint.TimeSeriesConstraint)1 ScheduledEvent (com.emc.storageos.db.client.model.uimodels.ScheduledEvent)1 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)1 StorageOSUser (com.emc.storageos.security.authentication.StorageOSUser)1 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)1 OrderCreateParam (com.emc.vipr.model.catalog.OrderCreateParam)1 ScheduleInfo (com.emc.vipr.model.catalog.ScheduleInfo)1 URISyntaxException (java.net.URISyntaxException)1 InvalidParameterException (java.security.InvalidParameterException)1