diff options
author | Filip Krzywka <filip.krzywka@nokia.com> | 2018-09-06 13:37:27 +0200 |
---|---|---|
committer | Filip Krzywka <filip.krzywka@nokia.com> | 2018-09-11 07:28:16 +0000 |
commit | 6b1777bf08c682fab15f158e9946e36f3cb9698a (patch) | |
tree | b3c7c0893d4f4655120eabb609e04e71cd2d7b5e /hv-collector-ves-message-generator/src/main/kotlin | |
parent | 83c14bf9d6f908abfb5f5ad681570615a9ce541d (diff) |
Fix common event header parsing
During JSON parsing we were using 3 times 'version' field. Replaced
parsing with protobuf parser to avoid mistakes like this in future
Change-Id: I6224dc6533ab553e7e2315a95567a1fa48c1c5ad
Issue-ID: DCAEGEN2-710
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
Diffstat (limited to 'hv-collector-ves-message-generator/src/main/kotlin')
2 files changed, 22 insertions, 22 deletions
diff --git a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt index e5977fb7..768685c1 100644 --- a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/CommonEventHeaderParser.kt @@ -19,11 +19,10 @@ */ package org.onap.dcae.collectors.veshv.ves.message.generator.impl -import com.google.protobuf.ByteString -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Priority -import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.newBuilder +import arrow.core.Option +import com.google.protobuf.util.JsonFormat +import org.onap.dcae.collectors.veshv.domain.headerRequiredFieldDescriptors +import org.onap.ves.VesEventV5.VesEvent.* import javax.json.JsonObject /** @@ -31,22 +30,21 @@ import javax.json.JsonObject * @since July 2018 */ class CommonEventHeaderParser { - fun parse(json: JsonObject): CommonEventHeader = newBuilder() - .setVersion(json.getString("version")) - .setDomain(Domain.valueOf(json.getString("domain"))) - .setSequence(json.getInt("sequence")) - .setPriority(Priority.forNumber(json.getInt("priority"))) - .setEventId(json.getString("version")) - .setEventName(json.getString("version")) - .setEventType(json.getString("version")) - .setStartEpochMicrosec(json.getJsonNumber("startEpochMicrosec").longValue()) - .setLastEpochMicrosec(json.getJsonNumber("lastEpochMicrosec").longValue()) - .setNfNamingCode(json.getString("nfNamingCode")) - .setNfcNamingCode(json.getString("nfcNamingCode")) - .setReportingEntityId(json.getString("reportingEntityId")) - .setReportingEntityName(ByteString.copyFromUtf8(json.getString("reportingEntityName"))) - .setSourceId(ByteString.copyFromUtf8(json.getString("sourceId"))) - .setSourceName(json.getString("sourceName")) - .build() + fun parse(json: JsonObject): Option<CommonEventHeader> = + Option.fromNullable( + CommonEventHeader.newBuilder() + .apply { JsonFormat.parser().merge(json.toString(), this) } + .build() + .takeUnless { !isValid(it) } + ) + + + private fun isValid(header: CommonEventHeader): Boolean { + return allMandatoryFieldsArePresent(header) + } + + private fun allMandatoryFieldsArePresent(header: CommonEventHeader) = + headerRequiredFieldDescriptors + .all { fieldDescriptor -> header.hasField(fieldDescriptor) } } diff --git a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt index f3095618..ea0ee282 100644 --- a/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt +++ b/hv-collector-ves-message-generator/src/main/kotlin/org/onap/dcae/collectors/veshv/ves/message/generator/impl/MessageParametersParserImpl.kt @@ -21,6 +21,7 @@ package org.onap.dcae.collectors.veshv.ves.message.generator.impl import arrow.core.Option import arrow.core.Try +import arrow.core.identity import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParameters import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageParametersParser import org.onap.dcae.collectors.veshv.ves.message.generator.api.MessageType @@ -42,6 +43,7 @@ internal class MessageParametersParserImpl( .map { val commonEventHeader = commonEventHeaderParser .parse(it.getJsonObject("commonEventHeader")) + .fold({ throw IllegalStateException("Invalid common header") }, ::identity) val messageType = MessageType.valueOf(it.getString("messageType")) val messagesAmount = it.getJsonNumber("messagesAmount")?.longValue() ?: throw NullPointerException("\"messagesAmount\" could not be parsed from message.") |