diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-07-22 08:19:51 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-08-06 08:07:39 +0200 |
commit | f6260a26de44a9338ca998626a93c0d0fa56abc3 (patch) | |
tree | 2a3dffaf850a7a934861c0fd10003168324c9f69 /src/main/java/org/onap/dcae/restapi/EventValidator.java | |
parent | dc29687c2c83b387718c6a26b227170b17dcf08d (diff) |
StndDefined event routing
Route stndDefined events to streams defined in namespace event field.
Change-Id: I3963e220095665f8ca3fd1b21c5c20b44057cf76
Issue-ID: DCAEGEN2-1771
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Diffstat (limited to 'src/main/java/org/onap/dcae/restapi/EventValidator.java')
-rw-r--r-- | src/main/java/org/onap/dcae/restapi/EventValidator.java | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/src/main/java/org/onap/dcae/restapi/EventValidator.java b/src/main/java/org/onap/dcae/restapi/EventValidator.java index 3261c3b3..0eb0967a 100644 --- a/src/main/java/org/onap/dcae/restapi/EventValidator.java +++ b/src/main/java/org/onap/dcae/restapi/EventValidator.java @@ -20,34 +20,54 @@ */ package org.onap.dcae.restapi; -import java.util.Optional; -import org.json.JSONObject; +import com.networknt.schema.JsonSchema; import org.onap.dcae.ApplicationSettings; -import org.springframework.http.ResponseEntity; +import org.onap.dcae.common.model.VesEvent; + +/** + * This class is using ApplicationSetting and SchemaValidator to validate VES event. + * + * @author Zebek + */ public class EventValidator { - private final SchemaValidator schemaValidator = new SchemaValidator(); - private ApplicationSettings applicationSettings; + private final SchemaValidator schemaValidator; + private final ApplicationSettings applicationSettings; public EventValidator(ApplicationSettings applicationSettings) { + this(applicationSettings, new SchemaValidator()); + } + + EventValidator(ApplicationSettings applicationSettings, SchemaValidator schemaValidator) { this.applicationSettings = applicationSettings; + this.schemaValidator = schemaValidator; } - public Optional<ResponseEntity<String>> validate(JSONObject jsonObject, String type, String version){ + /** + * This method is validating given event using schema adn throws exception if event is not valid + * + * @param vesEvent event that will be validate + * @param type expected type of event + * @param version json schema version that will be used + * @throws EventValidatorException when event is not valid or have wrong type + */ + public void validate(VesEvent vesEvent, String type, String version) throws EventValidatorException { if (applicationSettings.eventSchemaValidationEnabled()) { - if (jsonObject.has(type)) { - if (!schemaValidator.conformsToSchema(jsonObject, applicationSettings.jsonSchema(version))) { - return errorResponse(ApiException.SCHEMA_VALIDATION_FAILED); - } - } else { - return errorResponse(ApiException.INVALID_JSON_INPUT); + doValidation(vesEvent, type, version); + } + } + + private void doValidation(VesEvent vesEvent, String type, String version) throws EventValidatorException { + if (vesEvent.hasType(type)) { + if (!isEventMatchToSchema(vesEvent, applicationSettings.jsonSchema(version))) { + throw new EventValidatorException(ApiException.SCHEMA_VALIDATION_FAILED); } + } else { + throw new EventValidatorException(ApiException.INVALID_JSON_INPUT); } - return Optional.empty(); } - private Optional<ResponseEntity<String>> errorResponse(ApiException noServerResources) { - return Optional.of(ResponseEntity.status(noServerResources.httpStatusCode) - .body(noServerResources.toJSON().toString())); + private boolean isEventMatchToSchema(VesEvent vesEvent, JsonSchema schema) { + return schemaValidator.conformsToSchema(vesEvent.asJsonObject(), schema); } } |