Search in sources :

Example 1 with BridgeSynapseException

use of org.sagebionetworks.bridge.exceptions.BridgeSynapseException in project BridgeServer2 by Sage-Bionetworks.

the class Exporter3Service method initExporter3.

/**
 * Initializes configs and Synapse resources for Exporter 3.0. Note that if any config already exists, this API
 * will simply ignore them. This allows for two notable scenarios
 * (a) Advanced users can use existing projects or data access teams for Exporter 3.0.
 * (b) If in the future, we add something new (like a notification queue, or a default view), we can re-run this
 * API to create the new stuff without affecting the old stuff.
 */
public Exporter3Configuration initExporter3(String appId) throws BridgeSynapseException, IOException, SynapseException {
    boolean isAppModified = false;
    App app = appService.getApp(appId);
    // Init the Exporter3Config object.
    Exporter3Configuration ex3Config = app.getExporter3Configuration();
    if (ex3Config == null) {
        ex3Config = new Exporter3Configuration();
        app.setExporter3Configuration(ex3Config);
        isAppModified = true;
    }
    // Name in Synapse are globally unique, so we add a random token to the name to ensure it
    // doesn't conflict with an existing name. Also, Synapse names can only contain a certain
    // subset of characters. We've verified this name is acceptable for this transformation.
    String synapseName = BridgeUtils.toSynapseFriendlyName(app.getName());
    String nameScopingToken = getNameScopingToken();
    // Create data access team.
    Long dataAccessTeamId = ex3Config.getDataAccessTeamId();
    if (dataAccessTeamId == null) {
        Team team = new Team();
        team.setName(synapseName + " Access Team " + nameScopingToken);
        team = synapseHelper.createTeamWithRetry(team);
        dataAccessTeamId = Long.parseLong(team.getId());
        LOG.info("Created Synapse team " + dataAccessTeamId);
        ex3Config.setDataAccessTeamId(dataAccessTeamId);
        isAppModified = true;
    }
    Set<Long> adminPrincipalIds = ImmutableSet.of(exporterSynapseId, bridgeAdminTeamId);
    Set<Long> readOnlyPrincipalIds = ImmutableSet.of(bridgeStaffTeamId, dataAccessTeamId);
    // Create project.
    String projectId = ex3Config.getProjectId();
    if (projectId == null) {
        Project project = new Project();
        project.setName(synapseName + " Project " + nameScopingToken);
        project = synapseHelper.createEntityWithRetry(project);
        projectId = project.getId();
        LOG.info("Created Synapse project " + projectId);
        // Create ACLs for project.
        synapseHelper.createAclWithRetry(projectId, adminPrincipalIds, readOnlyPrincipalIds);
        ex3Config.setProjectId(projectId);
        isAppModified = true;
        // We also need to add this project to the tracking view.
        if (StringUtils.isNotBlank(synapseTrackingViewId)) {
            try {
                EntityView view = synapseHelper.getEntityWithRetry(synapseTrackingViewId, EntityView.class);
                if (view != null) {
                    // For whatever reason, view.getScopes() doesn't include the "syn" prefix.
                    view.getScopeIds().add(projectId.substring(3));
                    synapseHelper.updateEntityWithRetry(view);
                }
            } catch (SynapseException ex) {
                LOG.error("Error adding new project " + projectId + " to tracking view " + synapseTrackingViewId + ": " + ex.getMessage(), ex);
            }
        }
    }
    // Create Participant Version Table.
    String participantVersionTableId = ex3Config.getParticipantVersionTableId();
    if (participantVersionTableId == null) {
        participantVersionTableId = synapseHelper.createTableWithColumnsAndAcls(PARTICIPANT_VERSION_COLUMN_MODELS, readOnlyPrincipalIds, adminPrincipalIds, projectId, TABLE_NAME_PARTICIPANT_VERSIONS);
        LOG.info("Created Synapse table " + participantVersionTableId);
        ex3Config.setParticipantVersionTableId(participantVersionTableId);
        isAppModified = true;
    }
    // Create Folder for raw data.
    String rawDataFolderId = ex3Config.getRawDataFolderId();
    if (rawDataFolderId == null) {
        Folder folder = new Folder();
        folder.setName(FOLDER_NAME_BRIDGE_RAW_DATA);
        folder.setParentId(projectId);
        folder = synapseHelper.createEntityWithRetry(folder);
        rawDataFolderId = folder.getId();
        LOG.info("Created Synapse folder " + rawDataFolderId);
        // Create ACLs for folder. This is a separate ACL because we don't want to allow people to modify the
        // raw data.
        synapseHelper.createAclWithRetry(rawDataFolderId, adminPrincipalIds, readOnlyPrincipalIds);
        ex3Config.setRawDataFolderId(rawDataFolderId);
        isAppModified = true;
    }
    // Create storage location.
    Long storageLocationId = ex3Config.getStorageLocationId();
    if (storageLocationId == null) {
        // Create owner.txt so that we can create the storage location.
        s3Helper.writeLinesToS3(rawHealthDataBucket, appId + "/owner.txt", ImmutableList.of(exporterSynapseUser));
        // Create storage location.
        ExternalS3StorageLocationSetting storageLocation = new ExternalS3StorageLocationSetting();
        storageLocation.setBaseKey(appId);
        storageLocation.setBucket(rawHealthDataBucket);
        storageLocation.setStsEnabled(true);
        storageLocation = synapseHelper.createStorageLocationForEntity(rawDataFolderId, storageLocation);
        storageLocationId = storageLocation.getStorageLocationId();
        LOG.info("Created Synapse storage location " + storageLocationId);
        ex3Config.setStorageLocationId(storageLocationId);
        isAppModified = true;
    }
    // Finally, enable Exporter 3.0 if it's not already enabled.
    if (!app.isExporter3Enabled()) {
        app.setExporter3Enabled(true);
        isAppModified = true;
    }
    // exporter3config and exporter3enabled.
    if (isAppModified) {
        appService.updateApp(app, true);
    }
    return ex3Config;
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Project(org.sagebionetworks.repo.model.Project) ExternalS3StorageLocationSetting(org.sagebionetworks.repo.model.project.ExternalS3StorageLocationSetting) BridgeSynapseException(org.sagebionetworks.bridge.exceptions.BridgeSynapseException) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) EntityView(org.sagebionetworks.repo.model.table.EntityView) Team(org.sagebionetworks.repo.model.Team) Exporter3Configuration(org.sagebionetworks.bridge.models.apps.Exporter3Configuration) Folder(org.sagebionetworks.repo.model.Folder)

Aggregations

BridgeSynapseException (org.sagebionetworks.bridge.exceptions.BridgeSynapseException)1 App (org.sagebionetworks.bridge.models.apps.App)1 Exporter3Configuration (org.sagebionetworks.bridge.models.apps.Exporter3Configuration)1 SynapseException (org.sagebionetworks.client.exceptions.SynapseException)1 Folder (org.sagebionetworks.repo.model.Folder)1 Project (org.sagebionetworks.repo.model.Project)1 Team (org.sagebionetworks.repo.model.Team)1 ExternalS3StorageLocationSetting (org.sagebionetworks.repo.model.project.ExternalS3StorageLocationSetting)1 EntityView (org.sagebionetworks.repo.model.table.EntityView)1