Search in sources :

Example 46 with RepositoryFileReference

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

the class BackendUtils method synchronizeReferences.

/**
 * Synchronizes the known plans with the data in the XML. When there is a stored file, but no known entry in the
 * XML, we guess "BPEL" as language and "buildProvenanceSmartContract plan" as type.
 */
public static void synchronizeReferences(ServiceTemplateId id, IRepository repository) throws IOException {
    final TServiceTemplate serviceTemplate = repository.getElement(id);
    // locally stored plans
    List<TPlan> plans = serviceTemplate.getPlans();
    // plans stored in the repository
    PlansId plansContainerId = new PlansId(id);
    SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
    Set<PlanId> plansToAdd = new HashSet<>(nestedPlans);
    if (nestedPlans.isEmpty() && plans == null) {
        // data on the file system equals the data -> no plans
        return;
    }
    if (plans == null) {
        plans = new ArrayList<>();
        serviceTemplate.setPlans(plans);
    }
    for (Iterator<TPlan> iterator = plans.iterator(); iterator.hasNext(); ) {
        TPlan plan = iterator.next();
        if (plan.getPlanModel() != null) {
            // in case, a plan is directly contained in a Model element, we do not need to do anything
            continue;
        }
        TPlan.PlanModelReference planModelReference;
        if ((planModelReference = plan.getPlanModelReference()) != null) {
            String ref = planModelReference.getReference();
            if ((ref == null) || ref.startsWith("../")) {
                // special case (due to errors in the importer): empty PlanModelReference field
                if (plan.getId() == null || plan.getId().isEmpty()) {
                    // invalid plan entry: no id.
                    // we remove the entry
                    iterator.remove();
                    continue;
                }
                PlanId planId = new PlanId(plansContainerId, new XmlId(plan.getId(), false));
                if (nestedPlans.contains(planId)) {
                    // everything alright
                    // we do NOT need to add the plan on the HDD to the XML
                    plansToAdd.remove(planId);
                } else {
                    // no local storage for the plan, we remove it from the XML
                    iterator.remove();
                }
            }
        }
    }
    // add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
    for (PlanId planId : plansToAdd) {
        SortedSet<RepositoryFileReference> files = repository.getContainedFiles(planId);
        if (files.size() != 1) {
            throw new IllegalStateException("Currently, only one file per plan is supported.");
        }
        RepositoryFileReference ref = files.iterator().next();
        TPlan plan = new TPlan.Builder(planId.getXmlId().getDecoded(), Constants.TOSCA_PLANTYPE_BUILD_PLAN, Namespaces.URI_BPEL20_EXECUTABLE).setName(planId.getXmlId().getDecoded()).build();
        // create a PlanModelReferenceElement pointing to that file
        String path = Util.getUrlPath(ref);
        // path is relative from the definitions element
        path = "../" + path;
        TPlan.PlanModelReference pref = new TPlan.PlanModelReference();
        pref.setReference(path);
        plan.setPlanModelReference(pref);
        plans.add(plan);
    }
    if (serviceTemplate.getPlans() != null && serviceTemplate.getPlans().isEmpty()) {
        serviceTemplate.setPlans(null);
    }
    repository.setElement(id, serviceTemplate);
}
Also used : PlanId(org.eclipse.winery.model.ids.elements.PlanId) PlansId(org.eclipse.winery.model.ids.elements.PlansId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.model.ids.XmlId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet)

Example 47 with RepositoryFileReference

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

the class BackendUtils method getGitInformation.

/**
 * @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
 * @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
 * is given.
 */
public static GitInfo getGitInformation(DirectoryId directoryId, IRepository repo) {
    if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
        return null;
    }
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
    try {
        TDefinitions defs = repo.definitionsFromRef(ref);
        Map<QName, String> attributes = defs.getOtherAttributes();
        String src = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
        String branch = attributes.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
        if (src == null && branch == null) {
            return null;
        }
        if (src == null || branch == null) {
            LOGGER.error("Git information not complete, URL or branch missing");
            return null;
        }
        return new GitInfo(src, branch);
    } catch (IOException e) {
        LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
    }
    return null;
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) QName(javax.xml.namespace.QName) IOException(java.io.IOException) GitInfo(org.eclipse.winery.repository.GitInfo) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)

Example 48 with RepositoryFileReference

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

the class SelfServiceMetaDataUtils method ensureDataXmlExists.

public static void ensureDataXmlExists(IRepository repository, SelfServiceMetaDataId id) throws IOException {
    RepositoryFileReference data_xml_ref = getDataXmlRef(id);
    if (!repository.exists(data_xml_ref)) {
        final Application application = new Application();
        BackendUtils.persist(application, data_xml_ref, MediaTypes.MEDIATYPE_TEXT_XML, repository);
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Application(org.eclipse.winery.model.selfservice.Application)

Example 49 with RepositoryFileReference

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

the class ConsistencyChecker method checkXmlSchemaValidation.

private void checkXmlSchemaValidation(DefinitionsChildId id) {
    RepositoryFileReference refOfDefinitions = BackendUtils.getRefOfDefinitions(id);
    if (!configuration.getRepository().exists(refOfDefinitions)) {
        printAndAddError(id, "Id exists, but corresponding XML file does not.");
        return;
    }
    try (InputStream inputStream = configuration.getRepository().newInputStream(refOfDefinitions)) {
        DocumentBuilder documentBuilder = ToscaDocumentBuilderFactory.INSTANCE.getSchemaAwareToscaDocumentBuilder();
        StringBuilder errorStringBuilder = new StringBuilder();
        documentBuilder.setErrorHandler(BackendUtils.getErrorHandler(errorStringBuilder));
        documentBuilder.parse(inputStream);
        String errors = errorStringBuilder.toString();
        if (!errors.isEmpty()) {
            printAndAddError(id, errors);
        }
    } catch (IOException e) {
        LOGGER.debug("I/O error", e);
        printAndAddError(id, "I/O error during XML validation " + e.getMessage());
    } catch (SAXException e) {
        LOGGER.debug("SAX exception", e);
        printAndAddError(id, "SAX error during XML validation: " + e.getMessage());
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 50 with RepositoryFileReference

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

the class AbstractFileBasedRepository method getZippedContents.

@Override
public void getZippedContents(final GenericId id, OutputStream out) throws WineryRepositoryException {
    Objects.requireNonNull(id);
    Objects.requireNonNull(out);
    SortedSet<RepositoryFileReference> containedFiles = this.getContainedFiles(id);
    try (final ZipOutputStream zos = new ZipOutputStream(out)) {
        for (RepositoryFileReference ref : containedFiles) {
            ZipEntry zipArchiveEntry;
            final Optional<Path> subDirectory = ref.getSubDirectory();
            if (subDirectory.isPresent()) {
                zipArchiveEntry = new ZipEntry(subDirectory.get().resolve(ref.getFileName()).toString());
            } else {
                zipArchiveEntry = new ZipEntry(ref.getFileName());
            }
            zos.putNextEntry(zipArchiveEntry);
            try (InputStream is = this.newInputStream(ref)) {
                IOUtils.copy(is, zos);
            }
            zos.closeEntry();
        }
    } catch (IOException e) {
        throw new WineryRepositoryException("I/O exception during export", e);
    }
}
Also used : Path(java.nio.file.Path) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException)

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