aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-core/src/main/kotlin/org/onap/dcae
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-12-13 13:20:51 +0000
committerGerrit Code Review <gerrit@onap.org>2018-12-13 13:20:51 +0000
commitfb4914b7b200772f70e9a7b011d9b17c35df5bfc (patch)
tree3c4a62a685db9a315e716f3c1fb7b6baeec90609 /sources/hv-collector-core/src/main/kotlin/org/onap/dcae
parente44176d17c406b34f6ededb272acfcf3ba234c60 (diff)
parent95803fb41f942eb3b746c0b3d4c742aa63ad81af (diff)
Merge "Log details about header validation failure"
Diffstat (limited to 'sources/hv-collector-core/src/main/kotlin/org/onap/dcae')
-rw-r--r--sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/HeaderValidator.kt60
-rw-r--r--sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt35
-rw-r--r--sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/ValidationError.kt34
3 files changed, 109 insertions, 20 deletions
diff --git a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/HeaderValidator.kt b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/HeaderValidator.kt
new file mode 100644
index 00000000..9d8accae
--- /dev/null
+++ b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/HeaderValidator.kt
@@ -0,0 +1,60 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA
+ * ================================================================================
+ * 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.collectors.veshv.impl
+
+import arrow.core.Either
+import arrow.data.Nel
+import arrow.data.NonEmptyList
+import com.google.protobuf.Descriptors
+import org.onap.dcae.collectors.veshv.domain.headerRequiredFieldDescriptors
+import org.onap.dcae.collectors.veshv.domain.vesEventListenerVersionRegex
+import org.onap.ves.VesEventOuterClass.CommonEventHeader
+
+typealias Validator = (CommonEventHeader) -> List<ValidationError>
+
+object HeaderValidator {
+ private val validators = (listOf(validateEventListenerVersion()) +
+ headerRequiredFieldDescriptors.map { fieldDescriptor ->
+ validateRequiredField(fieldDescriptor)
+ })
+
+
+ fun validate(header: CommonEventHeader): Either<Nel<ValidationError>, CommonEventHeader> {
+ val result: List<ValidationError> = validators.flatMap { it(header) }
+
+ return Either.cond(result.isEmpty(), { header }, { NonEmptyList.fromListUnsafe(result) })
+ }
+
+ private fun validateEventListenerVersion(): Validator = { header: CommonEventHeader ->
+ if (!vesEventListenerVersionRegex.matches(header.vesEventListenerVersion))
+ listOf(ValidationError.VersionMismatch(header.vesEventListenerVersion))
+ else
+ emptyList()
+ }
+
+ private fun validateRequiredField(requiredField: Descriptors.FieldDescriptor): Validator =
+ { header: CommonEventHeader ->
+ if (!header.hasField(requiredField))
+ listOf(ValidationError.MissingField(requiredField.name))
+ else
+ emptyList()
+ }
+}
diff --git a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt
index 93940752..66d2ea0c 100644
--- a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt
+++ b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt
@@ -20,11 +20,9 @@
package org.onap.dcae.collectors.veshv.impl
import arrow.core.Either
+import arrow.data.Nel
import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
-import org.onap.dcae.collectors.veshv.domain.headerRequiredFieldDescriptors
-import org.onap.dcae.collectors.veshv.domain.vesEventListenerVersionRegex
import org.onap.dcae.collectors.veshv.model.VesMessage
-import org.onap.ves.VesEventOuterClass.CommonEventHeader
typealias ValidationFailMessage = () -> String
typealias ValidationSuccessMessage = () -> String
@@ -33,24 +31,21 @@ typealias ValidationResult = Either<ValidationFailMessage, ValidationSuccessMess
internal object MessageValidator {
fun validateFrameMessage(message: WireFrameMessage): ValidationResult =
- message.validate().fold({
- Either.left { "Invalid wire frame header, reason: ${it.message}" }
- }, {
- Either.right { "Wire frame header is valid" }
- })
+ message.validate().fold({
+ Either.left { "Invalid wire frame header, reason: ${it.message}" }
+ }, {
+ Either.right { "Wire frame header is valid" }
+ })
fun validateProtobufMessage(message: VesMessage): ValidationResult =
- if (message.isValid()) {
+ HeaderValidator.validate(message.header).fold(
+ { validationErrors: Nel<ValidationError> ->
+ Either.left {
+ "Protocol buffer message is invalid, reasons:" + validationErrors.all
+ .joinToString(prefix = "\n-") { it.errorMessage }
+ }
+ },
+ {
Either.right { "Protocol buffers message is valid" }
- } else {
- Either.left { "Unsupported protocol buffers message." }
- }
-
- fun VesMessage.isValid() = allMandatoryFieldsArePresent(this.header)
- .and(vesEventListenerVersionRegex.matches(header.vesEventListenerVersion))
-
- private fun allMandatoryFieldsArePresent(header: CommonEventHeader) =
- headerRequiredFieldDescriptors
- .all { fieldDescriptor -> header.hasField(fieldDescriptor) }
-
+ })
}
diff --git a/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/ValidationError.kt b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/ValidationError.kt
new file mode 100644
index 00000000..56a77f4f
--- /dev/null
+++ b/sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/ValidationError.kt
@@ -0,0 +1,34 @@
+/*
+ * ============LICENSE_START=======================================================
+ * dcaegen2-collectors-veshv
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA
+ * ================================================================================
+ * 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.collectors.veshv.impl
+
+import org.onap.dcae.collectors.veshv.domain.vesEventListenerVersionRegex
+
+sealed class ValidationError(val errorMessage: String) {
+ class MissingField<A>(field: A) : ValidationError(
+ "Invalid header - missing $field field"
+ )
+
+ class VersionMismatch(actualVersion: String) : ValidationError(
+ "Invalid header - vesEventListenerVersion mismatch. " +
+ "Expected $vesEventListenerVersionRegex, but was $actualVersion"
+ )
+}
+