diff options
Diffstat (limited to 'src/main/java/org/onap/dcae/restapi')
5 files changed, 27 insertions, 145 deletions
diff --git a/src/main/java/org/onap/dcae/restapi/ApiException.java b/src/main/java/org/onap/dcae/restapi/ApiException.java index 9ea02076..dbd41a4d 100644 --- a/src/main/java/org/onap/dcae/restapi/ApiException.java +++ b/src/main/java/org/onap/dcae/restapi/ApiException.java @@ -37,7 +37,11 @@ public enum ApiException { INVALID_CUSTOM_HEADER(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Bad Parameter (Incorrect request api version)", 400), MISSING_NAMESPACE_PARAMETER(ExceptionType.SERVICE_EXCEPTION, "SVC2006", "Mandatory input %1 %2 is missing from request", List.of("attribute", "event.commonEventHeader.stndDefinedNamespace"), 400), EMPTY_NAMESPACE_PARAMETER(ExceptionType.SERVICE_EXCEPTION, "SVC2006", "Mandatory input %1 %2 is empty in request", List.of("attribute", "event.commonEventHeader.stndDefinedNamespace"), 400), - NO_SERVER_RESOURCES(ExceptionType.SERVICE_EXCEPTION, "SVC1000", "No server resources (internal processing queue full)", 503); + NO_SERVER_RESOURCES(ExceptionType.SERVICE_EXCEPTION, "SVC1000", "No server resources (internal processing queue full)", 503), + STND_DEFINED_VALIDATION_FAILED(ExceptionType.SERVICE_EXCEPTION, "SVC2000", "The following service error occurred: %1. Error code is %2", List.of("event.stndDefinedFields.data invalid against event.stndDefinedFields.schemaReference", "400"), 400), + NO_LOCAL_SCHEMA_REFERENCE(ExceptionType.SERVICE_EXCEPTION, "SVC2004", "Invalid input value for %1 %2: %3", List.of("attribute", "event.stndDefinedFields.schemaReference", "Referred external schema not present in schema repository"), 400), + INCORRECT_INTERNAL_FILE_REFERENCE(ExceptionType.SERVICE_EXCEPTION, "SVC2000", "The following service error occurred: %1. Error code is %2", List.of("event.stndDefinedFields.schemaReference value does not correspond to any external event schema file in externalSchema repo", "400"), 400); + public final int httpStatusCode; private final ExceptionType type; diff --git a/src/main/java/org/onap/dcae/restapi/EventValidator.java b/src/main/java/org/onap/dcae/restapi/EventValidator.java deleted file mode 100644 index 0eb0967a..00000000 --- a/src/main/java/org/onap/dcae/restapi/EventValidator.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.restapi; - -import com.networknt.schema.JsonSchema; -import org.onap.dcae.ApplicationSettings; -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; - private final ApplicationSettings applicationSettings; - - public EventValidator(ApplicationSettings applicationSettings) { - this(applicationSettings, new SchemaValidator()); - } - - EventValidator(ApplicationSettings applicationSettings, SchemaValidator schemaValidator) { - this.applicationSettings = applicationSettings; - this.schemaValidator = schemaValidator; - } - - /** - * 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()) { - 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); - } - } - - private boolean isEventMatchToSchema(VesEvent vesEvent, JsonSchema schema) { - return schemaValidator.conformsToSchema(vesEvent.asJsonObject(), schema); - } -} diff --git a/src/main/java/org/onap/dcae/restapi/EventValidatorException.java b/src/main/java/org/onap/dcae/restapi/EventValidatorException.java index 65ad457f..380694d1 100644 --- a/src/main/java/org/onap/dcae/restapi/EventValidatorException.java +++ b/src/main/java/org/onap/dcae/restapi/EventValidatorException.java @@ -19,7 +19,7 @@ */ package org.onap.dcae.restapi; -public class EventValidatorException extends Exception { +public class EventValidatorException extends RuntimeException { private final ApiException apiException; public EventValidatorException(ApiException apiException) { diff --git a/src/main/java/org/onap/dcae/restapi/SchemaValidator.java b/src/main/java/org/onap/dcae/restapi/SchemaValidator.java deleted file mode 100644 index 94638071..00000000 --- a/src/main/java/org/onap/dcae/restapi/SchemaValidator.java +++ /dev/null @@ -1,58 +0,0 @@ -/*
- * ============LICENSE_START=======================================================
- * PROJECT
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright (C) 2019 Nokia. All rights reserved.s
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcae.restapi;
-
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.networknt.schema.JsonSchema;
-import com.networknt.schema.ValidationMessage;
-import org.json.JSONObject;
-import org.onap.dcae.ApplicationException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Set;
-
-class SchemaValidator {
- public static final Logger log = LoggerFactory.getLogger(SchemaValidator.class);
-
- public boolean conformsToSchema(JSONObject payload, JsonSchema schema) {
- try {
- ObjectMapper mapper = new ObjectMapper();
-
- String content = payload.toString();
- JsonNode node = mapper.readTree(content);
- Set<ValidationMessage> messageSet = schema.validate(node);
-
- if (messageSet.isEmpty()) {
- return true;
- }
-
- log.warn("Schema validation failed for event: " + payload);
- messageSet.stream().forEach(it->log.warn(it.getMessage()) );
-
- return false;
- } catch (Exception e) {
- throw new ApplicationException("Unable to validate against schema", e);
- }
- }
-}
diff --git a/src/main/java/org/onap/dcae/restapi/VesRestController.java b/src/main/java/org/onap/dcae/restapi/VesRestController.java index f6dde6d2..de0392e6 100644 --- a/src/main/java/org/onap/dcae/restapi/VesRestController.java +++ b/src/main/java/org/onap/dcae/restapi/VesRestController.java @@ -29,6 +29,8 @@ import org.onap.dcae.ApplicationSettings; import org.onap.dcae.common.EventSender; import org.onap.dcae.common.EventUpdater; import org.onap.dcae.common.HeaderUtils; +import org.onap.dcae.common.validator.GeneralEventValidator; +import org.onap.dcae.common.validator.StndDefinedDataValidator; import org.onap.dcae.common.VESLogger; import org.onap.dcae.common.model.StndDefinedNamespaceParameterHasEmptyValueException; import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException; @@ -61,18 +63,20 @@ public class VesRestController { private final Logger requestLogger; private EventSender eventSender; private final HeaderUtils headerUtils; - private final EventValidator eventValidator; + private final GeneralEventValidator generalEventValidator; private final EventUpdater eventUpdater; + private final StndDefinedDataValidator stndDefinedValidator; - @Autowired - VesRestController(ApplicationSettings settings, - @Qualifier("incomingRequestsLogger") Logger incomingRequestsLogger, - @Qualifier("eventSender") EventSender eventSender, HeaderUtils headerUtils) { + @Autowired + VesRestController(ApplicationSettings settings, @Qualifier("incomingRequestsLogger") Logger incomingRequestsLogger, + @Qualifier("eventSender") EventSender eventSender, HeaderUtils headerUtils, + StndDefinedDataValidator stndDefinedDataValidator) { this.settings = settings; this.requestLogger = incomingRequestsLogger; this.eventSender = eventSender; this.headerUtils = headerUtils; - this.eventValidator = new EventValidator(settings); + this.stndDefinedValidator = stndDefinedDataValidator; + this.generalEventValidator = new GeneralEventValidator(settings); this.eventUpdater = new EventUpdater(settings); } @@ -84,7 +88,6 @@ public class VesRestController { return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version)); } - @PostMapping(value = {"/eventListener/{version}/eventBatch"}, consumes = "application/json") ResponseEntity<String> events(@RequestBody String events, @PathVariable String version, HttpServletRequest request) { if (settings.isVersionSupported(version)) { @@ -100,13 +103,14 @@ public class VesRestController { final String requestURI = request.getRequestURI(); return handleEvent(vesEvent, version, type, headerUtils, requestURI); } - return badRequest().body(String.format(ApiException.INVALID_CUSTOM_HEADER.toString())); + return badRequest().body(ApiException.INVALID_CUSTOM_HEADER.toString()); } private ResponseEntity<String> handleEvent(VesEvent vesEvent, String version, String type, CustomHeaderUtils headerUtils, String requestURI) { try { - eventValidator.validate(vesEvent, type, version); + generalEventValidator.validate(vesEvent, type, version); List<VesEvent> vesEvents = transformEvent(vesEvent, type, version, requestURI); + executeStndDefinedValidation(vesEvents); eventSender.send(vesEvents); } catch (EventValidatorException e) { return ResponseEntity.status(e.getApiException().httpStatusCode) @@ -124,6 +128,12 @@ public class VesRestController { .contentType(MediaType.APPLICATION_JSON).body("Accepted"); } + private void executeStndDefinedValidation(List<VesEvent> vesEvents) { + if (settings.getExternalSchemaValidationCheckflag()) { + vesEvents.forEach(stndDefinedValidator::validate); + } + } + private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request) { return new CustomHeaderUtils(version.toLowerCase().replace("v", ""), headerUtils.extractHeaders(request), @@ -133,8 +143,7 @@ public class VesRestController { } private List<VesEvent> transformEvent(VesEvent vesEvent, String type, String version, String requestURI) { - return this.eventUpdater.convert( - vesEvent, version, generateUUID(vesEvent, version, requestURI), type); + return this.eventUpdater.convert(vesEvent, version, generateUUID(vesEvent, version, requestURI), type); } private UUID generateUUID(VesEvent vesEvent, String version, String uri) { @@ -148,4 +157,4 @@ public class VesRestController { LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); } -} +}
\ No newline at end of file |