use of org.opentosca.toscana.model.EntityId in project TOSCAna by StuPro-TOSCAna.
the class LinkResolver method resolveRequirements.
private static void resolveRequirements(ServiceGraph graph) {
logger.debug(LogFormat.indent(1, "requirements"));
Iterator<Entity> nodeIt = graph.iterator(ToscaStructure.NODE_TEMPLATES);
while (nodeIt.hasNext()) {
Entity nodeEntity = nodeIt.next();
Optional<Entity> requirementsEntity = nodeEntity.getChild(RootNode.REQUIREMENTS.name);
if (requirementsEntity.isPresent()) {
Collection<Entity> requirements = requirementsEntity.get().getChildren();
for (Entity requirement : requirements) {
MappingEntity mappingRequirement = (MappingEntity) requirement;
ScalarEntity fulfillerEntity = (ScalarEntity) mappingRequirement.getChild(Requirement.NODE_NAME).get();
String fulfillerName = fulfillerEntity.getValue();
EntityId fulfillerId = ToscaStructure.NODE_TEMPLATES.descend(fulfillerName);
logger.debug(LogFormat.pointAt(2, mappingRequirement.getId() + "." + Requirement.NODE_NAME, fulfillerId));
Entity fulfiller = graph.getEntity(fulfillerId).orElseThrow(() -> new ToscaTemplateException(String.format("No node with name '%s' found, but required as fulfiller in requirement", fulfillerName)));
graph.removeVertex(fulfillerEntity);
graph.addConnection(mappingRequirement, fulfiller, Requirement.NODE_NAME);
}
}
}
}
use of org.opentosca.toscana.model.EntityId in project TOSCAna by StuPro-TOSCAna.
the class ServiceGraph method addEntity.
/**
* Adds a new entity to the graph. Also adds the edge to its parent entity.
* If one ore more parent entities do not exist, automatically adds intermediate entities.
* If equivalent entity already exists, does nothing.
*/
public void addEntity(Entity entity) {
Entity parent = root;
Entity child;
EntityId id = entity.getId();
EntityId currentId = new EntityId(new ArrayList<>());
for (String segment : id.getPath()) {
currentId = currentId.descend(segment);
child = parent.getChild(segment).orElse(null);
if (child == null) {
if (id.equals(currentId)) {
child = entity;
} else {
child = new MappingEntity(currentId, this);
}
boolean added = addVertex(child);
if (added) {
addEdge(parent, child);
}
}
parent = child;
}
}
use of org.opentosca.toscana.model.EntityId in project TOSCAna by StuPro-TOSCAna.
the class ServiceGraph method populateGraph.
/**
* Adds the information of given SnakeYAML node and all of its child nodes to this service graph.
*
* @param node SnakeYAML's node
* @param id the id which shall be used to identify given node
*/
private void populateGraph(Node node, EntityId id) {
if (node instanceof ScalarNode) {
ScalarNode scalarNode = (ScalarNode) node;
ScalarEntity scalarEntity = new ScalarEntity(scalarNode.getValue(), id, this);
addEntity(scalarEntity);
} else if (node instanceof MappingNode) {
MappingNode mappingNode = (MappingNode) node;
MappingEntity mappingEntity = new MappingEntity(id, this);
addEntity(mappingEntity);
for (NodeTuple tuple : mappingNode.getValue()) {
String key = ((ScalarNode) tuple.getKeyNode()).getValue();
Node childNode = tuple.getValueNode();
EntityId childId = id.descend(key);
populateGraph(childNode, childId);
}
} else if (node instanceof SequenceNode) {
SequenceNode sequenceNode = (SequenceNode) node;
SequenceEntity sequenceEntity = new SequenceEntity(sequenceNode, id, this);
addEntity(sequenceEntity);
for (int i = 0; i < sequenceNode.getValue().size(); i++) {
Node childNode = sequenceNode.getValue().get(i);
String childName;
if (childNode instanceof MappingNode) {
NodeTuple childTuple = ((MappingNode) childNode).getValue().get(0);
childName = ((ScalarNode) childTuple.getKeyNode()).getValue();
childNode = childTuple.getValueNode();
} else {
childName = String.valueOf(i);
}
EntityId childId = id.descend(childName);
populateGraph(childNode, childId);
}
}
}
Aggregations