Search in sources :

Example 16 with PerunClient

use of cz.metacentrum.perun.core.api.PerunClient in project perun by CESNET.

the class RegistrarBaseIntegrationTest method setupTest.

@Before
public void setupTest() throws Exception {
    if (vo == null || session == null) {
        session = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
        // create test VO
        vo = new Vo(0, "registrarTestVO", "regTestVO");
        vo = perun.getVosManagerBl().createVo(session, vo);
    }
}
Also used : PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Vo(cz.metacentrum.perun.core.api.Vo) Before(org.junit.Before)

Example 17 with PerunClient

use of cz.metacentrum.perun.core.api.PerunClient in project perun by CESNET.

the class PerunBlImpl method getPerunSession.

/**
 * This method is used only internally.
 */
private PerunSession getPerunSession() {
    PerunPrincipal principal = new PerunPrincipal(INTERNALPRINCIPAL, ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL);
    PerunClient client = new PerunClient();
    return new PerunSessionImpl(this, principal, client);
}
Also used : PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) PerunSessionImpl(cz.metacentrum.perun.core.impl.PerunSessionImpl)

Example 18 with PerunClient

use of cz.metacentrum.perun.core.api.PerunClient in project perun by CESNET.

the class EventProcessor method createTaskFromEvent.

/**
 * Creates Task from Event data. Tries to resolve Service and Facility pairs from Event.
 * Events for non existing entities are discarded.
 *
 * @param event Event to parse
 * @throws ServiceNotExistsException When Service from Event doesn't exists anymore
 * @throws InvalidEventMessageException  When Message has invalid format.
 * @throws InternalErrorException  When implementation fails
 * @throws PrivilegeException  When dispatcher lack privileges to call core methods
 */
private void createTaskFromEvent(Event event) throws ServiceNotExistsException, InvalidEventMessageException, PrivilegeException {
    Map<Facility, Set<Service>> resolvedServices = eventServiceResolver.resolveEvent(event.getData());
    for (Entry<Facility, Set<Service>> map : resolvedServices.entrySet()) {
        Facility facility = map.getKey();
        for (Service service : map.getValue()) {
            if (!service.isEnabled()) {
                log.debug("Service not enabled: {}.", service);
                continue;
            }
            if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnFacility(service, facility)) {
                log.debug("Service blocked on Facility: {} , {}.", service, facility);
                continue;
            }
            // Check if all destinations are not blocked
            try {
                // init session
                try {
                    if (sess == null) {
                        sess = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.getProperty("perun.principal.extSourceType")), new PerunClient());
                    }
                } catch (InternalErrorException e1) {
                    log.error("Error establishing perun session to create Task from Event: ", e1);
                    continue;
                }
                List<Destination> destinations = perun.getServicesManager().getDestinations(sess, service, facility);
                if (destinations != null && !destinations.isEmpty()) {
                    Iterator<Destination> iter = destinations.iterator();
                    while (iter.hasNext()) {
                        Destination dest = iter.next();
                        if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnDestination(service, dest.getId())) {
                            iter.remove();
                        }
                    }
                    if (destinations.isEmpty()) {
                        // All service destinations were blocked -> Task is denied to be sent to engine just like
                        // when service is blocked globally in Perun or on facility as a whole.
                        log.debug("{} blocked on all destinations on {}.", service, facility);
                        continue;
                    }
                }
            } catch (ServiceNotExistsException e) {
                log.error("Service not exist: {}.", service);
            } catch (FacilityNotExistsException e) {
                log.error("Facility not exist: {}.", facility);
            } catch (InternalErrorException | PrivilegeException e) {
                log.error("{}", e);
            }
            // check for presence of task for this <Service, Facility> pair
            // NOTE: this must be atomic enough to not create duplicate
            // tasks in schedulingPool (are we running in parallel
            // here?)
            boolean isForced = determineForcedPropagation(event);
            Task task = schedulingPool.getTask(facility, service);
            if (task != null) {
                // there already is a task in schedulingPool
                // signal that task needs to regenerate data and be forced next time
                task.setDestinations(null);
                task.setSourceUpdated(true);
                if (isForced)
                    task.setPropagationForced(true);
                task.setRecurrence(0);
                log.debug("[{}] Task is already in pool. Re-setting source updated and forced flags, {}.", task.getId(), task);
            } else {
                // no such task yet, create one
                task = new Task();
                task.setFacility(facility);
                task.setService(service);
                task.setStatus(TaskStatus.WAITING);
                task.setRecurrence(0);
                task.setDelay(service.getDelay());
                task.setSchedule(LocalDateTime.now());
                task.setSourceUpdated(false);
                task.setPropagationForced(isForced);
                try {
                    schedulingPool.addToPool(task);
                    log.debug("[{}] New Task added to pool. {}.", task.getId(), task);
                } catch (TaskStoreException e) {
                    log.error("[{}] Could not add Task to pool. Task {} will be lost: {}", task.getId(), task, e);
                }
                schedulingPool.scheduleTask(task, -1);
            }
        }
    }
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) Set(java.util.Set) Service(cz.metacentrum.perun.core.api.Service) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) TaskStoreException(cz.metacentrum.perun.taskslib.exceptions.TaskStoreException) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Facility(cz.metacentrum.perun.core.api.Facility)

Example 19 with PerunClient

use of cz.metacentrum.perun.core.api.PerunClient in project perun by CESNET.

the class AuditerListener method run.

// ----- methods -------------------------------------
@Override
public void run() {
    boolean whichOfTwoRules = false;
    try {
        try {
            if (sess == null) {
                sess = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.getProperty("perun.principal.extSourceType")), new PerunClient());
            }
        } catch (InternalErrorException e1) {
            log.error("Error establishing perun session in AuditerListener.", e1);
            // we can't continue without session
            stop();
        }
        while (!shouldStop()) {
            try {
                for (AuditEvent message : perun.getAuditMessagesManager().pollConsumerEvents(sess, "dispatcher")) {
                    // create event for each message
                    Event event = new Event();
                    event.setTimeStamp(System.currentTimeMillis());
                    if (whichOfTwoRules) {
                        event.setHeader("portishead");
                        whichOfTwoRules = false;
                    } else {
                        event.setHeader("clockworkorange");
                        whichOfTwoRules = true;
                    }
                    event.setData(message);
                    // pass event to queue for further processing
                    eventQueue.put(event);
                }
                Thread.sleep(1000);
            } catch (InternalErrorException | PrivilegeException ex) {
                log.error("AuditerListener couldn't get AuditEvents.", ex);
                Thread.sleep(1000);
            }
        }
        log.debug("AuditerListener has stopped.");
    } catch (InterruptedException e) {
        log.error("Error in AuditerListener: {}" + e);
        throw new RuntimeException("Somebody has interrupted us...", e);
    }
}
Also used : PerunClient(cz.metacentrum.perun.core.api.PerunClient) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Event(cz.metacentrum.perun.dispatcher.model.Event) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 20 with PerunClient

use of cz.metacentrum.perun.core.api.PerunClient in project perun by CESNET.

the class UsersManagerBlImplTest method setUp.

@Before
public void setUp() throws Exception {
    candidate = new Candidate();
    candidate.setFirstName("some");
    candidate.setId(0);
    candidate.setMiddleName("");
    candidate.setLastName("testingUser");
    candidate.setTitleBefore("");
    candidate.setTitleAfter("");
    ues = new UserExtSource(extSource, "extLogin");
    candidate.setUserExtSource(ues);
    candidate.setAttributes(new HashMap<>());
    sess = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    vo = new Vo(0, "UsersBlImplTestVo", "UsrMgrBlImplTestVo");
    vo = perun.getVosManagerBl().createVo(sess, vo);
    member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
    group = new Group("testGroup", "testGroup");
    group = perun.getGroupsManagerBl().createGroup(sess, vo, group);
    perun.getGroupsManagerBl().addMember(sess, group, member);
    facility = new Facility(0, "testFac");
    facility = perun.getFacilitiesManagerBl().createFacility(sess, facility);
    resource = new Resource(0, "testRes", null, facility.getId(), vo.getId());
    resource = perun.getResourcesManagerBl().createResource(sess, resource, vo, facility);
    perun.getResourcesManagerBl().assignGroupToResource(sess, group, resource, false, false, false);
    // second branch
    vo2 = new Vo(0, "FacilitiesManagerBlImplTestVo2", "FacMgrBlImplTestVo2");
    vo2 = perun.getVosManagerBl().createVo(sess, vo2);
    member2 = perun.getMembersManagerBl().createMemberSync(sess, vo2, candidate);
    group2 = new Group("testGroup", "testGroup");
    group2 = perun.getGroupsManagerBl().createGroup(sess, vo2, group2);
    perun.getGroupsManagerBl().addMember(sess, group2, member2);
    resource2 = new Resource(0, "testRes2", null, facility.getId(), vo2.getId());
    resource2 = perun.getResourcesManagerBl().createResource(sess, resource2, vo2, facility);
    // third branch
    facility2 = new Facility(0, "testFac2");
    facility2 = perun.getFacilitiesManagerBl().createFacility(sess, facility2);
    resource3 = new Resource(0, "testRes3", null, facility2.getId(), vo2.getId());
    resource3 = perun.getResourcesManagerBl().createResource(sess, resource3, vo2, facility2);
    perun.getResourcesManagerBl().assignGroupToResources(sess, group2, Arrays.asList(resource2, resource3), false, false, false);
    user = perun.getUsersManagerBl().getUserByMember(sess, member);
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) Group(cz.metacentrum.perun.core.api.Group) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) PerunClient(cz.metacentrum.perun.core.api.PerunClient) Resource(cz.metacentrum.perun.core.api.Resource) RichResource(cz.metacentrum.perun.core.api.RichResource) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility) Before(org.junit.Before)

Aggregations

PerunClient (cz.metacentrum.perun.core.api.PerunClient)25 PerunPrincipal (cz.metacentrum.perun.core.api.PerunPrincipal)24 Before (org.junit.Before)13 Facility (cz.metacentrum.perun.core.api.Facility)8 Service (cz.metacentrum.perun.core.api.Service)8 Vo (cz.metacentrum.perun.core.api.Vo)8 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)8 Group (cz.metacentrum.perun.core.api.Group)7 Resource (cz.metacentrum.perun.core.api.Resource)7 ArrayList (java.util.ArrayList)6 PerunSession (cz.metacentrum.perun.core.api.PerunSession)5 User (cz.metacentrum.perun.core.api.User)5 JdbcPerunTemplate (org.springframework.jdbc.core.JdbcPerunTemplate)5 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)4 Owner (cz.metacentrum.perun.core.api.Owner)4 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)4 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)4 Attribute (cz.metacentrum.perun.core.api.Attribute)3 Candidate (cz.metacentrum.perun.core.api.Candidate)3 Destination (cz.metacentrum.perun.core.api.Destination)3