use of org.eclipse.winery.model.tosca.HasId in project winery by eclipse.
the class ServiceTemplateResource method cullElementReferences.
private void cullElementReferences() {
final TTopologyTemplate topology = this.getServiceTemplate().getTopologyTemplate();
TBoundaryDefinitions boundaryDefs = this.getServiceTemplate().getBoundaryDefinitions();
if (topology == null || boundaryDefs == null) {
return;
}
if (boundaryDefs.getProperties() != null && boundaryDefs.getProperties().getPropertyMappings() != null) {
for (Iterator<TPropertyMapping> it = boundaryDefs.getProperties().getPropertyMappings().iterator(); it.hasNext(); ) {
TPropertyMapping propMapping = it.next();
HasId targetObject = propMapping.getTargetObjectRef();
if (!containsTarget(topology, targetObject)) {
// cull the property mapping pointing towards a no-longer existing element
it.remove();
}
}
}
if (boundaryDefs.getCapabilities() != null) {
for (Iterator<TCapabilityRef> it = boundaryDefs.getCapabilities().iterator(); it.hasNext(); ) {
TCapabilityRef ref = it.next();
TCapability target = ref.getRef();
if (!containsCapability(topology, target)) {
// cull the capability referencing a no longer existing capability in the topology
it.remove();
}
}
}
if (boundaryDefs.getRequirements() != null) {
for (Iterator<TRequirementRef> it = boundaryDefs.getRequirements().iterator(); it.hasNext(); ) {
TRequirementRef ref = it.next();
TRequirement target = ref.getRef();
if (!containsRequirement(topology, target)) {
// cull the requirement referencing a no longer existing requirement in the topology
it.remove();
}
}
}
}
use of org.eclipse.winery.model.tosca.HasId in project winery by eclipse.
the class ServiceTemplateResource method setTopology.
@Override
public void setTopology(TTopologyTemplate topologyTemplate, String type) {
// if we are in yaml mode, replacing the topology can result in yaml artifacts having to be deleted.
if (Environments.getInstance().getRepositoryConfig().getProvider() == RepositoryConfigurationObject.RepositoryProvider.YAML) {
try {
YamlArtifactsSynchronizer synchronizer = new YamlArtifactsSynchronizer.Builder(RepositoryFactory.getRepository()).setOriginalTemplate(this.getServiceTemplate().getTopologyTemplate()).setNewTemplate(topologyTemplate).setServiceTemplateId((ServiceTemplateId) this.getId()).build();
synchronizer.synchronizeNodeTemplates();
synchronizer.synchronizeRelationshipTemplates();
} catch (IOException e) {
LOGGER.error("Failed to delete yaml artifact files from disk. Reason {}", e.getMessage());
}
if (topologyTemplate.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).anyMatch(nt -> nt.getRequirements().stream().anyMatch(req -> req.getRelationship() != null))) {
// filter unused requirements
// (1) get a list of requirement template ids
// (2) filter requirement entry on node template if there is relations assigned
Set<String> usedRelationshipTemplateIds = topologyTemplate.getRelationshipTemplates().stream().map(HasId::getId).collect(Collectors.toSet());
topologyTemplate.getNodeTemplates().stream().filter(node -> node.getRequirements() != null).forEach(node -> node.getRequirements().removeIf(r -> !usedRelationshipTemplateIds.contains(r.getRelationship())));
}
}
this.getServiceTemplate().setTopologyTemplate(topologyTemplate);
this.cullElementReferences();
}
use of org.eclipse.winery.model.tosca.HasId in project winery by eclipse.
the class PermutationGenerator method checkMutability.
public boolean checkMutability(OTTopologyFragmentRefinementModel refinementModel) {
logger.info("Starting mutability check of {}", refinementModel.getIdFromIdOrNameField());
this.mutabilityErrorReason = "";
List<TNodeTemplate> detectorNodeTemplates = refinementModel.getDetector().getNodeTemplates();
Set<TNodeTemplate> mutableNodes = detectorNodeTemplates.stream().filter(nodeTemplate -> !isStayPlaceholder(nodeTemplate, refinementModel)).collect(Collectors.toSet());
List<OTStringList> permutationOptions = new ArrayList<>();
refinementModel.setPermutationOptions(permutationOptions);
Sets.powerSet(mutableNodes).stream().filter(set -> !(set.size() == 0 || set.size() == mutableNodes.size())).forEach(permutation -> permutationOptions.add(new OTStringList(permutation.stream().map(HasId::getId).collect(Collectors.toList()))));
refinementModel.setComponentSets(new ArrayList<>());
for (TNodeTemplate detectorNode : detectorNodeTemplates) {
getAllMappingsForDetectorNode(detectorNode, refinementModel).stream().filter(mapping -> mapping.getRefinementElement() instanceof TNodeTemplate).map(mapping -> (TNodeTemplate) mapping.getRefinementElement()).forEach(refinementNode -> this.checkComponentMutability(refinementNode, detectorNode, refinementModel));
ModelUtilities.getIncomingRelationshipTemplates(refinementModel.getDetector(), detectorNode).stream().filter(relation -> refinementModel.getComponentSets().stream().noneMatch(setList -> setList.getValues().contains(relation.getTargetElement().getRef().getId()) && setList.getValues().contains(relation.getSourceElement().getRef().getId()))).forEach(relation -> {
TNodeTemplate dependantNode = (TNodeTemplate) relation.getSourceElement().getRef();
if (refinementModel.getRelationMappings() != null) {
refinementModel.getRelationMappings().stream().filter(relMap -> relMap.getDetectorElement().equals(dependantNode)).filter(relMap -> RefinementUtils.canRedirectRelation(relMap, relation, this.relationshipTypes, this.nodeTypes)).findFirst().ifPresent(relMap -> addMutabilityMapping(relMap.getDetectorElement(), relMap.getRefinementElement(), refinementModel));
}
});
}
if (refinementModel.getPermutationMappings() == null) {
this.mutabilityErrorReason = "No permutation mappings could be identified";
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappedDetectorNodes = refinementModel.getDetector().getNodeTemplates().stream().filter(detectorNode -> !isStayPlaceholder(detectorNode, refinementModel)).filter(detectorNode -> !permutabilityMappingExistsForDetectorElement(detectorNode, refinementModel)).map(HasId::getId).collect(Collectors.toList());
if (unmappedDetectorNodes.size() > 0) {
this.mutabilityErrorReason = "There are detector nodes which could not be mapped to a refinement node: " + String.join(", ", unmappedDetectorNodes);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappedRefinementNodes = refinementModel.getRefinementStructure().getNodeTemplates().stream().filter(refinementNode -> !isStayingRefinementElement(refinementNode, refinementModel)).filter(refinementNode -> !permutabilityMappingExistsForRefinementNode(refinementNode, refinementModel)).map(HasId::getId).collect(Collectors.toList());
if (unmappedRefinementNodes.size() > 0) {
this.mutabilityErrorReason = "There are refinement nodes which could not be mapped to a detector node: " + String.join(", ", unmappedRefinementNodes);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
List<String> unmappableRelationIds = chekMutabilityOfDetectorRelations(refinementModel);
if (unmappableRelationIds.size() > 0) {
this.mutabilityErrorReason = "There are relations that cannot be redirected during the generation: " + String.join(", ", unmappableRelationIds);
logger.info(this.errorMessage, this.mutabilityErrorReason);
return false;
}
return true;
}
use of org.eclipse.winery.model.tosca.HasId in project winery by eclipse.
the class TopologyFragmentRefinement method applyRefinement.
@Override
public Map<String, String> applyRefinement(RefinementCandidate refinement, TTopologyTemplate topology) {
if (!(refinement.getRefinementModel() instanceof OTTopologyFragmentRefinementModel)) {
throw new UnsupportedOperationException("The refinement candidate is not a PRM!");
}
// determine the elements that are staying
OTTopologyFragmentRefinementModel prm = (OTTopologyFragmentRefinementModel) refinement.getRefinementModel();
List<String> stayingRefinementElementIds = RefinementUtils.getStayingRefinementElements(prm).stream().map(HasId::getId).collect(Collectors.toList());
// import the refinement structure
Map<String, String> idMapping = BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(refinement.getRefinementModel().getRefinementTopology(), topology, stayingRefinementElementIds);
// only for UI: position the imported nodes next to the nodes to be refined
Map<String, Map<String, Integer>> coordinates = calculateNewPositions(refinement.getDetectorGraph(), refinement.getGraphMapping(), refinement.getRefinementModel().getRefinementTopology());
refinement.getRefinementModel().getRefinementTopology().getNodeTemplates().stream().filter(element -> !stayingRefinementElementIds.contains(element.getId())).forEach(node -> {
Map<String, Integer> newCoordinates = coordinates.get(node.getId());
TNodeTemplate nodeTemplate = topology.getNodeTemplate(idMapping.get(node.getId()));
if (nodeTemplate != null) {
nodeTemplate.setX(newCoordinates.get("x").toString());
nodeTemplate.setY(newCoordinates.get("y").toString());
}
});
// iterate over the detector nodes
refinement.getDetectorGraph().vertexSet().forEach(vertex -> {
// get the matching node in the topology
TNodeTemplate matchingNode = refinement.getGraphMapping().getVertexCorrespondence(vertex, false).getTemplate();
this.redirectInternalRelations(prm, vertex.getTemplate(), matchingNode, topology);
this.redirectExternalRelations(refinement, vertex.getTemplate(), matchingNode, topology, idMapping);
this.applyPropertyMappings(refinement, vertex.getId(), matchingNode, topology, idMapping);
this.applyDeploymentArtifactMapping(refinement, vertex.getTemplate(), matchingNode, topology, idMapping);
if (!getStayMappingsOfCurrentElement(prm, vertex.getTemplate()).findFirst().isPresent()) {
topology.getNodeTemplateOrRelationshipTemplate().remove(matchingNode);
} else if (shouldRemoveBehaviorPatterns(vertex.getTemplate(), matchingNode)) {
if (vertex.getTemplate().getPolicies() != null && matchingNode.getPolicies() != null)
vertex.getTemplate().getPolicies().forEach(detectorPolicy -> matchingNode.getPolicies().removeIf(matchingPolicy -> matchingPolicy.getPolicyType().equals(detectorPolicy.getPolicyType())));
}
});
refinement.getDetectorGraph().edgeSet().forEach(edge -> {
TRelationshipTemplate relationshipTemplate = refinement.getGraphMapping().getEdgeCorrespondence(edge, false).getTemplate();
this.applyPropertyMappings(refinement, edge.getId(), relationshipTemplate, topology, idMapping);
if (!getStayMappingsOfCurrentElement(prm, edge.getTemplate()).findFirst().isPresent()) {
topology.getNodeTemplateOrRelationshipTemplate().remove(relationshipTemplate);
}
});
return idMapping;
}
Aggregations