Skip to content

Commit

Permalink
add support for racks in specification importer
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-dax committed Feb 6, 2025
1 parent 69489db commit 93c1034
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 29 deletions.
114 changes: 104 additions & 10 deletions OpenFTTH.APIGateway/Specifications/SpecificationImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class SpecificationImporter
private IQueryDispatcher _queryDispatcher;
private IEventStore _eventStore;

private ILogger<SpecificationExporter> _logger;
private ILogger<SpecificationImporter> _logger;

public SpecificationImporter(ILoggerFactory loggerFactory, ICommandDispatcher commandDispatcher, IQueryDispatcher queryDispatcher, IEventStore eventStore)
{
_logger = loggerFactory.CreateLogger<SpecificationExporter>();
_logger = loggerFactory.CreateLogger<SpecificationImporter>();
_commandDispatcher = commandDispatcher;
_queryDispatcher = queryDispatcher;
_eventStore = eventStore;
Expand Down Expand Up @@ -73,13 +73,13 @@ public void ImportFromDictionary(string dictionary)
}
}

private void ImportFromFile(string fileName, bool terminalStructuresOnly)
public void ImportFromFile(string fileName, bool terminalStructuresOnly)
{
var specificationData = JsonConvert.DeserializeObject<Specifications>(File.ReadAllText(fileName));
Import(specificationData, false);
}

private void Import(Specifications specifications, bool terminalStructuresOnly)
private void Import(Specifications specifications, bool terminalAndSpanStructuresOnly)
{

// Create dict with manufacture specs
Expand All @@ -101,10 +101,21 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)

foreach (var nodeContainerSpec in nodeContainerSpecs)
{
if (!nodeContainerSpecByName.ContainsKey(nodeContainerSpec.Name.ToLower()))
if (!nodeContainerSpecByName.ContainsKey(nodeContainerSpec.Description.ToLower()))
nodeContainerSpecByName.Add(nodeContainerSpec.Description.ToLower(), nodeContainerSpec);
}

// Create dict with rack specs specs
var rackSpecs = _eventStore.Projections.Get<RackSpecificationsProjection>().Specifications;

Dictionary<string, RackSpecification> rackSpecByName = new();

foreach (var rackSpec in rackSpecs)
{
if (!rackSpecByName.ContainsKey(rackSpec.Name.ToLower()))
rackSpecByName.Add(rackSpec.Name.ToLower(), rackSpec);
}


// Create dict with terminal equipment specs
var terminalEquipmentSpecs = _eventStore.Projections.Get<TerminalEquipmentSpecificationsProjection>().Specifications;
Expand Down Expand Up @@ -202,6 +213,30 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)
}
}

// Create node containers that don't exist
if (specifications.Racks != null)
{
foreach (var rackSpec in specifications.Racks)
{
if (!rackSpecByName.ContainsKey(rackSpec.Name.ToLower()))
{
_logger.LogInformation($"Creating rack specification: " + rackSpec.Name);

AddSpecification(
new RackSpecification(
Guid.NewGuid(),
rackSpec.Name,
rackSpec.ShortName
)
{
Description = rackSpec.Name,
Deprecated = rackSpec.Deprecated,
}
);
}
}
}

// Create terminal structure specs that don't exist
if (specifications.TerminalStructures != null)
{
Expand Down Expand Up @@ -230,16 +265,55 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)
}
}

// reload terminal structures
// Create span structure specs that don't exist
if (specifications.SpanStructures != null)
{
foreach (var spanStructureSpec in specifications.SpanStructures)
{
if (!spanStructureSpecByName.ContainsKey(spanStructureSpec.Name.ToLower()))
{
_logger.LogInformation($"Creating span structure: " + spanStructureSpec.Name);

AddSpecification(
new SpanStructureSpecification(
Guid.NewGuid(),
spanStructureSpec.SpanClassType,
spanStructureSpec.Name,
spanStructureSpec.Color
)
{
Description = spanStructureSpec.Description,
Deprecated = spanStructureSpec.Deprecated,
InnerDiameter = spanStructureSpec.InnerDiameter,
OuterDiameter = spanStructureSpec.OuterDiameter,
}
);
}
}
}



// Reload terminal structures
terminalStructureSpecs = _eventStore.Projections.Get<TerminalStructureSpecificationsProjection>().Specifications;

terminalStructureSpecByName = new();

foreach (var terminalStructureSpec in terminalStructureSpecs)
terminalStructureSpecByName.Add(terminalStructureSpec.Name.ToLower(), terminalStructureSpec);

// Reload span structures
spanStructureSpecs = _eventStore.Projections.Get<SpanStructureSpecificationsProjection>().Specifications;

spanStructureSpecByName = new();

if (!terminalStructuresOnly)
foreach (var spanStructureSpec in spanStructureSpecs)
{
if (!spanStructureSpecByName.ContainsKey(spanStructureSpec.Name.ToLower()))
spanStructureSpecByName.Add(spanStructureSpec.Name.ToLower(), spanStructureSpec);
}

if (!terminalAndSpanStructuresOnly)
{

// Create terminal equipment specs that don't exist
Expand Down Expand Up @@ -285,7 +359,7 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)
{
foreach (var spanEquipmentSpec in specifications.SpanEquipments)
{
if (!terminalEquipmentSpecByName.ContainsKey(spanEquipmentSpec.Name.ToLower()))
if (!spanEquipmentSpecByName.ContainsKey(spanEquipmentSpec.Name.ToLower()))
{
_logger.LogInformation($"Creating span equipment: " + spanEquipmentSpec.Name);

Expand All @@ -295,7 +369,7 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)
Guid.NewGuid(),
spanEquipmentSpec.Category,
spanEquipmentSpec.Name,
CreateSpanStructureTemplates(spanEquipmentSpec.RootStructure, spanStructureSpecByName)
CreateSpanStructureTemplates(spanEquipmentSpec.OuterStructure, spanStructureSpecByName)
)
{
Description = spanEquipmentSpec.Description,
Expand All @@ -309,7 +383,7 @@ private void Import(Specifications specifications, bool terminalStructuresOnly)
}
else
{
_logger.LogInformation($"Terminal equipment already exists: " + spanEquipmentSpec.Name);
_logger.LogInformation($"Span equipment already exists: " + spanEquipmentSpec.Name);
}
}
}
Expand Down Expand Up @@ -501,6 +575,26 @@ private void AddSpecification(NodeContainerSpecification spec)
_logger.LogInformation("Node container specification created OK");
}

private void AddSpecification(RackSpecification spec)
{
var specifications = _eventStore.Projections.Get<RackSpecificationsProjection>().Specifications;

if (specifications.ContainsKey(spec.Id))
return;

var cmd = new AddRackSpecification(Guid.NewGuid(), new UserContext("specification seeder", _specSeederId), spec);
var cmdResult = _commandDispatcher.HandleAsync<AddRackSpecification, Result>(cmd).Result;

if (cmdResult.IsFailed)
{
var errorMsg = cmdResult.Errors.First().Message;
_logger.LogError(errorMsg);
throw new ApplicationException(errorMsg);
}
else
_logger.LogInformation("Rack specification created OK");
}

private void AddSpecification(TerminalStructureSpecification spec)
{
var specifications = _eventStore.Projections.Get<TerminalStructureSpecificationsProjection>().Specifications;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenFTTH.UtilityGraphService.API.Model.UtilityNetwork;
using System;
using System;
using System.Collections.Generic;

namespace OpenFTTH.APIGateway.Specifications
Expand All @@ -10,8 +9,9 @@ public class Specifications
public List<TerminalStructureSpec> TerminalStructures { get; set; }
public List<TerminalEquipmentSpec> TerminalEquipments { get; set; }
public List<SpanEquipmentSpec> SpanEquipments { get; set; }
public List<SpanStructureSpec> SpanStructure { get; set; }
public List<SpanStructureSpec> SpanStructures { get; set; }
public List<NodeContainerSpec> NodeContainers { get; set; }
public List<RackSpec> Racks { get; set; }
}

public record ManufacturerSpec
Expand All @@ -29,13 +29,13 @@ public record TerminalEquipmentSpec
public string Category { get; set; }
public string ShortName { get; set; }
public string? Description { get; set; }
public List<string>? Manufacturers { get; init; }
public List<string>? Manufacturers { get; set; }
public bool IsRackEquipment { get; set; }
public int HeightInRackUnits { get; set; }
public bool IsFixed { get; set; }
public bool IsAddressable { get; init; }
public bool IsCustomerTermination { get; init; }
public bool IsLineTermination { get; init; }
public bool IsAddressable { get; set; }
public bool IsCustomerTermination { get; set; }
public bool IsLineTermination { get; set; }
public bool Deprecated { get; set; }
public List<TerminalStructureTemplateSpec> Structures { get; set; }
}
Expand All @@ -54,7 +54,7 @@ public record TerminalStructureSpec
public string? Description { get; set; }
public List<string>? Manufacturers { get; set; }
public List<TerminalTemplateSpec> Terminals { get; set; }
public bool Deprecated { get; init; }
public bool Deprecated { get; set; }
}

public record TerminalTemplateSpec
Expand All @@ -68,20 +68,27 @@ public record TerminalTemplateSpec
}



public record NodeContainerSpec
{
public string Name { get; set; }
public string Category { get; set; }
public string ShortName { get; set; }
public string? Description { get; set; }
public List<string>? Manufacturers { get; set; }
public bool Deprecated { get; set; }
}

public record RackSpec
{
public string Name { get; set; }
public string Category { get; set; }
public string ShortName { get; set; }
public string? Description { get; set; }
public bool Deprecated { get; init; }
}

public record SpanEquipmentSpec
{
//public Guid Id { get; }
public string Category { get; set; }
public string Name { get; set; }
public bool Deprecated { get; set; }
Expand All @@ -91,7 +98,7 @@ public record SpanEquipmentSpec
public List<string>? Manufacturers { get; set; }
public bool IsCable { get; set; }

public SpanStructureTemplateSpec RootStructure { get; set; }
public SpanStructureTemplateSpec OuterStructure { get; set; }
}

public class SpanStructureTemplateSpec
Expand All @@ -105,13 +112,12 @@ public class SpanStructureTemplateSpec

public record SpanStructureSpec
{
//public Guid Id { get; }
public string SpanClassType { get; }
public string Name { get; }
public string Color { get; }
public int? InnerDiameter { get; init; }
public int? OuterDiameter { get; init; }
public bool Deprecated { get; init; }
public string? Description { get; init; }
public string SpanClassType { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public int? InnerDiameter { get; set; }
public int? OuterDiameter { get; set; }
public bool Deprecated { get; set; }
public string? Description { get; set; }
}
}

0 comments on commit 93c1034

Please sign in to comment.