Skip to content

Commit

Permalink
feat: new incoming rsm-012 json message parser (#1377)
Browse files Browse the repository at this point in the history
feat: new incoming rsm-012 json message parser
  • Loading branch information
MadsDue authored Nov 20, 2024
1 parent 0643d76 commit e02ba0f
Show file tree
Hide file tree
Showing 24 changed files with 1,329 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,11 @@ public static IServiceCollection AddIncomingMessagesModule(this IServiceCollecti
.AddSingleton<JsonSchemaProvider>();

services
.AddTransient<Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointEbixMessageParser>();
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointJsonMessageParser>();
services
.AddTransient<Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointXmlMessageParser>();

services.AddTransient<IDictionary<(IncomingDocumentType, DocumentFormat), IMessageParser>>(provider => new Dictionary<(IncomingDocumentType, DocumentFormat), IMessageParser>
{
{ (IncomingDocumentType.MeteredDataForMeasurementPoint, DocumentFormat.Ebix), provider.GetRequiredService<Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointEbixMessageParser>() },
{ (IncomingDocumentType.MeteredDataForMeasurementPoint, DocumentFormat.Xml), provider.GetRequiredService<Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointXmlMessageParser>() },
});
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointEbixMessageParser>();
services
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointXmlMessageParser>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ReceiveIncomingMarketMessage

public ReceiveIncomingMarketMessage(
MarketMessageParser marketMessageParser,
IDictionary<(IncomingDocumentType DocumentType, DocumentFormat DocumentFormat), IMessageParser> messageParsers,
IEnumerable<IMessageParser> messageParsers,
IFeatureFlagManager featureFlagManager,
ValidateIncomingMessage validateIncomingMessage,
ResponseFactory responseFactory,
Expand All @@ -57,7 +57,10 @@ public ReceiveIncomingMarketMessage(
AuthenticatedActor actorAuthenticator)
{
_marketMessageParser = marketMessageParser;
_messageParsers = messageParsers;
_messageParsers = messageParsers
.ToDictionary(
parser => (parser.DocumentType, parser.DocumentFormat),
parser => parser);
_featureFlagManager = featureFlagManager;
_validateIncomingMessage = validateIncomingMessage;
_responseFactory = responseFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public record MeteredDataForMeasurementPointSeries(
string StartDateTime,
string? EndDateTime,
string? ProductNumber,
// This field is not used in Ebix
string? RegisteredAt,
string? ProductUnitType,
string? MeteringPointType,
string? MeteringPointLocationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@
<EmbeddedResource Include="Schemas\Ebix\Schemas\document\DK_MeteredDataTimeSeries\ebIX_DK_MeteredDataTimeSeries-2.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="Schemas\Cim\Json\Schemas\Notify-Validated-measure-data-assembly-model.schema.json" />
<EmbeddedResource Include="Schemas\Cim\Json\Schemas\Notify-Validated-measure-data-assembly-model.schema.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="Schemas\Cim\Xml\Schemas\urn-ediel-org-measure-notifyvalidatedmeasuredata-0-1.xsd" />
<EmbeddedResource Include="Schemas\Cim\Xml\Schemas\urn-ediel-org-measure-notifyvalidatedmeasuredata-0-1.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public abstract class EbixMessageParserBase(EbixSchemaProvider schemaProvider) :
{
private const string HeaderElementName = "HeaderEnergyDocument";
private const string EnergyContextElementName = "ProcessEnergyContext";
private const string Identification = "Identification";
private const string DocumentType = "DocumentType";
private const string Creation = "Creation";
private const string SenderEnergyParty = "SenderEnergyParty";
private const string RecipientEnergyParty = "RecipientEnergyParty";
private const string EnergyBusinessProcess = "EnergyBusinessProcess";
private const string EnergyBusinessProcessRole = "EnergyBusinessProcessRole";
private const string EnergyIndustryClassification = "EnergyIndustryClassification";
private const string IdentificationElementName = "Identification";
private const string DocumentTypeElementName = "DocumentType";
private const string CreationElementName = "Creation";
private const string SenderEnergyPartyElementName = "SenderEnergyParty";
private const string RecipientEnergyPartyElementName = "RecipientEnergyParty";
private const string EnergyBusinessProcessElementName = "EnergyBusinessProcess";
private const string EnergyBusinessProcessRoleElementName = "EnergyBusinessProcessRole";
private const string EnergyIndustryClassificationElementName = "EnergyIndustryClassification";
private readonly EbixSchemaProvider _schemaProvider = schemaProvider;

protected abstract string RootPayloadElementName { get; }
Expand Down Expand Up @@ -160,18 +160,18 @@ private MessageHeader ParseHeader(XDocument document, XNamespace ns)
var headerElement = document.Descendants(ns + HeaderElementName).SingleOrDefault();
if (headerElement == null) throw new InvalidOperationException("Header element not found");

var messageId = headerElement.Element(ns + Identification)?.Value ?? string.Empty;
var messageType = headerElement.Element(ns + DocumentType)?.Value ?? string.Empty;
var createdAt = headerElement.Element(ns + Creation)?.Value ?? string.Empty;
var senderId = headerElement.Element(ns + SenderEnergyParty)?.Element(ns + Identification)?.Value ?? string.Empty;
var receiverId = headerElement.Element(ns + RecipientEnergyParty)?.Element(ns + Identification)?.Value ?? string.Empty;
var messageId = headerElement.Element(ns + IdentificationElementName)?.Value ?? string.Empty;
var messageType = headerElement.Element(ns + DocumentTypeElementName)?.Value ?? string.Empty;
var createdAt = headerElement.Element(ns + CreationElementName)?.Value ?? string.Empty;
var senderId = headerElement.Element(ns + SenderEnergyPartyElementName)?.Element(ns + IdentificationElementName)?.Value ?? string.Empty;
var receiverId = headerElement.Element(ns + RecipientEnergyPartyElementName)?.Element(ns + IdentificationElementName)?.Value ?? string.Empty;

var energyContextElement = document.Descendants(ns + EnergyContextElementName).FirstOrDefault();
if (energyContextElement == null) throw new InvalidOperationException("Energy Context element not found");

var businessReason = energyContextElement.Element(ns + EnergyBusinessProcess)?.Value ?? string.Empty;
var senderRole = energyContextElement.Element(ns + EnergyBusinessProcessRole)?.Value ?? string.Empty;
var businessType = energyContextElement.Element(ns + EnergyIndustryClassification)?.Value;
var businessReason = energyContextElement.Element(ns + EnergyBusinessProcessElementName)?.Value ?? string.Empty;
var senderRole = energyContextElement.Element(ns + EnergyBusinessProcessRoleElementName)?.Value ?? string.Empty;
var businessType = energyContextElement.Element(ns + EnergyIndustryClassificationElementName)?.Value;

return new MessageHeader(
messageId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers;

public interface IMessageParser
{
IncomingDocumentType DocumentType { get; }

DocumentFormat DocumentFormat { get; }

Task<IncomingMarketMessageParserResult> ParseAsync(
IIncomingMarketMessageStream marketMessage,
CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// 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.

using System.Collections.ObjectModel;
using System.Globalization;
using System.Text.Json;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Validation.ValidationErrors;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.BaseParsers;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.Schemas.Cim.Json;
using Json.Schema;

namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers;

public abstract class JsonMessageParserBase(JsonSchemaProvider schemaProvider) : MessageParserBase<JsonSchema>()
{
protected const string ValueElementName = "value";
protected const string IdentificationElementName = "mRID";
private const string MessageTypeElementName = "type";
private const string ProcessTypeElementName = "process.processType";
private const string SenderIdentificationElementName = "sender_MarketParticipant.mRID";
private const string ReceiverIdentificationElementName = "receiver_MarketParticipant.mRID";
private const string SenderRoleElementName = "sender_MarketParticipant.marketRole.type";
private const string ReceiverRoleElementName = "receiver_MarketParticipant.marketRole.type";
private const string CreatedDateElementName = "createdDateTime";
private readonly JsonSchemaProvider _schemaProvider = schemaProvider;

protected abstract string HeaderElementName { get; }

protected abstract string DocumentName { get; }

private Collection<ValidationError> ValidationErrors { get; } = [];

protected override async Task<IncomingMarketMessageParserResult> ParseMessageAsync(
IIncomingMarketMessageStream marketMessage,
JsonSchema schemaResult,
CancellationToken cancellationToken)
{
var document = await JsonDocument.ParseAsync(marketMessage.Stream, cancellationToken: cancellationToken).ConfigureAwait(false);
if (IsValid(document, schemaResult) == false)
{
return new IncomingMarketMessageParserResult(ValidationErrors.ToArray());
}

var header = ParseHeader(document);
var transactions = ParseTransactions(document, header.SenderId);
return CreateResult(header, transactions);
}

protected abstract IReadOnlyCollection<IIncomingMessageSeries> ParseTransactions(JsonDocument document, string senderNumber);

protected override async Task<(JsonSchema? Schema, ValidationError? ValidationError)> GetSchemaAsync(IIncomingMarketMessageStream marketMessage, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(DocumentName);
var schemaVersion = "0";
var jsonSchema = await _schemaProvider.GetSchemaAsync<JsonSchema>(DocumentName.ToUpper(CultureInfo.InvariantCulture), schemaVersion, cancellationToken).ConfigureAwait(false);
if (jsonSchema is null)
{
return (jsonSchema, new InvalidBusinessReasonOrVersion(DocumentName, schemaVersion));
}

return (jsonSchema, null);
}

protected abstract IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection<IIncomingMessageSeries> transactions);

private static string GetJsonDateStringWithoutQuotes(JsonElement element)
{
return element.ToString().Trim('"');
}

private static string? GetBusinessType(JsonElement element)
{
return element.TryGetProperty("businessSector.type", out var property) ? property.GetProperty("value").ToString() : null;
}

private MessageHeader ParseHeader(JsonDocument document)
{
var headerElement = document.RootElement.GetProperty(HeaderElementName);
return new MessageHeader(
headerElement.GetProperty(IdentificationElementName).ToString(),
headerElement.GetProperty(MessageTypeElementName).GetProperty(ValueElementName).ToString(),
headerElement.GetProperty(ProcessTypeElementName).GetProperty(ValueElementName).ToString(),
headerElement.GetProperty(SenderIdentificationElementName).GetProperty(ValueElementName).ToString(),
headerElement.GetProperty(SenderRoleElementName).GetProperty(ValueElementName).ToString(),
headerElement.GetProperty(ReceiverIdentificationElementName).GetProperty(ValueElementName).ToString(),
headerElement.GetProperty(ReceiverRoleElementName).GetProperty(ValueElementName).ToString(),
GetJsonDateStringWithoutQuotes(headerElement.GetProperty(CreatedDateElementName)),
GetBusinessType(headerElement));
}

private bool IsValid(JsonDocument jsonDocument, JsonSchema schema)
{
var result = schema.Evaluate(jsonDocument, new EvaluationOptions() { OutputFormat = OutputFormat.Hierarchical, });
if (result.IsValid == false)
{
FindErrorsForInvalidEvaluation(result);
}

return result.IsValid;
}

private void FindErrorsForInvalidEvaluation(EvaluationResults result)
{
if (!result.IsValid)
{
foreach (var detail in result.Details)
{
FindErrorsForInvalidEvaluation(detail);
}
}

if (!result.HasErrors || result.Errors == null) return;

var propertyName = result.InstanceLocation.ToString();
foreach (var error in result.Errors)
{
AddValidationError($"{propertyName}: {error}");
}
}

private void AddValidationError(string errorMessage)
{
ValidationErrors.Add(InvalidMessageStructure.From(errorMessage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers;

public abstract class MessageParserBase<TSchema>() : IMessageParser
{
public abstract IncomingDocumentType DocumentType { get; }

public abstract DocumentFormat DocumentFormat { get; }

public async Task<IncomingMarketMessageParserResult> ParseAsync(
IIncomingMarketMessageStream marketMessage,
CancellationToken cancellationToken)
Expand Down
Loading

0 comments on commit e02ba0f

Please sign in to comment.