Search in sources :

Example 1 with TServiceTemplate

use of org.eclipse.winery.model.tosca.yaml.TServiceTemplate in project winery by eclipse.

the class Reader method readServiceTemplate.

/**
 * Reads a file and converts it to a ServiceTemplate
 *
 * @return ServiceTemplate
 * @throws MultiException the ServiceTemplate or the file is invalid.
 */
private TServiceTemplate readServiceTemplate(Path path, Path file, String namespace) throws MultiException {
    Path filePath;
    if (Objects.isNull(path)) {
        filePath = file;
    } else {
        filePath = path.resolve(file);
    }
    // 
    if (!fileChanged(filePath)) {
        if (exceptionBuffer.containsKey(filePath)) {
            throw exceptionBuffer.get(filePath);
        }
        if (serviceTemplateBuffer.containsKey(filePath)) {
            return serviceTemplateBuffer.get(filePath);
        }
    }
    logger.debug("Read Service Template: {}", filePath);
    try {
        // pre parse checking
        try {
            ObjectValidator objectValidator = new ObjectValidator();
            objectValidator.validateObject(readObject(filePath));
        } catch (ConstructorException e) {
            ExceptionInterpreter interpreter = new ExceptionInterpreter();
            throw new MultiException().add(interpreter.interpret(e));
        } catch (ScannerException e) {
            ExceptionInterpreter interpreter = new ExceptionInterpreter();
            throw new MultiException().add(interpreter.interpret(e));
        } catch (YAMLParserException e) {
            throw new MultiException().add(e);
        }
        // parse checking
        TServiceTemplate result = buildServiceTemplate(readObject(filePath), namespace);
        // post parse checking
        Validator validator = new Validator(path);
        validator.validate(result, namespace);
        serviceTemplateBuffer.put(filePath, result);
        return result;
    } catch (MultiException e) {
        exceptionBuffer.put(filePath, e);
        throw e.add(file.toString());
    }
}
Also used : Path(java.nio.file.Path) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) ExceptionInterpreter(org.eclipse.winery.yaml.common.validator.support.ExceptionInterpreter) YAMLParserException(org.eclipse.winery.yaml.common.exception.YAMLParserException) ObjectValidator(org.eclipse.winery.yaml.common.validator.ObjectValidator) ConstructorException(org.yaml.snakeyaml.constructor.ConstructorException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) ObjectValidator(org.eclipse.winery.yaml.common.validator.ObjectValidator) Validator(org.eclipse.winery.yaml.common.validator.Validator) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate)

Example 2 with TServiceTemplate

use of org.eclipse.winery.model.tosca.yaml.TServiceTemplate in project winery by eclipse.

the class ImportVisitor method visit.

@Override
public Result visit(TServiceTemplate node, Parameter parameter) {
    Reader reader = Reader.getReader();
    if (!this.namespace.equals(Namespaces.TOSCA_NS)) {
        Set<String> typeDefinitions = new HashSet<>(Arrays.asList(Defaults.TOSCA_NORMATIVE_TYPES, Defaults.TOSCA_NONNORMATIVE_TYPES));
        String tmpNamespace = this.namespace;
        this.namespace = Namespaces.TOSCA_NS;
        Path tmpDir = Utils.getTmpDir(Paths.get("types"));
        for (String typeDefinition : typeDefinitions) {
            try {
                Path outFilePath = tmpDir.resolve(typeDefinition);
                InputStream inputStream = this.getClass().getResourceAsStream(// Do not use File.separator here (https://stackoverflow.com/a/41677152/8235252)
                "/".concat(typeDefinition));
                Files.copy(inputStream, outFilePath, StandardCopyOption.REPLACE_EXISTING);
                TServiceTemplate serviceTemplate = reader.parseSkipTest(outFilePath, Namespaces.TOSCA_NS);
                if (Objects.nonNull(serviceTemplate)) {
                    serviceTemplate.accept(this, new Parameter());
                }
            } catch (MultiException e) {
                setException(e);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.namespace = tmpNamespace;
    }
    super.visit(node, parameter);
    return null;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) Parameter(org.eclipse.winery.yaml.common.validator.support.Parameter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) HashSet(java.util.HashSet) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate)

Example 3 with TServiceTemplate

use of org.eclipse.winery.model.tosca.yaml.TServiceTemplate in project winery by eclipse.

the class YamlWriterTest method roundTripTest.

@DisplayName("Test read and write round trip")
@ParameterizedTest(name = "{index} name=''{0}''")
@MethodSource("getYamlFiles")
public void roundTripTest(Path fileName) throws Exception {
    TServiceTemplate serviceTemplate = this.getYamlServiceTemplate(fileName);
    writeYamlServiceTemplate(serviceTemplate, temporaryFolder.resolve(fileName));
    TServiceTemplate out = this.getYamlServiceTemplate(fileName, temporaryFolder);
    Assertions.assertEquals(serviceTemplate, out);
}
Also used : TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) DisplayName(org.junit.jupiter.api.DisplayName) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with TServiceTemplate

use of org.eclipse.winery.model.tosca.yaml.TServiceTemplate in project winery by eclipse.

the class Converter method convertY2X.

public void convertY2X(InputStream zip) throws MultiException {
    Path path = Utils.unzipFile(zip);
    LOGGER.debug("Unzip path: {}", path);
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{yml,yaml}");
    MultiException exception = Arrays.stream(Optional.ofNullable(path.toFile().listFiles()).orElse(new File[] {})).map(File::toPath).filter(file -> matcher.matches(file.getFileName())).map(file -> {
        Reader reader = Reader.getReader();
        try {
            String id = file.getFileName().toString().substring(0, file.getFileName().toString().lastIndexOf("."));
            Path fileName = file.subpath(path.getNameCount(), file.getNameCount());
            Path fileOutPath = path.resolve("tmp");
            String namespace = reader.getNamespace(path, fileName);
            TServiceTemplate serviceTemplate = reader.parse(path, fileName);
            LOGGER.debug("Convert filePath = {}, fileName = {}, id = {}, namespace = {}, fileOutPath = {}", path, fileName, id, namespace, fileOutPath);
            this.convertY2X(serviceTemplate, id, namespace, path, fileOutPath);
        } catch (MultiException e) {
            return e;
        }
        return null;
    }).filter(Objects::nonNull).reduce(MultiException::add).orElse(new MultiException());
    if (exception.hasException())
        throw exception;
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Utils(org.eclipse.winery.yaml.common.Utils) Y2XConverter(org.eclipse.winery.yaml.converter.yaml.Y2XConverter) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) java.nio.file(java.nio.file) TOSCAMetaFileParser(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFileParser) X2YConverter(org.eclipse.winery.yaml.converter.xml.X2YConverter) Objects(java.util.Objects) IRepository(org.eclipse.winery.repository.backend.IRepository) Map(java.util.Map) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) Optional(java.util.Optional) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) InputStream(java.io.InputStream) Objects(java.util.Objects) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) File(java.io.File) TOSCAMetaFile(org.eclipse.winery.model.csar.toscametafile.TOSCAMetaFile) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate)

Example 5 with TServiceTemplate

use of org.eclipse.winery.model.tosca.yaml.TServiceTemplate in project winery by eclipse.

the class Datatypes method testDataTypes.

@Test
public void testDataTypes() throws Exception {
    String name = "data_types";
    String namespace = "http://www.example.com/DataTypesTest";
    TServiceTemplate serviceTemplate = readServiceTemplate(name, namespace);
    Definitions definitions = convert(serviceTemplate, name, namespace);
    writeXml(definitions, name, namespace);
    Assert.assertNotNull(definitions);
}
Also used : Definitions(org.eclipse.winery.model.tosca.Definitions) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate) Test(org.junit.Test)

Aggregations

TServiceTemplate (org.eclipse.winery.model.tosca.yaml.TServiceTemplate)11 Definitions (org.eclipse.winery.model.tosca.Definitions)5 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)5 Path (java.nio.file.Path)4 Test (org.junit.Test)4 Reader (org.eclipse.winery.yaml.common.reader.yaml.Reader)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Parameter (org.eclipse.winery.yaml.common.validator.support.Parameter)2 DisplayName (org.junit.jupiter.api.DisplayName)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 File (java.io.File)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.nio.file (java.nio.file)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 QName (javax.xml.namespace.QName)1