use of org.eclipse.winery.model.ids.Namespace in project winery by eclipse.
the class ConsistencyChecker method checkNamespaceUri.
public void checkNamespaceUri(@NonNull DefinitionsChildId id) {
Objects.requireNonNull(id);
String uriStr = id.getNamespace().getDecoded();
if (!uriStr.trim().equals(uriStr)) {
printAndAddError(id, "Namespace starts or ends with white spaces");
}
URI uri;
try {
uri = new URI(uriStr);
} catch (URISyntaxException e) {
LOGGER.debug("Invalid URI", e);
printAndAddError(id, "Invalid URI: " + e.getMessage());
return;
}
if (!uri.isAbsolute()) {
printAndAddError(id, "URI is relative");
}
if ((uriStr.startsWith("http://www.opentosca.org/") && (!uriStr.toLowerCase().equals(uriStr)))) {
// URI is not lowercase
// There are some special URIs, which are OK
String[] splitUri = uriStr.split("/");
String lastElement = splitUri[splitUri.length - 1];
String uriStrWithoutLastElement = uriStr.substring(0, (uriStr.length() - lastElement.length()));
if (!(id.getXmlId().toString().startsWith(lastElement)) || (!uriStrWithoutLastElement.toLowerCase().equals(uriStrWithoutLastElement))) {
printAndAddError(id, "opentosca URI is not lowercase");
}
}
if (uriStr.endsWith("/")) {
printAndAddError(id, "URI ends with a slash");
}
if (uriStr.contains(ARTEFACT_BE)) {
printAndAddError(id, "artifact is spelled with i in American English, not artefact as in British English");
}
// We could just check OpenTOSCA namespace rule examples. However, this would be too strict
// Here, the idea is to check whether a string of another (!) id class appers in the namespace
// If this is the case, the namespace is not consistent
// For instance, a node type residing in the namespace: http://servicetemplates.example.org should not exist.
boolean namespaceUriContainsDifferentType = DefinitionsChildId.ALL_TOSCA_COMPONENT_ID_CLASSES.stream().filter(definitionsChildIdClass -> !definitionsChildIdClass.isAssignableFrom(id.getClass())).flatMap(definitionsChildIdClass -> {
final String lowerCaseIdClass = IdUtil.getTypeForComponentId(definitionsChildIdClass).toLowerCase();
return Stream.of(lowerCaseIdClass + "s", lowerCaseIdClass + "/");
}).anyMatch(uriStr::contains);
if (namespaceUriContainsDifferentType) {
if ((id instanceof ServiceTemplateId) && (id.getNamespace().getDecoded().contains("compliance"))) {
// special case, because TComplianceRule models a service template, but Compliance Rules are treated as Service Template during modeling
// example: class org.eclipse.winery.common.ids.definitions.ServiceTemplateId / {http://www.compliance.opentosca.org/compliancerules}Satisfied_Compliance_Rule_Example_w1
printAndAddWarning(id, "Cannot perform checks for compliance rules...");
} else {
printAndAddError(id, "Namespace URI contains tosca definitions name from other type. E.g., Namespace is ...servicetemplates..., but the type is an artifact template");
}
}
}
use of org.eclipse.winery.model.ids.Namespace in project winery by eclipse.
the class ConsistencyChecker method checkPropertiesValidation.
private void checkPropertiesValidation(DefinitionsChildId id) {
if (!(id instanceof EntityTemplateId)) {
return;
}
TEntityTemplate entityTemplate;
try {
// TEntityTemplate is abstract. IRepository does not offer getElement for abstract ids
// Therefore, we have to use the detour through getDefinitions
entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions(id).getElement();
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
} catch (ClassCastException e) {
LOGGER.error("Something wrong in the consistency between Ids and the TOSCA data model. See http://eclipse.github.io/winery/dev/id-system.html for more information on the ID system.");
printAndAddError(id, "Critical error at analysis: " + e.getMessage());
return;
}
if (Objects.isNull(entityTemplate.getType())) {
// no printing necessary; type consistency is checked at other places
return;
}
TEntityType entityType;
try {
entityType = configuration.getRepository().getTypeForTemplate(entityTemplate);
} catch (IllegalStateException e) {
LOGGER.debug("Illegal State Exception during getting type for template {}", entityTemplate.getId(), e);
printAndAddError(id, "Reading error " + e.getMessage());
return;
}
TEntityTemplate.Properties definedProps = entityTemplate.getProperties();
if (requiresProperties(entityType) && definedProps == null) {
printAndAddError(id, "Properties required, but no properties defined");
return;
} else if (!requiresProperties(entityType) && definedProps != null) {
printAndAddError(id, "No properties required by type, but properties were defined on template");
return;
} else if (definedProps == null) {
// no properties required and none defined
return;
}
if (definedProps instanceof TEntityTemplate.XmlProperties) {
// check defined properties any against the xml schema
@Nullable final Object any = ((TEntityTemplate.XmlProperties) definedProps).getAny();
if (any == null) {
printAndAddError(id, "Properties required, but no XmlProperties were empty (any case)");
return;
}
TEntityType.PropertiesDefinition def = entityType.getProperties();
if (def == null) {
printAndAddError(id, "XmlProperties were given, but no XmlPropertiesDefinition was specified");
return;
}
if (def instanceof TEntityType.XmlElementDefinition) {
final QName element = ((TEntityType.XmlElementDefinition) def).getElement();
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(id, "No Xml Schema definition found for " + element);
return;
}
validate(repositoryFileReference, any, id);
}
} else if (definedProps instanceof TEntityTemplate.WineryKVProperties) {
final WinerysPropertiesDefinition winerysPropertiesDefinition = entityType.getWinerysPropertiesDefinition();
Map<String, String> kvProperties = ((TEntityTemplate.WineryKVProperties) definedProps).getKVProperties();
if (kvProperties.isEmpty()) {
printAndAddError(id, "Properties required, but no properties set (kvproperties case)");
return;
}
for (PropertyDefinitionKV propertyDefinitionKV : winerysPropertiesDefinition.getPropertyDefinitions()) {
String key = propertyDefinitionKV.getKey();
if (kvProperties.get(key) == null) {
printAndAddError(id, "Property " + key + " required, but not set.");
} else {
// removeNamespaceProperties 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(id, "Property " + o + " set, but not defined at schema.");
}
} else if (definedProps instanceof TEntityTemplate.YamlProperties) {
// FIXME todo
LOGGER.debug("YAML Properties checking is not yet implemented!");
}
}
use of org.eclipse.winery.model.ids.Namespace in project winery by eclipse.
the class YamlRepository method getDefinitionsChildIds.
/**
* Creates Set of Definitions Child Id Mapps xml definition to compatible yaml definition
*
* @param inputIdClass requested id class
* @param omitDevelopmentVersions omit development versions
* @return set of definitions child id
*/
@Override
public <T extends DefinitionsChildId> SortedSet<T> getDefinitionsChildIds(Class<T> inputIdClass, boolean omitDevelopmentVersions) {
SortedSet<T> res = new TreeSet<>();
List<Class<T>> idClasses = new ArrayList<>();
idClasses.add(inputIdClass);
idClasses = convertDefinitionsChildIdIfNeeded(idClasses);
for (Class<T> idClass : idClasses) {
String rootPathFragment = IdUtil.getRootPathFragment(idClass);
Path dir = this.getRepositoryRoot().resolve(rootPathFragment);
if (!Files.exists(dir)) {
// return empty list if no ids are available
return res;
}
assert (Files.isDirectory(dir));
final OnlyNonHiddenDirectories onhdf = new OnlyNonHiddenDirectories();
// list all directories contained in this directory
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, onhdf)) {
for (Path nsP : ds) {
// the current path is the namespace
Namespace ns = new Namespace(nsP.getFileName().toString(), true);
try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, onhdf)) {
for (Path idP : idDS) {
List<XmlId> xmlIds = new ArrayList<>();
if (ArtifactTemplateId.class.isAssignableFrom(inputIdClass)) {
List<String> artifactNames = getAllArtifactNamesFromType(idP, idClass, ns.getDecoded());
for (String artifactName : artifactNames) {
xmlIds.add(new XmlId(artifactName + "@" + IdUtil.getFolderName(idClass), true));
}
} else {
xmlIds.add(new XmlId(idP.getFileName().toString(), true));
}
for (XmlId xmlId : xmlIds) {
if (omitDevelopmentVersions) {
WineryVersion version = VersionUtils.getVersion(xmlId.getDecoded());
if (version.toString().length() > 0 && version.getWorkInProgressVersion() > 0) {
continue;
}
}
Constructor<T> constructor;
try {
constructor = inputIdClass.getConstructor(Namespace.class, XmlId.class);
} catch (Exception e) {
LOGGER.debug("Internal error at determining id constructor", e);
// abort everything, return invalid result
return res;
}
T id;
try {
id = constructor.newInstance(ns, xmlId);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.debug("Internal error at invocation of id constructor", e);
// abort everything, return invalid result
return res;
}
res.add(id);
}
}
}
}
} catch (IOException e) {
LOGGER.debug("Cannot close ds", e);
}
}
return res;
}
use of org.eclipse.winery.model.ids.Namespace in project winery by eclipse.
the class ImportUtilsTest method getLocationForImportTest.
@Test
public void getLocationForImportTest() throws Exception {
this.setRevisionTo("5fdcffa9ccd17743d5498cab0914081fc33606e9");
XSDImportId id = new XSDImportId(new Namespace("http://opentosca.org/nodetypes", false), new XmlId("CloudProviderProperties", false));
Optional<String> importLocation = ImportUtils.getLocation(repository, id);
assertEquals(true, importLocation.isPresent());
}
use of org.eclipse.winery.model.ids.Namespace in project winery by eclipse.
the class MultiRepository method addNamespacesToRepository.
private void addNamespacesToRepository(IRepository repository, GenericId id) {
if (id instanceof DefinitionsChildId) {
Namespace namespace = ((DefinitionsChildId) id).getNamespace();
String ns = namespace.getDecoded();
IRepository r = determineRepositoryRef(repository);
Set<String> set = repositoryGlobal.get(r);
set.add(ns);
repositoryGlobal.put(r, set);
String pns;
try {
if (this.localRepository.getRepository() instanceof XmlRepository) {
pns = namespace.getEncoded().substring(0, namespace.getEncoded().lastIndexOf(RepositoryUtils.getUrlSeparatorEncoded()));
} else {
pns = namespace.getEncoded();
}
} catch (UnsupportedEncodingException ex) {
LOGGER.error("Error when generating the namespace", ex);
return;
}
Set<Namespace> setPre = repositoryCommonNamespace.get(r);
setPre.add(new Namespace(pns, true));
repositoryCommonNamespace.put(r, setPre);
}
}
Aggregations