Search in sources :

Example 6 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)

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