Search in sources :

Example 1 with INodeParser

use of alien4cloud.tosca.parser.INodeParser in project alien4cloud by alien4cloud.

the class KeyDiscriminatorParser method parse.

@Override
public T parse(Node node, ParsingContextExecution context) {
    Set<String> keySet = Sets.newHashSet();
    if (node instanceof MappingNode) {
        // create a set of available keys
        MappingNode mappingNode = (MappingNode) node;
        for (NodeTuple tuple : mappingNode.getValue()) {
            keySet.add(((ScalarNode) tuple.getKeyNode()).getValue());
        }
        INodeParser<T> mappingNodeFallbackParser = null;
        // check if one of the discriminator key exists and if so use it for parsing.
        for (Map.Entry<String, INodeParser<T>> entry : parserByExistKey.entrySet()) {
            if (keySet.contains(entry.getKey())) {
                return entry.getValue().parse(node, context);
            } else if (MAPPING_NODE_FALLBACK_KEY.equals(entry.getKey())) {
                mappingNodeFallbackParser = entry.getValue();
            }
        }
        // if not we should use the mapping node fallback parser.
        if (mappingNodeFallbackParser != null) {
            return mappingNodeFallbackParser.parse(node, context);
        }
    }
    if (fallbackParser != null) {
        return fallbackParser.parse(node, context);
    } else {
        context.getParsingErrors().add(new ParsingError(ErrorCode.UNKNWON_DISCRIMINATOR_KEY, "Invalid scalar value.", node.getStartMark(), "Tosca type cannot be expressed with the given scalar value.", node.getEndMark(), keySet.toString()));
    }
    return null;
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) INodeParser(alien4cloud.tosca.parser.INodeParser) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Map(java.util.Map)

Example 2 with INodeParser

use of alien4cloud.tosca.parser.INodeParser in project alien4cloud by alien4cloud.

the class RelationshipTemplateParser method parse.

@Override
public RelationshipTemplate parse(Node node, ParsingContextExecution context) {
    // To parse a relationship template we actually get the parent node to retrieve the requirement name;
    if (!(node instanceof MappingNode) || ((MappingNode) node).getValue().size() != 1) {
        ParserUtils.addTypeError(node, context.getParsingErrors(), "Requirement assignment");
    }
    MappingNode assignmentNode = (MappingNode) node;
    RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
    String relationshipId = scalarParser.parse(assignmentNode.getValue().get(0).getKeyNode(), context);
    // The relationship's id which is used to identify the relationship within the source
    relationshipTemplate.setName(relationshipId);
    // By default the relationship id is the requirement name, it can be overridden with 'type_requirement'
    relationshipTemplate.setRequirementName(relationshipId);
    // Now parse the content of the relationship assignment.
    node = assignmentNode.getValue().get(0).getValueNode();
    if (node instanceof ScalarNode) {
        // Short notation (host: compute)
        relationshipTemplate.setTarget(scalarParser.parse(node, context));
    } else if (node instanceof MappingNode) {
        MappingNode mappingNode = (MappingNode) node;
        for (NodeTuple nodeTuple : mappingNode.getValue()) {
            String key = scalarParser.parse(nodeTuple.getKeyNode(), context);
            switch(key) {
                case "node":
                    relationshipTemplate.setTarget(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "capability":
                    relationshipTemplate.setTargetedCapabilityName(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "type_requirement":
                    relationshipTemplate.setRequirementName(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "relationship":
                    relationshipTemplate.setType(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "properties":
                    INodeParser<AbstractPropertyValue> propertyValueParser = context.getRegistry().get("node_template_property");
                    MapParser<AbstractPropertyValue> mapParser = baseParserFactory.getMapParser(propertyValueParser, "node_template_property");
                    relationshipTemplate.setProperties(mapParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "interfaces":
                    INodeParser<Map<String, Interface>> interfacesParser = context.getRegistry().get("interfaces");
                    relationshipTemplate.setInterfaces(interfacesParser.parse(nodeTuple.getValueNode(), context));
                    break;
                default:
                    context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNKNOWN_ARTIFACT_KEY, null, node.getStartMark(), "Unrecognized key while parsing implementation artifact", node.getEndMark(), key));
            }
        }
    } else {
        ParserUtils.addTypeError(node, context.getParsingErrors(), "Requirement assignment");
    }
    return relationshipTemplate;
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) INodeParser(alien4cloud.tosca.parser.INodeParser) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) MapParser(alien4cloud.tosca.parser.impl.base.MapParser) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Interface(org.alien4cloud.tosca.model.definitions.Interface)

Example 3 with INodeParser

use of alien4cloud.tosca.parser.INodeParser in project alien4cloud by alien4cloud.

the class RelationshipTemplateParser method parse.

@Override
public RelationshipTemplate parse(Node node, ParsingContextExecution context) {
    // To parse a relationship template we actually get the parent node to retrieve the requirement name;
    if (!(node instanceof MappingNode) || ((MappingNode) node).getValue().size() != 1) {
        ParserUtils.addTypeError(node, context.getParsingErrors(), "Requirement assignment");
    }
    MappingNode assignmentNode = (MappingNode) node;
    RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
    relationshipTemplate.setRequirementName(scalarParser.parse(assignmentNode.getValue().get(0).getKeyNode(), context));
    // Now parse the content of the relationship assignment.
    node = assignmentNode.getValue().get(0).getValueNode();
    if (node instanceof ScalarNode) {
        // Short notation (host: compute)
        relationshipTemplate.setTarget(scalarParser.parse(node, context));
    } else if (node instanceof MappingNode) {
        MappingNode mappingNode = (MappingNode) node;
        for (NodeTuple nodeTuple : mappingNode.getValue()) {
            String key = scalarParser.parse(nodeTuple.getKeyNode(), context);
            switch(key) {
                case "node":
                    relationshipTemplate.setTarget(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "capability":
                    relationshipTemplate.setTargetedCapabilityName(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "relationship":
                    relationshipTemplate.setType(scalarParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "properties":
                    INodeParser<AbstractPropertyValue> propertyValueParser = context.getRegistry().get("node_template_property");
                    MapParser<AbstractPropertyValue> mapParser = baseParserFactory.getMapParser(propertyValueParser, "node_template_property");
                    relationshipTemplate.setProperties(mapParser.parse(nodeTuple.getValueNode(), context));
                    break;
                case "interfaces":
                    INodeParser<Map<String, Interface>> interfacesParser = context.getRegistry().get("interfaces");
                    relationshipTemplate.setInterfaces(interfacesParser.parse(nodeTuple.getValueNode(), context));
                    break;
                default:
                    context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNKNOWN_ARTIFACT_KEY, null, node.getStartMark(), "Unrecognized key while parsing implementation artifact", node.getEndMark(), key));
            }
        }
    } else {
        ParserUtils.addTypeError(node, context.getParsingErrors(), "Requirement assignment");
    }
    return relationshipTemplate;
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) INodeParser(alien4cloud.tosca.parser.INodeParser) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) MapParser(alien4cloud.tosca.parser.impl.base.MapParser) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Interface(org.alien4cloud.tosca.model.definitions.Interface)

Example 4 with INodeParser

use of alien4cloud.tosca.parser.INodeParser in project alien4cloud by alien4cloud.

the class DiscriminatorMappingBuilder method buildMapping.

@Override
public MappingTarget buildMapping(MappingNode mappingNode, ParsingContextExecution context) {
    String mappingTarget = "";
    Map<String, INodeParser<?>> parsersByKey = Maps.newHashMap();
    INodeParser<?> fallbackParser = null;
    for (NodeTuple tuple : mappingNode.getValue()) {
        String tupleKey = ParserUtils.getScalar(tuple.getKeyNode(), context);
        if (DISCRIMINATOR.equals(tupleKey)) {
            mappingTarget = ParserUtils.getScalar(tuple.getValueNode(), context);
        } else if (KEYS.equals(tupleKey)) {
            MappingNode keys = (MappingNode) tuple.getValueNode();
            for (NodeTuple keyMappingTuple : keys.getValue()) {
                parsersByKey.put(ParserUtils.getScalar(keyMappingTuple.getKeyNode(), context), baseParserFactory.getReferencedParser(ParserUtils.getScalar(keyMappingTuple.getValueNode(), context)));
            }
        } else if (DEFAULT.equals(tupleKey)) {
            fallbackParser = baseParserFactory.getReferencedParser(ParserUtils.getScalar(tuple.getValueNode(), context));
        }
    }
    return new MappingTarget(mappingTarget, baseParserFactory.getKeyDiscriminatorParser(parsersByKey, fallbackParser));
}
Also used : INodeParser(alien4cloud.tosca.parser.INodeParser) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) MappingTarget(alien4cloud.tosca.parser.MappingTarget)

Aggregations

INodeParser (alien4cloud.tosca.parser.INodeParser)4 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)4 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)4 ParsingError (alien4cloud.tosca.parser.ParsingError)3 MapParser (alien4cloud.tosca.parser.impl.base.MapParser)2 Interface (org.alien4cloud.tosca.model.definitions.Interface)2 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)2 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)2 MappingTarget (alien4cloud.tosca.parser.MappingTarget)1 Map (java.util.Map)1