2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2020 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.dcae.common.validator;
23 import com.networknt.schema.JsonSchema;
24 import org.onap.dcae.ApplicationSettings;
25 import org.onap.dcae.common.model.VesEvent;
26 import org.onap.dcae.restapi.ApiException;
27 import org.onap.dcae.restapi.EventValidatorException;
29 * This class is using ApplicationSetting and SchemaValidator to validate VES event.
33 public class GeneralEventValidator {
35 private final SchemaValidator schemaValidator;
36 private final ApplicationSettings applicationSettings;
38 public GeneralEventValidator(ApplicationSettings applicationSettings) {
39 this(applicationSettings, new SchemaValidator());
42 GeneralEventValidator(ApplicationSettings applicationSettings, SchemaValidator schemaValidator) {
43 this.applicationSettings = applicationSettings;
44 this.schemaValidator = schemaValidator;
48 * This method is validating given event using schema adn throws exception if event is not valid
50 * @param vesEvent event that will be validate
51 * @param type expected type of event
52 * @param version json schema version that will be used
53 * @throws EventValidatorException when event is not valid or have wrong type
55 public void validate(VesEvent vesEvent, String type, String version) throws EventValidatorException {
56 if (applicationSettings.eventSchemaValidationEnabled()) {
57 doValidation(vesEvent, type, version);
61 private void doValidation(VesEvent vesEvent, String type, String version) throws EventValidatorException {
62 if (vesEvent.hasType(type)) {
63 if (!isEventMatchToSchema(vesEvent, applicationSettings.jsonSchema(version))) {
64 throw new EventValidatorException(ApiException.SCHEMA_VALIDATION_FAILED);
67 throw new EventValidatorException(ApiException.INVALID_JSON_INPUT);
71 private boolean isEventMatchToSchema(VesEvent vesEvent, JsonSchema schema) {
72 return schemaValidator.conformsToSchema(vesEvent.asJsonObject(), schema);