use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class BackendUtils method mergeTopologyTemplateAinTopologyTemplateB.
/**
* Merges two Topology Templates and returns the mapping between the topology elements from the original Topology
* Template and their respective clones inside the merged topology. Hereby, the staying elements must not be
* merged.
*
* @param topologyTemplateA the topology to merged into <code>topologyTemplateB</code>
* @param topologyTemplateB the target topology in which <dode>topologyTemplateA</dode> should be merged in
* @param stayingElementIds the TEntityTemplates that must not be merged from A to B.
* @return A mapping between the ids in the <code>topologyTemplateA</code> and their corresponding ids in
* <code>topologyTemplateB</code>
*/
public static Map<String, String> mergeTopologyTemplateAinTopologyTemplateB(TTopologyTemplate topologyTemplateA, TTopologyTemplate topologyTemplateB, List<String> stayingElementIds) {
Objects.requireNonNull(topologyTemplateA);
Objects.requireNonNull(topologyTemplateB);
TTopologyTemplate topologyTemplateToBeMerged = new TTopologyTemplate.Builder().build();
Map<String, String> idMapping = new HashMap<>();
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 (Objects.nonNull(stayingElementIds)) {
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().forEach(entity -> {
if (!stayingElementIds.contains(entity.getId())) {
if (entity instanceof TNodeTemplate) {
topologyTemplateToBeMerged.addNodeTemplate((TNodeTemplate) entity);
} else if (entity instanceof TRelationshipTemplate) {
topologyTemplateToBeMerged.addRelationshipTemplate((TRelationshipTemplate) entity);
}
}
});
} else {
topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateA.getNodeTemplateOrRelationshipTemplate());
}
if (shiftLeft.isPresent()) {
ModelUtilities.collectIdsOfExistingTopologyElements(topologyTemplateB, idMapping);
// patch ids of reqs change them if required
topologyTemplateToBeMerged.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().forEach(oldReq -> {
TRequirement req = SerializationUtils.clone(oldReq);
ModelUtilities.generateNewIdOfTemplate(req, idMapping);
topologyTemplateToBeMerged.getRelationshipTemplates().stream().filter(rt -> rt.getSourceElement().getRef() instanceof TRequirement).forEach(rt -> {
TRequirement sourceElement = (TRequirement) rt.getSourceElement().getRef();
if (sourceElement.getId().equalsIgnoreCase(oldReq.getId())) {
sourceElement.setId(req.getId());
}
});
}));
// patch ids of caps change them if required
topologyTemplateToBeMerged.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().forEach(oldCap -> {
TCapability cap = SerializationUtils.clone(oldCap);
ModelUtilities.generateNewIdOfTemplate(cap, idMapping);
topologyTemplateToBeMerged.getRelationshipTemplates().stream().filter(rt -> rt.getTargetElement().getRef() instanceof TCapability).forEach(rt -> {
TCapability targetElement = (TCapability) rt.getTargetElement().getRef();
if (targetElement.getId().equalsIgnoreCase(oldCap.getId())) {
targetElement.setId(cap.getId());
}
});
}));
ArrayList<TRelationshipTemplate> newRelationships = new ArrayList<>();
// patch the ids of templates and add them
topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate().forEach(element -> {
TEntityTemplate rtOrNt = SerializationUtils.clone(element);
ModelUtilities.generateNewIdOfTemplate(rtOrNt, idMapping);
if (rtOrNt instanceof TNodeTemplate) {
int newLeft = ModelUtilities.getLeft((TNodeTemplate) rtOrNt).orElse(0) + shiftLeft.get();
((TNodeTemplate) rtOrNt).setX(Integer.toString(newLeft));
} else if (rtOrNt instanceof TRelationshipTemplate) {
newRelationships.add((TRelationshipTemplate) rtOrNt);
}
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(rtOrNt);
});
// update references to the new elements
newRelationships.forEach(rel -> {
RelationshipSourceOrTarget source = rel.getSourceElement().getRef();
RelationshipSourceOrTarget target = rel.getTargetElement().getRef();
if (source instanceof TNodeTemplate && (stayingElementIds == null || stayingElementIds.stream().noneMatch(elementId -> elementId.equals(source.getId())))) {
TNodeTemplate newSource = topologyTemplateB.getNodeTemplate(idMapping.get(source.getId()));
rel.setSourceNodeTemplate(newSource);
}
if (target instanceof TNodeTemplate && (stayingElementIds == null || stayingElementIds.stream().noneMatch(elementId -> elementId.equals(target.getId())))) {
TNodeTemplate newTarget = topologyTemplateB.getNodeTemplate(idMapping.get(target.getId()));
rel.setTargetNodeTemplate(newTarget);
}
});
} else {
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateToBeMerged.getNodeTemplateOrRelationshipTemplate());
}
return idMapping;
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IRepository 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<>();
OTComplianceRule 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
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// crawl through policies
List<TPolicy> policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies) {
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.TEntityTemplate in project container by OpenTOSCA.
the class BoundaryDefinitionController method getBoundaryDefinitionProperties.
@GET
@Path("/properties")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ApiOperation(value = "Get properties of a service tempate", response = PropertiesDTO.class)
public Response getBoundaryDefinitionProperties(@ApiParam("ID of CSAR") @PathParam("csar") final String csarId, @ApiParam("qualified name of the service template") @PathParam("servicetemplate") final String servicetemplate) {
logger.debug("Invoking getBoundaryDefinitionProperties");
final Csar csar = this.storage.findById(new CsarId(csarId));
final TServiceTemplate serviceTemplate;
try {
serviceTemplate = ToscaEngine.resolveServiceTemplate(csar, servicetemplate);
} catch (org.opentosca.container.core.common.NotFoundException e) {
throw new NotFoundException(e);
}
if (Objects.isNull(serviceTemplate)) {
return Response.status(Response.Status.NOT_FOUND).entity("ServiceTemplate not found!").build();
}
TBoundaryDefinitions boundary = serviceTemplate.getBoundaryDefinitions();
if (Objects.isNull(boundary)) {
return Response.status(Response.Status.NOT_FOUND).entity("BoundaryDefinitions not found!").build();
}
List<TPropertyMapping> propertyMappings = Optional.of(boundary).map(TBoundaryDefinitions::getProperties).map(TBoundaryDefinitions.Properties::getPropertyMappings).orElse(Collections.emptyList());
logger.debug("Found <{}> property mappings", propertyMappings.size());
final List<PropertyMappingDTO> propertyMappingDTOs = propertyMappings.stream().map(mapping -> {
final PropertyMappingDTO result = new PropertyMappingDTO();
result.setServiceTemplatePropertyRef(mapping.getServiceTemplatePropertyRef());
result.setTargetPropertyRef(mapping.getTargetPropertyRef());
if (!(mapping.getTargetObjectRef() instanceof TEntityTemplate)) {
logger.error("Unexpected mapping target detected for the property (" + mapping.getServiceTemplatePropertyRef() + ")");
} else {
result.setTargetObjectRef(mapping.getTargetObjectRef().getId());
}
return result;
}).collect(Collectors.toList());
final PropertiesDTO dto = new PropertiesDTO();
if (Objects.nonNull(boundary.getProperties())) {
dto.setXmlFragment(boundary.getProperties().getAny());
if (!propertyMappingDTOs.isEmpty()) {
dto.setPropertyMappings(propertyMappingDTOs);
}
}
dto.add(Link.fromUri(UriUtil.encode(this.uriInfo.getAbsolutePath())).rel("self").build());
return Response.ok(dto).build();
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IGenericRepository method getReferenceCount.
default int getReferenceCount(ArtifactTemplateId id) {
// We do not use a database, therefore, we have to go through all possibilities pointing to the artifact template
// DAs and IAs point to an artifact template
// DAs are contained in Node Type Implementations and Node Templates
// IAs are contained in Node Type Implementations and Relationship Type Implementations
int count = 0;
Collection<TDeploymentArtifact> allDAs = new HashSet<>();
Collection<TImplementationArtifact> allIAs = new HashSet<>();
// handle Node Type Implementation, which contains DAs and IAs
SortedSet<NodeTypeImplementationId> nodeTypeImplementations = this.getAllDefinitionsChildIds(NodeTypeImplementationId.class);
for (NodeTypeImplementationId ntiId : nodeTypeImplementations) {
final TNodeTypeImplementation nodeTypeImplementation = this.getElement(ntiId);
TDeploymentArtifacts deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
}
TImplementationArtifacts implementationArtifacts = nodeTypeImplementation.getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts.getImplementationArtifact());
}
}
// check all Relationshiptype Implementations for IAs
SortedSet<RelationshipTypeImplementationId> relationshipTypeImplementations = this.getAllDefinitionsChildIds(RelationshipTypeImplementationId.class);
for (RelationshipTypeImplementationId rtiId : relationshipTypeImplementations) {
TImplementationArtifacts implementationArtifacts = this.getElement(rtiId).getImplementationArtifacts();
if (implementationArtifacts != null) {
allIAs.addAll(implementationArtifacts.getImplementationArtifact());
}
}
// check all node templates for DAs
SortedSet<ServiceTemplateId> serviceTemplates = this.getAllDefinitionsChildIds(ServiceTemplateId.class);
for (ServiceTemplateId sid : serviceTemplates) {
TTopologyTemplate topologyTemplate = this.getElement(sid).getTopologyTemplate();
if (topologyTemplate != null) {
List<TEntityTemplate> nodeTemplateOrRelationshipTemplate = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
for (TEntityTemplate template : nodeTemplateOrRelationshipTemplate) {
if (template instanceof TNodeTemplate) {
TNodeTemplate nodeTemplate = (TNodeTemplate) template;
TDeploymentArtifacts deploymentArtifacts = nodeTemplate.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
allDAs.addAll(deploymentArtifacts.getDeploymentArtifact());
}
}
}
}
}
// now we have all DAs and IAs
QName ourQName = id.getQName();
// check DAs for artifact templates
for (TDeploymentArtifact da : allDAs) {
QName artifactRef = da.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
// check IAs for artifact templates
for (TImplementationArtifact ia : allIAs) {
QName artifactRef = ia.getArtifactRef();
if (ourQName.equals(artifactRef)) {
count++;
}
}
return count;
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project winery by eclipse.
the class IGenericRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId 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<>();
TServiceTemplate serviceTemplate = this.getElement(id);
// add included things to export queue
TBoundaryDefinitions boundaryDefs;
if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
TBoundaryDefinitions.Policies policies = boundaryDefs.getPolicies();
if (policies != null) {
for (TPolicy policy : policies.getPolicy()) {
PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
ids.add(policyTypeId);
PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
ids.add(policyTemplateId);
}
}
// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
}
if (serviceTemplate.getTopologyTemplate() != null) {
for (TEntityTemplate entityTemplate : serviceTemplate.getTopologyTemplate().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);
QName template = pol.getPolicyRef();
if (template != null) {
PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
ids.add(policyTemplateId);
}
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
Aggregations