Replace cambria with DmaaP client
[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  * This class is using ApplicationSetting and SchemaValidator to validate VES event.
30  *
31  * @author Zebek
32  */
33 public class GeneralEventValidator {
34
35     private final SchemaValidator schemaValidator;
36     private final ApplicationSettings applicationSettings;
37
38     public GeneralEventValidator(ApplicationSettings applicationSettings) {
39         this(applicationSettings, new SchemaValidator());
40     }
41
42     GeneralEventValidator(ApplicationSettings applicationSettings, SchemaValidator schemaValidator) {
43         this.applicationSettings = applicationSettings;
44         this.schemaValidator = schemaValidator;
45     }
46
47     /**
48      * This method is validating given event using schema adn throws exception if event is not valid
49      *
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
54      */
55     public void validate(VesEvent vesEvent, String type, String version) throws EventValidatorException {
56         if (applicationSettings.eventSchemaValidationEnabled()) {
57             doValidation(vesEvent, type, version);
58         }
59     }
60
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);
65             }
66         } else {
67             throw new EventValidatorException(ApiException.INVALID_JSON_INPUT);
68         }
69     }
70
71     private boolean isEventMatchToSchema(VesEvent vesEvent, JsonSchema schema) {
72         return schemaValidator.conformsToSchema(vesEvent.asJsonObject(), schema);
73     }
74 }