use of io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility in project hopsworks by logicalclocks.
the class SubjectsCompatibilityController method setSubjectCompatibility.
public Compatibility setSubjectCompatibility(Project project, String subject, SchemaCompatibility sc) throws SchemaException {
if (sc == null) {
throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INVALID_COMPATIBILITY, Level.WARNING, "Compatibility cannot be null");
}
if (subject == null || subject.equals(Settings.PROJECT_COMPATIBILITY_SUBJECT) || subjectsFacade.findSubjectByName(project, subject).isEmpty()) {
throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SUBJECT_NOT_FOUND, Level.WARNING, "Incorrect subject");
}
subjectsCompatibilityFacade.updateSubjectCompatibility(project, subject, sc);
CompatibilityLevel levelDto = getSubjectCompatibility(project, subject);
return new Compatibility(levelDto.getCompatibilityLevel());
}
use of io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility in project hopsworks by logicalclocks.
the class SubjectsController method isCompatible.
private boolean isCompatible(Project project, String subject, Schema schema) throws SchemaException {
SchemaCompatibility sc = getSubjectOrProjectCompatibility(project, subject);
if (sc.equals(SchemaCompatibility.NONE)) {
return true;
}
SchemaValidator validator = getSchemaValidator(sc);
List<Schema> previousSchemas = subjectsFacade.findSubjectByName(project, subject).stream().sorted(Comparator.comparing(Subjects::getVersion).reversed()).map(s -> new Schema.Parser().parse(s.getSchema().getSchema())).collect(Collectors.toList());
try {
validator.validate(schema, previousSchemas);
} catch (SchemaValidationException e) {
return false;
}
return true;
}
use of io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility in project hopsworks by logicalclocks.
the class SubjectsController method checkIfSchemaCompatible.
public CompatibilityCheck checkIfSchemaCompatible(Project project, String subject, String version, String schemaToTest) throws SchemaException {
validateVersion(version);
Schema schema;
try {
schema = new Schema.Parser().parse(schemaToTest);
} catch (SchemaParseException e) {
throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INVALID_AVRO_SCHEMA, Level.FINE, "schema=" + schemaToTest);
}
if (!subjectsFacade.getListOfSubjects(project).contains(subject)) {
throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SUBJECT_NOT_FOUND, Level.FINE, "subject=" + subject);
}
SchemaCompatibility sc = getSubjectOrProjectCompatibility(project, subject);
Optional<Subjects> optional;
if (version.equals("latest")) {
optional = subjectsFacade.findSubjectLatestVersion(project, subject);
} else {
optional = subjectsFacade.findSubjectByNameAndVersion(project, subject, Integer.valueOf(version));
}
if (!optional.isPresent()) {
throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.VERSION_NOT_FOUND, Level.FINE, "project=" + project.getName() + ", subject=" + subject + ", version=" + version);
}
boolean isCompatible = isCompatible(new Schema.Parser().parse(optional.get().getSchema().getSchema()), schema, sc);
return new CompatibilityCheck(isCompatible);
}
Aggregations