Search in sources :

Example 1 with ExportTemplate

use of com.thinkbiganalytics.metadata.api.template.export.ExportTemplate in project kylo by Teradata.

the class FilesystemRepositoryService method publishTemplate.

@Override
public TemplateMetadataWrapper publishTemplate(String repositoryName, String repositoryType, String templateId, boolean overwrite) throws Exception {
    // export template
    ExportTemplate zipFile = templateExporter.exportTemplate(templateId);
    // get repository
    TemplateRepository repository = getRepositoryByNameAndType(repositoryName, repositoryType);
    // Check if template already exists
    Optional<TemplateMetadataWrapper> foundMetadataOpt = Optional.empty();
    for (TemplateMetadataWrapper item : listTemplatesByRepository(repository)) {
        if (item.getTemplateName().equals(zipFile.getTemplateName())) {
            foundMetadataOpt = Optional.of(item);
            break;
        }
    }
    if (foundMetadataOpt.isPresent()) {
        if (!overwrite) {
            throw new UnsupportedOperationException("Template with same name already exists.");
        }
        log.info("Overwriting template with same name.");
    }
    String digest = DigestUtils.md5DigestAsHex(zipFile.getFile());
    Path templatePath = Paths.get(repository.getLocation() + "/" + zipFile.getFileName());
    TemplateMetadata metadata = new TemplateMetadata(zipFile.getTemplateName(), zipFile.getDescription(), zipFile.getFileName().toString(), digest, zipFile.isStream(), false, templatePath.toFile().lastModified());
    // create repositoryItem
    TemplateMetadataWrapper templateMetadata = foundMetadataOpt.isPresent() ? foundMetadataOpt.get() : new TemplateMetadataWrapper(metadata);
    String baseName = FilenameUtils.getBaseName(templateMetadata.getFileName());
    // write file in first of templateLocations
    Files.write(templatePath, zipFile.getFile());
    log.info("Finished publishing template {} to repository {}.", templateMetadata.getTemplateName(), repository.getName());
    return new TemplateMetadataWrapper(metadata);
}
Also used : Path(java.nio.file.Path) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata)

Example 2 with ExportTemplate

use of com.thinkbiganalytics.metadata.api.template.export.ExportTemplate in project kylo by Teradata.

the class FilesystemRepositoryServiceTest method testPublishTemplate_NewTemplateInRepo_NoOverwriteRequired.

@Test
public void testPublishTemplate_NewTemplateInRepo_NoOverwriteRequired() throws Exception {
    ExportTemplate template = new ExportTemplate("testFile", "testTemplate", "testDesc", false, new byte[0]);
    when(templateExporter.exportTemplate(anyString())).thenReturn(template);
    FilesystemRepositoryService spy = PowerMockito.spy(filesystemRepositoryService);
    doReturn(repository).when(spy, "getRepositoryByNameAndType", anyString(), anyString());
    doReturn(new ArrayList<TemplateMetadata>()).when(spy, "listTemplatesByRepository", repository);
    PowerMockito.when(FilenameUtils.getBaseName(anyString())).thenReturn("testName");
    Path pathMock = mock(Path.class);
    PowerMockito.when(Paths.get(anyString())).thenReturn(pathMock);
    Mockito.when(pathMock.toFile()).thenReturn(mock(File.class));
    Mockito.doNothing().when(mapper).writeValue(any(File.class), any());
    TemplateMetadataWrapper newTemplate = spy.publishTemplate("test", "test", "test", false);
    assertNotNull(newTemplate);
    assertTrue(newTemplate.getTemplateName().equals("testTemplate"));
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) File(java.io.File) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ExportTemplate

use of com.thinkbiganalytics.metadata.api.template.export.ExportTemplate in project kylo by Teradata.

the class FilesystemRepositoryServiceTest method testPublishTemplate_ExistingTemplate_OverwriteFalse.

@Test(expected = UnsupportedOperationException.class)
public void testPublishTemplate_ExistingTemplate_OverwriteFalse() throws Exception {
    ExportTemplate template = new ExportTemplate("testFile", "testTemplate", "testDesc", false, new byte[0]);
    TemplateMetadata existingTemplate = new TemplateMetadata("testTemplate", "testDesc", "xyz.zip", false);
    List<TemplateMetadataWrapper> templates = new ArrayList<>();
    templates.add(new TemplateMetadataWrapper(existingTemplate));
    when(templateExporter.exportTemplate(anyString())).thenReturn(template);
    FilesystemRepositoryService spy = PowerMockito.spy(filesystemRepositoryService);
    doReturn(repository).when(spy, "getRepositoryByNameAndType", anyString(), anyString());
    doReturn(templates).when(spy, "listTemplatesByRepository", repository);
    spy.publishTemplate("test", "test", "test", false);
    PowerMockito.verifyStatic(never());
    FilenameUtils.getBaseName(anyString());
}
Also used : TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ArrayList(java.util.ArrayList) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ExportTemplate

use of com.thinkbiganalytics.metadata.api.template.export.ExportTemplate in project kylo by Teradata.

the class RepositoryMonitor method createMetadata.

private void createMetadata(Path templateFilePath, Map<String, RegisteredTemplate> registeredTemplateMap) {
    RegisteredTemplate tmplt = null;
    try {
        File templateZipFile = templateFilePath.toFile();
        byte[] content = ImportUtil.streamToByteArray(new FileInputStream(templateZipFile));
        InputStream inputStream = new ByteArrayInputStream(content);
        ImportTemplate importTemplate = ImportUtil.openZip(templateFilePath.getFileName().toString(), inputStream);
        tmplt = importTemplate.getTemplateToImport();
        File json = getMetadataFileName(templateFilePath).toFile();
        String checksum = DigestUtils.md5DigestAsHex(content);
        boolean updateAvailable = false;
        TemplateMetadata metadata = new TemplateMetadata(tmplt.getTemplateName(), tmplt.getDescription(), templateFilePath.getFileName().toString(), checksum, tmplt.isStream(), updateAvailable, tmplt.getUpdateDate().getTime());
        // startup or new template is published
        if (!json.exists()) {
            if (registeredTemplateMap.containsKey(tmplt.getTemplateName())) {
                final String templateName = tmplt.getTemplateName();
                ExportTemplate zipFile = metadataAccess.read(() -> {
                    this.accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.ACCESS_TEMPLATES);
                    return templateExporter.exportTemplate(registeredTemplateMap.get(templateName).getId());
                }, MetadataAccess.SERVICE);
                String digest = DigestUtils.md5DigestAsHex(zipFile.getFile());
                updateAvailable = !StringUtils.equals(checksum, digest);
                metadata.setUpdateAvailable(updateAvailable);
                templateUpdateInfoCache.put(templateName, updateAvailable);
            }
        } else {
            // if registered template was updated
            metadata = mapper.readValue(json, TemplateMetadata.class);
            templateUpdateInfoCache.put(tmplt.getTemplateName(), metadata.isUpdateAvailable());
            if (metadata.getLastModified() >= tmplt.getUpdateDate().getTime()) {
                return;
            }
            if (StringUtils.equals(metadata.getChecksum(), checksum)) {
                return;
            }
            metadata.setUpdateAvailable(true);
            templateUpdateInfoCache.put(tmplt.getTemplateName(), true);
        }
        log.info("Writing template metadata for {}.", tmplt.getTemplateName());
        mapper.writerWithDefaultPrettyPrinter().writeValue(json, metadata);
    } catch (Exception e) {
        log.error("Error occurred trying to generate template metadata.", e);
        if (tmplt != null) {
            templateUpdateInfoCache.invalidate(tmplt.getTemplateName());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) File(java.io.File) FileInputStream(java.io.FileInputStream) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate)

Example 5 with ExportTemplate

use of com.thinkbiganalytics.metadata.api.template.export.ExportTemplate in project kylo by Teradata.

the class DefaultFeedExporter method exportFeed.

/**
 * Export a feed as a zip file
 *
 * @param feedId the id {@link Feed#getId()} of the feed to export
 * @return object containing the zip file with data about the feed.
 */
@Override
public ExportFeed exportFeed(String feedId) throws IOException {
    this.accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EXPORT_FEEDS);
    this.metadataService.checkFeedPermission(feedId, FeedAccessControl.EXPORT);
    // Prepare feed metadata
    final FeedMetadata feed = metadataService.getFeedById(feedId);
    if (feed == null) {
        // feed will not be found when user is allowed to export feeds but has no entity access to feed with feed id
        throw new NotFoundException("Feed not found for id " + feedId);
    }
    /*
        final List<Datasource> userDatasources = Optional.ofNullable(feed.getDataTransformation())
            .map(FeedDataTransformation::getDatasourceIds)
            .map(datasourceIds -> metadataAccess.read(
                () ->
                    datasourceIds.stream()
                        .map(datasourceProvider::resolve)
                        .map(datasourceProvider::getDatasource)
                        .map(domain -> datasourceTransform.toDatasource(domain, DatasourceModelTransform.Level.FULL))
                        .map(datasource -> {
                            // Clear sensitive fields
                            datasource.getDestinationForFeeds().clear();
                            datasource.getSourceForFeeds().clear();
                            return datasource;
                        })
                        .collect(Collectors.toList())
                 )
            )
            .orElse(null);
        if (userDatasources != null && !userDatasources.isEmpty()) {
            this.accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.ACCESS_DATASOURCES);
            feed.setUserDatasources(userDatasources);
        }
        */
    // export the datasets ???
    String dataSetJSON = "[]";
    if (!feed.getSourceDataSets().isEmpty()) {
        // refetch them?
        List<com.thinkbiganalytics.kylo.catalog.rest.model.DataSet> dataSets = feed.getSourceDataSets().stream().map(dataSet -> {
            return metadataAccess.read(() -> {
                return dataSetProvider.find(dataSetProvider.resolveId(dataSet.getId())).map(ds -> catalogModelTransform.dataSetToRestModel().apply(ds)).orElse(null);
            });
        }).filter(dataSet -> dataSet != null).collect(Collectors.toList());
        dataSetJSON = ObjectMapperSerializer.serialize(dataSets);
    }
    // Add feed json to template zip file
    final ExportTemplate exportTemplate = templateExporter.exportTemplateForFeedExport(feed.getTemplateId());
    final String feedJson = ObjectMapperSerializer.serialize(feed);
    byte[] zipFile = ZipFileUtil.addToZip(exportTemplate.getFile(), feedJson, ImportFeed.FEED_JSON_FILE);
    final byte[] zipFile2 = ZipFileUtil.addToZip(zipFile, dataSetJSON, ImportFeed.FEED_DATASETS_FILE);
    return new ExportFeed(feed.getSystemFeedName() + ".feed.zip", zipFile2);
}
Also used : FeedDataTransformation(com.thinkbiganalytics.feedmgr.rest.model.FeedDataTransformation) Feed(com.thinkbiganalytics.metadata.api.feed.Feed) ExportFeed(com.thinkbiganalytics.metadata.api.feed.export.ExportFeed) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) DataSet(com.thinkbiganalytics.metadata.api.catalog.DataSet) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) DefaultTemplateExporter(com.thinkbiganalytics.feedmgr.service.template.exporting.DefaultTemplateExporter) Inject(javax.inject.Inject) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) DataSetProvider(com.thinkbiganalytics.metadata.api.catalog.DataSetProvider) FeedAccessControl(com.thinkbiganalytics.metadata.api.feed.security.FeedAccessControl) AccessController(com.thinkbiganalytics.security.AccessController) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) CatalogModelTransform(com.thinkbiganalytics.kylo.catalog.rest.model.CatalogModelTransform) MetadataService(com.thinkbiganalytics.feedmgr.service.MetadataService) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) TemplateExporter(com.thinkbiganalytics.metadata.api.template.export.TemplateExporter) IOException(java.io.IOException) FeedExporter(com.thinkbiganalytics.metadata.api.feed.export.FeedExporter) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ZipFileUtil(com.thinkbiganalytics.feedmgr.support.ZipFileUtil) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) List(java.util.List) ImportFeed(com.thinkbiganalytics.feedmgr.service.feed.importing.model.ImportFeed) Optional(java.util.Optional) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) ExportFeed(com.thinkbiganalytics.metadata.api.feed.export.ExportFeed) DataSet(com.thinkbiganalytics.metadata.api.catalog.DataSet) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NotFoundException(javax.ws.rs.NotFoundException) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate)

Aggregations

ExportTemplate (com.thinkbiganalytics.metadata.api.template.export.ExportTemplate)6 TemplateMetadataWrapper (com.thinkbiganalytics.repository.api.TemplateMetadataWrapper)3 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)2 FeedServicesAccessControl (com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl)2 ImportTemplate (com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate)2 ObjectMapperSerializer (com.thinkbiganalytics.json.ObjectMapperSerializer)2 TemplateExporter (com.thinkbiganalytics.metadata.api.template.export.TemplateExporter)2 TemplateMetadata (com.thinkbiganalytics.repository.api.TemplateMetadata)2 AccessController (com.thinkbiganalytics.security.AccessController)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TemplateConnectionUtil (com.thinkbiganalytics.feedmgr.nifi.TemplateConnectionUtil)1