package alien4cloud.tosca.parser.impl.advanced;

import javax.annotation.Resource;

import org.alien4cloud.tosca.model.CSARDependency;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.nodes.Node;

import alien4cloud.paas.exception.NotSupportedException;
import alien4cloud.tosca.parser.INodeParser;
import alien4cloud.tosca.parser.ParsingContextExecution;
import alien4cloud.tosca.parser.ParsingError;
import alien4cloud.tosca.parser.ParsingErrorLevel;
import alien4cloud.tosca.parser.impl.ErrorCode;
import alien4cloud.tosca.parser.impl.base.ScalarParser;
import lombok.extern.slf4j.Slf4j;

/**
 * Import parser that doesn't validate anything
 * For validation of version or presence in catalog, see {@link ImportParser}
 */
@Slf4j
@Component
public class LaxImportParser implements INodeParser<CSARDependency> {
    @Resource
    private ScalarParser scalarParser;

    @Override
    public CSARDependency parse(Node node, ParsingContextExecution context) {
        String valueAsString = scalarParser.parse(node, context);
        if (StringUtils.isNotBlank(valueAsString)) {
            if (valueAsString.contains(":")) {
                String[] dependencyStrs = valueAsString.split(":");
                if (dependencyStrs.length == 2) {
                    String dependencyName = dependencyStrs[0];
                    String dependencyVersion = dependencyStrs[1];
                    return new CSARDependency(dependencyName, dependencyVersion);
                }
                context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.SYNTAX_ERROR, "Import definition is not valid",
                        node.getStartMark(), "Dependency should be specified as name:version", node.getEndMark(), "Import"));
            } else {
                // TODO check that the relative file exists and trigger import
                throw new NotSupportedException("Relative import is currently not supported in Alien 4 Cloud");
            }
        }
        return null;
    }
}
@@ -73,24 +73,120 @@
 .Resource;%0A%0A
+import alien4cloud.utils.VersionUtil;%0Aimport alien4cloud.utils.version.InvalidVersionException;%0A
 import org.a
@@ -224,16 +224,16 @@
 ndency;%0A
-
 import o
@@ -1533,16 +1533,691 @@
 trs%5B1%5D;%0A
+                    // check that version has the righ format%0A                    try %7B%0A                        VersionUtil.parseVersion(dependencyVersion);%0A                    %7D catch (InvalidVersionException e) %7B%0A                        context.getParsingErrors()%0A                                .add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.SYNTAX_ERROR,%0A                                        %22Version specified in the dependency is not a valid version.%22, node.getStartMark(),%0A                                        %22Dependency should be specified as name:version%22, node.getEndMark(), %22Import%22));%0A                        return null;%0A                    %7D%0A
         
@@ -2624,180 +2624,326 @@
     
-// TODO check that the relative file exists and trigger import%0A                throw new NotSupportedException(%22Relative import is currently not supported in Alien 4 Cloud%22
+context.getParsingErrors()%0A                        .add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.SYNTAX_ERROR, %22Relative import is currently not supported in Alien 4 Cloud%22,%0A                                node.getStartMark(), %22Dependency should be specified as name:version%22, node.getEndMark(), %22Import%22)
 );%0A 

Add a validation of the version format in import strings.