Search in sources :

Example 11 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class WriterUtils method storeDefinitions.

public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
    Path path = null;
    try {
        path = Files.createTempDirectory("winery");
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOGGER.debug("Store definition: {}", definitions.getId());
    saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
    Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
    CsarImporter csarImporter = new CsarImporter();
    List<Exception> exceptions = new ArrayList<>();
    cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
        String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
        csarImporter.setNamespace(entry, namespace);
        String id = ModelUtilities.getId(entry);
        Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
        final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
        if (RepositoryFactory.getRepository().exists(wid)) {
            if (overwrite) {
                try {
                    RepositoryFactory.getRepository().forceDelete(wid);
                } catch (IOException e) {
                    exceptions.add(e);
                }
            } else {
                return;
            }
        }
        if (entry instanceof TArtifactTemplate) {
            TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
            Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
                String reference = ref.getReference();
                URI refURI;
                try {
                    refURI = new URI(reference);
                } catch (URISyntaxException e) {
                    LOGGER.error("Invalid URI {}", reference);
                    return;
                }
                if (refURI.isAbsolute()) {
                    return;
                }
                Path artifactPath = dir.resolve(reference);
                if (!Files.exists(artifactPath)) {
                    LOGGER.error("File not found {}", artifactPath);
                    return;
                }
                ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
                RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
                MediaType mediaType = null;
                try (InputStream is = Files.newInputStream(artifactPath);
                    BufferedInputStream bis = new BufferedInputStream(is)) {
                    mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
                    RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
                } catch (IOException e) {
                    LOGGER.error("Could not read artifact template file: {}", artifactPath);
                    return;
                }
            });
        }
        final Definitions part = BackendUtils.createWrapperDefinitions(wid);
        part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
        String content = BackendUtils.getXMLAsString(part, true);
        try {
            RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
        } catch (Exception e) {
            exceptions.add(e);
        }
    });
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) Definitions(org.eclipse.winery.model.tosca.Definitions) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType)

Example 12 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class X2YConverter method convert.

public TArtifactDefinition convert(ArtifactTemplateId id) {
    TArtifactTemplate node = repository.getElement(id);
    List<String> files = Optional.ofNullable(repository.getContainedFiles(new ArtifactTemplateFilesDirectoryId(id))).orElse(new TreeSet<>()).stream().map(ref -> {
        try {
            InputStream inputStream = repository.newInputStream(ref);
            Path path = this.path.resolve(id.getGroup()).resolve(id.getNamespace().getEncoded()).resolve(node.getIdFromIdOrNameField()).resolve(ref.getFileName());
            if (!path.toFile().exists()) {
                // noinspection ResultOfMethodCallIgnored
                path.getParent().toFile().mkdirs();
                Files.copy(inputStream, path);
            }
            return this.path.relativize(path).toString();
        } catch (IOException e) {
            LOGGER.error("Failed to copy Artifact file", e);
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    if (Objects.isNull(node) || Objects.isNull(node.getType()) || Objects.isNull(files) || files.isEmpty())
        return null;
    return new TArtifactDefinition.Builder(getQName(new ArtifactTypeId(node.getType()), node.getType().getNamespaceURI(), node.getType().getLocalPart()), files).build();
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 13 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class Utils method compressTarBallAndAddToArtifact.

public static void compressTarBallAndAddToArtifact(Path tempDirectory, IRepository repository, ArtifactTemplateId generatedArtifactTemplateId, String tarball) throws IOException {
    Path compressedFile = Paths.get(Utils.compressTarFile(tempDirectory.resolve(tarball).toFile()));
    ArtifactTemplateFilesDirectoryId filesDirectoryId = new ArtifactTemplateFilesDirectoryId(generatedArtifactTemplateId);
    try (FileInputStream inputStream = new FileInputStream(compressedFile.toFile())) {
        repository.putContentToFile(new RepositoryFileReference(filesDirectoryId, compressedFile.getFileName().toString()), inputStream, MediaType.parse("application/x-gzip"));
    }
    BackendUtils.synchronizeReferences(repository, generatedArtifactTemplateId);
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) FileInputStream(java.io.FileInputStream)

Example 14 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class BackendUtils method synchronizeReferences.

/**
 * Synchronizes the list of files of the given artifact template with the list of files contained in the given
 * repository. The repository is updated after synchronization.
 * <p>
 * This was intended if a user manually added files in the "files" directory and expected winery to correctly export
 * a CSAR
 *
 * @param repository The repository to search for the files
 * @param id         the id of the artifact template
 * @return The synchronized artifact template. Used for testing only, because mockito cannot mock static methods
 * (https://github.com/mockito/mockito/issues/1013).
 */
public static TArtifactTemplate synchronizeReferences(IRepository repository, ArtifactTemplateId id) throws IOException {
    TArtifactTemplate template = repository.getElement(id);
    List<TArtifactReference> toRemove = new ArrayList<>();
    List<RepositoryFileReference> toAdd = new ArrayList<>();
    List<TArtifactReference> artifactReferences = template.getArtifactReferences();
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = repository.getContainedFiles(fileDir);
    if (artifactReferences == null) {
        artifactReferences = new ArrayList<>();
        template.setArtifactReferences(artifactReferences);
    }
    determineChanges(artifactReferences, files, toRemove, toAdd);
    if (toAdd.size() > 0 || toRemove.size() > 0) {
        // apply removal list
        toRemove.forEach(artifactReferences::remove);
        // apply addition list
        artifactReferences.addAll(toAdd.stream().map(fileRef -> {
            String path = Util.getUrlPath(fileRef);
            return new TArtifactReference.Builder(path).build();
        }).collect(Collectors.toList()));
        // finally, persist only if something changed
        BackendUtils.persist(repository, id, template);
    }
    return template;
}
Also used : TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) TArtifactReference(org.eclipse.winery.model.tosca.TArtifactReference) ArrayList(java.util.ArrayList) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) GenericDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.GenericDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 15 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class UbuntuVMPlugin method downloadDependenciesBasedOnNodeType.

@Override
public void downloadDependenciesBasedOnNodeType(TNodeTypeImplementation nodeTypeImplementation, IRepository repository) {
    QName nodeType = nodeTypeImplementation.getNodeType();
    WineryVersion nodeTypeVersion = VersionUtils.getVersion(nodeType.getLocalPart());
    String componentVersion = nodeTypeVersion.getComponentVersion();
    if (componentVersion != null) {
        String codeName = getCodeName(componentVersion);
        if (codeName != null) {
            logger.info("Found code name '{}' for Ubuntu Node Type {}", codeName, nodeType);
            String nameWithoutVersion = VersionUtils.getNameWithoutVersion(nodeType.getLocalPart());
            WineryVersion artifactVersion = new WineryVersion(nodeTypeVersion.getComponentVersion() + "-CloudImage", 1, 1);
            ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId(OpenToscaBaseTypes.artifactTemplateNamespace, nameWithoutVersion + "-DA" + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + artifactVersion, false);
            TArtifactTemplate element = repository.getElement(artifactTemplateId);
            element.setType(OpenToscaBaseTypes.cloudImageArtifactType);
            logger.info("Generated ArtifactTemplate {}", artifactTemplateId.getQName());
            if (!repository.exists(artifactTemplateId)) {
                logger.info("Trying to download image file...");
                String baseUrl = "https://cloud-images.ubuntu.com/" + codeName + "/current/" + codeName + "-server-cloudimg-amd64";
                try {
                    URL url = new URL(baseUrl + imageFileType);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("HEAD");
                    int responseCode = connection.getResponseCode();
                    if (responseCode != 200) {
                        connection.disconnect();
                        logger.info("Image not found, trying with '-disk' suffix...");
                        url = new URL(baseUrl + imageDiskType);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("HEAD");
                        responseCode = connection.getResponseCode();
                    }
                    if (responseCode == 200) {
                        repository.setElement(artifactTemplateId, element);
                        ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
                        String fileName = url.getFile().substring(url.getFile().lastIndexOf("/") + 1);
                        RepositoryFileReference repositoryFileReference = new RepositoryFileReference(filesId, fileName);
                        try (InputStream inputStream = url.openStream()) {
                            repository.putContentToFile(repositoryFileReference, inputStream, MediaType.parse("application/x-image"));
                        }
                        BackendUtils.synchronizeReferences(repository, artifactTemplateId);
                        TDeploymentArtifact imageDa = new TDeploymentArtifact.Builder("CloudImage", OpenToscaBaseTypes.cloudImageArtifactType).setArtifactRef(artifactTemplateId.getQName()).build();
                        List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
                        if (deploymentArtifacts == null) {
                            deploymentArtifacts = new ArrayList<>();
                            nodeTypeImplementation.setDeploymentArtifacts(deploymentArtifacts);
                        }
                        deploymentArtifacts.add(imageDa);
                    } else {
                        logger.info("Could not download image -- the URLs do not exist: \n\t{}\n\t{}", baseUrl + imageFileType, baseUrl + imageDiskType);
                    }
                } catch (IOException e) {
                    logger.info("Error while downloading image file!", e);
                }
            }
        } else {
            logger.info("Could not identify code name of given Ubuntu Node Type! {}", nodeType);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) InputStream(java.io.InputStream) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) URL(java.net.URL) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) HttpURLConnection(java.net.HttpURLConnection) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TDeploymentArtifact(org.eclipse.winery.model.tosca.TDeploymentArtifact) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Aggregations

ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)20 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)8 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)8 Path (java.nio.file.Path)7 IOException (java.io.IOException)6 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)6 Test (org.junit.jupiter.api.Test)6 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)4 QName (javax.xml.namespace.QName)4 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)4 TArtifactReference (org.eclipse.winery.model.tosca.TArtifactReference)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 FileInputStream (java.io.FileInputStream)2 JAXBException (javax.xml.bind.JAXBException)2 TransformerException (javax.xml.transform.TransformerException)2 MediaType (org.apache.tika.mime.MediaType)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2