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/ApiException.java | 4 +- .../java/org/onap/dcae/restapi/EventValidator.java | 52 ++++++++---- .../onap/dcae/restapi/EventValidatorException.java | 32 +++++++ .../org/onap/dcae/restapi/VesRestController.java | 97 +++++++++++++--------- 4 files changed, 131 insertions(+), 54 deletions(-) create mode 100644 src/main/java/org/onap/dcae/restapi/EventValidatorException.java (limited to 'src/main/java/org/onap/dcae/restapi') diff --git a/src/main/java/org/onap/dcae/restapi/ApiException.java b/src/main/java/org/onap/dcae/restapi/ApiException.java index 5867e52b..255999a6 100644 --- a/src/main/java/org/onap/dcae/restapi/ApiException.java +++ b/src/main/java/org/onap/dcae/restapi/ApiException.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2018 Nokia. 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. @@ -32,6 +32,8 @@ public enum ApiException { INVALID_CONTENT_TYPE(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Bad Parameter (Incorrect request Content-Type)", 400), UNAUTHORIZED_USER(ExceptionType.POLICY_EXCEPTION, "POL2000", "Unauthorized user", 401), INVALID_CUSTOM_HEADER(ExceptionType.SERVICE_EXCEPTION, "SVC0002", "Bad Parameter (Incorrect request api version)", 400), + MISSING_NAMESPACE_PARAMETER(ExceptionType.SERVICE_EXCEPTION, "SVC2006", "Mandatory input attribute event.commonEventHeader.stndDefinedNamespace is missing from request", 400), + EMPTY_NAMESPACE_PARAMETER(ExceptionType.SERVICE_EXCEPTION, "SVC2006", "Mandatory input attribute event.commonEventHeader.stndDefinedNamespace is empty in request", 400), NO_SERVER_RESOURCES(ExceptionType.SERVICE_EXCEPTION, "SVC1000", "No server resources (internal processing queue full)", 503); public final int httpStatusCode; 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); } } diff --git a/src/main/java/org/onap/dcae/restapi/EventValidatorException.java b/src/main/java/org/onap/dcae/restapi/EventValidatorException.java new file mode 100644 index 00000000..65ad457f --- /dev/null +++ b/src/main/java/org/onap/dcae/restapi/EventValidatorException.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * VES Collector + * ================================================================================ + * 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; + +public class EventValidatorException extends Exception { + private final ApiException apiException; + + public EventValidatorException(ApiException apiException) { + this.apiException = apiException; + } + + public ApiException getApiException() { + return apiException; + } +} diff --git a/src/main/java/org/onap/dcae/restapi/VesRestController.java b/src/main/java/org/onap/dcae/restapi/VesRestController.java index b07b58df..f6dde6d2 100644 --- a/src/main/java/org/onap/dcae/restapi/VesRestController.java +++ b/src/main/java/org/onap/dcae/restapi/VesRestController.java @@ -1,9 +1,9 @@ /* * ============LICENSE_START======================================================= - * PROJECT + * VES Collector * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2018 Nokia. All rights reserved.s + * 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. @@ -21,22 +21,18 @@ package org.onap.dcae.restapi; -import static org.springframework.http.ResponseEntity.accepted; -import static org.springframework.http.ResponseEntity.badRequest; - import com.att.nsa.clock.SaClock; import com.att.nsa.logging.LoggingContext; import com.att.nsa.logging.log4j.EcompFields; -import java.util.Optional; -import java.util.UUID; -import javax.servlet.http.HttpServletRequest; -import org.json.JSONArray; import org.json.JSONObject; import org.onap.dcae.ApplicationSettings; import org.onap.dcae.common.EventSender; -import org.onap.dcae.common.VESLogger; import org.onap.dcae.common.EventUpdater; import org.onap.dcae.common.HeaderUtils; +import org.onap.dcae.common.VESLogger; +import org.onap.dcae.common.model.StndDefinedNamespaceParameterHasEmptyValueException; +import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException; +import org.onap.dcae.common.model.VesEvent; import org.onap.dcaegen2.services.sdk.standardization.header.CustomHeaderUtils; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -48,6 +44,13 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.UUID; + +import static org.springframework.http.ResponseEntity.accepted; +import static org.springframework.http.ResponseEntity.badRequest; + @RestController public class VesRestController { @@ -58,15 +61,19 @@ public class VesRestController { private final Logger requestLogger; private EventSender eventSender; private final HeaderUtils headerUtils; + private final EventValidator eventValidator; + private final EventUpdater eventUpdater; - @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) { this.settings = settings; this.requestLogger = incomingRequestsLogger; this.eventSender = eventSender; this.headerUtils = headerUtils; + this.eventValidator = new EventValidator(settings); + this.eventUpdater = new EventUpdater(settings); } @PostMapping(value = {"/eventListener/{version}"}, consumes = "application/json") @@ -86,38 +93,54 @@ public class VesRestController { return badRequest().contentType(MediaType.APPLICATION_JSON).body(String.format("API version %s is not supported", version)); } - private ResponseEntity process(String events, String version, HttpServletRequest request, String type) { + private ResponseEntity process(String payload, String version, HttpServletRequest request, String type) { CustomHeaderUtils headerUtils = createHeaderUtils(version, request); - if(headerUtils.isOkCustomHeaders()){ - JSONObject jsonObject = new JSONObject(events); - - EventValidator eventValidator = new EventValidator(settings); - Optional> validationResult = eventValidator.validate(jsonObject, type, version); - - if (validationResult.isPresent()){ - return validationResult.get(); - } - JSONArray arrayOfEvents = new EventUpdater(settings).convert(jsonObject,version, generateUUID(version, request.getRequestURI(), jsonObject), type); - eventSender.send(arrayOfEvents); - // TODO call service and return status, replace CambriaClient, split event to single object and list of them - return accepted().headers(this.headerUtils.fillHeaders(headerUtils.getRspCustomHeader())) - .contentType(MediaType.APPLICATION_JSON).body("Accepted"); + if (headerUtils.isOkCustomHeaders()) { + final VesEvent vesEvent = new VesEvent(new JSONObject(payload)); + final String requestURI = request.getRequestURI(); + return handleEvent(vesEvent, version, type, headerUtils, requestURI); } return badRequest().body(String.format(ApiException.INVALID_CUSTOM_HEADER.toString())); } - private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request){ - return new CustomHeaderUtils(version.toLowerCase().replace("v", ""), - headerUtils.extractHeaders(request), - headerUtils.getApiVerFilePath("api_version_config.json"), - headerUtils.getRestApiIdentify(request.getRequestURI())); + private ResponseEntity handleEvent(VesEvent vesEvent, String version, String type, CustomHeaderUtils headerUtils, String requestURI) { + try { + eventValidator.validate(vesEvent, type, version); + List vesEvents = transformEvent(vesEvent, type, version, requestURI); + eventSender.send(vesEvents); + } catch (EventValidatorException e) { + return ResponseEntity.status(e.getApiException().httpStatusCode) + .body(e.getApiException().toJSON().toString()); + } catch (StndDefinedNamespaceParameterNotDefinedException e) { + return ResponseEntity.status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode) + .body(ApiException.MISSING_NAMESPACE_PARAMETER.toJSON().toString()); + } catch (StndDefinedNamespaceParameterHasEmptyValueException e) { + return ResponseEntity.status(ApiException.MISSING_NAMESPACE_PARAMETER.httpStatusCode) + .body(ApiException.EMPTY_NAMESPACE_PARAMETER.toJSON().toString()); + } + + // TODO call service and return status, replace CambriaClient, split event to single object and list of them + return accepted().headers(this.headerUtils.fillHeaders(headerUtils.getRspCustomHeader())) + .contentType(MediaType.APPLICATION_JSON).body("Accepted"); + } + + private CustomHeaderUtils createHeaderUtils(String version, HttpServletRequest request) { + return new CustomHeaderUtils(version.toLowerCase().replace("v", ""), + headerUtils.extractHeaders(request), + headerUtils.getApiVerFilePath("api_version_config.json"), + headerUtils.getRestApiIdentify(request.getRequestURI())); + + } + private List transformEvent(VesEvent vesEvent, String type, String version, String requestURI) { + return this.eventUpdater.convert( + vesEvent, version, generateUUID(vesEvent, version, requestURI), type); } - private UUID generateUUID(String version, String uri, JSONObject jsonObject) { + private UUID generateUUID(VesEvent vesEvent, String version, String uri) { UUID uuid = UUID.randomUUID(); setUpECOMPLoggingForRequest(uuid); - requestLogger.info(String.format(VES_EVENT_MESSAGE, jsonObject, uuid, version, uri)); + requestLogger.info(String.format(VES_EVENT_MESSAGE, vesEvent.asJsonObject(), uuid, version, uri)); return uuid; } @@ -125,4 +148,4 @@ public class VesRestController { LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); } -} \ No newline at end of file +} -- cgit 1.2.3-korg