Search in sources :

Example 1 with TPlan

use of org.eclipse.winery.model.tosca.TPlan 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 "build plan" as type.
 */
public static void synchronizeReferences(ServiceTemplateId id) throws IOException {
    final IRepository repository = RepositoryFactory.getRepository();
    final TServiceTemplate serviceTemplate = repository.getElement(id);
    // locally stored plans
    TPlans 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<>();
    plansToAdd.addAll(nestedPlans);
    if (nestedPlans.isEmpty()) {
        if (plans == null) {
            // data on the file system equals the data -> no plans
            return;
        } else {
        // noinspection StatementWithEmptyBody
        // we have to check for equality later
        }
    }
    if (plans == null) {
        plans = new TPlans();
        serviceTemplate.setPlans(plans);
    }
    for (Iterator<TPlan> iterator = plans.getPlan().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) {
                    // 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 allright
                    // 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.
    List<TPlan> thePlans = plans.getPlan();
    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();
        plan.setId(planId.getXmlId().getDecoded());
        plan.setName(planId.getXmlId().getDecoded());
        plan.setPlanType(org.eclipse.winery.repository.Constants.TOSCA_PLANTYPE_BUILD_PLAN);
        plan.setPlanLanguage(Namespaces.URI_BPEL20_EXECUTABLE);
        // 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);
        thePlans.add(plan);
    }
    if (serviceTemplate.getPlans().getPlan().isEmpty()) {
        serviceTemplate.setPlans(null);
    }
    RepositoryFactory.getRepository().setElement(id, serviceTemplate);
}
Also used : TPlans(org.eclipse.winery.model.tosca.TPlans) PlanId(org.eclipse.winery.common.ids.elements.PlanId) PlansId(org.eclipse.winery.common.ids.elements.PlansId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.common.ids.XmlId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet)

Example 2 with TPlan

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

the class BoundaryDefinitionsJSPData method getlistOfAllPlans.

public List<Select2DataItem> getlistOfAllPlans() {
    TPlans plans = this.ste.getPlans();
    if (plans == null) {
        return null;
    } else {
        List<Select2DataItem> res = new ArrayList<>(plans.getPlan().size());
        for (TPlan plan : plans.getPlan()) {
            String id = plan.getId();
            String name = ModelUtilities.getNameWithIdFallBack(plan);
            Select2DataItem di = new Select2DataItem(id, name);
            res.add(di);
        }
        return res;
    }
}
Also used : TPlans(org.eclipse.winery.model.tosca.TPlans) ArrayList(java.util.ArrayList) TPlan(org.eclipse.winery.model.tosca.TPlan) Select2DataItem(org.eclipse.winery.repository.rest.datatypes.select2.Select2DataItem)

Example 3 with TPlan

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

the class InterfacesResource method onPost.

/**
 * A special handling for TExportedInterface is required as this object uses IDREF, which must not be a string, but
 * the real object when persisting.
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response onPost(TExportedInterface[] exportedInterfacesList) {
    if (exportedInterfacesList == null) {
        return Response.status(Response.Status.BAD_REQUEST).entity("a valid XML/JSON element has to be posted").build();
    }
    for (TExportedInterface exportedInterface : exportedInterfacesList) {
        for (TExportedOperation exportedOperation : exportedInterface.getOperation()) {
            if (exportedOperation.getNodeOperation() != null) {
                final String nodeRef = (String) exportedOperation.getNodeOperation().getNodeRef();
                final TNodeTemplate nodeTemplate = ((ServiceTemplateResource) res).getServiceTemplate().getTopologyTemplate().getNodeTemplates().stream().filter(node -> node.getId().contentEquals(nodeRef)).findFirst().get();
                exportedOperation.getNodeOperation().setNodeRef(nodeTemplate);
            } else if (exportedOperation.getRelationshipOperation() != null) {
                final String relationshipRef = (String) exportedOperation.getRelationshipOperation().getRelationshipRef();
                final TRelationshipTemplate relationshipTemplate = ((ServiceTemplateResource) res).getServiceTemplate().getTopologyTemplate().getRelationshipTemplates().stream().filter(relationship -> relationship.getId().contentEquals(relationshipRef)).findFirst().get();
                exportedOperation.getRelationshipOperation().setRelationshipRef(relationshipTemplate);
            } else if (exportedOperation.getPlan() != null) {
                final String planRef = (String) exportedOperation.getPlan().getPlanRef();
                final TPlan planTemplate = ((ServiceTemplateResource) res).getServiceTemplate().getPlans().stream().filter(relationship -> relationship.getId().contentEquals(planRef)).findFirst().get();
                exportedOperation.getPlan().setPlanRef(planTemplate);
            } else {
                return Response.status(Response.Status.BAD_REQUEST).entity("No linked operation provided").build();
            }
        }
    }
    this.list.clear();
    this.list.addAll(Arrays.asList(exportedInterfacesList));
    return RestUtils.persist(this.res);
}
Also used : Arrays(java.util.Arrays) PathParam(javax.ws.rs.PathParam) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) EntityWithIdCollectionResource(org.eclipse.winery.repository.rest.resources._support.collections.withid.EntityWithIdCollectionResource) List(java.util.List) MediaType(javax.ws.rs.core.MediaType) TExportedInterface(org.eclipse.winery.model.tosca.TExportedInterface) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) TPlan(org.eclipse.winery.model.tosca.TPlan) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) IPersistable(org.eclipse.winery.repository.rest.resources._support.IPersistable) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) RestUtils(org.eclipse.winery.repository.rest.RestUtils) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TExportedOperation(org.eclipse.winery.model.tosca.TExportedOperation) TRelationshipTemplate(org.eclipse.winery.model.tosca.TRelationshipTemplate) TExportedInterface(org.eclipse.winery.model.tosca.TExportedInterface) TPlan(org.eclipse.winery.model.tosca.TPlan) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) TExportedOperation(org.eclipse.winery.model.tosca.TExportedOperation) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 4 with TPlan

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

the class Visitor method visit.

public void visit(TServiceTemplate serviceTemplate) {
    Objects.requireNonNull(serviceTemplate);
    visit((TExtensibleElements) serviceTemplate);
    final TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
    if (topologyTemplate != null) {
        topologyTemplate.accept(this);
    }
    final List<TTag> tags = serviceTemplate.getTags();
    if (tags != null) {
        for (TTag tag : tags) {
            tag.accept(this);
        }
    }
    final List<TPlan> plans = serviceTemplate.getPlans();
    if (plans != null) {
        for (TPlan plan : plans) {
            plan.accept(this);
        }
    }
    final TBoundaryDefinitions boundaryDefinitions = serviceTemplate.getBoundaryDefinitions();
    if (boundaryDefinitions != null) {
        boundaryDefinitions.accept(this);
    }
}
Also used : TTag(org.eclipse.winery.model.tosca.TTag) TTopologyTemplate(org.eclipse.winery.model.tosca.TTopologyTemplate) TPlan(org.eclipse.winery.model.tosca.TPlan) TBoundaryDefinitions(org.eclipse.winery.model.tosca.TBoundaryDefinitions)

Example 5 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