@GET @Path("consistencycheck") @Produces(MediaType.APPLICATION_JSON) public ConsistencyErrorCollector checkConsistency(@QueryParam("serviceTemplatesOnly") boolean serviceTemplatesOnly, @QueryParam("checkDocumentation") boolean checkDocumentation) { IRepository repo = RepositoryFactory.getRepository(); EnumSet<ConsistencyCheckerVerbosity> verbosity = EnumSet.of(ConsistencyCheckerVerbosity.NONE); ConsistencyCheckerConfiguration config = new ConsistencyCheckerConfiguration(serviceTemplatesOnly, checkDocumentation, verbosity, repo); final ConsistencyChecker consistencyChecker = new ConsistencyChecker(config); consistencyChecker.checkCorruption(); return consistencyChecker.getErrorCollector(); }
public ConsistencyChecker(@NonNull ConsistencyCheckerConfiguration configuration, @NonNull ConsistencyCheckerProgressListener consistencyCheckerProgressListener) { this.configuration = Objects.requireNonNull(configuration); this.progressListener = Objects.requireNonNull(consistencyCheckerProgressListener); if (configuration.getRepository() == null) { LOGGER.trace("Running in testing mode"); this.allDefinitionsChildIds = Collections.emptyList(); } else { LOGGER.trace("Running in normal mode"); Set<DefinitionsChildId> allDefinitionsChildIds = configuration.getRepository().getAllDefinitionsChildIds(); if (configuration.isServiceTemplatesOnly()) { allDefinitionsChildIds = allDefinitionsChildIds.stream().filter(id -> id instanceof ServiceTemplateId).collect(Collectors.toSet()); } if (configuration.isTestMode()) { // we need a predictable ordering // in the current implementation, the set is sorted --> just convert it to a list this.allDefinitionsChildIds = allDefinitionsChildIds.stream().collect(Collectors.toList()); } else { // Random sorting of definitions ids to have the progressbar running at the same speed (and not being VERY slow at the end) this.allDefinitionsChildIds = allDefinitionsChildIds.stream().sorted(Comparator.comparingInt(DefinitionsChildId::hashCode)).collect(Collectors.toList()); } } }
private void printError(String error) { if (configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_ERRORS)) { if (!configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_CURRENT_TOSCA_COMPONENT_ID)) { System.out.println(); } System.out.println(error); } }
private void validate(RepositoryFileReference xmlSchemaFileReference, @Nullable Object any, DefinitionsChildId id) { if (!(any instanceof Element)) { printAndAddError(id, "any is not instance of Document, but " + any.getClass()); return; } Element element = (Element) any; SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try (InputStream inputStream = configuration.getRepository().newInputStream(xmlSchemaFileReference)) { Source schemaFile = new StreamSource(inputStream); Schema schema = factory.newSchema(schemaFile); Validator validator = schema.newValidator(); validator.validate(new DOMSource(element)); } catch (Exception e) { printAndAddError(id, "error during validating XML schema " + e.getMessage()); } }
for (DefinitionsChildId id : allDefinitionsChildIds) { float progress = ++elementsChecked / size; if (configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_CURRENT_TOSCA_COMPONENT_ID)) { progressListener.updateProgress(progress, id.toReadableString()); } else { checkServiceTemplate((ServiceTemplateId) id); if (configuration.isCheckDocumentation()) { checkDocumentation(id);
private void checkXmlSchemaValidation(DefinitionsChildId id) { RepositoryFileReference refOfDefinitions = BackendUtils.getRefOfDefinitions(id); if (!configuration.getRepository().exists(refOfDefinitions)) { printAndAddError(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(id, errors); } } catch (IOException e) { LOGGER.debug("I/O error", e); printAndAddError(id, "I/O error during XML validation " + e.getMessage()); } catch (SAXException e) { LOGGER.debug("SAX exception", e); printAndAddError(id, "SAX error during XML validation: " + e.getMessage()); } }
ConsistencyCheckerConfiguration configuration = new ConsistencyCheckerConfiguration(serviceTemplatesOnly, checkDocumentation, verbosity, repository);
/** * Checks whether the given filename exists within the given defintions child id and if the size is above a threshold (currently 100 bytes) */ private void checkFileExistenceAndSize(DefinitionsChildId id, String filename) { RepositoryFileReference repositoryFileReference = new RepositoryFileReference(id, filename); if (!configuration.getRepository().exists(repositoryFileReference)) { printAndAddWarning(id, filename + " does not exist."); return; } long size; try { size = configuration.getRepository().getSize(repositoryFileReference); } catch (IOException e) { LOGGER.debug("Could not determine size for {}", id.toReadableString(), e); printAndAddError(id, "Could not determine size, because " + e.toString()); return; } if (size < 100) { printAndAddWarning(id, filename + " has size of less then 100 bytes."); } }
/** * This method may be called only once during the lifecycle of this object */ public void checkCorruption() { if (configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_NUMBER_OF_TOSCA_COMPONENTS)) { System.out.format("Number of TOSCA definitions to check: %d\n", numberOfDefinitionsToCheck()); } checkAllDefinitions(); // some console output cleanup if (configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_ERRORS) && !configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_CURRENT_TOSCA_COMPONENT_ID)) { System.out.println(); } }
/** * Checks all references QNames whether they are valid */ private void checkReferencedQNames(DefinitionsChildId id) { final QNameValidator qNameValidator = new QNameValidator(error -> printAndAddError(id, error)); try { configuration.getRepository().getDefinitions(id).getElement().accept(qNameValidator); } catch (IllegalStateException e) { LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e); printAndAddError(id, "Reading error " + e.getMessage()); } }
entityTemplate = (TEntityTemplate) configuration.getRepository().getDefinitions((EntityTemplateId) id).getElement(); } catch (IllegalStateException e) { LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e); entityType = configuration.getRepository().getTypeForTemplate(entityTemplate); } catch (IllegalStateException e) { LOGGER.debug("Illegal State Exception during getting type for template {}", entityTemplate.getId(), e); 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) {
private void checkServiceTemplate(ServiceTemplateId id) { TServiceTemplate serviceTemplate; try { serviceTemplate = configuration.getRepository().getElement(id); } catch (IllegalStateException e) { LOGGER.debug("Illegal State Exception during reading of id {}", id.toReadableString(), e); TNodeType nodeType; try { nodeType = configuration.getRepository().getElement(nodeTypeId); } catch (IllegalStateException e) { LOGGER.debug("Illegal State Exception during reading of id {}", nodeTypeId.toReadableString(), e);