Search in sources :

Example 11 with EntityId

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);
            }
        }
    }
}
Also used : EntityId(org.opentosca.toscana.model.EntityId) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Entity(org.opentosca.toscana.core.parse.model.Entity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) ToscaTemplateException(org.opentosca.toscana.core.parse.ToscaTemplateException) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity)

Example 12 with EntityId

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;
    }
}
Also used : EntityId(org.opentosca.toscana.model.EntityId)

Example 13 with EntityId

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);
        }
    }
}
Also used : EntityId(org.opentosca.toscana.model.EntityId) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode)

Aggregations

EntityId (org.opentosca.toscana.model.EntityId)13 MappingEntity (org.opentosca.toscana.core.parse.model.MappingEntity)5 ScalarEntity (org.opentosca.toscana.core.parse.model.ScalarEntity)5 Entity (org.opentosca.toscana.core.parse.model.Entity)4 Test (org.junit.Test)3 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)3 Artifact (org.opentosca.toscana.model.artifact.Artifact)3 ServiceGraph (org.opentosca.toscana.core.parse.model.ServiceGraph)2 ToscaTemplateException (org.opentosca.toscana.core.parse.ToscaTemplateException)1 DatabaseEndpointCapability (org.opentosca.toscana.model.capability.DatabaseEndpointCapability)1 RootNode (org.opentosca.toscana.model.node.RootNode)1 WebApplication (org.opentosca.toscana.model.node.WebApplication)1 Operation (org.opentosca.toscana.model.operation.Operation)1 ConnectsTo (org.opentosca.toscana.model.relation.ConnectsTo)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)1 SequenceNode (org.yaml.snakeyaml.nodes.SequenceNode)1