use of org.eclipse.winery.model.tosca.TCapability 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.TCapability in project winery by eclipse.
the class IGenericRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ComplianceRuleId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
TComplianceRule complianceRule = this.getElement(id);
// TODO extend to required Structure
if (complianceRule.getIdentifier() != null) {
for (TEntityTemplate entityTemplate : complianceRule.getIdentifier().getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through deployment artifacts
TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
for (TDeploymentArtifact da : das) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
// crawl through reqs/caps
TNodeTemplate.Requirements requirements = n.getRequirements();
if (requirements != null) {
for (TRequirement req : requirements.getRequirement()) {
QName type = req.getType();
RequirementTypeId rtId = new RequirementTypeId(type);
ids.add(rtId);
}
}
TNodeTemplate.Capabilities capabilities = n.getCapabilities();
if (capabilities != null) {
for (TCapability cap : capabilities.getCapability()) {
QName type = cap.getType();
CapabilityTypeId ctId = new CapabilityTypeId(type);
ids.add(ctId);
}
}
// crawl through policies
org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies.getPolicy()) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
use of org.eclipse.winery.model.tosca.TCapability in project winery by eclipse.
the class CapabilitiesResource method addNewElement.
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response addNewElement(@FormParam("name") String name, @FormParam("ref") String reference) {
if (reference == null) {
return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
}
TCapabilityRef ref = new TCapabilityRef();
// may also be null
ref.setName(name);
// The XML model fordces us to put a reference to the object and not just the string
ServiceTemplateResource rs = (ServiceTemplateResource) this.res;
TCapability resolved = ModelUtilities.resolveCapability(rs.getServiceTemplate(), reference);
// In case nothing was found: report back to the user
if (resolved == null) {
return Response.status(Status.BAD_REQUEST).entity("Reference could not be resolved").build();
}
ref.setRef(resolved);
// "this.alreadyContains(ref)" cannot be called as this leads to a mappable exception: The data does not contain an id where the given ref attribute may point to
this.list.add(ref);
return CollectionsHelper.persist(this.res, this, ref, true);
}
use of org.eclipse.winery.model.tosca.TCapability in project winery by eclipse.
the class NodeTemplateConnector method findRelationshipType.
/**
* Searches a compatible {@link TRelationshipType} to connect two {@link TNodeTemplate}s.
*
* @param source the source {@link TNodeTemplate}
* @param target the target {@link TNodeTemplate}
* @param toscaAnalyzer the {@link TOSCAAnalyzer} object to access the data model
* @param requirement the {@link TRequirement} of the source {@link TNodeTemplate}
* @return a list of suitable {@link TRelationshipType}s
*/
public static List<TRelationshipType> findRelationshipType(TNodeTemplate source, TNodeTemplate target, TOSCAAnalyzer toscaAnalyzer, TRequirement requirement) {
List<TRelationshipType> suitableRelationshipTypes = new ArrayList<TRelationshipType>();
List<TRelationshipType> allRelationshipTypes = toscaAnalyzer.getRelationshipTypes();
// in case the connection to a placeholder is searched, no requirement exists
if (requirement != null) {
List<TCapability> capabilities = target.getCapabilities().getCapability();
// check if a RelationshipType can connect a requirement of the source NodeTemplate to a capability of the target NodeTemplate
for (TRelationshipType relationshipType : allRelationshipTypes) {
if (relationshipType.getValidSource() != null && relationshipType.getValidTarget() != null) {
for (TCapability capability : capabilities) {
if ((relationshipType.getValidSource().getTypeRef().equals(requirement.getType()) && relationshipType.getValidTarget().getTypeRef().equals(capability.getType()))) {
suitableRelationshipTypes.add(relationshipType);
}
}
}
}
}
// to extend the selection check if a RelationshipType can connect the type of the source NodeTemplate to the type of the target NodeTemplate
for (TRelationshipType rt : allRelationshipTypes) {
if (rt.getValidSource() != null && rt.getValidTarget() != null) {
if ((rt.getValidSource().getTypeRef().equals(source.getType()) && rt.getValidTarget().getTypeRef().equals(target.getType()))) {
suitableRelationshipTypes.add(rt);
}
}
}
// in case no suitable relationship type could be found, search for generic types without the optional ValidSource / ValidTarget elements.
if (suitableRelationshipTypes.isEmpty()) {
for (TRelationshipType rt : allRelationshipTypes) {
if (rt.getValidSource() == null && rt.getValidTarget() == null) {
suitableRelationshipTypes.add(rt);
}
}
}
return suitableRelationshipTypes;
}
use of org.eclipse.winery.model.tosca.TCapability in project winery by eclipse.
the class CapabilitiesResource method addNewElementJSON.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addNewElementJSON(RequirementsOrCapabilityApiData reqOrCap) {
if (reqOrCap.ref == null) {
return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
}
TCapabilityRef ref = new TCapabilityRef();
// may also be null
ref.setName(reqOrCap.name);
// The XML model fordces us to put a reference to the object and not just the string
ServiceTemplateResource rs = (ServiceTemplateResource) this.res;
TCapability resolved = ModelUtilities.resolveCapability(rs.getServiceTemplate(), reqOrCap.ref);
// In case nothing was found: report back to the user
if (resolved == null) {
return Response.status(Status.BAD_REQUEST).entity("Reference could not be resolved").build();
}
ref.setRef(resolved);
// "this.alreadyContains(ref)" cannot be called as this leads to a mappable exception: The data does not contain an id where the given ref attribute may point to
this.list.add(ref);
return CollectionsHelper.persist(this.res, this, ref, true);
}
Aggregations