Skip to content

Commit

Permalink
feat: grid area ownership event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmajgaard committed Dec 2, 2024
1 parent ed4bc04 commit edd308c
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
<EmbeddedResource Include="Scripts\202406210900_Add_IsHiddenFromActor_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202409081607_Add_JobId_To_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202410261405_Add_IsNotificationSent_to_SettlementReport.sql" />
<EmbeddedResource Include="Scripts\202412021100_Add_GridAreaOwner.sql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE [settlementreports].[GridAreaOwner]
(
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Code VARCHAR(4) NOT NULL,
ActorNumber VARCHAR(50) NOT NULL,
ValidFrom datetimeoffset(7) NOT NULL,
SequenceNumber INT NOT NULL DEFAULT 0,
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
// limitations under the License.

using Energinet.DataHub.Core.App.Common.Extensions.DependencyInjection;
using Energinet.DataHub.Core.Messaging.Communication;
using Energinet.DataHub.Core.Messaging.Communication.Extensions.DependencyInjection;
using Energinet.DataHub.Core.Messaging.Communication.Extensions.Options;
using Energinet.DataHub.SettlementReport.Application.Handlers;
using Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Extensions.Options;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.HealthChecks;
using Energinet.DataHub.SettlementReport.Infrastructure.Contracts;
using Energinet.DataHub.SettlementReport.Infrastructure.Extensions.DependencyInjection;
using Energinet.DataHub.SettlementReport.Infrastructure.Helpers;
using Energinet.DataHub.SettlementReport.Infrastructure.Notifications;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence;
using Energinet.DataHub.SettlementReport.Infrastructure.Persistence.SettlementReportRequest;
using Energinet.DataHub.SettlementReport.Infrastructure.Services;
using Energinet.DataHub.SettlementReport.Infrastructure.SettlementReports_v2;
using Energinet.DataHub.SettlementReport.Interfaces.Helpers;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2;
Expand All @@ -38,6 +42,23 @@ public static IServiceCollection AddSettlementReportFunctionModule(this IService
{
ArgumentNullException.ThrowIfNull(configuration);

services
.AddOptions<IntegrationEventsOptions>()
.BindConfiguration(IntegrationEventsOptions.SectionName)
.ValidateDataAnnotations();

services
.AddOptions<ServiceBusNamespaceOptions>()
.BindConfiguration(ServiceBusNamespaceOptions.SectionName)
.ValidateDataAnnotations();

services.AddScoped<IGridAreaOwnershipAssignedEventStore, GridAreaOwnerRepository>();
services.AddScoped<IGridAreaOwnerRepository, GridAreaOwnerRepository>();
services.AddSubscriber<IntegrationEventSubscriptionHandler>(
[
GridAreaOwnershipAssigned.Descriptor,
]);

// general services
services.AddScoped<IRequestSettlementReportJobHandler, RequestSettlementReportHandler>();
services.AddScoped<ISettlementReportDatabaseContext, SettlementReportDatabaseContext>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 Azure.Messaging.ServiceBus;
using Energinet.DataHub.Core.Messaging.Communication;
using Energinet.DataHub.Core.Messaging.Communication.Extensions.Options;
using Energinet.DataHub.Core.Messaging.Communication.Subscriber;
using Energinet.DataHub.RevisionLog.Integration;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Microsoft.Azure.Functions.Worker;
using NodaTime;

namespace SettlementReports.Function.Functions;

public sealed class ExchangeEventTrigger
{
private readonly ISubscriber _subscriber;
private readonly IRevisionLogClient _revisionLogClient;

public ExchangeEventTrigger(ISubscriber subscriber, IRevisionLogClient revisionLogClient)
{
_subscriber = subscriber;
_revisionLogClient = revisionLogClient;
}

[Function(nameof(ExchangeEventTrigger))]
public async Task RunAsync(
[ServiceBusTrigger(
$"%{IntegrationEventsOptions.SectionName}:{nameof(IntegrationEventsOptions.TopicName)}%",
$"%{IntegrationEventsOptions.SectionName}:{nameof(IntegrationEventsOptions.SubscriptionName)}%",
Connection = ServiceBusNamespaceOptions.SectionName)]
ServiceBusReceivedMessage message,
FunctionContext context)
{
ArgumentNullException.ThrowIfNull(message);
ArgumentNullException.ThrowIfNull(context);

var msg = IntegrationEventServiceBusMessage.Create(message);

await _revisionLogClient.LogAsync(
new RevisionLogEntry(
logId: msg.MessageId,
systemId: SubsystemInformation.Id,
occurredOn: SystemClock.Instance.GetCurrentInstant(),
activity: "ConsumedIntegrationEvent",
origin: nameof(ExchangeEventTrigger),
payload: Convert.ToBase64String(message.Body)))
.ConfigureAwait(false);

await _subscriber.HandleAsync(msg).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
using Energinet.DataHub.Core.App.FunctionApp.Extensions.DependencyInjection;
using Energinet.DataHub.Core.Databricks.Jobs.Extensions.DependencyInjection;
using Energinet.DataHub.RevisionLog.Integration.Extensions.DependencyInjection;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Security;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Telemetry;
using Energinet.DataHub.SettlementReport.Infrastructure.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SettlementReports.Function.Extensions.DependencyInjection;
using SettlementReports.WebAPI.Extensions.DependencyInjection;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Contracts;

public partial class GridAreaOwnershipAssigned
{
public const string EventName = "GridAreaOwnershipAssigned";
public const int CurrentMinorVersion = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* 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.
*/

syntax = "proto3";

option csharp_namespace = "Energinet.DataHub.SettlementReport.Infrastructure.Contracts";

enum EicFunction {
/*
* Unspecified is unused but according to best practice.
* Read more at https://protobuf.dev/programming-guides/style/#enums.
*/
EIC_FUNCTION_UNSPECIFIED = 0;
EIC_FUNCTION_GRID_ACCESS_PROVIDER = 1;
EIC_FUNCTION_BALANCE_RESPONSIBLE_PARTY = 2;
EIC_FUNCTION_BILLING_AGENT = 3;
EIC_FUNCTION_ENERGY_SUPPLIER = 4;
EIC_FUNCTION_IMBALANCE_SETTLEMENT_RESPONSIBLE = 5;
EIC_FUNCTION_METERED_DATA_ADMINISTRATOR = 6;
EIC_FUNCTION_METERED_DATA_RESPONSIBLE = 7;
EIC_FUNCTION_METERING_POINT_ADMINISTRATOR = 8;
EIC_FUNCTION_SYSTEM_OPERATOR = 9;
EIC_FUNCTION_DANISH_ENERGY_AGENCY = 10;
EIC_FUNCTION_DATAHUB_ADMINISTRATOR = 11;
EIC_FUNCTION_INDEPENDENT_AGGREGATOR = 12;
EIC_FUNCTION_SERIAL_ENERGY_TRADER = 13;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* 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.
*/

syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "Contracts/eic_function.proto";

option csharp_namespace = "Energinet.DataHub.SettlementReport.Infrastructure.Contracts";

/*
* GridAreaOwnershipAssigned Integration Event.
*
* Occurs when an actor is assigned ownership of a grid area through the specified market role.
*/
message GridAreaOwnershipAssigned {

/*
* The EIC or GLN identifier of the actor that got assigned ownership.
*/
string actor_number = 1;

/*
* The code of the grid area that the actor got ownership of.
*/
string grid_area_code = 2;

/*
* The market role for which the ownership is assigned.
*/
EicFunction actor_role = 3;

/*
* A timestamp from when the ownership is valid.
*/
google.protobuf.Timestamp valid_from = 4;

/*
* The sequence number of the event. Used to distinguish between events with identical valid_from timestamps.
*/
int32 sequence_number = 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;

public sealed record ActorNumber(string Value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;

public sealed record GridAreaCode(string Value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 NodaTime;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Model;

public sealed record GridAreaOwner(GridAreaCode Code, ActorNumber ActorNumber, Instant ValidFrom);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.

namespace Energinet.DataHub.SettlementReport.Infrastructure.Persistence;

public class GridAreaOwnerEntity
{
public int Id { get; set; }

public string Code { get; set; } = null!;

public string ActorNumber { get; set; } = null!;

public DateTimeOffset ValidFrom { get; set; }

public int SequenceNumber { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Persistence;

public class GridAreaOwnerEntityConfiguration : IEntityTypeConfiguration<GridAreaOwnerEntity>
{
public void Configure(EntityTypeBuilder<GridAreaOwnerEntity> builder)
{
ArgumentNullException.ThrowIfNull(builder);

builder.ToTable("GridAreaOwner");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).ValueGeneratedOnAdd();
builder.Property(e => e.Code).HasMaxLength(4);
builder.Property(e => e.ActorNumber).HasMaxLength(50);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface ISettlementReportDatabaseContext
{
DbSet<Application.SettlementReports_v2.SettlementReport> SettlementReports { get; }

DbSet<GridAreaOwnerEntity> GridAreaOwners { get; }

Task<int> SaveChangesAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public SettlementReportDatabaseContext() { }

public DbSet<Application.SettlementReports_v2.SettlementReport> SettlementReports { get; init; } = null!;

public DbSet<GridAreaOwnerEntity> GridAreaOwners { get; init; } = null!;

public Task<int> SaveChangesAsync() => base.SaveChangesAsync();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(Schema);

modelBuilder.ApplyConfiguration(new SettlementReportEntityConfiguration());
modelBuilder.ApplyConfiguration(new GridAreaOwnerEntityConfiguration());

base.OnModelCreating(modelBuilder);
}
Expand Down
Loading

0 comments on commit edd308c

Please sign in to comment.