Search in sources :

Example 1 with ParsingError

use of alien4cloud.tosca.parser.ParsingError in project yorc-a4c-plugin by ystia.

the class DeployTask method csar2zip.

/**
 * Get csar and add entries in zip file for it
 * @return relative path to the yml, ex: welcome-types/3.0-SNAPSHOT/welcome-types.yaml
 */
private String csar2zip(ZipOutputStream zout, Csar csar, int location) throws IOException, ParsingException {
    // Get path directory to the needed info:
    // should be something like: ...../runtime/csar/<module>/<version>/expanded
    // We should have a yml or a yaml here
    Path csarPath = orchestrator.getCSAR(csar.getName(), csar.getVersion());
    String dirname = csarPath.toString();
    File directory = new File(dirname);
    String relative = csar.getName() + "/" + csar.getVersion() + "/";
    String ret = relative + csar.getYamlFilePath();
    // All files under this directory must be put in the zip
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);
    while (!queue.isEmpty()) {
        directory = queue.pop();
        for (File kid : directory.listFiles()) {
            String name = base.relativize(kid.toURI()).getPath();
            if (kid.isDirectory()) {
                queue.push(kid);
            } else {
                File file = kid;
                createZipEntries(relative + name, zout);
                if (name.equals(csar.getYamlFilePath())) {
                    ToscaContext.Context oldCtx = ToscaContext.get();
                    ParsingResult<ArchiveRoot> parsingResult;
                    try {
                        ToscaContext.init(Sets.newHashSet());
                        parsingResult = orchestrator.getParser().parseFile(Paths.get(file.getAbsolutePath()));
                    } finally {
                        ToscaContext.set(oldCtx);
                    }
                    if (parsingResult.getContext().getParsingErrors().size() > 0) {
                        Boolean hasFatalError = false;
                        for (ParsingError error : parsingResult.getContext().getParsingErrors()) {
                            if (error.getErrorLevel().equals(ParsingErrorLevel.ERROR)) {
                                log.error(error.toString());
                                hasFatalError = true;
                            } else {
                                log.warn(error.toString());
                            }
                        }
                        if (hasFatalError) {
                            continue;
                        }
                    }
                    ArchiveRoot root = parsingResult.getResult();
                    if (location == LOC_KUBERNETES) {
                        matchKubernetesImplementation(root);
                    }
                    String yaml = orchestrator.getToscaComponentExporter().getYaml(root);
                    zout.write(yaml.getBytes(Charset.forName("UTF-8")));
                } else {
                    copy(file, zout);
                }
            }
        }
    }
    return ret;
}
Also used : Path(java.nio.file.Path) ToscaContext(alien4cloud.tosca.context.ToscaContext) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) File(java.io.File) URI(java.net.URI) LinkedList(java.util.LinkedList)

Example 2 with ParsingError

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

the class PropertyValueChecker method checkProperty.

public void checkProperty(String propertyName, Node propertyValueNode, AbstractPropertyValue propertyValue, PropertyDefinition propertyDefinition, Map<String, PropertyDefinition> inputs, String templateName) {
    if (propertyValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) propertyValue;
        String parameters = function.getParameters().get(0);
        // check get_input only
        if (function.getFunction().equals("get_input")) {
            if (inputs == null || !inputs.keySet().contains(parameters)) {
                ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.MISSING_TOPOLOGY_INPUT, templateName, propertyValueNode.getStartMark(), parameters, propertyValueNode.getEndMark(), propertyName));
            }
        }
    } else if (propertyValue instanceof PropertyValue<?>) {
        checkProperty(propertyName, propertyValueNode, (PropertyValue<?>) propertyValue, propertyDefinition, templateName);
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)

Example 3 with ParsingError

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

the class RelationshipPostProcessor method process.

public void process(NodeType nodeTemplateType, Map.Entry<String, RelationshipTemplate> instance) {
    RelationshipTemplate relationshipTemplate = instance.getValue();
    if (relationshipTemplate.getTarget() == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
        // the node template name is required
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NODE_TEMPLATE_NAME_REQUIRED, null, node.getStartMark(), null, node.getEndMark(), null));
    }
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    propertyValueChecker.checkProperties(relationshipType, relationshipTemplate.getProperties(), instance.getKey());
    RequirementDefinition rd = getRequirementDefinitionByName(nodeTemplateType, relationshipTemplate.getRequirementName());
    if (rd == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getRequirementName());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    if (relationshipTemplate.getType() == null) {
        // if the relationship type has not been defined on the requirement assignment it may be defined on the requirement definition.
        relationshipTemplate.setType(rd.getRelationshipType());
    }
    referencePostProcessor.process(new ReferencePostProcessor.TypeReference(relationshipTemplate, relationshipTemplate.getType(), RelationshipType.class));
    relationshipTemplate.setRequirementType(rd.getType());
    ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
    // now find the target of the relation
    NodeTemplate targetNodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(relationshipTemplate.getTarget());
    if (targetNodeTemplate == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getTarget());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getTarget()));
        return;
    }
    // alien actually supports a capability type in the TOSCA yaml
    String capabilityStr = relationshipTemplate.getTargetedCapabilityName();
    Capability capability = null;
    if (capabilityStr == null) {
        // the capability type is not known, we assume that we are parsing a Short notation (node only)
        if (targetNodeTemplate.getCapabilities() != null) {
            // let's try to find all match for a given type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, relationshipTemplate.getRequirementType());
            if (capability == null) {
                capability = targetNodeTemplate.getCapabilities().get(relationshipTemplate.getRequirementName());
                if (capability != null) {
                    relationshipTemplate.setTargetedCapabilityName(rd.getId());
                }
            }
        }
    } else {
        // Let's try to find if the target node has a capability as named in the capability string of the relationship (requirement assignment)
        if (targetNodeTemplate.getCapabilities() != null) {
            capability = targetNodeTemplate.getCapabilities().get(capabilityStr);
        }
        if (capability == null) {
            // The capabilityStr may be the name of a type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, capabilityStr);
        }
    }
    if (capability == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
        // we should fail
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_CAPABILITY_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    RelationshipType indexedRelationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    if (indexedRelationshipType == null) {
        // Error managed by the reference post processor.
        return;
    }
    Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
    TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), relationshipTemplate.getProperties(), false);
    relationshipTemplate.setProperties(properties);
    relationshipTemplate.setAttributes(indexedRelationshipType.getAttributes());
    // FIXME we should check that the artifact is defined at the type level.
    safe(instance.getValue().getArtifacts()).values().forEach(templateDeploymentArtifactPostProcessor);
    Map<String, DeploymentArtifact> mergedArtifacts = instance.getValue().getArtifacts();
    if (mergedArtifacts == null) {
        mergedArtifacts = new HashMap<>();
    }
    mergedArtifacts.putAll(safe(indexedRelationshipType.getArtifacts()));
    relationshipTemplate.setArtifacts(mergedArtifacts);
    // TODO Manage interfaces inputs to copy them to all operations.
    for (Interface anInterface : safe(instance.getValue().getInterfaces()).values()) {
        safe(anInterface.getOperations()).values().stream().map(Operation::getImplementationArtifact).filter(Objects::nonNull).forEach(implementationArtifactPostProcessor);
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) Node(org.yaml.snakeyaml.nodes.Node) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate)

Example 4 with ParsingError

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

the class RelationshipPostProcessor method getCapabilityByType.

private Capability getCapabilityByType(NodeTemplate targetNodeTemplate, RelationshipTemplate relationshipTemplate, String capabilityType) {
    Capability capability = null;
    Map<String, Capability> compatibleCapabilityByType = capabilityMatcherService.getCompatibleCapabilityByType(targetNodeTemplate, capabilityType);
    Entry<String, Capability> capabilityEntry = null;
    if (compatibleCapabilityByType.size() == 1) {
        capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
    } else if (compatibleCapabilityByType.size() > 1) {
        capabilityEntry = compatibleCapabilityByType.entrySet().iterator().next();
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.REQUIREMENT_CAPABILITY_MULTIPLE_MATCH, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
    }
    if (capabilityEntry != null) {
        capability = capabilityEntry.getValue();
        relationshipTemplate.setTargetedCapabilityName(capabilityEntry.getKey());
    }
    return capability;
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) Capability(org.alien4cloud.tosca.model.templates.Capability) Node(org.yaml.snakeyaml.nodes.Node)

Example 5 with ParsingError

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

the class TopologyPostProcessor method process.

@Override
public void process(Topology instance) {
    if (instance == null) {
        return;
    }
    ArchiveRoot archiveRoot = ParsingContextExecution.getRootObj();
    // The yaml node for the topology
    Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
    setDependencies(instance, archiveRoot);
    if (instance.isEmpty()) {
        // if the topology doesn't contains any node template it won't be imported so add a warning.
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.EMPTY_TOPOLOGY, null, node.getStartMark(), null, node.getEndMark(), ""));
    }
    // archive name and version
    instance.setArchiveName(archiveRoot.getArchive().getName());
    instance.setArchiveVersion(archiveRoot.getArchive().getVersion());
    // Inputs validation
    safe(instance.getInputs()).entrySet().forEach(propertyDefinitionPostProcessor);
    safe(instance.getInputArtifacts()).values().forEach(typeDeploymentArtifactPostProcessor);
    int groupIndex = 0;
    // Groups validation
    for (NodeGroup nodeGroup : safe(instance.getGroups()).values()) {
        nodeGroup.setIndex(groupIndex++);
        groupPostProcessor.process(nodeGroup);
    }
    // Policies templates validation
    safe(instance.getPolicies()).forEach((policyName, policyTemplate) -> {
        // set the templateName
        policyTemplate.setName(policyName);
        policyTemplatePostProcessor.process(policyTemplate);
    });
    // Node templates validation
    for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : safe(instance.getNodeTemplates()).entrySet()) {
        nodeTemplateEntry.getValue().setName(nodeTemplateEntry.getKey());
        nodeTemplatePostProcessor.process(nodeTemplateEntry.getValue());
    }
    safe(instance.getNodeTemplates()).values().forEach(nodeTemplateRelationshipPostProcessor);
    substitutionMappingPostProcessor.process(instance.getSubstitutionMapping());
    // first validate names
    TopologyUtils.normalizeAllNodeTemplateName(instance, ParsingContextExecution.getParsingErrors(), ParsingContextExecution.getObjectToNodeMap());
    // Post process workflows
    workflowPostProcessor.processWorkflows(instance, node);
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Node(org.yaml.snakeyaml.nodes.Node) Map(java.util.Map) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

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