Search in sources :

Example 56 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.

// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
    Objects.requireNonNull(id);
    Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
    if (!ref.isPresent()) {
        return Collections.emptyList();
    }
    short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
    Date lastUpdate = owner.getLastUpdate(ref.get());
    @NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
    @NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
    boolean cacheNeedsUpdate = true;
    if (owner.exists(cacheRef)) {
        Date lastUpdateCache = owner.getLastUpdate(cacheRef);
        if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
            cacheNeedsUpdate = false;
        }
    }
    List<String> result;
    if (cacheNeedsUpdate) {
        final Optional<XSModel> model = BackendUtils.getXSModel(ref.get(), owner);
        if (!model.isPresent()) {
            return Collections.emptyList();
        }
        XSNamedMap components = model.get().getComponents(type);
        // @SuppressWarnings("unchecked")
        int len = components.getLength();
        result = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            XSObject item = components.item(i);
            // We want to return only types defined in the namespace of this resource
            if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
                result.add(item.getName());
            }
        }
        String cacheContent = null;
        try {
            cacheContent = JacksonProvider.mapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOGGER.error("Could not generate cache content", e);
        }
        try {
            owner.putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } catch (IOException e) {
            LOGGER.error("Could not update cache", e);
        }
    } else {
        // cache should contain most recent information
        try (InputStream is = owner.newInputStream(cacheRef)) {
            result = JacksonProvider.mapper.readValue(is, java.util.List.class);
        } catch (IOException e) {
            LOGGER.error("Could not read from cache", e);
            result = Collections.emptyList();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) XSObject(org.apache.xerces.xs.XSObject) IOException(java.io.IOException) Date(java.util.Date) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) XSModel(org.apache.xerces.xs.XSModel) ArrayList(java.util.ArrayList) List(java.util.List) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 57 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class ScriptPlugin method createDeploymentArtifact.

private String createDeploymentArtifact(ArtifactTemplateId artifactTemplate, IRepository repository, GeneratedArtifacts generatedArtifacts, String packageToInstall) {
    QName packageDaId = new QName(artifactTemplate.getQName().getNamespaceURI(), VersionSupport.getNewComponentVersionId(artifactTemplate, packageToInstall + "-DA"));
    ArtifactTemplateId deployArtId = new ArtifactTemplateId(packageDaId);
    String generatedPackage = packageToInstall + ".tar.gz";
    if (!repository.exists(deployArtId)) {
        String cmd = "/usr/bin/apt download $(/usr/bin/apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts " + "--no-breaks --no-replaces --no-enhances --no-pre-depends " + packageToInstall + " | /bin/grep \"^\\w\")";
        logger.info("Executing command: \"{}\"", cmd);
        Path tempDirectory = null;
        try {
            repository.setElement(deployArtId, new TArtifactTemplate.Builder(deployArtId.getXmlId().getDecoded(), ToscaBaseTypes.archiveArtifactType).build());
            tempDirectory = Files.createTempDirectory(packageDaId.getLocalPart());
            ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(deployArtId);
            try {
                Utils.execute(tempDirectory.toString(), "bash", "-c", cmd);
            } catch (IOException | InterruptedException e) {
                logger.info("Cannot perform download, skipping it! You must add the DA contents yourself!");
                logger.debug("Root cause:", e);
            }
            // Ensure the folder structure exists.
            RepositoryFileReference tarFileRef = new RepositoryFileReference(filesId, generatedPackage);
            if (repository.id2AbsolutePath(filesId).toFile().mkdirs()) {
                compressFolderContents(tempDirectory.toString(), repository.ref2AbsolutePath(tarFileRef).toString());
                BackendUtils.synchronizeReferences(repository, deployArtId);
            } else {
                logger.error("Could not create folders: {}", repository.id2AbsolutePath(filesId).toFile().getAbsolutePath());
            }
        } catch (Exception e) {
            logger.error("Error while downloading artifacts...", e);
        } finally {
            if (tempDirectory != null) {
                FileUtils.forceDelete(tempDirectory);
            }
        }
    }
    generatedArtifacts.deploymentArtifactsToAdd.add(packageDaId);
    return generatedPackage;
}
Also used : Path(java.nio.file.Path) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) QName(javax.xml.namespace.QName) IOException(java.io.IOException) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) IOException(java.io.IOException)

Example 58 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference 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)

Example 59 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class RestUtils method cloneServiceTemplate.

public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
    ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
    RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
    TDefinitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
    defs.setId(newName + "Definitions");
    defs.setName(newName + "Definitions generated from Artifact " + artifactName);
    TServiceTemplate oldSTModel = null;
    for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
        if (el instanceof TServiceTemplate) {
            oldSTModel = (TServiceTemplate) el;
        }
    }
    if (oldSTModel != null) {
        oldSTModel.setId(newName);
        oldSTModel.setName(newName + " generated from Artifact " + artifactName);
        // remove xaaspackager tags
        Collection<TTag> toRemove = new ArrayList<>();
        for (TTag tag : oldSTModel.getTags()) {
            switch(tag.getName()) {
                case "xaasPackageNode":
                case "xaasPackageArtifactType":
                case "xaasPackageDeploymentArtifact":
                    toRemove.add(tag);
                    break;
                default:
                    break;
            }
        }
        oldSTModel.getTags().removeAll(toRemove);
    }
    JAXBContext context = JAXBContext.newInstance(TDefinitions.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.marshal(defs, sw);
    String xmlString = sw.toString();
    RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
    return newServiceTemplateId;
}
Also used : TTag(org.eclipse.winery.model.tosca.TTag) Marshaller(javax.xml.bind.Marshaller) ArrayList(java.util.ArrayList) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) JAXBContext(javax.xml.bind.JAXBContext) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) StringWriter(java.io.StringWriter) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate)

Example 60 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class GenericFileResource method putContentToFile.

public Response putContentToFile(String fileName, InputStream uploadedInputStream, String mediaTypeString) {
    RepositoryFileReference ref = this.fileName2fileRef(fileName, false);
    // TODO: instead of fixing the media type, we could overwrite the browser's mediatype by using some user configuration
    BufferedInputStream bis = new BufferedInputStream(uploadedInputStream);
    org.apache.tika.mime.MediaType mediaType = BackendUtils.getFixedMimeType(bis, fileName, org.apache.tika.mime.MediaType.parse(mediaTypeString));
    return RestUtils.putContentToFile(ref, bis, mediaType);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream)

Aggregations

RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)79 IOException (java.io.IOException)33 Path (java.nio.file.Path)24 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)17 QName (javax.xml.namespace.QName)14 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)14 InputStream (java.io.InputStream)13 ArrayList (java.util.ArrayList)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)9 Test (org.junit.jupiter.api.Test)9 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)8 PlanId (org.eclipse.winery.model.ids.elements.PlanId)8 PlansId (org.eclipse.winery.model.ids.elements.PlansId)8 IRepository (org.eclipse.winery.repository.backend.IRepository)8 MediaType (org.apache.tika.mime.MediaType)7 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 BufferedInputStream (java.io.BufferedInputStream)6 HashMap (java.util.HashMap)6 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)6