Search in sources :

Example 6 with TPlan

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

TPlan (org.eclipse.winery.model.tosca.TPlan)6 HashSet (java.util.HashSet)2 XmlId (org.eclipse.winery.model.ids.XmlId)2 PlanId (org.eclipse.winery.model.ids.elements.PlanId)2 PlansId (org.eclipse.winery.model.ids.elements.PlansId)2 TPlans (org.eclipse.winery.model.tosca.TPlans)2 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)2 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 PathParam (javax.ws.rs.PathParam)1 Produces (javax.ws.rs.Produces)1 MediaType (javax.ws.rs.core.MediaType)1