Skip to content

Commit

Permalink
Factory for creating AAS service provider
Browse files Browse the repository at this point in the history
Changed the implementation of the AAS repository service provider to use
a factory instead of an extension method for service provider creation

References eclipse-basyx#3
  • Loading branch information
bjoernhoeper committed Oct 16, 2022
1 parent cba29ea commit 381967e
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace BaSyx.API.Components
{
public class AssetAdministrationShellRepositoryServiceProvider : IAssetAdministrationShellRepositoryServiceProvider
{
private readonly IAssetAdministrationShellServiceProviderFactory _assetAdministrationShellServiceProviderFactory;
public IEnumerable<IAssetAdministrationShell> AssetAdministrationShells => GetBinding();

private Dictionary<string, IAssetAdministrationShellServiceProvider> AssetAdministrationShellServiceProviders { get; }
Expand All @@ -40,13 +41,15 @@ private set
_serviceDescriptor = value;
}
}
public AssetAdministrationShellRepositoryServiceProvider(IAssetAdministrationShellRepositoryDescriptor descriptor) : this()
public AssetAdministrationShellRepositoryServiceProvider(IAssetAdministrationShellRepositoryDescriptor descriptor,
IAssetAdministrationShellServiceProviderFactory assetAdministrationShellServiceProviderFactory) : this(assetAdministrationShellServiceProviderFactory)
{
ServiceDescriptor = descriptor;
}

public AssetAdministrationShellRepositoryServiceProvider()
public AssetAdministrationShellRepositoryServiceProvider(IAssetAdministrationShellServiceProviderFactory assetAdministrationShellServiceProviderFactory)
{
_assetAdministrationShellServiceProviderFactory = assetAdministrationShellServiceProviderFactory;
AssetAdministrationShellServiceProviders = new Dictionary<string, IAssetAdministrationShellServiceProvider>();
}

Expand Down Expand Up @@ -77,16 +80,15 @@ public IResult<IAssetAdministrationShell> CreateAssetAdministrationShell(IAssetA
{
if (aas == null)
return new Result<IAssetAdministrationShell>(new ArgumentNullException(nameof(aas)));

var registered = RegisterAssetAdministrationShellServiceProvider(aas.Identification.Id, aas.CreateServiceProvider(true));
if (!registered.Success)
return new Result<IAssetAdministrationShell>(registered);

var assetAdministrationShellServiceProvider = _assetAdministrationShellServiceProviderFactory.CreateServiceProvider(aas, true);
RegisterAssetAdministrationShellServiceProvider(aas.Identification.Id, assetAdministrationShellServiceProvider);

var retrievedShellServiceProvider = GetAssetAdministrationShellServiceProvider(aas.Identification.Id);
if (retrievedShellServiceProvider.TryGetEntity(out IAssetAdministrationShellServiceProvider serviceProvider))
return new Result<IAssetAdministrationShell>(true, serviceProvider.GetBinding());
else
return new Result<IAssetAdministrationShell>(false, new Message(MessageType.Error, "Could not retrieve Asset Administration Shell Service Provider"));

return new Result<IAssetAdministrationShell>(false, new Message(MessageType.Error, "Could not retrieve Asset Administration Shell Service Provider"));
}

public IResult DeleteAssetAdministrationShell(string aasId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// /*******************************************************************************
// * Copyright (c) 2022 LTSoft - Agentur für Leittechnik-Software GmbH
// * Author: Björn Höper
// *
// * This program and the accompanying materials are made available under the
// * terms of the Eclipse Public License 2.0 which is available at
// * http://www.eclipse.org/legal/epl-2.0
// *
// * SPDX-License-Identifier: EPL-2.0
// *******************************************************************************/

using BaSyx.API.AssetAdministrationShell.Extensions;
using BaSyx.Models.Core.AssetAdministrationShell.Generics;

namespace BaSyx.API.Components;

/// <summary>
/// Creates service providers for Asset Administration Shells
/// </summary>
public interface IAssetAdministrationShellServiceProviderFactory
{
/// <summary>
/// Create a new service provider for the AAS
/// </summary>
/// <param name="aas">Asset administration shell to create the service provider for</param>
/// <returns>Created service provider</returns>
public IAssetAdministrationShellServiceProvider CreateServiceProvider(IAssetAdministrationShell aas, bool includeSubmodels);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// /*******************************************************************************
// * Copyright (c) 2022 LTSoft - Agentur für Leittechnik-Software GmbH
// * Author: Björn Höper
// *
// * This program and the accompanying materials are made available under the
// * terms of the Eclipse Public License 2.0 which is available at
// * http://www.eclipse.org/legal/epl-2.0
// *
// * SPDX-License-Identifier: EPL-2.0
// *******************************************************************************/

using System.Linq;
using BaSyx.Models.Core.AssetAdministrationShell.Generics;

namespace BaSyx.API.Components;

/// <summary>
/// Creates internal In-Memory providers for the AAS
/// </summary>
public class InternalAssetAdministrationShellServiceProviderFactory : IAssetAdministrationShellServiceProviderFactory
{
private readonly ISubmodelServiceProviderFactory _submodelServiceProviderFactory;

public InternalAssetAdministrationShellServiceProviderFactory(ISubmodelServiceProviderFactory submodelServiceProviderFactory)
{
_submodelServiceProviderFactory = submodelServiceProviderFactory;
}

public IAssetAdministrationShellServiceProvider CreateServiceProvider(IAssetAdministrationShell aas,
bool includeSubmodels)
{
InternalAssetAdministrationShellServiceProvider sp = new InternalAssetAdministrationShellServiceProvider(aas);

if (includeSubmodels && aas.Submodels.Any())
{
foreach (var submodel in aas.Submodels.Values)
{
var submodelSp = _submodelServiceProviderFactory.CreateSubmodelServiceProvider(submodel);
sp.RegisterSubmodelServiceProvider(submodel.IdShort, submodelSp);
}
}

return sp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// /*******************************************************************************
// * Copyright (c) 2022 LTSoft - Agentur für Leittechnik-Software GmbH
// * Author: Björn Höper
// *
// * This program and the accompanying materials are made available under the
// * terms of the Eclipse Public License 2.0 which is available at
// * http://www.eclipse.org/legal/epl-2.0
// *
// * SPDX-License-Identifier: EPL-2.0
// *******************************************************************************/

using BaSyx.API.Components;
using BaSyx.Models.Core.AssetAdministrationShell.Generics;
using BaSyx.Models.Core.AssetAdministrationShell.Identification;
using Moq;

namespace Basyx.API.Tests.Components.ServiceProvider;

public class AssetAdministrationShellRepositoryServiceProviderTests
{
[Fact]
public async Task CreateAssetAdministrationShell_WhenSuccess_CallsFactory()
{
var aasMock = new Mock<IAssetAdministrationShell>();
aasMock.Setup(a => a.Identification)
.Returns(new Identifier("http://assetadminshell.io/1/0/0/testshell", KeyType.URI));

var serviceProviderMock = new Mock<IAssetAdministrationShellServiceProvider>();
serviceProviderMock.Setup(p => p.GetBinding()).Returns(aasMock.Object);

var factoryMock = new Mock<IAssetAdministrationShellServiceProviderFactory>();
factoryMock.Setup(f => f.CreateServiceProvider(aasMock.Object, true)).Returns(serviceProviderMock.Object);

var aasRepoProvider = new AssetAdministrationShellRepositoryServiceProvider(factoryMock.Object);
var result = aasRepoProvider.CreateAssetAdministrationShell(aasMock.Object);

factoryMock.Verify(f => f.CreateServiceProvider(aasMock.Object, true), Times.AtLeastOnce);
Assert.Equal(aasMock.Object, result.Entity);
}
}

0 comments on commit 381967e

Please sign in to comment.