Search in sources :

Example 6 with ScalarEntity

use of org.opentosca.toscana.core.parse.model.ScalarEntity in project TOSCAna by StuPro-TOSCAna.

the class GraphNormalizerTest method repositoryNormalization.

@Test
public void repositoryNormalization() {
    ServiceGraph graph = new ServiceGraph(REPOSITORY, logMock());
    Collection<Entity> repositories = graph.getEntity(ToscaStructure.REPOSITORIES).get().getChildren();
    Entity repository = repositories.iterator().next();
    Optional<Entity> url = repository.getChild("url");
    assertTrue(url.isPresent());
    Assert.assertEquals("http://test.repo.com/", ((ScalarEntity) url.get()).getValue());
}
Also used : ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Entity(org.opentosca.toscana.core.parse.model.Entity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) ServiceGraph(org.opentosca.toscana.core.parse.model.ServiceGraph) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Example 7 with ScalarEntity

use of org.opentosca.toscana.core.parse.model.ScalarEntity in project TOSCAna by StuPro-TOSCAna.

the class TypeResolverTest method supportForAllWineryNodeTypes.

@Test
public void supportForAllWineryNodeTypes() {
    Set<String> wineryNodeTypes = getWineryNodeTypes();
    for (String nodeType : wineryNodeTypes) {
        logger.info("Testing conversion of type '{}'", nodeType);
        ServiceGraph graph = new ServiceGraph(logMock());
        MappingEntity nodeEntity = new MappingEntity(ToscaStructure.NODE_TEMPLATES.descend("my-node"), graph);
        graph.addEntity(nodeEntity);
        ScalarEntity typeEntity = new ScalarEntity(nodeType, nodeEntity.getId().descend("type"), graph);
        graph.addEntity(typeEntity);
        BaseToscaElement node = TypeWrapper.wrapTypedElement(nodeEntity);
        assertNotNull(node);
        logger.info("Node Type '{}': known", nodeType);
        System.out.println();
    }
// successful if no exception thrown
}
Also used : BaseToscaElement(org.opentosca.toscana.model.BaseToscaElement) ServiceGraph(org.opentosca.toscana.core.parse.model.ServiceGraph) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Example 8 with ScalarEntity

use of org.opentosca.toscana.core.parse.model.ScalarEntity in project TOSCAna by StuPro-TOSCAna.

the class IntrinsicFunctionResolver method findTarget.

private static Entity findTarget(Entity current, Iterator<Entity> it) {
    while (it.hasNext()) {
        Entity next = it.next();
        Optional<Entity> child = current.getChild(((ScalarEntity) next).getValue());
        if (child.isPresent()) {
            current = child.get();
        } else {
            return null;
        }
    }
    return current;
}
Also used : ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Entity(org.opentosca.toscana.core.parse.model.Entity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity)

Example 9 with ScalarEntity

use of org.opentosca.toscana.core.parse.model.ScalarEntity 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 10 with ScalarEntity

use of org.opentosca.toscana.core.parse.model.ScalarEntity in project TOSCAna by StuPro-TOSCAna.

the class ScalarTypeConverter method convertScalarEntity.

/**
 *     Converts the value attached to given {@code scalarEntity } to an instance of a type which is specified by given {@code key}.
 */
static <T> T convertScalarEntity(ScalarEntity scalarEntity, ToscaKey<T> key, Entity parent) {
    String value = scalarEntity.getValue();
    Class targetType = key.getType();
    if (String.class.isAssignableFrom(targetType)) {
        return (T) value;
    } else if (Integer.class.isAssignableFrom(targetType)) {
        Integer number;
        if (UNBOUNDED.equals(value)) {
            number = Integer.MAX_VALUE;
        } else {
            number = Integer.valueOf(value);
        }
        return (T) number;
    } else if (Boolean.class.isAssignableFrom(targetType)) {
        return (T) Boolean.valueOf(value);
    } else if (targetType.isEnum()) {
        Map<String, T> enumMap = EnumUtils.getEnumMap(targetType);
        Optional<T> result = enumMap.entrySet().stream().filter(entry -> value.equalsIgnoreCase(entry.getKey())).map(Map.Entry::getValue).findAny();
        return result.orElseThrow(() -> new NoSuchElementException(String.format("No value with name '%s' in enum '%s'", value, targetType.getSimpleName())));
    } else if (OperationVariable.class.isAssignableFrom(targetType)) {
        Connection c = scalarEntity.getGraph().getEdge(parent, scalarEntity);
        String name = null;
        OperationVariable var;
        if (c != null) {
            name = c.getKey();
            var = new OperationVariable(scalarEntity, name);
        } else {
            var = new OperationVariable(scalarEntity);
        }
        return (T) var;
    } else if (SizeUnit.class.isAssignableFrom(targetType)) {
        SizeUnit.Unit fromDefaultUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.FROM);
        SizeUnit.Unit toUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.TO);
        if (fromDefaultUnit == null || toUnit == null) {
            throw new IllegalStateException("ToscaKey defining a SizeUnit is illegal: No directive set for source and target units");
        }
        return (T) SizeUnit.convert(value, fromDefaultUnit, toUnit);
    } else if (Port.class.isAssignableFrom(targetType)) {
        return (T) new Port(Integer.valueOf(value));
    } else {
        throw new UnsupportedOperationException(String.format("Cannot convert value of type %s: currently unsupported", targetType.getSimpleName()));
    }
}
Also used : Port(org.opentosca.toscana.model.datatype.Port) ToscaKey(org.opentosca.toscana.model.util.ToscaKey) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) EnumUtils(org.apache.commons.lang3.EnumUtils) Map(java.util.Map) Connection(org.opentosca.toscana.core.parse.model.Connection) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Optional(java.util.Optional) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) NoSuchElementException(java.util.NoSuchElementException) Entity(org.opentosca.toscana.core.parse.model.Entity) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) Optional(java.util.Optional) Port(org.opentosca.toscana.model.datatype.Port) Connection(org.opentosca.toscana.core.parse.model.Connection) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ScalarEntity (org.opentosca.toscana.core.parse.model.ScalarEntity)10 MappingEntity (org.opentosca.toscana.core.parse.model.MappingEntity)7 Entity (org.opentosca.toscana.core.parse.model.Entity)6 ServiceGraph (org.opentosca.toscana.core.parse.model.ServiceGraph)4 ToscaTemplateException (org.opentosca.toscana.core.parse.ToscaTemplateException)3 EntityId (org.opentosca.toscana.model.EntityId)3 Test (org.junit.Test)2 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)2 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 EnumUtils (org.apache.commons.lang3.EnumUtils)1 Connection (org.opentosca.toscana.core.parse.model.Connection)1 BaseToscaElement (org.opentosca.toscana.model.BaseToscaElement)1 Artifact (org.opentosca.toscana.model.artifact.Artifact)1 Port (org.opentosca.toscana.model.datatype.Port)1 SizeUnit (org.opentosca.toscana.model.datatype.SizeUnit)1 RootNode (org.opentosca.toscana.model.node.RootNode)1 OperationVariable (org.opentosca.toscana.model.operation.OperationVariable)1 ToscaKey (org.opentosca.toscana.model.util.ToscaKey)1