aboutsummaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorMichal Banka <michal.banka@nokia.com>2020-08-04 14:58:25 +0200
committerEdyta Krukowska <edyta.krukowska@nokia.com>2020-08-19 15:07:14 +0200
commita0ba464faeb2e979d20758bc1091143108355974 (patch)
tree944b0f588daace51449b50ae94816f33a6a3c62d /src/main
parentf1ea637a60bace906db5619d71a914ad601e9478 (diff)
Add implementation of stndDefined fields validation1.7.3
Added implementation of stndDefined fields from incoming events. Validation is performed using external-schema-manager tool from DCAE SDK. StndDefined fields schemas are stored in etc/externalRepo directory. Additional stndDefined related properties has been added to collector.properties. VES version has been set to 1.7.3. Issue-ID: DCAEGEN2-2254 Signed-off-by: Edyta Krukowska <edyta.krukowska@nokia.com> Signed-off-by: Michal Banka <michal.banka@nokia.com> Change-Id: Iedaa3622b1d527f6794822c8867b9dfd1860bb8f
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/onap/dcae/ApplicationSettings.java26
-rw-r--r--src/main/java/org/onap/dcae/VesApplication.java7
-rw-r--r--src/main/java/org/onap/dcae/common/ConfigProcessors.java181
-rw-r--r--src/main/java/org/onap/dcae/common/model/VesEvent.java20
-rw-r--r--src/main/java/org/onap/dcae/common/validator/GeneralEventValidator.java75
-rw-r--r--src/main/java/org/onap/dcae/common/validator/SchemaValidator.java (renamed from src/main/java/org/onap/dcae/restapi/SchemaValidator.java)6
-rw-r--r--src/main/java/org/onap/dcae/common/validator/StndDefinedDataValidator.java83
-rw-r--r--src/main/java/org/onap/dcae/common/validator/StndDefinedValidatorResolver.java53
-rw-r--r--src/main/java/org/onap/dcae/restapi/ApiException.java6
-rw-r--r--src/main/java/org/onap/dcae/restapi/EventValidator.java73
-rw-r--r--src/main/java/org/onap/dcae/restapi/EventValidatorException.java2
-rw-r--r--src/main/java/org/onap/dcae/restapi/VesRestController.java33
12 files changed, 399 insertions, 166 deletions
diff --git a/src/main/java/org/onap/dcae/ApplicationSettings.java b/src/main/java/org/onap/dcae/ApplicationSettings.java
index 33ad5bc7..7d5c7db2 100644
--- a/src/main/java/org/onap/dcae/ApplicationSettings.java
+++ b/src/main/java/org/onap/dcae/ApplicationSettings.java
@@ -55,6 +55,8 @@ public class ApplicationSettings {
private static final Logger log = LoggerFactory.getLogger(ApplicationSettings.class);
private static final String FALLBACK_VES_VERSION = "v5";
+ private static final int DISABLED = -1;
+ private static final int ENABLED = 1;
private final String appInvocationDir;
private final String configurationFileLocation;
private final PropertiesConfiguration properties = new PropertiesConfiguration();
@@ -96,7 +98,7 @@ public class ApplicationSettings {
}
public boolean eventSchemaValidationEnabled() {
- return properties.getInt("collector.schema.checkflag", -1) > 0;
+ return properties.getInt("collector.schema.checkflag", DISABLED) > 0;
}
public JsonSchema jsonSchema(String version) {
@@ -126,7 +128,7 @@ public class ApplicationSettings {
}
public boolean eventTransformingEnabled() {
- return properties.getInt("event.transform.flag", 1) > 0;
+ return properties.getInt("event.transform.flag", ENABLED) > 0;
}
public String keystorePasswordFileLocation() {
@@ -170,6 +172,26 @@ public class ApplicationSettings {
}
}
+ public boolean getExternalSchemaValidationCheckflag() {
+ return properties.getInt("collector.externalSchema.checkflag", DISABLED) > 0;
+ }
+
+ public String getExternalSchemaSchemasLocation() {
+ return properties.getString("collector.externalSchema.schemasLocation", "./etc/externalRepo");
+ }
+
+ public String getExternalSchemaMappingFileLocation() {
+ return properties.getString("collector.externalSchema.mappingFileLocation", "./etc/externalRepo/schema-map.json");
+ }
+
+ public String getExternalSchemaSchemaRefPath() {
+ return properties.getString("event.externalSchema.schemaRefPath", "/event/stndDefinedFields/schemaReference");
+ }
+
+ public String getExternalSchemaStndDefinedDataPath() {
+ return properties.getString("event.externalSchema.stndDefinedDataPath", "/event/stndDefinedFields/data");
+ }
+
public List<EventTransformation> getEventTransformations() {
return eventTransformations;
}
diff --git a/src/main/java/org/onap/dcae/VesApplication.java b/src/main/java/org/onap/dcae/VesApplication.java
index 9f628b5c..bb785dbd 100644
--- a/src/main/java/org/onap/dcae/VesApplication.java
+++ b/src/main/java/org/onap/dcae/VesApplication.java
@@ -27,10 +27,12 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.onap.dcae.common.EventSender;
+import org.onap.dcae.common.validator.StndDefinedValidatorResolver;
import org.onap.dcae.common.publishing.DMaaPConfigurationParser;
import org.onap.dcae.common.publishing.EventPublisher;
import org.onap.dcae.common.publishing.PublisherConfig;
import org.onap.dcae.controller.ConfigLoader;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.service.StndDefinedValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -136,4 +138,9 @@ public class VesApplication {
return new EventSender(eventPublisher, applicationSettings.getDmaapStreamIds());
}
+ @Bean
+ public StndDefinedValidator getStndDefinedValidator(StndDefinedValidatorResolver resolver) {
+ return resolver.resolve();
+ }
+
}
diff --git a/src/main/java/org/onap/dcae/common/ConfigProcessors.java b/src/main/java/org/onap/dcae/common/ConfigProcessors.java
index c459bb1c..d1c5e5a0 100644
--- a/src/main/java/org/onap/dcae/common/ConfigProcessors.java
+++ b/src/main/java/org/onap/dcae/common/ConfigProcessors.java
@@ -54,8 +54,9 @@ public class ConfigProcessors {
if (filter == null || isFilterMet(filter)) {
getEventObjectVal(field);
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -65,8 +66,9 @@ public class ConfigProcessors {
final JSONObject filter = jsonObject.optJSONObject(FILTER);
if (filter == null || isFilterMet(filter)) {
setEventObjectVal(field, value);
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -85,8 +87,9 @@ public class ConfigProcessors {
if (filter == null || isFilterMet(filter)) {
setEventObjectVal("suppressEvent", "true");
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -99,8 +102,9 @@ public class ConfigProcessors {
if (filter == null || isFilterMet(filter)) {
setEventObjectVal(field, value, fieldType);
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -111,8 +115,9 @@ public class ConfigProcessors {
final JSONObject filter = jsonObject.optJSONObject(FILTER);
if (filter == null || isFilterMet(filter)) {
setEventObjectVal(field, value);
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -123,8 +128,9 @@ public class ConfigProcessors {
if (filter == null || isFilterMet(filter)) {
removeEventKey(field);
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -152,8 +158,9 @@ public class ConfigProcessors {
removeEventKey(oldfsplit[0]);
setEventObjectVal(fsplit[0], ja);
}
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
private void renameObject(JSONObject jsonObject) // map
@@ -170,29 +177,30 @@ public class ConfigProcessors {
setEventObjectVal(field, oldValue);
removeEventKey(oldField);
}
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
-
+
public void map(JSONObject jsonObject) {
final String field = jsonObject.getString(FIELD);
final String mapType = jsonObject.optString(MAP_TYPE, "");
if (field.contains("[]")) {
- if (field.matches(".*\\[\\]\\..*\\[\\]"))
+ if (field.matches(".*\\[\\]\\..*\\[\\]")) {
renameArrayInArray(jsonObject);
- else
+ } else {
mapToJArray(jsonObject);
+ }
+ } else if ("hashmapToNameValueArray".equals(mapType)) {
+ mapHashmapToNameValueArray(jsonObject);
+ } else if ("nameValueArrayToHashmap".equals(mapType)) {
+ mapNameValueArrayToHashmap(jsonObject);
+ } else if ("renameObject".equals(mapType)) {
+ renameObject(jsonObject);
+ } else {
+ mapAttribute(jsonObject);
}
- else if (mapType.equals("hashmapToNameValueArray"))
- mapHashmapToNameValueArray(jsonObject);
- else if (mapType.equals("nameValueArrayToHashmap"))
- mapNameValueArrayToHashmap(jsonObject);
- else if (mapType.equals("renameObject"))
- renameObject(jsonObject);
-
- else
- mapAttribute(jsonObject);
}
private String performOperation(String operation, String value) {
@@ -216,15 +224,17 @@ public class ConfigProcessors {
value = getEventObjectVal(oldField).toString();
if (!value.equals(OBJECT_NOT_FOUND)) {
- if (operation != null && !operation.isEmpty())
+ if (operation != null && !operation.isEmpty()) {
value = performOperation(operation, value);
+ }
setEventObjectVal(field, value);
removeEventKey(oldField);
}
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -270,16 +280,21 @@ public class ConfigProcessors {
setEventObjectVal(field, ja);
}
} else // if new array
+ {
setEventObjectVal(field + "[0]", new JSONObject(value), "JArray");
+ }
} else // oldfield is jsonArray
+ {
setEventObjectVal(field, new JSONArray(value));
+ }
removeEventKey(oldField);
}
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
-
+
// this method is to support the mapping 5.x to VES7.x format for additionalInformation field
private void mapNameValueArrayToHashmap(JSONObject jsonObject) {
log.info("mapNameValueArrayToHashmap");
@@ -288,17 +303,17 @@ public class ConfigProcessors {
final JSONObject filter = jsonObject.optJSONObject(FILTER);
if (filter == null || isFilterMet(filter)) {
- JSONObject newHashMap = new JSONObject(); // this will hold the newly mapped hashmap elements
+ JSONObject newHashMap = new JSONObject(); // this will hold the newly mapped hashmap elements
JSONArray arrayValue = (JSONArray) getEventObjectVal(oldField); // old Array structure value
- JSONObject tempJObj = null;
- String tempName = "";
- String tempValue = "";
+ JSONObject tempJObj;
+ String tempName;
+ String tempValue;
if (!arrayValue.toString().equals(OBJECT_NOT_FOUND)) {
log.info("old value ==" + arrayValue.toString());
// Loop thru the JSONArray, get the name:value pair and write to new JSONObject as hashmap elements
for (int i = 0; i < arrayValue.length(); i++) {
- tempJObj = arrayValue.getJSONObject(i);
+ tempJObj = arrayValue.getJSONObject(i);
if (tempJObj != null) {
tempName = tempJObj.get("name").toString();
tempValue = tempJObj.get(VALUE).toString();
@@ -310,11 +325,12 @@ public class ConfigProcessors {
//Add the new Hashmap
setEventObjectVal(field, newHashMap);
}
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
-
- // this method is to support the mapping 7.x to VES5.x format for additionalInformation field
+
+ // this method is to support the mapping 7.x to VES5.x format for additionalInformation field
private void mapHashmapToNameValueArray(JSONObject jsonObject) {
log.info("mapHashmapToNameValueArray");
System.out.println("mapHashmapToNameValueArray");
@@ -323,29 +339,30 @@ public class ConfigProcessors {
final JSONObject filter = jsonObject.optJSONObject(FILTER);
if (filter == null || isFilterMet(filter)) {
- JSONArray newArray = new JSONArray(); // this will hold the new name:value JSONObject
- JSONObject nameValJObj;
- System.out.println("object ==" + getEventObjectVal(oldField).toString());
- if (!getEventObjectVal(oldField).toString().equals(OBJECT_NOT_FOUND)) {
-
- JSONObject hashMap = (JSONObject) getEventObjectVal(oldField); // old hashmap structure value
- if (hashMap != null) {
- log.info("old value ==" + hashMap.toString());
- // Loop thru the hashMap JSONObject, get the hashmap elements add them as name:value JsonObject into the newArray
- for (String key : hashMap.keySet()) {
- nameValJObj = new JSONObject(); //create new object so not to overwrite in memory for Array insertion
- nameValJObj.put("name", key);
- nameValJObj.put("value", hashMap.get(key));
- newArray.put(nameValJObj);
- }
- // remove the old hashMap structure
- removeEventKey(oldField);
- //Add the newArray containing the name:value Object
- setEventObjectVal(field, newArray);
- }
- }
- } else
+ JSONArray newArray = new JSONArray(); // this will hold the new name:value JSONObject
+ JSONObject nameValJObj;
+ System.out.println("object ==" + getEventObjectVal(oldField).toString());
+ if (!getEventObjectVal(oldField).toString().equals(OBJECT_NOT_FOUND)) {
+
+ JSONObject hashMap = (JSONObject) getEventObjectVal(oldField); // old hashmap structure value
+ if (hashMap != null) {
+ log.info("old value ==" + hashMap.toString());
+ // Loop thru the hashMap JSONObject, get the hashmap elements add them as name:value JsonObject into the newArray
+ for (String key : hashMap.keySet()) {
+ nameValJObj = new JSONObject(); //create new object so not to overwrite in memory for Array insertion
+ nameValJObj.put("name", key);
+ nameValJObj.put("value", hashMap.get(key));
+ newArray.put(nameValJObj);
+ }
+ // remove the old hashMap structure
+ removeEventKey(oldField);
+ //Add the newArray containing the name:value Object
+ setEventObjectVal(field, newArray);
+ }
+ }
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
/**
@@ -367,16 +384,18 @@ public class ConfigProcessors {
String tempVal = evaluate(values.getString(i));
if (!tempVal.equals(OBJECT_NOT_FOUND)) {
- if (i == 0)
+ if (i == 0) {
value.append(tempVal);
- else
+ } else {
value.append(delimiter).append(tempVal);
+ }
}
}
setEventObjectVal(field, value.toString());
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
public void subtractValue(JSONObject jsonObject) {
@@ -391,16 +410,18 @@ public class ConfigProcessors {
String tempVal = evaluate(values.getString(i));
log.info("tempVal==" + tempVal);
if (!tempVal.equals(OBJECT_NOT_FOUND)) {
- if (i == 0)
- value = value + Float.valueOf(tempVal);
- else
- value = value - Float.valueOf(tempVal);
+ if (i == 0) {
+ value = value + Float.parseFloat(tempVal);
+ } else {
+ value = value - Float.parseFloat(tempVal);
+ }
}
}
log.info("value ==" + value);
setEventObjectVal(field, value, "number");
- } else
+ } else {
log.info(FILTER_NOT_MET);
+ }
}
@@ -470,12 +491,14 @@ public class ConfigProcessors {
if ("not".equals(key)) {
JSONObject njo = jo.getJSONObject(key);
for (String njoKey : njo.keySet()) {
- if (!checkFilter(njo, njoKey, key))
+ if (!checkFilter(njo, njoKey, key)) {
return false;
+ }
}
} else {
- if (!checkFilter(jo, key, key))
+ if (!checkFilter(jo, key, key)) {
return false;
+ }
}
}
return true;
@@ -491,8 +514,9 @@ public class ConfigProcessors {
keySeriesStr = keySeriesStr.replaceAll("\\.\\.", ".");
}
- if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
+ if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1) {
keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
+ }
String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
Object keySeriesObj = event;
for (String aKeySet : keySet) {
@@ -512,8 +536,9 @@ public class ConfigProcessors {
}
}
- if (keySeriesObj == null)
+ if (keySeriesObj == null) {
return OBJECT_NOT_FOUND;
+ }
return keySeriesObj;
}
@@ -532,8 +557,9 @@ public class ConfigProcessors {
}
log.info("fieldType==" + fieldType);
- if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1)
+ if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1) {
keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1);
+ }
String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length());
Object keySeriesObj = event;
for (int i = 0; i < (keySet.length - 1); i++) {
@@ -552,9 +578,11 @@ public class ConfigProcessors {
{
log.info("Object is null, must add it");
if (keySet[i + 1].matches("[0-9]*")) // if index then array
+ {
((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONArray());
- else
+ } else {
((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONObject());
+ }
}
keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i]));
@@ -570,9 +598,11 @@ public class ConfigProcessors {
// it
{
if (keySet[i + 1].matches("[0-9]*")) // if index then array
+ {
((JSONObject) keySeriesObj).put(keySet[i], new JSONArray());
- else
+ } else {
((JSONObject) keySeriesObj).put(keySet[i], new JSONObject());
+ }
log.info("Object is null, must add it");
}
keySeriesObj = ((JSONObject) keySeriesObj).opt(keySet[i]);
@@ -582,17 +612,20 @@ public class ConfigProcessors {
}
if ("number".equals(fieldType)) {
DecimalFormat df = new DecimalFormat("#.0");
- if (value instanceof String)
+ if (value instanceof String) {
((JSONObject) keySeriesObj).put(keySet[keySet.length - 1],
Float.valueOf(df.format(Float.valueOf((String) value))));
- else
+ } else {
((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Float.valueOf(df.format(value)));
- } else if ("integer".equals(fieldType) && value instanceof String)
+ }
+ } else if ("integer".equals(fieldType) && value instanceof String) {
((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Integer.valueOf((String) value));
- else if ("JArray".equals(fieldType))
+ } else if ("JArray".equals(fieldType)) {
+ assert keySeriesObj instanceof JSONArray;
((JSONArray) keySeriesObj).put(value);
- else
+ } else {
((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], value);
+ }
}
}
diff --git a/src/main/java/org/onap/dcae/common/model/VesEvent.java b/src/main/java/org/onap/dcae/common/model/VesEvent.java
index ce709d1c..6c9a8ee2 100644
--- a/src/main/java/org/onap/dcae/common/model/VesEvent.java
+++ b/src/main/java/org/onap/dcae/common/model/VesEvent.java
@@ -19,6 +19,9 @@
*/
package org.onap.dcae.common.model;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
/**
@@ -35,6 +38,8 @@ public class VesEvent {
private static final String DOMAIN = "domain";
private static final String STND_DEFINED_NAMESPACE = "stndDefinedNamespace";
private static final String STND_DEFINED_DOMAIN = "stndDefined";
+ private static final String STND_DEFINED_FIELDS = "stndDefinedFields";
+ private static final String SCHEMA_REFERENCE = "schemaReference";
private final JSONObject event;
@@ -66,6 +71,16 @@ public class VesEvent {
return getEventHeader().getString(DOMAIN);
}
+ public String getSchemaReference() {
+ return getStndDefinedFields().getString(SCHEMA_REFERENCE);
+ }
+
+ private JSONObject getStndDefinedFields() {
+ return event
+ .getJSONObject(EVENT_LITERAL)
+ .getJSONObject(STND_DEFINED_FIELDS);
+ }
+
private String resolveDomainForStndDefinedEvent() {
final JSONObject eventHeader = getEventHeader();
if(eventHeader.has(STND_DEFINED_NAMESPACE)) {
@@ -108,6 +123,11 @@ public class VesEvent {
return new JSONObject(event.toString());
}
+ public JsonNode asJsonNode() throws JsonProcessingException {
+ ObjectMapper objectMapper = new ObjectMapper();
+ return objectMapper.readTree(event.toString());
+ }
+
/**
* Checks if type of event is same as given in paramaters.
*
diff --git a/src/main/java/org/onap/dcae/common/validator/GeneralEventValidator.java b/src/main/java/org/onap/dcae/common/validator/GeneralEventValidator.java
new file mode 100644
index 00000000..5cd5dc25
--- /dev/null
+++ b/src/main/java/org/onap/dcae/common/validator/GeneralEventValidator.java
@@ -0,0 +1,75 @@
+/*
+ * ============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.common.validator;
+
+import com.networknt.schema.JsonSchema;
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcae.common.model.VesEvent;
+import org.onap.dcae.restapi.ApiException;
+import org.onap.dcae.restapi.EventValidatorException;
+
+/**
+ * This class is using ApplicationSetting and SchemaValidator to validate VES event.
+ *
+ * @author Zebek
+ */
+public class GeneralEventValidator {
+
+ private final SchemaValidator schemaValidator;
+ private final ApplicationSettings applicationSettings;
+
+ public GeneralEventValidator(ApplicationSettings applicationSettings) {
+ this(applicationSettings, new SchemaValidator());
+ }
+
+ GeneralEventValidator(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/SchemaValidator.java b/src/main/java/org/onap/dcae/common/validator/SchemaValidator.java
index 94638071..80c8f6e7 100644
--- a/src/main/java/org/onap/dcae/restapi/SchemaValidator.java
+++ b/src/main/java/org/onap/dcae/common/validator/SchemaValidator.java
@@ -18,7 +18,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.onap.dcae.restapi;
+package org.onap.dcae.common.validator;
import com.fasterxml.jackson.databind.JsonNode;
@@ -47,8 +47,8 @@ class SchemaValidator {
return true;
}
- log.warn("Schema validation failed for event: " + payload);
- messageSet.stream().forEach(it->log.warn(it.getMessage()) );
+ log.warn("Schema validation failed for event: {}", payload);
+ messageSet.forEach(it->log.warn(it.getMessage()) );
return false;
} catch (Exception e) {
diff --git a/src/main/java/org/onap/dcae/common/validator/StndDefinedDataValidator.java b/src/main/java/org/onap/dcae/common/validator/StndDefinedDataValidator.java
new file mode 100644
index 00000000..d936f272
--- /dev/null
+++ b/src/main/java/org/onap/dcae/common/validator/StndDefinedDataValidator.java
@@ -0,0 +1,83 @@
+/*
+ * ============LICENSE_START=======================================================
+ * VES
+ * ================================================================================
+ * 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.common.validator;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import org.json.JSONException;
+import org.onap.dcae.common.model.VesEvent;
+import org.onap.dcae.restapi.ApiException;
+import org.onap.dcae.restapi.EventValidatorException;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.service.StndDefinedValidator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class StndDefinedDataValidator {
+
+ private static final String STND_DEFINED_DOMAIN = "stndDefined";
+
+ private final StndDefinedValidator stndDefinedValidator;
+
+ @Autowired
+ public StndDefinedDataValidator(StndDefinedValidator validator) {
+ this.stndDefinedValidator = validator;
+ }
+
+ /**
+ * Validates incoming event
+ *
+ * @param event as JsonNode
+ * @throws EventValidatorException exceptions related to failing StndDefined validation
+ */
+ public void validate(VesEvent event) throws EventValidatorException {
+ try {
+ if (shouldBeValidated(event) && !doValidation(event.asJsonNode())) {
+ throw new EventValidatorException(ApiException.STND_DEFINED_VALIDATION_FAILED);
+ }
+ } catch (JsonProcessingException ex) {
+ throw new EventValidatorException(ApiException.INVALID_JSON_INPUT);
+ }
+ }
+
+ private boolean doValidation(JsonNode event) throws EventValidatorException {
+ try {
+ return stndDefinedValidator.validate(event);
+ } catch (NoLocalReferenceException e) {
+ throw new EventValidatorException(ApiException.NO_LOCAL_SCHEMA_REFERENCE);
+ } catch (IncorrectInternalFileReferenceException e) {
+ throw new EventValidatorException(ApiException.INCORRECT_INTERNAL_FILE_REFERENCE);
+ }
+ }
+
+ private boolean shouldBeValidated(VesEvent event) {
+ boolean shouldBeValidated;
+ try {
+ shouldBeValidated = STND_DEFINED_DOMAIN.equals(event.getDomain())
+ && !event.getSchemaReference().isEmpty();
+ } catch (JSONException e) {
+ shouldBeValidated = false;
+ }
+ return shouldBeValidated;
+ }
+}
diff --git a/src/main/java/org/onap/dcae/common/validator/StndDefinedValidatorResolver.java b/src/main/java/org/onap/dcae/common/validator/StndDefinedValidatorResolver.java
new file mode 100644
index 00000000..3f06d0ae
--- /dev/null
+++ b/src/main/java/org/onap/dcae/common/validator/StndDefinedValidatorResolver.java
@@ -0,0 +1,53 @@
+/*
+ * ============LICENSE_START=======================================================
+ * VES
+ * ================================================================================
+ * 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.common.validator;
+
+import org.onap.dcae.ApplicationSettings;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.service.StndDefinedValidator;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class StndDefinedValidatorResolver {
+
+ private final ApplicationSettings settings;
+
+ @Autowired
+ public StndDefinedValidatorResolver(ApplicationSettings settings) {
+ this.settings = settings;
+ }
+
+ /**
+ * Creates StndDefinedValidator using settings defined in collector.properties
+ *
+ * @return StndDefinedValidator object for validation of configured fields of event
+ */
+ public StndDefinedValidator resolve() {
+ return new StndDefinedValidator.ValidatorBuilder()
+ .mappingFilePath(settings.getExternalSchemaMappingFileLocation())
+ .schemaRefPath(settings.getExternalSchemaSchemaRefPath())
+ .schemasPath(settings.getExternalSchemaSchemasLocation())
+ .stndDefinedDataPath(settings.getExternalSchemaStndDefinedDataPath())
+ .build();
+ }
+}
+
+
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/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