Harmonize logging and add new logs
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / main / kotlin / org / onap / dcae / collectors / veshv / impl / MessageValidator.kt
index fb94907..9394075 100644 (file)
  */
 package org.onap.dcae.collectors.veshv.impl
 
+import arrow.core.Either
+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
+typealias ValidationResult = Either<ValidationFailMessage, ValidationSuccessMessage>
+
 internal object MessageValidator {
 
-    fun isValid(message: VesMessage): Boolean {
-        return allMandatoryFieldsArePresent(message.header)
-    }
+    fun validateFrameMessage(message: WireFrameMessage): ValidationResult =
+            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()) {
+                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) }
-                    .and(vesEventListenerVersionRegex.matches(header.vesEventListenerVersion))
 
 }