5cd5dc253e6401e71a83c74f5da6ab133ff621ae
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / validator / GeneralEventValidator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.dcae.common.validator;
22
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;
28
29 /**
30  * This class is using ApplicationSetting and SchemaValidator to validate VES event.
31  *
32  * @author Zebek
33  */
34 public class GeneralEventValidator {
35
36     private final SchemaValidator schemaValidator;
37     private final ApplicationSettings applicationSettings;
38
39     public GeneralEventValidator(ApplicationSettings applicationSettings) {
40         this(applicationSettings, new SchemaValidator());
41     }
42
43     GeneralEventValidator(ApplicationSettings applicationSettings, SchemaValidator schemaValidator) {
44         this.applicationSettings = applicationSettings;
45         this.schemaValidator = schemaValidator;
46     }
47
48     /**
49      * This method is validating given event using schema adn throws exception if event is not valid
50      *
51      * @param vesEvent event that will be validate
52      * @param type     expected type of event
53      * @param version  json schema version that will be used
54      * @throws EventValidatorException when event is not valid or have wrong type
55      */
56     public void validate(VesEvent vesEvent, String type, String version) throws EventValidatorException {
57         if (applicationSettings.eventSchemaValidationEnabled()) {
58             doValidation(vesEvent, type, version);
59         }
60     }
61
62     private void doValidation(VesEvent vesEvent, String type, String version) throws EventValidatorException {
63         if (vesEvent.hasType(type)) {
64             if (!isEventMatchToSchema(vesEvent, applicationSettings.jsonSchema(version))) {
65                 throw new EventValidatorException(ApiException.SCHEMA_VALIDATION_FAILED);
66             }
67         } else {
68             throw new EventValidatorException(ApiException.INVALID_JSON_INPUT);
69         }
70     }
71
72     private boolean isEventMatchToSchema(VesEvent vesEvent, JsonSchema schema) {
73         return schemaValidator.conformsToSchema(vesEvent.asJsonObject(), schema);
74     }
75 }