Search in sources :

Example 21 with ParsingError

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

the class ArchiveRootPostProcessor method processImports.

/**
 * Process imports within the archive and compute its complete dependency set.
 * Resolve all dependency version conflicts using the following rules:
 * <ul>
 * <li>If two direct dependencies conflict with each other, use the latest version</li>
 * <li>If a transitive dependency conflicts with a direct dependency, use the direct dependency version</li>
 * <li>If two transitive dependency conflict with each other, use the latest version.</li>
 * </ul>
 *
 * @param archiveRoot The archive to process.
 */
private void processImports(ArchiveRoot archiveRoot) {
    if (archiveRoot.getArchive().getDependencies() == null || archiveRoot.getArchive().getDependencies().isEmpty()) {
        return;
    }
    // Dependencies defined in the import section only
    // These should override transitive deps regardless of type of conflict ?
    Set<CSARDependency> dependencies = archiveRoot.getArchive().getDependencies();
    // Ensure the archive does not import itself
    Csar archive = archiveRoot.getArchive();
    if (dependencies.contains(new CSARDependency(archive.getName(), archive.getVersion(), archive.getHash()))) {
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.CSAR_IMPORT_ITSELF, AlienUtils.prefixWith(":", archive.getVersion(), archive.getName()), null, "Import itself", null, null));
    }
    /*
         * Three types of conflicts :
         * - A transitive dep has a different version than a direct dependency => Force transitive to direct version
         * - Transitive dependencies with the same name and different version are used => Use latest
         * - Direct dependencies with the same name and different version are used => Error or use latest ?
         */
    // 1. Resolve all direct dependencies using latest version
    dependencies.removeIf(dependency -> dependencyConflictsWithLatest(dependency, dependencies));
    // Compute all distinct transitives dependencies
    final Set<CSARDependency> transitiveDependencies = new HashSet<>(dependencies.stream().map(csarDependency -> ToscaContext.get().getArchive(csarDependency.getName(), csarDependency.getVersion())).map(Csar::getDependencies).filter(c -> c != null).reduce(Sets::union).orElse(Collections.emptySet()));
    // 2. Resolve all transitive vs. direct dependencies conflicts using the direct dependency's version
    transitiveDependencies.removeIf(transitiveDependency -> dependencyConflictsWithDirect(transitiveDependency, dependencies));
    // 3. Resolve all transitive dependencies conflicts using latest version
    transitiveDependencies.removeIf(transitiveDependency -> dependencyConflictsWithLatest(transitiveDependency, transitiveDependencies));
    // Merge all dependencies (direct + transitives)
    final Set<CSARDependency> mergedDependencies = new HashSet<>(Sets.union(dependencies, transitiveDependencies));
    archiveRoot.getArchive().setDependencies(mergedDependencies);
    // Update Tosca context with the complete dependency set
    ToscaContext.get().resetDependencies(mergedDependencies);
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) VersionUtil(alien4cloud.utils.VersionUtil) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) HashSet(java.util.HashSet) Node(org.yaml.snakeyaml.nodes.Node) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) RepositoryDefinition(org.alien4cloud.tosca.model.definitions.RepositoryDefinition) ToscaContext(alien4cloud.tosca.context.ToscaContext) PropertyUtil(alien4cloud.utils.PropertyUtil) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Csar(org.alien4cloud.tosca.model.Csar) MapUtils(org.apache.commons.collections.MapUtils) AlienUtils(alien4cloud.utils.AlienUtils) ParsingErrorLevel(alien4cloud.tosca.parser.ParsingErrorLevel) Resource(javax.annotation.Resource) Set(java.util.Set) ErrorCode(alien4cloud.tosca.parser.impl.ErrorCode) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Component(org.springframework.stereotype.Component) ParsingContextExecution(alien4cloud.tosca.parser.ParsingContextExecution) NormativeCredentialConstant(org.alien4cloud.tosca.normative.constants.NormativeCredentialConstant) Collections(java.util.Collections) ParsingError(alien4cloud.tosca.parser.ParsingError) Csar(org.alien4cloud.tosca.model.Csar) ParsingError(alien4cloud.tosca.parser.ParsingError) Sets(com.google.common.collect.Sets) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Example 22 with ParsingError

use of alien4cloud.tosca.parser.ParsingError 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 23 with ParsingError

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

the class ReferencedParser method parse.

@Override
public T parse(Node node, ParsingContextExecution context) {
    INodeParser<?> delegate = context.getRegistry().get(typeName);
    if (delegate == null) {
        log.error("No parser found for yaml type {}", typeName);
        context.getParsingErrors().add(new ParsingError(ErrorCode.ALIEN_MAPPING_ERROR, "No parser found for yaml type", node.getStartMark(), "", node.getEndMark(), typeName));
        return null;
    }
    return (T) delegate.parse(node, context);
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError)

Example 24 with ParsingError

use of alien4cloud.tosca.parser.ParsingError 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 25 with ParsingError

use of alien4cloud.tosca.parser.ParsingError 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)

Aggregations

ParsingError (alien4cloud.tosca.parser.ParsingError)46 Node (org.yaml.snakeyaml.nodes.Node)21 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)13 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)7 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)7 Map (java.util.Map)6 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)4 Csar (org.alien4cloud.tosca.model.Csar)4 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)4 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)4 ToscaContext (alien4cloud.tosca.context.ToscaContext)3 INodeParser (alien4cloud.tosca.parser.INodeParser)3 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 Workflow (org.alien4cloud.tosca.model.workflow.Workflow)3 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)2 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)2 AlreadyExistException (alien4cloud.exception.AlreadyExistException)2