Search in sources :

Example 1 with FacilityState

use of cz.metacentrum.perun.controller.model.FacilityState in project perun by CESNET.

the class TasksManagerBlImpl method getAllFacilitiesStates.

@Override
public List<FacilityState> getAllFacilitiesStates(PerunSession session) throws FacilityNotExistsException {
    List<FacilityState> list = new ArrayList<>();
    // return facilities where user is admin or all if perun admin
    List<Facility> facs = perun.getFacilitiesManagerBl().getFacilities(session);
    Collections.sort(facs);
    for (Facility facility : facs) {
        list.add(getFacilityState(session, facility));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) FacilityState(cz.metacentrum.perun.controller.model.FacilityState)

Example 2 with FacilityState

use of cz.metacentrum.perun.controller.model.FacilityState in project perun by CESNET.

the class TasksManagerBlImpl method getAllFacilitiesStatesForVo.

@Override
public List<FacilityState> getAllFacilitiesStatesForVo(PerunSession session, Vo vo) throws VoNotExistsException, FacilityNotExistsException {
    perun.getVosManagerBl().checkVoExists(session, vo);
    List<FacilityState> list = new ArrayList<>();
    List<RichResource> facs = perun.getResourcesManagerBl().getRichResources(session, vo);
    Set<Facility> facilities = new HashSet<>();
    for (RichResource res : facs) {
        facilities.add(res.getFacility());
    }
    for (Facility f : facilities) {
        list.add(getFacilityState(session, f));
    }
    Collections.sort(list);
    return list;
}
Also used : RichResource(cz.metacentrum.perun.core.api.RichResource) ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) FacilityState(cz.metacentrum.perun.controller.model.FacilityState) HashSet(java.util.HashSet)

Example 3 with FacilityState

use of cz.metacentrum.perun.controller.model.FacilityState in project perun by CESNET.

the class PropagationStatsReaderImpl method getFacilityState.

@Override
public FacilityState getFacilityState(PerunSession session, Facility facility) throws PrivilegeException, FacilityNotExistsException, InternalErrorException {
    // get all tasks
    List<Task> tasks = taskDao.listAllTasksForFacility(facility.getId());
    // define state
    FacilityState state = new FacilityState();
    state.setFacility(facility);
    // if no tasks we can't determine facility state
    if (tasks.isEmpty() || tasks == null) {
        state.setState(FacilityPropagationState.NOT_DETERMINED);
        return state;
    } else {
        // OK if no change
        state.setState(FacilityPropagationState.OK);
    }
    // fill all available destinations
    List<RichDestination> destinations = perun.getServicesManager().getAllRichDestinations(session, facility);
    for (RichDestination rd : destinations) {
        state.getResults().put(rd.getDestination(), FacilityPropagationState.NOT_DETERMINED);
    }
    // magic with tasks :-)
    for (Task task : tasks) {
        // save previous facility state
        FacilityPropagationState facState = state.getState();
        // PROCESSING and not ERROR before
        if (TaskStatus.PROCESSING.equals(task.getStatus()) && (facState != FacilityPropagationState.ERROR)) {
            state.setState(FacilityPropagationState.PROCESSING);
        } else // ERROR - set ERROR
        if (TaskStatus.ERROR.equals(task.getStatus())) {
            state.setState(FacilityPropagationState.ERROR);
        }
        // get destination status
        if (task.getExecService().getExecServiceType().equals(ExecService.ExecServiceType.SEND)) {
            List<TaskResult> results = taskResultDao.getTaskResultsByTask(task.getId());
            Map<Service, Map<Destination, TaskResult>> latestResults = new HashMap<Service, Map<Destination, TaskResult>>();
            for (TaskResult res : results) {
                if (latestResults.get(res.getService()) == null) {
                    // put in map since result for service exists
                    Map<Destination, TaskResult> value = new HashMap<>();
                    value.put(res.getDestination(), res);
                    latestResults.put(res.getService(), value);
                } else if (latestResults.get(res.getService()) != null && latestResults.get(res.getService()).get(res.getDestination()) == null) {
                    // put in inner map, since destination for service not yet exists
                    latestResults.get(res.getService()).put(res.getDestination(), res);
                } else {
                    // update in inner map since this is later task result
                    if (latestResults.get(res.getService()).get(res.getDestination()).getId() < res.getId()) {
                        // put in map
                        latestResults.get(res.getService()).put(res.getDestination(), res);
                    }
                }
            }
            for (Map<Destination, TaskResult> res : latestResults.values()) {
                for (TaskResult result : res.values()) {
                    // iterate over all latest tasks results
                    String destination = result.getDestination().getDestination();
                    FacilityPropagationState propState = state.getResults().get(destination);
                    // if any error => state is error
                    if (TaskResult.TaskResultStatus.ERROR.equals(result.getStatus())) {
                        state.getResults().put(destination, FacilityPropagationState.ERROR);
                        continue;
                    }
                    // if result ok and previous was not bad
                    if (TaskResult.TaskResultStatus.DONE.equals(result.getStatus())) {
                        if (FacilityPropagationState.NOT_DETERMINED.equals(propState)) {
                            state.getResults().put(destination, FacilityPropagationState.OK);
                        }
                    }
                }
            }
        }
    }
    return state;
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) FacilityState(cz.metacentrum.perun.controller.model.FacilityState) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) FacilityPropagationState(cz.metacentrum.perun.controller.model.FacilityState.FacilityPropagationState)

Example 4 with FacilityState

use of cz.metacentrum.perun.controller.model.FacilityState in project perun by CESNET.

the class TasksManagerBlImpl method getFacilityState.

@Override
public FacilityState getFacilityState(PerunSession session, Facility facility) throws FacilityNotExistsException {
    perun.getFacilitiesManagerBl().checkFacilityExists(session, facility);
    // get all tasks
    List<Task> tasks = getTasksManagerImpl().listAllTasksForFacility(facility.getId());
    // define state
    FacilityState state = new FacilityState();
    state.setFacility(facility);
    // if no tasks we can't determine facility state
    if (tasks == null || tasks.isEmpty()) {
        state.setState(FacilityState.FacilityPropagationState.NOT_DETERMINED);
        return state;
    } else {
        // OK if no change
        state.setState(FacilityState.FacilityPropagationState.OK);
    }
    // fill all available destinations
    List<RichDestination> destinations = perun.getServicesManagerBl().getAllRichDestinations(session, facility);
    for (RichDestination rd : destinations) {
        state.getResults().put(rd.getDestination(), FacilityState.FacilityPropagationState.NOT_DETERMINED);
    }
    // magic with tasks :-)
    for (Task task : tasks) {
        // save previous facility state
        FacilityState.FacilityPropagationState facState = state.getState();
        // PROCESSING and not ERROR before
        if (Arrays.asList(Task.TaskStatus.GENERATED, Task.TaskStatus.GENERATING, Task.TaskStatus.PLANNED, Task.TaskStatus.SENDING).contains(task.getStatus()) && (facState != FacilityState.FacilityPropagationState.ERROR)) {
            state.setState(FacilityState.FacilityPropagationState.PROCESSING);
        } else // ERROR - set ERROR
        if (Arrays.asList(Task.TaskStatus.ERROR, Task.TaskStatus.GENERROR, Task.TaskStatus.SENDERROR).contains(task.getStatus())) {
            state.setState(FacilityState.FacilityPropagationState.ERROR);
        }
        // get destination status
        if (task.getService() != null) {
            List<TaskResult> results = getTasksManagerImpl().getTaskResultsByTask(task.getId());
            Map<Service, Map<Destination, TaskResult>> latestResults = new HashMap<>();
            for (TaskResult res : results) {
                if (latestResults.get(res.getService()) == null) {
                    // put in map since result for service exists
                    Map<Destination, TaskResult> value = new HashMap<>();
                    value.put(res.getDestination(), res);
                    latestResults.put(res.getService(), value);
                } else if (latestResults.get(res.getService()) != null && latestResults.get(res.getService()).get(res.getDestination()) == null) {
                    // put in inner map, since destination for service not yet exists
                    latestResults.get(res.getService()).put(res.getDestination(), res);
                } else {
                    // update in inner map since this is later task result
                    if (latestResults.get(res.getService()).get(res.getDestination()).getId() < res.getId()) {
                        // put in map
                        latestResults.get(res.getService()).put(res.getDestination(), res);
                    }
                }
            }
            for (Map<Destination, TaskResult> res : latestResults.values()) {
                for (TaskResult result : res.values()) {
                    // iterate over all latest tasks results
                    String destination = result.getDestination().getDestination();
                    FacilityState.FacilityPropagationState propState = state.getResults().get(destination);
                    // if any error => state is error
                    if (TaskResult.TaskResultStatus.ERROR.equals(result.getStatus())) {
                        state.getResults().put(destination, FacilityState.FacilityPropagationState.ERROR);
                        continue;
                    }
                    // if result ok and previous was not bad
                    if (TaskResult.TaskResultStatus.DONE.equals(result.getStatus())) {
                        if (FacilityState.FacilityPropagationState.NOT_DETERMINED.equals(propState)) {
                            state.getResults().put(destination, FacilityState.FacilityPropagationState.OK);
                        }
                    }
                }
            }
        }
    }
    return state;
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) RichDestination(cz.metacentrum.perun.core.api.RichDestination) Task(cz.metacentrum.perun.taskslib.model.Task) HashMap(java.util.HashMap) Service(cz.metacentrum.perun.core.api.Service) FacilityState(cz.metacentrum.perun.controller.model.FacilityState) RichDestination(cz.metacentrum.perun.core.api.RichDestination) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with FacilityState

use of cz.metacentrum.perun.controller.model.FacilityState in project perun by CESNET.

the class TasksManagerBlImplTest method testGetFacilityState.

@Test
public void testGetFacilityState() {
    System.out.println("TasksManagerBlImplTest.testGetFacilityState");
    // TODO - add more thorough test
    assertThatNoException().isThrownBy(() -> {
        FacilityState state = tasksManager.getFacilityState(perunSession, facility1);
        assertEquals(facility1, state.getFacility());
        assertEquals(FacilityPropagationState.OK, state.getState());
    });
}
Also used : FacilityState(cz.metacentrum.perun.controller.model.FacilityState) Test(org.junit.Test)

Aggregations

FacilityState (cz.metacentrum.perun.controller.model.FacilityState)5 Facility (cz.metacentrum.perun.core.api.Facility)2 Task (cz.metacentrum.perun.taskslib.model.Task)2 TaskResult (cz.metacentrum.perun.taskslib.model.TaskResult)2 ArrayList (java.util.ArrayList)2 FacilityPropagationState (cz.metacentrum.perun.controller.model.FacilityState.FacilityPropagationState)1 Destination (cz.metacentrum.perun.core.api.Destination)1 RichDestination (cz.metacentrum.perun.core.api.RichDestination)1 RichResource (cz.metacentrum.perun.core.api.RichResource)1 Service (cz.metacentrum.perun.core.api.Service)1 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Test (org.junit.Test)1