Search in sources :

Example 11 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredential2.

@Test
public void testGetBusinessObjectDataDownloadCredential2() throws Exception {
    DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
    manifest.setNamespace("test1");
    manifest.setBusinessObjectDefinitionName("test2");
    manifest.setBusinessObjectFormatUsage("test3");
    manifest.setBusinessObjectFormatFileType("test4");
    manifest.setBusinessObjectFormatVersion("test5");
    manifest.setPartitionValue("test6");
    manifest.setBusinessObjectDataVersion("test9");
    String storageName = "test10";
    downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(true);
    StorageUnitDownloadCredential storageUnitDownloadCredential = downloaderWebClient.getStorageUnitDownloadCredential(manifest, storageName);
    Assert.assertNotNull(storageUnitDownloadCredential);
    AwsCredential awsCredential = storageUnitDownloadCredential.getAwsCredential();
    Assert.assertNotNull(awsCredential);
    Assert.assertEquals("https://testWebServiceHostname:1234/herd-app/rest/storageUnits/download/credential/namespaces/test1" + "/businessObjectDefinitionNames/test2/businessObjectFormatUsages/test3/businessObjectFormatFileTypes/test4/businessObjectFormatVersions/test5" + "/partitionValues/test6/businessObjectDataVersions/test9/storageNames/test10", awsCredential.getAwsAccessKey());
}
Also used : DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Test(org.junit.Test)

Example 12 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetData.

@Test
public void testGetData() throws Exception {
    // Upload and register business object data parents.
    uploadAndRegisterTestDataParents(downloaderWebClient);
    // Upload and register the initial version if of the test business object data.
    uploadTestDataFilesToS3(S3_TEST_PATH_V0);
    final UploaderInputManifestDto uploaderInputManifestDto = getTestUploaderInputManifestDto();
    executeWithoutLogging(DataBridgeWebClient.class, new Command() {

        @Override
        public void execute() throws Exception {
            BusinessObjectData businessObjectData = downloaderWebClient.preRegisterBusinessObjectData(uploaderInputManifestDto, StorageEntity.MANAGED_STORAGE, false);
            BusinessObjectDataKey businessObjectDataKey = businessObjectDataHelper.getBusinessObjectDataKey(businessObjectData);
            downloaderWebClient.addStorageFiles(businessObjectDataKey, uploaderInputManifestDto, getTestS3FileTransferRequestParamsDto(S3_TEST_PATH_V0 + "/"), StorageEntity.MANAGED_STORAGE);
            downloaderWebClient.updateBusinessObjectDataStatus(businessObjectDataKey, BusinessObjectDataStatusEntity.VALID);
        }
    });
    // Get business object data information.
    DownloaderInputManifestDto downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    BusinessObjectData resultBusinessObjectData = downloaderWebClient.getBusinessObjectData(downloaderInputManifestDto);
    // Validate the results.
    assertNotNull(resultBusinessObjectData);
}
Also used : UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderManifestReaderTest method testReadJsonManifestMissingRequiredParameters.

@Test
public void testReadJsonManifestMissingRequiredParameters() throws IOException {
    DownloaderInputManifestDto downloaderInputManifestDto;
    // Try to create and read the downloader input manifest when business object definition name is not specified.
    downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    downloaderInputManifestDto.setBusinessObjectDefinitionName(BLANK_TEXT);
    try {
        downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
        fail("Should throw an IllegalArgumentException when business object definition name is not specified.");
    } catch (IllegalArgumentException e) {
        assertEquals("Manifest business object definition name must be specified.", e.getMessage());
    }
    // Try to create and read the downloader input manifest when business object format usage is not specified.
    downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    downloaderInputManifestDto.setBusinessObjectFormatUsage(BLANK_TEXT);
    try {
        downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
        fail("Should throw an IllegalArgumentException when business object format usage is not specified.");
    } catch (IllegalArgumentException e) {
        assertEquals("Manifest business object format usage must be specified.", e.getMessage());
    }
    // Try to create and read the downloader input manifest when business object format file type is not specified.
    downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    downloaderInputManifestDto.setBusinessObjectFormatFileType(BLANK_TEXT);
    try {
        downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
        fail("Should throw an IllegalArgumentException when business object format file type is not specified.");
    } catch (IllegalArgumentException e) {
        assertEquals("Manifest business object format file type must be specified.", e.getMessage());
    }
    // Try to create and read the downloader input manifest when partition key is not specified.
    downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    downloaderInputManifestDto.setPartitionKey(BLANK_TEXT);
    try {
        downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
        fail("Should throw an IllegalArgumentException when partition key is not specified.");
    } catch (IllegalArgumentException e) {
        assertEquals("Manifest business object format partition key must be specified.", e.getMessage());
    }
    // Try to create and read the downloader input manifest when partition value is not specified.
    downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    downloaderInputManifestDto.setPartitionValue(BLANK_TEXT);
    try {
        downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
        fail("Should throw an IllegalArgumentException when partition value is not specified.");
    } catch (IllegalArgumentException e) {
        assertEquals("Manifest business object data partition value must be specified.", e.getMessage());
    }
}
Also used : DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) Test(org.junit.Test)

Example 14 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderManifestReaderTest method testReadJsonManifest.

@Test
public void testReadJsonManifest() throws IOException {
    // Create and read a downloader input manifest file.
    DownloaderInputManifestDto downloaderInputManifestDto = getTestDownloaderInputManifestDto();
    DownloaderInputManifestDto resultManifest = downloaderManifestReader.readJsonManifest(createManifestFile(LOCAL_TEMP_PATH_INPUT.toString(), downloaderInputManifestDto));
    // Validate the results.
    assertDownloaderManifest(downloaderInputManifestDto, resultManifest);
}
Also used : DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) Test(org.junit.Test)

Example 15 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderController method performDownload.

/**
 * Executes the downloader workflow.
 *
 * @param regServerAccessParamsDto the DTO for the parameters required to communicate with the herd registration server
 * @param manifestPath the local path to the manifest file
 * @param s3FileTransferRequestParamsDto the S3 file transfer DTO request parameters
 *
 * @throws InterruptedException if the upload thread was interrupted.
 * @throws JAXBException if a JAXB error was encountered.
 * @throws IOException if an I/O error was encountered.
 * @throws URISyntaxException if a URI syntax error was encountered.
 */
@SuppressFBWarnings(value = { "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" }, justification = "manifestReader.readJsonManifest will always return an DownloaderInputManifestDto object. targetLocalDirectory.list().length will not" + " return a NullPointerException.")
public void performDownload(RegServerAccessParamsDto regServerAccessParamsDto, File manifestPath, S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) throws InterruptedException, JAXBException, IOException, URISyntaxException {
    boolean cleanUpTargetLocalDirectoryOnFailure = false;
    File targetLocalDirectory = null;
    try {
        // Process manifest file.
        DownloaderInputManifestDto manifest = manifestReader.readJsonManifest(manifestPath);
        String storageName = getStorageNameFromManifest(manifest);
        // Get business object data from the herd registration server.
        downloaderWebClient.setRegServerAccessParamsDto(regServerAccessParamsDto);
        BusinessObjectData businessObjectData = downloaderWebClient.getBusinessObjectData(manifest);
        manifest.setBusinessObjectDataVersion(String.valueOf(businessObjectData.getVersion()));
        manifest.setBusinessObjectFormatVersion(String.valueOf(businessObjectData.getBusinessObjectFormatVersion()));
        s3FileTransferRequestParamsDto.getAdditionalAwsCredentialsProviders().add(new AutoRefreshCredentialProvider() {

            @Override
            public AwsCredential getNewAwsCredential() throws Exception {
                return downloaderWebClient.getStorageUnitDownloadCredential(manifest, storageName).getAwsCredential();
            }
        });
        // Get a storage unit that belongs to the S3 storage.
        StorageUnit storageUnit = businessObjectDataHelper.getStorageUnitByStorageName(businessObjectData, storageName);
        // Get the expected S3 key prefix and S3 bucket name.
        S3KeyPrefixInformation s3KeyPrefixInformation = downloaderWebClient.getS3KeyPrefix(businessObjectData);
        // Check if the target folder (local directory + S3 key prefix) exists and try to create it if it does not.
        targetLocalDirectory = Paths.get(s3FileTransferRequestParamsDto.getLocalPath(), s3KeyPrefixInformation.getS3KeyPrefix()).toFile();
        if (!targetLocalDirectory.isDirectory()) {
            // Create the local directory including any necessary but nonexistent parent directories.
            if (!targetLocalDirectory.mkdirs()) {
                throw new IllegalArgumentException(String.format("Failed to create target local directory \"%s\".", targetLocalDirectory.getPath()));
            }
        } else {
            // Check if the target local directory is empty.
            if (targetLocalDirectory.list().length > 0) {
                throw new IllegalArgumentException(String.format("The target local directory \"%s\" is not empty.", targetLocalDirectory.getPath()));
            }
        }
        // Get S3 bucket information.
        Storage storage = downloaderWebClient.getStorage(storageName);
        // Get S3 bucket name.  Please note that since this value is required we pass a "true" flag.
        String s3BucketName = storageHelper.getStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), storage, true);
        // Get the list of S3 files matching the expected S3 key prefix.
        s3FileTransferRequestParamsDto.setS3BucketName(s3BucketName);
        // Since the S3 key prefix represents a directory, we add a trailing '/' character to it.
        s3FileTransferRequestParamsDto.setS3KeyPrefix(s3KeyPrefixInformation.getS3KeyPrefix() + "/");
        // When listing S3 files, we ignore 0 byte objects that represent S3 directories.
        List<String> actualS3Files = storageFileHelper.getFilePathsFromS3ObjectSummaries(s3Service.listDirectory(s3FileTransferRequestParamsDto, true));
        // Validate S3 files before we start the download.
        storageFileHelper.validateStorageUnitS3Files(storageUnit, actualS3Files, s3KeyPrefixInformation.getS3KeyPrefix());
        // Special handling for the maxThreads command line option.
        s3FileTransferRequestParamsDto.setMaxThreads(adjustIntegerValue(s3FileTransferRequestParamsDto.getMaxThreads(), MIN_THREADS, MAX_THREADS));
        // Download S3 files to the target local directory.
        s3FileTransferRequestParamsDto.setRecursive(true);
        cleanUpTargetLocalDirectoryOnFailure = true;
        s3Service.downloadDirectory(s3FileTransferRequestParamsDto);
        // Validate the downloaded files.
        storageFileHelper.validateDownloadedS3Files(s3FileTransferRequestParamsDto.getLocalPath(), s3KeyPrefixInformation.getS3KeyPrefix(), storageUnit);
        // Log a list of files downloaded to the target local directory.
        if (LOGGER.isInfoEnabled()) {
            logLocalDirectoryContents(targetLocalDirectory);
        }
        // Create a downloader output manifest file.
        DownloaderOutputManifestDto downloaderOutputManifestDto = createDownloaderOutputManifestDto(businessObjectData, storageUnit, s3KeyPrefixInformation.getS3KeyPrefix());
        manifestWriter.writeJsonManifest(targetLocalDirectory, OUTPUT_MANIFEST_FILE_NAME, downloaderOutputManifestDto);
    } catch (InterruptedException | JAXBException | IOException | URISyntaxException e) {
        // occurred, let's rollback the data transfer by cleaning up the local target directory.
        if (cleanUpTargetLocalDirectoryOnFailure) {
            LOGGER.info(String.format("Rolling back the S3 data transfer by cleaning up \"%s\" target local directory.", targetLocalDirectory));
            HerdFileUtils.cleanDirectoryIgnoreException(targetLocalDirectory);
        }
        throw e;
    }
}
Also used : BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) AutoRefreshCredentialProvider(org.finra.herd.tools.common.databridge.AutoRefreshCredentialProvider) JAXBException(javax.xml.bind.JAXBException) DownloaderOutputManifestDto(org.finra.herd.model.dto.DownloaderOutputManifestDto) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) Storage(org.finra.herd.model.api.xml.Storage) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) ManifestFile(org.finra.herd.model.dto.ManifestFile) File(java.io.File) StorageFile(org.finra.herd.model.api.xml.StorageFile) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

DownloaderInputManifestDto (org.finra.herd.model.dto.DownloaderInputManifestDto)16 Test (org.junit.Test)14 IOException (java.io.IOException)8 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)8 AwsCredential (org.finra.herd.model.api.xml.AwsCredential)7 S3KeyPrefixInformation (org.finra.herd.model.api.xml.S3KeyPrefixInformation)7 Storage (org.finra.herd.model.api.xml.Storage)7 StorageUnit (org.finra.herd.model.api.xml.StorageUnit)7 StorageUnitDownloadCredential (org.finra.herd.model.api.xml.StorageUnitDownloadCredential)7 RegServerAccessParamsDto (org.finra.herd.model.dto.RegServerAccessParamsDto)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 HttpClientOperations (org.finra.herd.dao.HttpClientOperations)5 File (java.io.File)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 BasicStatusLine (org.apache.http.message.BasicStatusLine)4 Command (org.finra.herd.core.Command)4 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)4 UploaderInputManifestDto (org.finra.herd.model.dto.UploaderInputManifestDto)4 Objects (com.google.common.base.Objects)3