From f6260a26de44a9338ca998626a93c0d0fa56abc3 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Wed, 22 Jul 2020 08:19:51 +0200 Subject: 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 --- .../java/org/onap/dcae/restapi/EventValidator.java | 52 +++++++++++++++------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'src/main/java/org/onap/dcae/restapi/EventValidator.java') 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> 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> 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); } } -- cgit 1.2.3-korg