use of org.eclipse.winery.model.tosca.TServiceTemplate in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static Definitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final Definitions definitions = createWrapperDefinitions(id);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.tosca.TServiceTemplate 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.TServiceTemplate in project winery by eclipse.
the class BackendUtils method mergeServiceTemplateAinServiceTemplateB.
public static void mergeServiceTemplateAinServiceTemplateB(ServiceTemplateId serviceTemplateIdA, ServiceTemplateId serviceTemplateIdB) throws IOException {
IRepository repository = RepositoryFactory.getRepository();
TTopologyTemplate topologyTemplateA = repository.getElement(serviceTemplateIdA).getTopologyTemplate();
TServiceTemplate serviceTemplateB = repository.getElement(serviceTemplateIdB);
TTopologyTemplate topologyTemplateB = serviceTemplateB.getTopologyTemplate();
Optional<Integer> shiftLeft = topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).max(Comparator.comparingInt(n -> ModelUtilities.getLeft(n).orElse(0))).map(n -> ModelUtilities.getLeft(n).orElse(0));
if (shiftLeft.isPresent()) {
Map<String, String> idMapping = new HashMap<>();
// collect existing node template ids
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).forEach(x -> idMapping.put(x.getId(), x.getId()));
// collect existing relationship template ids
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TRelationshipTemplate).map(x -> (TRelationshipTemplate) x).forEach(x -> idMapping.put(x.getId(), x.getId()));
if (topologyTemplateB.getNodeTemplates() != null) {
// collect existing requirement ids
topologyTemplateB.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().getRequirement().stream().forEach(x -> idMapping.put(x.getId(), x.getId())));
// collect existing capability ids
topologyTemplateB.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().getCapability().stream().forEach(x -> idMapping.put(x.getId(), x.getId())));
}
if (topologyTemplateA.getNodeTemplates() != null) {
// patch ids of reqs change them if required
topologyTemplateA.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().getRequirement().forEach(req -> {
String oldId = req.getId();
String newId = req.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(req.getId(), newId);
req.setId(newId);
topologyTemplateA.getRelationshipTemplates().stream().filter(rt -> rt.getSourceElement().getRef() instanceof TRequirement).forEach(rt -> {
TRequirement sourceElement = (TRequirement) rt.getSourceElement().getRef();
if (sourceElement.getId().equalsIgnoreCase(oldId)) {
sourceElement.setId(req.getId());
}
});
}));
// patch ids of caps change them if required
topologyTemplateA.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().getCapability().forEach(cap -> {
String oldId = cap.getId();
String newId = cap.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(cap.getId(), newId);
cap.setId(newId);
topologyTemplateA.getRelationshipTemplates().stream().filter(rt -> rt.getTargetElement().getRef() instanceof TCapability).forEach(rt -> {
TCapability targetElement = (TCapability) rt.getTargetElement().getRef();
if (targetElement.getId().equalsIgnoreCase(oldId)) {
targetElement.setId(cap.getId());
}
});
}));
}
// patch the ids of node templates and add them
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).forEach(nt -> {
String newId = nt.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(nt.getId(), newId);
nt.setId(newId);
int newLeft = ModelUtilities.getLeft((TNodeTemplate) nt).orElse(0) + shiftLeft.get();
((TNodeTemplate) nt).setX(Integer.toString(newLeft));
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(nt);
});
// patch the ids of relationship templates and add them
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TRelationshipTemplate).map(x -> (TRelationshipTemplate) x).forEach(rt -> {
String newId = rt.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(rt.getId(), newId);
rt.setId(newId);
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(rt);
});
} else {
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateA.getNodeTemplateOrRelationshipTemplate());
}
repository.setElement(serviceTemplateIdB, serviceTemplateB);
}
use of org.eclipse.winery.model.tosca.TServiceTemplate in project winery by eclipse.
the class Showcases method zipTypeTest.
@Test
public void zipTypeTest() throws Exception {
String name = "Showcase.csar";
InputStream inputStream = new FileInputStream(path + File.separator + name);
Converter converter = new Converter();
converter.convertY2X(inputStream);
TServiceTemplate serviceTemplate = RepositoryFactory.getRepository().getElement(new ServiceTemplateId(new Namespace(Namespaces.DEFAULT_NS, false), new XmlId("Showcase", false)));
Assert.assertNotNull(serviceTemplate);
}
use of org.eclipse.winery.model.tosca.TServiceTemplate in project winery by eclipse.
the class TOSCAModelHelper method createTServiceTemplate.
public static TServiceTemplate createTServiceTemplate(String id, String namespace) {
TServiceTemplate serviceTemplate = new TServiceTemplate();
serviceTemplate.setName(id);
serviceTemplate.setId(id);
serviceTemplate.setTargetNamespace(namespace);
return serviceTemplate;
}
Aggregations