Merge "Log details about header validation failure"
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Thu, 13 Dec 2018 13:20:51 +0000 (13:20 +0000)
committerGerrit Code Review <gerrit@onap.org>
Thu, 13 Dec 2018 13:20:51 +0000 (13:20 +0000)
sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/HeaderValidator.kt [new file with mode: 0644]
sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidator.kt
sources/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/ValidationError.kt [new file with mode: 0644]
sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/MessageValidatorTest.kt

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 (file)
index 0000000..9d8acca
--- /dev/null
@@ -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()
+        }
+}
index 9394075..66d2ea0 100644 (file)
 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 (file)
index 0000000..56a77f4
--- /dev/null
@@ -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"
+    )
+}
+
index 60bd767..f784daa 100644 (file)
@@ -25,19 +25,14 @@ import com.nhaarman.mockitokotlin2.doReturn
 import com.nhaarman.mockitokotlin2.mock
 import org.assertj.core.api.Assertions.assertThat
 import org.jetbrains.spek.api.Spek
-import org.jetbrains.spek.api.dsl.describe
-import org.jetbrains.spek.api.dsl.given
-import org.jetbrains.spek.api.dsl.it
-import org.jetbrains.spek.api.dsl.on
-import org.onap.dcae.collectors.veshv.domain.ByteData
-import org.onap.dcae.collectors.veshv.domain.InvalidMajorVersion
-import org.onap.dcae.collectors.veshv.domain.VesEventDomain
-import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
+import org.jetbrains.spek.api.dsl.*
+import org.onap.dcae.collectors.veshv.domain.*
 import org.onap.dcae.collectors.veshv.model.VesMessage
 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
 import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes
 import org.onap.ves.VesEventOuterClass.CommonEventHeader.*
 import kotlin.test.assertTrue
+import kotlin.test.fail
 
 internal object MessageValidatorTest : Spek({
 
@@ -50,7 +45,8 @@ internal object MessageValidatorTest : Spek({
             it("should accept message with fully initialized message header") {
                 val vesMessage = VesMessage(commonHeader, vesEventBytes(commonHeader))
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
+                    assertThat(validateProtobufMessage(vesMessage).isRight())
+                        .describedAs("message validation result").isTrue()
                 }
             }
 
@@ -59,7 +55,8 @@ internal object MessageValidatorTest : Spek({
                     val header = commonHeader(domain)
                     val vesMessage = VesMessage(header, vesEventBytes(header))
                     with(cut) {
-                        assertThat(vesMessage.isValid()).describedAs("message validation result").isTrue()
+                        assertThat(validateProtobufMessage(vesMessage).isRight())
+                            .describedAs("message validation result").isTrue()
                     }
                 }
             }
@@ -68,46 +65,83 @@ internal object MessageValidatorTest : Spek({
         on("ves hv message bytes") {
             val vesMessage = VesMessage(getDefaultInstance(), ByteData.EMPTY)
             it("should not accept message with default header") {
+
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
+                    validateProtobufMessage(vesMessage).fold({
+                        val failMessages = it.invoke()
+
+                        val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
+                                && failMessages.contains("missing domain field")
+                                && failMessages.contains("missing version field")
+                                && failMessages.contains("missing priority field")
+                                && failMessages.contains("missing eventId field")
+                                && failMessages.contains("missing eventName field")
+                                && failMessages.contains("missing lastEpochMicrosec field")
+                                && failMessages.contains("missing startEpochMicrosec field")
+                                && failMessages.contains("missing reportingEntityName field")
+                                && failMessages.contains("missing sourceName field")
+                                && failMessages.contains("missing vesEventListenerVersion field")
+
+                        assertThat(containsAllErrorMessages)
+                            .describedAs("message validation result").isTrue()
+                    }, {
+                        fail()
+                    })
                 }
             }
         }
 
-        val priorityTestCases = mapOf(
+        given("priority test cases") {
+            mapOf(
                 Priority.PRIORITY_NOT_PROVIDED to false,
                 Priority.LOW to true,
                 Priority.MEDIUM to true,
                 Priority.HIGH to true
-        )
+            ).forEach { value, expectedResult ->
+                on("ves hv message including header with priority $value") {
+                    val commonEventHeader = commonHeader(priority = value)
+                    val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))
 
-        priorityTestCases.forEach { value, expectedResult ->
-            on("ves hv message including header with priority $value") {
-                val commonEventHeader = commonHeader(priority = value)
-                val vesMessage = VesMessage(commonEventHeader, vesEventBytes(commonEventHeader))
-
-                it("should resolve validation result") {
-                    with(cut) {
-                        assertThat(vesMessage.isValid()).describedAs("message validation results")
+                    it("should resolve validation result") {
+                        with(cut) {
+                            assertThat(validateProtobufMessage(vesMessage).isRight())
+                                .describedAs("message validation results")
                                 .isEqualTo(expectedResult)
+                        }
                     }
                 }
             }
         }
 
+
         on("ves hv message including header with not initialized fields") {
             val commonHeader = newBuilder()
-                    .setVersion("1.9")
-                    .setEventName("Sample event name")
-                    .setEventId("Sample event Id")
-                    .setSourceName("Sample Source")
-                    .build()
+                .setVersion("1.9")
+                .setEventName("Sample event name")
+                .setEventId("Sample event Id")
+                .setSourceName("Sample Source")
+                .build()
             val rawMessageBytes = vesEventBytes(commonHeader)
 
             it("should not accept not fully initialized message header") {
                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
+                    validateProtobufMessage(vesMessage).fold({
+                        val failMessages = it.invoke()
+
+                        val containsAllErrorMessages = failMessages.contains("vesEventListenerVersion mismatch")
+                                && failMessages.contains("missing domain field")
+                                && failMessages.contains("missing priority field")
+                                && failMessages.contains("missing lastEpochMicrosec field")
+                                && failMessages.contains("missing startEpochMicrosec field")
+                                && failMessages.contains("missing reportingEntityName field")
+                                && failMessages.contains("missing vesEventListenerVersion field")
+
+                        assertThat(containsAllErrorMessages).describedAs("message validation result")
+                            .isTrue()
+                    }, {
+                        fail()
+                    })
                 }
             }
         }
@@ -120,7 +154,15 @@ internal object MessageValidatorTest : Spek({
             it("should not accept message header") {
                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
+                    validateProtobufMessage(vesMessage).fold({
+                        val failMessages = it.invoke()
+
+                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
+                            .describedAs("message validation result")
+                            .isTrue()
+                    }, {
+                        fail()
+                    })
                 }
             }
         }
@@ -133,7 +175,15 @@ internal object MessageValidatorTest : Spek({
                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
 
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
+                    validateProtobufMessage(vesMessage).fold({
+                        val failMessages = it.invoke()
+
+                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
+                            .describedAs("message validation result")
+                            .isTrue()
+                    }, {
+                        fail()
+                    })
                 }
             }
         }
@@ -146,7 +196,15 @@ internal object MessageValidatorTest : Spek({
                 val vesMessage = VesMessage(commonHeader, rawMessageBytes)
 
                 with(cut) {
-                    assertThat(vesMessage.isValid()).describedAs("message validation result").isFalse()
+                    validateProtobufMessage(vesMessage).fold({
+                        val failMessages = it.invoke()
+
+                        assertThat(failMessages.contains("vesEventListenerVersion mismatch"))
+                            .describedAs("message validation result")
+                            .isTrue()
+                    }, {
+                        fail()
+                    })
                 }
             }
         }