Search in sources :

Example 6 with PerunPrincipal

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

the class AttributesManagerBlImpl method initialize.

protected void initialize() throws InternalErrorException {
    log.debug("AttributesManagerBlImpl initialize started.");
    //Get PerunSession
    String attributesManagerInitializator = "attributesManagerBlImplInitializator";
    PerunPrincipal pp = new PerunPrincipal(attributesManagerInitializator, ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL);
    PerunSession sess = perunBl.getPerunSession(pp, new PerunClient());
    //Prepare all attribute definition from system perun
    Set<AttributeDefinition> allAttributesDef = new HashSet<AttributeDefinition>();
    allAttributesDef.addAll(this.getAttributesDefinition(sess));
    //Basic state of all maps (record for every existing attributeDefinitions)
    for (AttributeDefinition ad : allAttributesDef) {
        dependencies.put(ad, new HashSet<AttributeDefinition>());
        strongDependencies.put(ad, new HashSet<AttributeDefinition>());
        inverseDependencies.put(ad, new HashSet<AttributeDefinition>());
        inverseStrongDependencies.put(ad, new HashSet<AttributeDefinition>());
        allDependencies.put(ad, new HashSet<AttributeDefinition>());
    }
    log.debug("Dependencies and StrongDependencies filling started.");
    //Fill dep and strongDep maps
    for (AttributeDefinition ad : allAttributesDef) {
        AttributesModuleImplApi module = null;
        List<String> depList = new ArrayList<String>();
        List<String> strongDepList = new ArrayList<String>();
        Set<AttributeDefinition> depSet = new HashSet<AttributeDefinition>();
        Set<AttributeDefinition> strongDepSet = new HashSet<AttributeDefinition>();
        //Return null to object if module not exist
        Object attributeModule = getAttributesManagerImpl().getAttributesModule(sess, ad);
        //If there is any existing module
        if (attributeModule != null) {
            module = (AttributesModuleImplApi) attributeModule;
            depList = module.getDependencies();
            strongDepList = module.getStrongDependencies();
            //Fill Set of dependencies
            for (String s : depList) {
                if (!s.endsWith("*")) {
                    try {
                        AttributeDefinition attrDef = getAttributeDefinition(sess, s);
                        depSet.add(attrDef);
                    } catch (AttributeNotExistsException ex) {
                        log.error("For attribute name " + s + "can't be found attributeDefinition at Inicialization in AttributesManagerBlImpl.");
                    }
                //If there is something like AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace" + ":*" we need to replace * by all possibilities
                } else {
                    List<String> allVariantOfDependence = getAllSimilarAttributeNames(sess, s.substring(0, s.length() - 2));
                    for (String variant : allVariantOfDependence) {
                        try {
                            AttributeDefinition attrDef = getAttributeDefinition(sess, variant);
                            depSet.add(attrDef);
                        } catch (AttributeNotExistsException ex) {
                            log.error("For attribute name " + variant + "can't be found attributeDefinition at Inicialization in AttributesManagerBlImpl.");
                        }
                    }
                }
            }
            //Fil Set of strongDependencies
            for (String s : strongDepList) {
                if (!s.endsWith("*")) {
                    try {
                        AttributeDefinition attrDef = getAttributeDefinition(sess, s);
                        strongDepSet.add(attrDef);
                    } catch (AttributeNotExistsException ex) {
                        log.error("For attribute name " + s + "can't be found attributeDefinition at Inicialization in AttributesManagerBlImpl.");
                    }
                //If there is something like AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace" + ":*" we need to replace * by all possibilities
                } else {
                    List<String> allVariantOfDependence = getAllSimilarAttributeNames(sess, s.substring(0, s.length() - 2));
                    for (String variant : allVariantOfDependence) {
                        try {
                            AttributeDefinition attrDef = getAttributeDefinition(sess, variant);
                            strongDepSet.add(attrDef);
                        } catch (AttributeNotExistsException ex) {
                            log.error("For attribute name " + variant + "can't be found attributeDefinition at Inicialization in AttributesManagerBlImpl.");
                        }
                    }
                }
            }
        }
        dependencies.put(ad, depSet);
        strongDependencies.put(ad, strongDepSet);
    }
    log.debug("Dependencies and StrongDependencies was filled successfully.");
    log.debug("InverseDependencies and InverseStrongDependencies filling started.");
    //First create inversion map for simple dependencies
    Set<AttributeDefinition> depSet = dependencies.keySet();
    for (AttributeDefinition key : depSet) {
        Set<AttributeDefinition> keySet = new HashSet<AttributeDefinition>();
        keySet = dependencies.get(key);
        for (AttributeDefinition keySetItem : keySet) {
            Set<AttributeDefinition> changeSet = new HashSet<AttributeDefinition>();
            changeSet = inverseDependencies.get(keySetItem);
            changeSet.add(key);
        //inverseDependencies.put(keySetItem, changeSet);
        }
    }
    //Second create inversion map for strong dependencies
    depSet = strongDependencies.keySet();
    for (AttributeDefinition key : depSet) {
        Set<AttributeDefinition> keySet = new HashSet<AttributeDefinition>();
        keySet = strongDependencies.get(key);
        for (AttributeDefinition keySetItem : keySet) {
            Set<AttributeDefinition> changeSet = new HashSet<AttributeDefinition>();
            changeSet = inverseStrongDependencies.get(keySetItem);
            changeSet.add(key);
        //inverseDependencies.put(keySetItem, changeSet);
        }
    }
    log.debug("InverseDependencies and InverseStrongDependencies was filled successfully.");
    log.debug("Cycle test of InverseStrongDependencies started.");
    if (isMapOfAttributesDefCyclic(inverseStrongDependencies)) {
        log.error("There is cycle in inverseStrongDependencies so map of All attribute will be not created!");
    } else {
        log.debug("Cycle test of InverseStrongDependencies was successfull.");
        log.debug("Filling map of allDependencies started.");
        for (AttributeDefinition key : allDependencies.keySet()) {
            List<AttributeDefinition> stackingAttributes = new ArrayList<AttributeDefinition>();
            Set<AttributeDefinition> dependenciesOfAttribute = new HashSet<AttributeDefinition>();
            dependenciesOfAttribute.addAll(inverseStrongDependencies.get(key));
            dependenciesOfAttribute.addAll(inverseDependencies.get(key));
            stackingAttributes.addAll(inverseStrongDependencies.get(key));
            while (!stackingAttributes.isEmpty()) {
                AttributeDefinition firstAttr = stackingAttributes.get(0);
                stackingAttributes.remove(firstAttr);
                dependenciesOfAttribute.addAll(inverseStrongDependencies.get(firstAttr));
                dependenciesOfAttribute.addAll(inverseDependencies.get(firstAttr));
                stackingAttributes.addAll(inverseStrongDependencies.get(firstAttr));
            }
            allDependencies.put(key, dependenciesOfAttribute);
        }
        log.debug("Map of allDependencies was filled successfully.");
    }
    //DEBUG creating file with all dependencies of all attributes (180+- on devel)
    /*String pathToFile = "./AllDependencies.log";
			File f = new File(pathToFile);
			try {
			f.createNewFile();
			PrintWriter writer;
			writer = new PrintWriter(new FileWriter(f, true));
			int i=1;
			for(AttributeDefinition ad: allDependencies.keySet()) {
			writer.println(i + ") " + ad.toString());
			for(AttributeDefinition a: allDependencies.get(ad)) {
			writer.println(" ---> " + a);
			}
			i++;
			}
			writer.close();
			} catch (IOException ex) {
			log.error("Error at saving AllDependencies file.");
			}*/
    //DEBUG end
    log.debug("AttributesManagerBlImpl initialize ended.");
}
Also used : PerunSession(cz.metacentrum.perun.core.api.PerunSession) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) HashSet(java.util.HashSet) UserVirtualAttributesModuleImplApi(cz.metacentrum.perun.core.implApi.modules.attributes.UserVirtualAttributesModuleImplApi) AttributesModuleImplApi(cz.metacentrum.perun.core.implApi.modules.attributes.AttributesModuleImplApi)

Example 7 with PerunPrincipal

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

the class PerunBlImpl method getPerunSession.

/**
	 * This method is used only internally.
	 *
	 */
public PerunSession getPerunSession() throws InternalErrorException {
    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 8 with PerunPrincipal

use of cz.metacentrum.perun.core.api.PerunPrincipal 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);
    } else {
        return;
    }
}
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 9 with PerunPrincipal

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

the class Api method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    if (req.getPathInfo() == null || req.getPathInfo().equals("/")) {
        resp.setContentType("text/plain; charset=utf-8");
        Writer wrt = resp.getWriter();
        PerunPrincipal perunPrincipal;
        try {
            perunPrincipal = setupPerunPrincipal(req);
            wrt.write("OK! Version: " + PerunBl.PERUNVERSION + ", User: " + perunPrincipal.getActor() + ", extSource: " + perunPrincipal.getExtSourceName());
        } catch (InternalErrorException e) {
            wrt.write("ERROR! Exception " + e.getMessage());
        } catch (UserNotExistsException e) {
            wrt.write("ERROR! Exception " + e.getMessage());
        }
        wrt.write("\n");
        wrt.close();
    } else {
        serve(req, resp, true, false);
    }
}
Also used : UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Writer(java.io.Writer)

Example 10 with PerunPrincipal

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

the class ExecServiceDependencyDaoTest method beforeClass.

@Before
public void beforeClass() {
    try {
        perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    } catch (InternalErrorException e) {
        log.error(e.toString());
    }
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    // Test Owner
    int newOwnerId = 0;
    try {
        newOwnerId = Utils.getNewId(jdbcTemplate, "owners_id_seq");
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
    testOwner = new Owner();
    testOwner.setContact("Call me babe");
    testOwner.setType(OwnerType.technical);
    testOwner.setName("Tester-" + Long.toHexString(System.currentTimeMillis()));
    testOwner.setId(newOwnerId);
    jdbcTemplate.update("insert into owners(id, name, contact, type) values (?,?,?,?)", newOwnerId, testOwner.getName(), testOwner.getContact(), testOwner.getType().toString());
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    try {
        testService1.setId(servicesManager.createService(perunSession, testService1).getId());
        testService2.setId(servicesManager.createService(perunSession, testService2).getId());
    } catch (InternalErrorException e) {
        log.error(e.toString());
    } catch (PrivilegeException e) {
        log.error(e.toString());
    } catch (ServiceExistsException e) {
        log.error(e.toString());
    }
    // Test ExecService #1 (Parent:testService1)
    testExecService1 = new ExecService();
    testExecService1.setDefaultDelay(1);
    testExecService1.setDefaultRecurrence(1);
    testExecService1.setEnabled(true);
    testExecService1.setService(testService1);
    testExecService1.setScript("/hellish/test/script");
    testExecService1.setExecServiceType(ExecServiceType.GENERATE);
    try {
        testExecService1.setId(execServiceDao.insertExecService(testExecService1));
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
    // Test ExecService #2 (Parent:testService1)
    testExecService2 = new ExecService();
    testExecService2.setDefaultDelay(2);
    testExecService2.setDefaultRecurrence(2);
    testExecService2.setEnabled(true);
    testExecService2.setService(testService1);
    testExecService2.setScript("/hellish/test/script2");
    testExecService2.setExecServiceType(ExecServiceType.SEND);
    try {
        testExecService2.setId(execServiceDao.insertExecService(testExecService2));
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
    // Test ExecService #3 (Parent:testService2)
    testExecService3 = new ExecService();
    testExecService3.setDefaultDelay(3);
    testExecService3.setDefaultRecurrence(3);
    testExecService3.setEnabled(true);
    testExecService3.setService(testService2);
    testExecService3.setScript("/hellish/test/script3");
    testExecService3.setExecServiceType(ExecServiceType.SEND);
    try {
        testExecService3.setId(execServiceDao.insertExecService(testExecService3));
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
}
Also used : JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) Owner(cz.metacentrum.perun.core.api.Owner) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) ServiceExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Before(org.junit.Before)

Aggregations

PerunPrincipal (cz.metacentrum.perun.core.api.PerunPrincipal)13 PerunClient (cz.metacentrum.perun.core.api.PerunClient)11 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 Before (org.junit.Before)5 Service (cz.metacentrum.perun.core.api.Service)4 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)4 Owner (cz.metacentrum.perun.core.api.Owner)3 PerunSession (cz.metacentrum.perun.core.api.PerunSession)3 JdbcPerunTemplate (org.springframework.jdbc.core.JdbcPerunTemplate)3 Attribute (cz.metacentrum.perun.core.api.Attribute)2 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)2 User (cz.metacentrum.perun.core.api.User)2 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)1 Facility (cz.metacentrum.perun.core.api.Facility)1 Group (cz.metacentrum.perun.core.api.Group)1 Host (cz.metacentrum.perun.core.api.Host)1 Member (cz.metacentrum.perun.core.api.Member)1