Search in sources :

Example 1 with SubstitutionMapping

use of org.alien4cloud.tosca.model.templates.SubstitutionMapping in project alien4cloud by alien4cloud.

the class AddSubstitutionTypeProcessor method process.

@Override
public void process(Csar csar, Topology topology, AddSubstitutionTypeOperation operation) {
    if (topology.getSubstitutionMapping() == null) {
        topology.setSubstitutionMapping(new SubstitutionMapping());
    }
    NodeType nodeType = toscaTypeSearchService.getElementInDependencies(NodeType.class, operation.getElementId(), topology.getDependencies());
    // if not null the node type exists in the dependencies, there is no choices for this type version
    if (nodeType == null) {
        // the node type does'nt exist in this topology dependencies
        // we need to find the latest version of this component and use it as default
        nodeType = toscaTypeSearchService.findMostRecent(NodeType.class, operation.getElementId());
        if (nodeType == null) {
            throw new NotFoundException("Node type with name <" + operation.getElementId() + "> cannot be found in the catalog.");
        }
        topologyService.loadType(topology, nodeType);
    }
    topology.getSubstitutionMapping().setSubstitutionType(operation.getElementId());
}
Also used : NodeType(org.alien4cloud.tosca.model.types.NodeType) NotFoundException(alien4cloud.exception.NotFoundException) SubstitutionMapping(org.alien4cloud.tosca.model.templates.SubstitutionMapping)

Example 2 with SubstitutionMapping

use of org.alien4cloud.tosca.model.templates.SubstitutionMapping in project alien4cloud by alien4cloud.

the class SustitutionMappingParser method parse.

@Override
public SubstitutionMapping parse(Node node, ParsingContextExecution context) {
    Topology topology = (Topology) context.getParent();
    if (!(node instanceof MappingNode)) {
        // we expect a MappingNode
        context.getParsingErrors().add(new ParsingError(ErrorCode.YAML_MAPPING_NODE_EXPECTED, null, node.getStartMark(), null, node.getEndMark(), null));
        return null;
    }
    SubstitutionMapping result = new SubstitutionMapping();
    MappingNode mappingNode = ((MappingNode) node);
    List<NodeTuple> nodeTuples = mappingNode.getValue();
    for (NodeTuple nodeTuple : nodeTuples) {
        String key = scalarParser.parse(nodeTuple.getKeyNode(), context);
        Node valueNode = nodeTuple.getValueNode();
        switch(key) {
            case NODE_TYPE:
                String nodeTypeName = scalarParser.parse(valueNode, context);
                result.setSubstitutionType(nodeTypeName);
                break;
            case CAPABILITIES:
                result.setCapabilities(parseSubstitutionTargets(valueNode, context));
                break;
            case REQUIREMENTS:
                result.setRequirements(parseSubstitutionTargets(valueNode, context));
                break;
            default:
                context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNRECOGNIZED_PROPERTY, null, valueNode.getStartMark(), "Key <" + key + "> is not recognized for substitution mapping.", valueNode.getEndMark(), "key"));
        }
    }
    return result;
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) Topology(org.alien4cloud.tosca.model.templates.Topology) SubstitutionMapping(org.alien4cloud.tosca.model.templates.SubstitutionMapping) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Aggregations

SubstitutionMapping (org.alien4cloud.tosca.model.templates.SubstitutionMapping)2 NotFoundException (alien4cloud.exception.NotFoundException)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 Topology (org.alien4cloud.tosca.model.templates.Topology)1 NodeType (org.alien4cloud.tosca.model.types.NodeType)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)1