Search in sources :

Example 76 with Task

use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.

the class EventParserImpl method parseEvent.

@Override
public Task parseEvent(String event) throws InvalidEventMessageException {
    log.info("Going to process event: {}", event);
    /*
		 * Expected string format:
		 * "task|[task_id][is_forced]|[service]|[facility]|[destination_list]|[dependency_list]"
		 *
		 *  String eventParsingPattern =
		 * "^event\\|([0-9]{1,6})\\|\\[([a-zA-Z0-9: ]+)\\]\\[([^\\]]+)\\]\\[(.*)\\]$";
		 */
    String eventParsingPattern = "^task\\|\\[([0-9]+)\\]\\[([^\\]]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]\\|\\[([^\\|]+)\\]$";
    Pattern pattern = Pattern.compile(eventParsingPattern);
    Matcher matcher = pattern.matcher(event);
    boolean matchFound = matcher.find();
    if (matchFound) {
        log.debug("Message format matched ok...");
        // Data should provide information regarding the target Service (Processing rule).
        String eventTaskId = matcher.group(1);
        String eventIsForced = matcher.group(2);
        String eventService = matcher.group(3);
        String eventFacility = matcher.group(4);
        String eventDestinationList = matcher.group(5);
        // check possible enconding
        if (!eventService.startsWith("Service")) {
            eventService = new String(Base64.decodeBase64(eventService));
        }
        if (!eventService.startsWith("Service")) {
            throw new InvalidEventMessageException("Wrong exec service: parse exception");
        }
        if (!eventFacility.startsWith("Facility")) {
            eventFacility = new String(Base64.decodeBase64(eventFacility));
        }
        if (!eventFacility.startsWith("Facility")) {
            throw new InvalidEventMessageException("Wrong facility: parse exception");
        }
        if (!eventDestinationList.startsWith("Destinations")) {
            eventDestinationList = new String(Base64.decodeBase64(eventDestinationList));
        }
        if (!eventDestinationList.startsWith("Destinations")) {
            throw new InvalidEventMessageException("Wrong destination list: parse exception");
        }
        log.debug("Event data to be parsed: task id {}, forced {}, facility {}, service {}, destination list {}", eventTaskId, eventIsForced, eventFacility, eventService, eventDestinationList);
        // Prepare variables
        Facility facility;
        Service service;
        List<Destination> destinationList = new ArrayList<Destination>();
        // resolve facility and deserialize event data
        List<PerunBean> listOfBeans = AuditParser.parseLog(eventFacility);
        try {
            facility = (Facility) listOfBeans.get(0);
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve facility from event [" + eventFacility + "]", e);
        }
        // resolve exec service and deserialize event data
        listOfBeans = AuditParser.parseLog(eventService);
        try {
            service = (Service) listOfBeans.get(0);
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve service from event [" + eventService + "]", e);
        }
        // resolve list of destinations
        listOfBeans = AuditParser.parseLog(eventDestinationList);
        log.debug("Found list of destination beans: {}", listOfBeans);
        try {
            for (PerunBean bean : listOfBeans) {
                destinationList.add((Destination) bean);
            }
        } catch (Exception e) {
            throw new InvalidEventMessageException("Could not resolve list of destinations from event.", e);
        }
        Task task = new Task();
        task.setId(Integer.parseInt(eventTaskId));
        task.setFacility(facility);
        task.setService(service);
        task.setDestinations(destinationList);
        task.setDelay(service.getDelay());
        task.setRecurrence(service.getRecurrence());
        task.setPropagationForced(Boolean.parseBoolean(eventIsForced));
        return task;
    } else {
        throw new InvalidEventMessageException("Invalid message format: Message[" + event + "]");
    }
}
Also used : Pattern(java.util.regex.Pattern) Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Service(cz.metacentrum.perun.core.api.Service) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) PerunBean(cz.metacentrum.perun.core.api.PerunBean) Facility(cz.metacentrum.perun.core.api.Facility) InvalidEventMessageException(cz.metacentrum.perun.engine.exceptions.InvalidEventMessageException)

Example 77 with Task

use of cz.metacentrum.perun.taskslib.model.Task in project perun by CESNET.

the class TasksManagerBlImplTest method setUp.

@Before
public void setUp() throws Exception {
    perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    tasksManager = ((PerunBl) perun).getTasksManagerBl();
    facility1 = new Facility();
    facility2 = new Facility();
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    testService1.setDelay(1);
    testService1.setRecurrence(1);
    testService1.setEnabled(true);
    testService1.setScript("/hellish/test/script");
    testService1.setId(servicesManager.createService(perunSession, testService1).getId());
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    testService2.setDelay(1);
    testService2.setRecurrence(1);
    testService2.setEnabled(true);
    testService2.setScript("/hellish/test/script");
    testService2.setId(servicesManager.createService(perunSession, testService2).getId());
    // 
    // Testing Destination #1
    testDestinationId1 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId1, "test.destination." + testDestinationId1);
    // Testing Destination #2
    testDestinationId2 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId2, "test.destination." + testDestinationId2);
    // Testing Facility #1
    testFacilityId1 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId1, "Cluster_" + testFacilityId1);
    facility1.setId(testFacilityId1);
    // Testing Facility #2
    testFacilityId2 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId2, "Cluster_" + testFacilityId2);
    facility2.setId(testFacilityId2);
    // facility_service_destinations
    destination1 = ((PerunBl) perun).getServicesManagerBl().getDestinationById(perunSession, testDestinationId1);
    destination2 = ((PerunBl) perun).getServicesManagerBl().getDestinationById(perunSession, testDestinationId2);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility1, destination1);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility1, destination2);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility2, destination2);
    // vo
    vo = new Vo(0, "TasksManagerTestVo", "TMTestVo");
    vo = ((PerunBl) perun).getVosManagerBl().createVo(perunSession, vo);
    // resource
    resource = new Resource();
    resource.setName("TasksManagerTestResource");
    resource.setDescription("Testovaci");
    resource = ((PerunBl) perun).getResourcesManagerBl().createResource(perunSession, resource, vo, facility1);
    // tasks
    task1 = new Task();
    task1.setFacility(facility1);
    task1.setService(testService1);
    task1.setSchedule(0L);
    task1.setStatus(TaskStatus.DONE);
    List<Destination> destinationsList = new ArrayList<>();
    destinationsList.add(destination1);
    destinationsList.add(destination2);
    task1.setDestinations(destinationsList);
    task1Id = tasksManager.insertTask(perunSession, task1);
    task1.setId(task1Id);
    // tasks
    task2 = new Task();
    task2.setFacility(facility2);
    task2.setService(testService1);
    task2.setSchedule(0L);
    task2.setStatus(TaskStatus.WARNING);
    destinationsList = new ArrayList<>();
    destinationsList.add(destination2);
    task2.setDestinations(destinationsList);
    task2Id = tasksManager.insertTask(perunSession, task2);
    task2.setId(task2Id);
    // task results
    result1 = new TaskResult();
    result1.setDestination(destination1);
    result1.setDestinationId(testDestinationId1);
    result1.setService(testService1);
    result1.setTaskId(task1Id);
    result1.setStatus(TaskResultStatus.DONE);
    result1.setTimestamp(new Date());
    result1Id = tasksManager.insertNewTaskResult(perunSession, result1);
    result1.setId(result1Id);
    // task results
    result2 = new TaskResult();
    result2.setDestination(destination1);
    result2.setDestinationId(testDestinationId1);
    result2.setService(testService1);
    result2.setTaskId(task1Id);
    result2.setStatus(TaskResultStatus.DONE);
    result2.setTimestamp(Date.from(LocalDate.now().minusDays(7).atStartOfDay(ZoneId.systemDefault()).toInstant()));
    result2Id = tasksManager.insertNewTaskResult(perunSession, result2);
    result2.setId(result2Id);
    // task results
    result3 = new TaskResult();
    result3.setDestination(destination2);
    result3.setDestinationId(testDestinationId2);
    result3.setService(testService1);
    result3.setTaskId(task1Id);
    result3.setStatus(TaskResultStatus.DONE);
    result3.setTimestamp(Date.from(LocalDate.now().minusDays(7).atStartOfDay(ZoneId.systemDefault()).toInstant()));
    result3Id = tasksManager.insertNewTaskResult(perunSession, result3);
    result3.setId(result3Id);
    jdbcTemplate.query("select id from tasks_results where task_id = ?", row -> {
        System.out.println("ID: " + row.getInt("id"));
    }, task2Id);
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) Service(cz.metacentrum.perun.core.api.Service) Date(java.util.Date) LocalDate(java.time.LocalDate) JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Vo(cz.metacentrum.perun.core.api.Vo) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) Facility(cz.metacentrum.perun.core.api.Facility) Before(org.junit.Before)

Aggregations

Task (cz.metacentrum.perun.taskslib.model.Task)77 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)16 Date (java.util.Date)16 Test (org.junit.Test)15 Facility (cz.metacentrum.perun.core.api.Facility)14 Destination (cz.metacentrum.perun.core.api.Destination)11 Service (cz.metacentrum.perun.core.api.Service)10 TaskStoreException (cz.metacentrum.perun.taskslib.exceptions.TaskStoreException)10 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)9 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)9 ArrayList (java.util.ArrayList)9 AbstractDispatcherTest (cz.metacentrum.perun.dispatcher.AbstractDispatcherTest)6 SendTask (cz.metacentrum.perun.taskslib.model.SendTask)6 TaskResult (cz.metacentrum.perun.taskslib.model.TaskResult)6 JMSException (javax.jms.JMSException)6 Pair (cz.metacentrum.perun.core.api.Pair)5 AbstractEngineTest (cz.metacentrum.perun.engine.AbstractEngineTest)5 TaskExecutionException (cz.metacentrum.perun.engine.exceptions.TaskExecutionException)5 LocalDateTime (java.time.LocalDateTime)5 Resource (cz.metacentrum.perun.core.api.Resource)4