Search in sources :

Example 1 with SharingRegistryServerHandler

use of org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler in project airavata by apache.

the class AiravataDataMigrator method main.

public static void main(String[] args) throws SQLException, ClassNotFoundException, TException, ApplicationSettingsException {
    Connection expCatConnection = ConnectionFactory.getInstance().getExpCatConnection();
    SharingRegistryServerHandler sharingRegistryServerHandler = new SharingRegistryServerHandler();
    String query = "SELECT * FROM GATEWAY";
    Statement statement = expCatConnection.createStatement();
    ResultSet rs = statement.executeQuery(query);
    while (rs.next()) {
        try {
            // Creating domain entries
            Domain domain = new Domain();
            domain.setDomainId(rs.getString("GATEWAY_ID"));
            domain.setName(rs.getString("GATEWAY_ID"));
            domain.setDescription("Domain entry for " + domain.name);
            if (!sharingRegistryServerHandler.isDomainExists(domain.domainId))
                sharingRegistryServerHandler.createDomain(domain);
            // Creating Entity Types for each domain
            EntityType entityType = new EntityType();
            entityType.setEntityTypeId(domain.domainId + ":PROJECT");
            entityType.setDomainId(domain.domainId);
            entityType.setName("PROJECT");
            entityType.setDescription("Project entity type");
            if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
                sharingRegistryServerHandler.createEntityType(entityType);
            entityType = new EntityType();
            entityType.setEntityTypeId(domain.domainId + ":EXPERIMENT");
            entityType.setDomainId(domain.domainId);
            entityType.setName("EXPERIMENT");
            entityType.setDescription("Experiment entity type");
            if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
                sharingRegistryServerHandler.createEntityType(entityType);
            entityType = new EntityType();
            entityType.setEntityTypeId(domain.domainId + ":FILE");
            entityType.setDomainId(domain.domainId);
            entityType.setName("FILE");
            entityType.setDescription("File entity type");
            if (!sharingRegistryServerHandler.isEntityTypeExists(entityType.domainId, entityType.entityTypeId))
                sharingRegistryServerHandler.createEntityType(entityType);
            // Creating Permission Types for each domain
            PermissionType permissionType = new PermissionType();
            permissionType.setPermissionTypeId(domain.domainId + ":READ");
            permissionType.setDomainId(domain.domainId);
            permissionType.setName("READ");
            permissionType.setDescription("Read permission type");
            if (!sharingRegistryServerHandler.isPermissionExists(permissionType.domainId, permissionType.permissionTypeId))
                sharingRegistryServerHandler.createPermissionType(permissionType);
            permissionType = new PermissionType();
            permissionType.setPermissionTypeId(domain.domainId + ":WRITE");
            permissionType.setDomainId(domain.domainId);
            permissionType.setName("WRITE");
            permissionType.setDescription("Write permission type");
            if (!sharingRegistryServerHandler.isPermissionExists(permissionType.domainId, permissionType.permissionTypeId))
                sharingRegistryServerHandler.createPermissionType(permissionType);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // Creating user entries
    query = "SELECT * FROM USERS";
    statement = expCatConnection.createStatement();
    rs = statement.executeQuery(query);
    while (rs.next()) {
        try {
            User user = new User();
            user.setUserId(rs.getString("AIRAVATA_INTERNAL_USER_ID"));
            user.setDomainId(rs.getString("GATEWAY_ID"));
            user.setUserName(rs.getString("USER_NAME"));
            if (!sharingRegistryServerHandler.isUserExists(user.domainId, user.userId))
                sharingRegistryServerHandler.createUser(user);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // Creating project entries
    query = "SELECT * FROM PROJECT";
    statement = expCatConnection.createStatement();
    rs = statement.executeQuery(query);
    while (rs.next()) {
        try {
            Entity entity = new Entity();
            entity.setEntityId(rs.getString("PROJECT_ID"));
            entity.setDomainId(rs.getString("GATEWAY_ID"));
            entity.setEntityTypeId(rs.getString("GATEWAY_ID") + ":PROJECT");
            entity.setOwnerId(rs.getString("USER_NAME") + "@" + rs.getString("GATEWAY_ID"));
            entity.setName(rs.getString("PROJECT_NAME"));
            entity.setDescription(rs.getString("DESCRIPTION"));
            if (entity.getDescription() == null)
                entity.setFullText(entity.getName());
            else
                entity.setFullText(entity.getName() + " " + entity.getDescription());
            Map<String, String> metadata = new HashMap<>();
            metadata.put("CREATION_TIME", rs.getDate("CREATION_TIME").toString());
            if (!sharingRegistryServerHandler.isEntityExists(entity.domainId, entity.entityId))
                sharingRegistryServerHandler.createEntity(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // Creating experiment entries
    query = "SELECT * FROM EXPERIMENT";
    statement = expCatConnection.createStatement();
    rs = statement.executeQuery(query);
    while (rs.next()) {
        try {
            Entity entity = new Entity();
            entity.setEntityId(rs.getString("EXPERIMENT_ID"));
            entity.setDomainId(rs.getString("GATEWAY_ID"));
            entity.setEntityTypeId(rs.getString("GATEWAY_ID") + ":EXPERIMENT");
            entity.setOwnerId(rs.getString("USER_NAME") + "@" + rs.getString("GATEWAY_ID"));
            entity.setParentEntityId(rs.getString("PROJECT_ID"));
            entity.setName(rs.getString("EXPERIMENT_NAME"));
            entity.setDescription(rs.getString("DESCRIPTION"));
            if (entity.getDescription() == null)
                entity.setFullText(entity.getName());
            else
                entity.setFullText(entity.getName() + " " + entity.getDescription());
            Map<String, String> metadata = new HashMap<>();
            metadata.put("CREATION_TIME", rs.getDate("CREATION_TIME").toString());
            metadata.put("EXPERIMENT_TYPE", rs.getString("EXPERIMENT_TYPE"));
            metadata.put("EXECUTION_ID", rs.getString("EXECUTION_ID"));
            metadata.put("GATEWAY_EXECUTION_ID", rs.getString("GATEWAY_EXECUTION_ID"));
            metadata.put("ENABLE_EMAIL_NOTIFICATION", rs.getString("ENABLE_EMAIL_NOTIFICATION"));
            metadata.put("EMAIL_ADDRESSES", rs.getString("EMAIL_ADDRESSES"));
            metadata.put("GATEWAY_INSTANCE_ID", rs.getString("GATEWAY_INSTANCE_ID"));
            metadata.put("ARCHIVE", rs.getString("ARCHIVE"));
            if (!sharingRegistryServerHandler.isEntityExists(entity.domainId, entity.entityId))
                sharingRegistryServerHandler.createEntity(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    expCatConnection.close();
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) SharingRegistryServerHandler(org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler) Connection(java.sql.Connection) SQLException(java.sql.SQLException) TException(org.apache.thrift.TException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) ResultSet(java.sql.ResultSet)

Example 2 with SharingRegistryServerHandler

use of org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler in project airavata by apache.

the class SharingRegistryServerHandlerTest method test.

@Test
public void test() throws TException, ApplicationSettingsException {
    SharingRegistryServerHandler sharingRegistryServerHandler = new SharingRegistryServerHandler();
    // Creating domain
    Domain domain = new Domain();
    String domainId = "test-domain." + System.currentTimeMillis();
    domain.setDomainId(domainId);
    domain.setName(domainId);
    domain.setDescription("test domain description");
    domain.setCreatedTime(System.currentTimeMillis());
    domain.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createDomain(domain));
    Assert.assertTrue(sharingRegistryServerHandler.getDomains(0, 10).size() > 0);
    // Creating users
    User user1 = new User();
    String userName1 = "test-user-1." + System.currentTimeMillis();
    String userId1 = domainId + ":" + userName1;
    user1.setUserId(userId1);
    user1.setUserName(userName1);
    user1.setDomainId(domainId);
    user1.setCreatedTime(System.currentTimeMillis());
    user1.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createUser(user1));
    User user2 = new User();
    String userName2 = "test-user-2." + System.currentTimeMillis();
    String userId2 = domainId + ":" + userName2;
    user2.setUserId(userId2);
    user2.setUserName(userName2);
    user2.setDomainId(domainId);
    user2.setCreatedTime(System.currentTimeMillis());
    user2.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createUser(user2));
    User user3 = new User();
    String userName3 = "test-user-3." + System.currentTimeMillis();
    String userId3 = domainId + ":" + userName3;
    user3.setUserId(userId3);
    user3.setUserName(userName3);
    user3.setDomainId(domainId);
    user3.setCreatedTime(System.currentTimeMillis());
    user3.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createUser(user3));
    Assert.assertTrue(sharingRegistryServerHandler.getUsers(domainId, 0, 10).size() > 0);
    // Creating user groups
    UserGroup userGroup1 = new UserGroup();
    String groupName1 = "test-group-1." + System.currentTimeMillis();
    String groupId1 = domainId + ":" + groupName1;
    userGroup1.setGroupId(groupId1);
    userGroup1.setDomainId(domainId);
    userGroup1.setName(groupName1);
    userGroup1.setDescription("test group description");
    userGroup1.setOwnerId(userId1);
    userGroup1.setGroupType(GroupType.USER_LEVEL_GROUP);
    userGroup1.setGroupCardinality(GroupCardinality.MULTI_USER);
    userGroup1.setCreatedTime(System.currentTimeMillis());
    userGroup1.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createGroup(userGroup1));
    Assert.assertTrue(sharingRegistryServerHandler.getAllMemberGroupsForUser(domainId, userId1).size() == 1);
    UserGroup userGroup2 = new UserGroup();
    String groupName2 = "test-group-2." + System.currentTimeMillis();
    String groupId2 = domainId + ":" + groupName2;
    userGroup2.setGroupId(groupId2);
    userGroup2.setDomainId(domainId);
    userGroup2.setName(groupName2);
    userGroup2.setDescription("test group description");
    userGroup2.setOwnerId(userId2);
    userGroup2.setGroupType(GroupType.USER_LEVEL_GROUP);
    userGroup2.setGroupCardinality(GroupCardinality.MULTI_USER);
    userGroup2.setCreatedTime(System.currentTimeMillis());
    userGroup2.setUpdatedTime(System.currentTimeMillis());
    Assert.assertNotNull(sharingRegistryServerHandler.createGroup(userGroup2));
    sharingRegistryServerHandler.addUsersToGroup(domainId, Arrays.asList(userId1), groupId1);
    sharingRegistryServerHandler.addUsersToGroup(domainId, Arrays.asList(userId2, userId3), groupId2);
    Assert.assertTrue(sharingRegistryServerHandler.getAllMemberGroupsForUser(domainId, userId3).size() == 1);
    sharingRegistryServerHandler.addChildGroupsToParentGroup(domainId, Arrays.asList(groupId2), groupId1);
    Assert.assertTrue(sharingRegistryServerHandler.getGroupMembersOfTypeGroup(domainId, groupId1, 0, 10).size() == 1);
    Assert.assertTrue(sharingRegistryServerHandler.getGroupMembersOfTypeUser(domainId, groupId2, 0, 10).size() == 2);
    // Creating permission types
    PermissionType permissionType1 = new PermissionType();
    String permissionName1 = "READ";
    permissionType1.setPermissionTypeId(domainId + ":" + permissionName1);
    permissionType1.setDomainId(domainId);
    permissionType1.setName(permissionName1);
    permissionType1.setDescription("READ description");
    permissionType1.setCreatedTime(System.currentTimeMillis());
    permissionType1.setUpdatedTime(System.currentTimeMillis());
    String permissionTypeId1 = sharingRegistryServerHandler.createPermissionType(permissionType1);
    Assert.assertNotNull(permissionTypeId1);
    PermissionType permissionType2 = new PermissionType();
    String permissionName2 = "WRITE";
    permissionType2.setPermissionTypeId(domainId + ":" + permissionName2);
    permissionType2.setDomainId(domainId);
    permissionType2.setName(permissionName2);
    permissionType2.setDescription("WRITE description");
    permissionType2.setCreatedTime(System.currentTimeMillis());
    permissionType2.setUpdatedTime(System.currentTimeMillis());
    String permissionTypeId2 = sharingRegistryServerHandler.createPermissionType(permissionType2);
    Assert.assertNotNull(permissionTypeId2);
    // Creating entity types
    EntityType entityType1 = new EntityType();
    String entityType1Name = "Project";
    entityType1.setEntityTypeId(domainId + ":" + entityType1Name);
    entityType1.setDomainId(domainId);
    entityType1.setName(entityType1Name);
    entityType1.setDescription("test entity type");
    entityType1.setCreatedTime(System.currentTimeMillis());
    entityType1.setUpdatedTime(System.currentTimeMillis());
    String entityTypeId1 = sharingRegistryServerHandler.createEntityType(entityType1);
    Assert.assertNotNull(entityTypeId1);
    EntityType entityType2 = new EntityType();
    String entityType2Name = "Experiment";
    entityType2.setEntityTypeId(domainId + ":" + entityType2Name);
    entityType2.setDomainId(domainId);
    entityType2.setName(entityType2Name);
    entityType2.setDescription("test entity type");
    entityType2.setCreatedTime(System.currentTimeMillis());
    entityType2.setUpdatedTime(System.currentTimeMillis());
    String entityTypeId2 = sharingRegistryServerHandler.createEntityType(entityType2);
    Assert.assertNotNull(entityTypeId2);
    EntityType entityType3 = new EntityType();
    String entityType3Name = "FileInput";
    entityType3.setEntityTypeId(domainId + ":" + entityType3Name);
    entityType3.setDomainId(domainId);
    entityType3.setName(entityType3Name);
    entityType3.setDescription("file input type");
    entityType3.setCreatedTime(System.currentTimeMillis());
    entityType3.setUpdatedTime(System.currentTimeMillis());
    String entityTypeId3 = sharingRegistryServerHandler.createEntityType(entityType3);
    Assert.assertNotNull(entityTypeId3);
    // Creating Entities
    Entity entity1 = new Entity();
    entity1.setEntityId(domainId + ":Entity1");
    entity1.setDomainId(domainId);
    entity1.setEntityTypeId(entityTypeId1);
    entity1.setOwnerId(userId1);
    entity1.setName("Project name 1");
    entity1.setDescription("Project description");
    entity1.setFullText("Project name project description");
    entity1.setCreatedTime(System.currentTimeMillis());
    entity1.setUpdatedTime(System.currentTimeMillis());
    String entityId1 = sharingRegistryServerHandler.createEntity(entity1);
    Assert.assertNotNull(entityId1);
    Entity entity2 = new Entity();
    entity2.setEntityId(domainId + ":Entity2");
    entity2.setDomainId(domainId);
    entity2.setEntityTypeId(entityTypeId2);
    entity2.setOwnerId(userId1);
    entity2.setName("Experiment name");
    entity2.setDescription("Experiment description");
    entity2.setParentEntityId(entityId1);
    entity2.setFullText("Project name project description");
    entity2.setCreatedTime(System.currentTimeMillis());
    entity2.setUpdatedTime(System.currentTimeMillis());
    String entityId2 = sharingRegistryServerHandler.createEntity(entity2);
    Assert.assertNotNull(entityId2);
    Entity entity3 = new Entity();
    entity3.setEntityId(domainId + ":Entity3");
    entity3.setDomainId(domainId);
    entity3.setEntityTypeId(entityTypeId2);
    entity3.setOwnerId(userId1);
    entity3.setName("Experiment name");
    entity3.setDescription("Experiment description");
    entity3.setParentEntityId(entityId1);
    entity3.setFullText("Project name project description");
    entity3.setCreatedTime(System.currentTimeMillis());
    entity3.setUpdatedTime(System.currentTimeMillis());
    String entityId3 = sharingRegistryServerHandler.createEntity(entity3);
    Assert.assertNotNull(entityId3);
    sharingRegistryServerHandler.shareEntityWithUsers(domainId, entityId1, Arrays.asList(userId2), permissionTypeId1, true);
    sharingRegistryServerHandler.shareEntityWithGroups(domainId, entityId3, Arrays.asList(groupId2), permissionTypeId1, true);
    Entity entity4 = new Entity();
    entity4.setEntityId(domainId + ":Entity4");
    entity4.setDomainId(domainId);
    entity4.setEntityTypeId(entityTypeId3);
    entity4.setOwnerId(userId3);
    entity4.setName("Input name");
    entity4.setDescription("Input file description");
    entity4.setParentEntityId(entityId3);
    entity4.setFullText("Input File");
    entity4.setCreatedTime(System.currentTimeMillis());
    entity4.setUpdatedTime(System.currentTimeMillis());
    String entityId4 = sharingRegistryServerHandler.createEntity(entity4);
    Assert.assertNotNull(entityId4);
    Assert.assertTrue(sharingRegistryServerHandler.userHasAccess(domainId, userId3, entityId4, permissionTypeId1));
    Assert.assertTrue(sharingRegistryServerHandler.userHasAccess(domainId, userId2, entityId4, permissionTypeId1));
    Assert.assertTrue(sharingRegistryServerHandler.userHasAccess(domainId, userId1, entityId4, permissionTypeId1));
    Assert.assertFalse(sharingRegistryServerHandler.userHasAccess(domainId, userId3, entityId1, permissionTypeId1));
    ArrayList<SearchCriteria> filters = new ArrayList<>();
    SearchCriteria searchCriteria = new SearchCriteria();
    searchCriteria.setSearchCondition(SearchCondition.LIKE);
    searchCriteria.setValue("Input");
    searchCriteria.setSearchField(EntitySearchField.NAME);
    filters.add(searchCriteria);
    searchCriteria = new SearchCriteria();
    searchCriteria.setSearchCondition(SearchCondition.EQUAL);
    searchCriteria.setValue(entityTypeId3);
    searchCriteria.setSearchField(EntitySearchField.ENTITY_TYPE_ID);
    filters.add(searchCriteria);
    Assert.assertTrue(sharingRegistryServerHandler.searchEntities(domainId, userId1, filters, 0, -1).size() > 0);
    Assert.assertNotNull(sharingRegistryServerHandler.getListOfSharedUsers(domainId, entityId1, permissionTypeId1));
    Assert.assertNotNull(sharingRegistryServerHandler.getListOfSharedGroups(domainId, entityId1, permissionTypeId1));
    Assert.assertTrue(sharingRegistryServerHandler.getListOfSharedUsers(domainId, entityId1, domainId + ":OWNER").size() == 1);
// sharingRegistryServerHandler.revokeEntitySharingFromUsers(entityId1, Arrays.asList(userId2), permissionTypeId1);
// sharingRegistryServerHandler.revokeEntitySharingFromGroups(entityId3, Arrays.asList(groupId2), permissionTypeId1);
}
Also used : SharingRegistryServerHandler(org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SharingRegistryServerHandler (org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)1 TException (org.apache.thrift.TException)1 Test (org.junit.Test)1