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());
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations