Search in sources :

Example 1 with PlansId

use of org.eclipse.winery.model.ids.elements.PlansId in project winery by eclipse.

the class ToscaExportUtil method prepareForExport.

/**
 * Synchronizes the plan model references and adds the plans to the csar (putRefAsReferencedItemInCsar)
 */
private void prepareForExport(IRepository repository, ServiceTemplateId id) throws IOException {
    // ensure that the plans stored locally are the same ones as stored in the definitions
    BackendUtils.synchronizeReferences(id, repository);
    // add all plans as reference in the CSAR
    // the data model is consistent with the repository
    // we crawl through the repository to as putRefAsReferencedItemInCsar expects a repository file reference
    PlansId plansContainerId = new PlansId(id);
    SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
    for (PlanId planId : nestedPlans) {
        SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(planId);
        // even if we currently support only one file in the directory, we just add everything
        for (RepositoryFileReference ref : containedFiles) {
            putRefAsReferencedItemInCsar(repository, ref);
        }
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 2 with PlansId

use of org.eclipse.winery.model.ids.elements.PlansId 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 3 with PlansId

use of org.eclipse.winery.model.ids.elements.PlansId in project winery by eclipse.

the class PlansResource method saveFile.

private Response saveFile(TPlan tPlan, InputStream uploadedInputStream, FormDataContentDisposition fileDetail, FormDataBodyPart body) {
    boolean bpmn4toscaMode = Namespaces.URI_BPMN4TOSCA_20.equals(tPlan.getPlanLanguage());
    if (uploadedInputStream != null || bpmn4toscaMode) {
        // Determine Id
        PlansId plansId = new PlansId((ServiceTemplateId) ((ServiceTemplateResource) this.res).getId());
        PlanId planId = new PlanId(plansId, new XmlId(tPlan.getId(), false));
        // Ensure overwriting
        if (RepositoryFactory.getRepository().exists(planId)) {
            try {
                RepositoryFactory.getRepository().forceDelete(planId);
                // Quick hack to remove the deleted plan from the plans element
                ((ServiceTemplateResource) this.res).synchronizeReferences();
            } catch (IOException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
        }
        String fileName;
        if (bpmn4toscaMode) {
            fileName = tPlan.getId() + Constants.SUFFIX_BPMN4TOSCA;
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, "{}", MediaType.APPLICATION_JSON_TYPE);
        } else {
            // We use the filename also as local file name. Alternatively, we could use the xml id
            // With URL encoding, this should not be an issue
            fileName = EncodingUtil.URLencode(fileDetail.getFileName());
            // Really store it
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
        }
        PlansResource.setPlanModelReference(tPlan, planId, fileName);
    }
    return RestUtils.persist(this.res);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) XmlId(org.eclipse.winery.model.ids.XmlId) IOException(java.io.IOException) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 4 with PlansId

use of org.eclipse.winery.model.ids.elements.PlansId in project winery by eclipse.

the class PlanResource method getId.

/**
 * Determines the id of the current resource
 */
private PlanId getId() {
    ServiceTemplateId sId = (ServiceTemplateId) this.getServiceTemplateResource().getId();
    PlansId psId = new PlansId(sId);
    return new PlanId(psId, new XmlId(this.o.getId(), false));
}
Also used : PlanId(org.eclipse.winery.model.ids.elements.PlanId) XmlId(org.eclipse.winery.model.ids.XmlId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 5 with PlansId

use of org.eclipse.winery.model.ids.elements.PlansId in project winery by eclipse.

the class CsarImporter method adjustServiceTemplate.

/**
 * In case plans are provided, the plans are imported into Winery's storage
 *
 * @param rootPath the root path of the extracted csar
 * @param tmf      the TOSCAMetaFile object used to determine the mime type of the plan
 * @param wid      Winery's internal id of the service template
 * @param st       the the service template to be imported {@inheritDoc}
 */
private void adjustServiceTemplate(Path rootPath, TOSCAMetaFile tmf, ServiceTemplateId wid, TServiceTemplate st, final List<String> errors) {
    List<TPlan> plans = st.getPlans();
    if (plans != null) {
        for (TPlan plan : plans) {
            PlanModelReference refContainer = plan.getPlanModelReference();
            if (refContainer != null) {
                String ref = refContainer.getReference();
                if (ref != null) {
                    // URLs are stored encoded -> undo the encoding
                    ref = EncodingUtil.URLdecode(ref);
                    URI refURI;
                    try {
                        refURI = new URI(ref);
                    } catch (URISyntaxException e) {
                        errors.add(String.format("Invalid URI %1$s", ref));
                        continue;
                    }
                    if (refURI.isAbsolute()) {
                        // We have to do nothing
                        continue;
                    }
                    Path path = rootPath.resolve(ref);
                    if (!Files.exists(path)) {
                        // possibly, the reference is relative to the Definitions subfolder
                        // COS01 does not make any explicit statement how to resolve the reference here
                        path = rootPath.resolve("Definitions").resolve(ref);
                        if (!Files.exists(path)) {
                            errors.add(String.format("Plan reference %1$s not found", ref));
                            // we quickly remove the reference to reflect the not-found in the data
                            refContainer.setReference(null);
                            continue;
                        }
                    }
                    PlansId plansId = new PlansId(wid);
                    PlanId pid = new PlanId(plansId, new XmlId(plan.getId(), false));
                    if (Files.isDirectory(path)) {
                        errors.add(String.format("Reference %1$s is a directory and Winery currently does not support importing directories", ref));
                        continue;
                    }
                    RepositoryFileReference fref = new RepositoryFileReference(pid, path.getFileName().toString());
                    importFile(path, fref, tmf, rootPath, errors);
                    // file is imported
                    // Adjust the reference
                    refContainer.setReference("../" + Util.getUrlPath(fref));
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) PlanModelReference(org.eclipse.winery.model.tosca.TPlan.PlanModelReference) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.model.ids.XmlId) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Aggregations

PlanId (org.eclipse.winery.model.ids.elements.PlanId)5 PlansId (org.eclipse.winery.model.ids.elements.PlansId)5 XmlId (org.eclipse.winery.model.ids.XmlId)4 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)4 TPlan (org.eclipse.winery.model.tosca.TPlan)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)1 PlanModelReference (org.eclipse.winery.model.tosca.TPlan.PlanModelReference)1 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)1 ServiceTemplateResource (org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource)1