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);
}
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;
}
}
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);
}
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);
}
}
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));
}
}
}
}
}
Aggregations