Search in sources :

Example 1 with PlanModelReference

use of org.eclipse.winery.model.tosca.TPlan.PlanModelReference in project winery by eclipse.

the class PlansResource method setPlanModelReference.

static void setPlanModelReference(TPlan plan, PlanId planId, String fileName) {
    PlanModelReference pref = new PlanModelReference();
    // Set path relative to Definitions/ path inside CSAR.
    pref.setReference("../" + Util.getUrlPath(planId) + fileName);
    plan.setPlanModelReference(pref);
}
Also used : PlanModelReference(org.eclipse.winery.model.tosca.TPlan.PlanModelReference)

Example 2 with PlanModelReference

use of org.eclipse.winery.model.tosca.TPlan.PlanModelReference 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

PlanModelReference (org.eclipse.winery.model.tosca.TPlan.PlanModelReference)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 XmlId (org.eclipse.winery.model.ids.XmlId)1 PlanId (org.eclipse.winery.model.ids.elements.PlanId)1 PlansId (org.eclipse.winery.model.ids.elements.PlansId)1 TPlan (org.eclipse.winery.model.tosca.TPlan)1 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)1