-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
373 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
...IncomingMessages.Infrastructure/MessageParsers/BaseParsers/Ebix/MessageHeaderExtractor.cs
This file was deleted.
Oops, something went wrong.
177 changes: 177 additions & 0 deletions
177
source/IncomingMessages.Infrastructure/MessageParsers/EbixMessageParserBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// 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.Xml; | ||
using System.Xml.Linq; | ||
using System.Xml.Schema; | ||
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.Ebix; | ||
|
||
namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers; | ||
|
||
public abstract class EbixMessageParserBase(EbixSchemaProvider schemaProvider) : MessageParserBase<XmlSchema>(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"; | ||
|
||
protected override async Task<IncomingMarketMessageParserResult> ParseMessageAsync( | ||
IIncomingMarketMessageStream marketMessage, | ||
XmlSchema schemaResult, | ||
string @namespace, | ||
CancellationToken cancellationToken) | ||
{ | ||
using var reader = XmlReader.Create(marketMessage.Stream, CreateXmlReaderSettings(schemaResult)); | ||
if (Errors.Count > 0) | ||
{ | ||
return new IncomingMarketMessageParserResult(Errors.ToArray()); | ||
} | ||
|
||
var document = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken).ConfigureAwait(false); | ||
var ns = XNamespace.Get(@namespace); | ||
|
||
var header = ParseHeader(document, ns); | ||
var transactions = ParseTransactions(document, ns, header.SenderId); | ||
|
||
if (Errors.Count != 0) | ||
{ | ||
return new IncomingMarketMessageParserResult(Errors.ToArray()); | ||
} | ||
|
||
return CreateResult(header, transactions); | ||
} | ||
|
||
protected abstract IReadOnlyCollection<IIncomingMessageSeries> ParseTransactions(XDocument document, XNamespace ns, string senderNumber); | ||
|
||
protected abstract IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection<IIncomingMessageSeries> transactions); | ||
|
||
protected override string GetNamespace(IIncomingMarketMessageStream marketMessage) | ||
{ | ||
ArgumentNullException.ThrowIfNull(marketMessage); | ||
|
||
var settings = new XmlReaderSettings | ||
{ | ||
Async = true, | ||
IgnoreWhitespace = true, | ||
IgnoreComments = true, | ||
}; | ||
|
||
using var reader = XmlReader.Create(marketMessage.Stream, settings); | ||
while (reader.Read()) | ||
{ | ||
if (reader.NodeType == XmlNodeType.Element && reader.Name.Contains(RootPayloadElementName)) | ||
{ | ||
return reader.NamespaceURI; | ||
} | ||
} | ||
|
||
throw new XmlException($"Namespace for element '{RootPayloadElementName}' not found."); | ||
} | ||
|
||
protected override string BusinessProcessType(string @namespace) | ||
{ | ||
ArgumentNullException.ThrowIfNull(@namespace); | ||
var split = SplitNamespace(@namespace); | ||
if (split.Length < 5) | ||
{ | ||
throw new XmlException($"Invalid namespace format"); | ||
} | ||
|
||
var businessReason = split[4]; | ||
var parts = businessReason.Split('-'); | ||
return parts.Last(); | ||
} | ||
|
||
protected override string GetVersion(string @namespace) | ||
{ | ||
ArgumentNullException.ThrowIfNull(@namespace); | ||
var split = SplitNamespace(@namespace); | ||
if (split.Length < 6) | ||
{ | ||
throw new XmlException($"Invalid namespace format"); | ||
} | ||
|
||
var version = split[5]; | ||
return version.StartsWith('v') ? version[1..] : version; | ||
} | ||
|
||
private static string[] SplitNamespace(string @namespace) | ||
{ | ||
ArgumentNullException.ThrowIfNull(@namespace); | ||
return @namespace.Split(':'); | ||
} | ||
|
||
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 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; | ||
|
||
return new MessageHeader( | ||
messageId, | ||
messageType, | ||
businessReason, | ||
senderId, | ||
senderRole, | ||
receiverId, | ||
// ReceiverRole is not specified in incoming Ebix documents | ||
ActorRole.MeteredDataAdministrator.Code, | ||
createdAt, | ||
businessType); | ||
} | ||
|
||
private XmlReaderSettings CreateXmlReaderSettings(XmlSchema xmlSchema) | ||
{ | ||
var settings = new XmlReaderSettings | ||
{ | ||
Async = true, | ||
ValidationType = ValidationType.Schema, | ||
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | | ||
XmlSchemaValidationFlags.ReportValidationWarnings, | ||
}; | ||
|
||
settings.Schemas.Add(xmlSchema); | ||
settings.ValidationEventHandler += OnValidationError; | ||
return settings; | ||
} | ||
|
||
private void OnValidationError(object? sender, ValidationEventArgs arguments) | ||
{ | ||
var message = | ||
$"XML schema validation error at line {arguments.Exception.LineNumber}, position {arguments.Exception.LinePosition}: {arguments.Message}."; | ||
Errors.Add(InvalidMessageStructure.From(message)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
source/IncomingMessages.Infrastructure/MessageParsers/IMessageParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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 Energinet.DataHub.EDI.BuildingBlocks.Domain.Models; | ||
|
||
namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers; | ||
|
||
public interface IMessageParser | ||
{ | ||
Task<IncomingMarketMessageParserResult> ParseAsync( | ||
IIncomingMarketMessageStream marketMessage, | ||
CancellationToken cancellationToken); | ||
} |
54 changes: 0 additions & 54 deletions
54
source/IncomingMessages.Infrastructure/MessageParsers/MessageParser.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.