use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class WriterUtils method storeDefinitions.
public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
Path path = null;
try {
path = Files.createTempDirectory("winery");
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("Store definition: {}", definitions.getId());
saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
CsarImporter csarImporter = new CsarImporter();
List<Exception> exceptions = new ArrayList<>();
cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
csarImporter.setNamespace(entry, namespace);
String id = ModelUtilities.getId(entry);
Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
if (RepositoryFactory.getRepository().exists(wid)) {
if (overwrite) {
try {
RepositoryFactory.getRepository().forceDelete(wid);
} catch (IOException e) {
exceptions.add(e);
}
} else {
return;
}
}
if (entry instanceof TArtifactTemplate) {
TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
String reference = ref.getReference();
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
LOGGER.error("Invalid URI {}", reference);
return;
}
if (refURI.isAbsolute()) {
return;
}
Path artifactPath = dir.resolve(reference);
if (!Files.exists(artifactPath)) {
LOGGER.error("File not found {}", artifactPath);
return;
}
ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
MediaType mediaType = null;
try (InputStream is = Files.newInputStream(artifactPath);
BufferedInputStream bis = new BufferedInputStream(is)) {
mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
} catch (IOException e) {
LOGGER.error("Could not read artifact template file: {}", artifactPath);
return;
}
});
}
final Definitions part = BackendUtils.createWrapperDefinitions(wid);
part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
String content = BackendUtils.getXMLAsString(part, true);
try {
RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (Exception e) {
exceptions.add(e);
}
});
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method getGitInformation.
/**
* @param directoryId DirectoryId of the ArtifactTemplate that should contain a reference to a git repository.
* @return The URL and the branch/tag that contains the files for the ArtifactTemplate. null if no git information
* is given.
*/
public static GitInfo getGitInformation(DirectoryId directoryId) {
if (!(directoryId.getParent() instanceof ArtifactTemplateId)) {
return null;
}
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions((ArtifactTemplateId) directoryId.getParent());
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
Unmarshaller u = JAXBSupport.createUnmarshaller();
Definitions defs = ((Definitions) u.unmarshal(is));
Map<QName, String> atts = defs.getOtherAttributes();
String src = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitsrc"));
String branch = atts.get(new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "gitbranch"));
// ^ is the XOR operator
if (src == null ^ branch == null) {
LOGGER.error("Git information not complete, URL or branch missing");
return null;
} else if (src == null && branch == null) {
return null;
}
return new GitInfo(src, branch);
} catch (IOException e) {
LOGGER.error("Error reading definitions of " + directoryId.getParent() + " at " + ref.getFileName(), e);
} catch (JAXBException e) {
LOGGER.error("Error in XML in " + ref.getFileName(), e);
}
return null;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class BackendUtils method persist.
/**
* Persists the given definitions
*
* @param id the id of the definition child to persist
* @param definitions the definitions to persist
*/
public static void persist(DefinitionsChildId id, Definitions definitions) throws IOException {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
BackendUtils.persist(definitions, ref, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class ConsistencyChecker method checkXmlSchemaValidation.
public static void checkXmlSchemaValidation(ConsistencyErrorLogger errorLogger, ConsistencyCheckerConfiguration configuration, DefinitionsChildId id) {
RepositoryFileReference refOfDefinitions = BackendUtils.getRefOfDefinitions(id);
if (!configuration.getRepository().exists(refOfDefinitions)) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Id exists, but corresponding XML file does not.");
return;
}
try (InputStream inputStream = configuration.getRepository().newInputStream(refOfDefinitions)) {
DocumentBuilder documentBuilder = ToscaDocumentBuilderFactory.INSTANCE.getSchemaAwareToscaDocumentBuilder();
StringBuilder errorStringBuilder = new StringBuilder();
documentBuilder.setErrorHandler(BackendUtils.getErrorHandler(errorStringBuilder));
documentBuilder.parse(inputStream);
String errors = errorStringBuilder.toString();
if (!errors.isEmpty()) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, errors);
}
} catch (IOException e) {
LOGGER.debug("I/O error", e);
printAndAddError(errorLogger, configuration.getVerbosity(), id, "I/O error during XML validation " + e.getMessage());
} catch (SAXException e) {
LOGGER.debug("SAX exception", e);
printAndAddError(errorLogger, configuration.getVerbosity(), id, "SAX error during XML validation: " + e.getMessage());
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class ConsistencyChecker method checkPropertiesValidation.
public static void checkPropertiesValidation(ConsistencyErrorLogger errorLogger, ConsistencyCheckerConfiguration configuration, DefinitionsChildId id) {
if (id instanceof EntityTemplateId) {
TEntityTemplate entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
if (Objects.isNull(entityTemplate.getType())) {
// no printing necessary; type consistency is checked at other places
return;
}
final TEntityType entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
final TEntityType.PropertiesDefinition propertiesDefinition = entityType.getPropertiesDefinition();
if ((winerysPropertiesDefinition != null) || (propertiesDefinition != null)) {
final TEntityTemplate.Properties properties = entityTemplate.getProperties();
if (properties == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined");
return;
}
if (winerysPropertiesDefinition != null) {
Map<String, String> kvProperties = entityTemplate.getProperties().getKVProperties();
if (kvProperties.isEmpty()) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties set (any case)");
return;
}
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitionKVList().getPropertyDefinitionKVs()) {
String key = propertyDefinitionKV.getKey();
if (kvProperties.get(key) == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + key + " required, but not set.");
} else {
// remove the key from the map to enable checking below whether a property is defined which not requried by the property definition
kvProperties.remove(key);
}
}
// If any key is left, this is a key not defined at the schema
for (Object o : kvProperties.keySet()) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Property " + o + " set, but not defined at schema.");
}
} else if (propertiesDefinition != null) {
@Nullable final Object any = properties.getAny();
if (any == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "Properties required, but no properties defined (any case)");
return;
}
@Nullable final QName element = propertiesDefinition.getElement();
if (element != null) {
final Map<String, RepositoryFileReference> mapFromLocalNameToXSD = configuration.getRepository().getXsdImportManager().getMapFromLocalNameToXSD(new Namespace(element.getNamespaceURI(), false), false);
final RepositoryFileReference repositoryFileReference = mapFromLocalNameToXSD.get(element.getLocalPart());
if (repositoryFileReference == null) {
printAndAddError(errorLogger, configuration.getVerbosity(), id, "No Xml Schema definition found for " + element);
return;
}
validate(errorLogger, repositoryFileReference, any, configuration, id);
}
}
}
}
}
Aggregations