Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Commit - Bring ModelRepository Project to the mono repo #18706

Merged
merged 5 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/modelsrepository/Azure.Iot.ModelsRepository/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
launchSettings.json
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Iot.ModelsRepository", "src\Azure.Iot.ModelsRepository.csproj", "{5E11A377-0D20-49F8-952B-50390196EF4B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Iot.ModelsRepository.Tests", "tests\Azure.Iot.ModelsRepository.Tests.csproj", "{092E6CE2-9998-428C-A801-2BAB4E14A577}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{1FC8A3EA-3C0D-4DDF-B710-A7091F2CEBB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E11A377-0D20-49F8-952B-50390196EF4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E11A377-0D20-49F8-952B-50390196EF4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E11A377-0D20-49F8-952B-50390196EF4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E11A377-0D20-49F8-952B-50390196EF4B}.Release|Any CPU.Build.0 = Release|Any CPU
{092E6CE2-9998-428C-A801-2BAB4E14A577}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{092E6CE2-9998-428C-A801-2BAB4E14A577}.Debug|Any CPU.Build.0 = Debug|Any CPU
{092E6CE2-9998-428C-A801-2BAB4E14A577}.Release|Any CPU.ActiveCfg = Release|Any CPU
{092E6CE2-9998-428C-A801-2BAB4E14A577}.Release|Any CPU.Build.0 = Release|Any CPU
{1FC8A3EA-3C0D-4DDF-B710-A7091F2CEBB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FC8A3EA-3C0D-4DDF-B710-A7091F2CEBB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FC8A3EA-3C0D-4DDF-B710-A7091F2CEBB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FC8A3EA-3C0D-4DDF-B710-A7091F2CEBB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {671D1EFB-2BB9-4846-91EF-A3FB1FF9DDA6}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions sdk/modelsrepository/Azure.Iot.ModelsRepository/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Release History

## 1.0.0-preview.1 (Unreleased)

### New features

- Initial preview of Azure.IoT.ModelRepository SDK

### Breaking changes

- N/A

### Added

- N/A

### Fixes and improvements

- N/A
41 changes: 41 additions & 0 deletions sdk/modelsrepository/Azure.Iot.ModelsRepository/CodeMaid.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="SteveCadwallader.CodeMaid.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<userSettings>
<SteveCadwallader.CodeMaid.Properties.Settings>
<setting name="Cleaning_AutoCleanupOnFileSave" serializeAs="String">
<value>True</value>
</setting>
<setting name="Formatting_CommentXmlSpaceSingleTags" serializeAs="String">
<value>True</value>
</setting>
<setting name="Cleaning_InsertEndOfFileTrailingNewLine" serializeAs="String">
<value>True</value>
</setting>
<setting name="Formatting_CommentWrapColumn" serializeAs="String">
<value>120</value>
</setting>
<setting name="Formatting_CommentRunDuringCleanup" serializeAs="String">
<value>False</value>
</setting>
<setting name="Cleaning_UpdateFileHeaderCSharp" serializeAs="String">
<value>// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
</value>
</setting>
<setting name="Reorganizing_PrimaryOrderByAccessLevel" serializeAs="String">
<value>True</value>
</setting>
<setting name="Cleaning_PerformPartialCleanupOnExternal" serializeAs="String">
<value>1</value>
</setting>
<setting name="Progressing_ShowBuildProgressOnBuildStart" serializeAs="String">
<value>False</value>
</setting>
</SteveCadwallader.CodeMaid.Properties.Settings>
</userSettings>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
-->
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyTitle>Azure IoT Models Repository SDK</AssemblyTitle>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<!-- Nuget properties -->
<PropertyGroup>
<PackageTags>IoT;ModelsRepository;Pnp;DigitalTwins$(PackageCommonTags)</PackageTags>
<Description>SDK for the Azure IoT Models Repository</Description>
<Version>1.0.0-preview.1</Version>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<Import Project="$(RepoRoot)\sdk\core\Azure.Core\src\Azure.Core.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Azure.Iot.ModelsRepository
{
internal static class DtmiConventions
{
public static bool IsDtmi(string dtmi) => !string.IsNullOrEmpty(dtmi) && new Regex(@"^dtmi:[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?(?::[A-Za-z](?:[A-Za-z0-9_]*[A-Za-z0-9])?)*;[1-9][0-9]{0,8}$").IsMatch(dtmi);
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
public static string DtmiToPath(string dtmi) => IsDtmi(dtmi) ? $"{dtmi.ToLowerInvariant().Replace(":", "/").Replace(";", "-")}.json" : null;

public static string DtmiToQualifiedPath(string dtmi, string basePath, bool fromExpanded = false)
{
string dtmiPath = DtmiToPath(dtmi);
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
if (dtmiPath == null)
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, StandardStrings.InvalidDtmiFormat, dtmi));

if (!basePath.EndsWith("/", StringComparison.InvariantCultureIgnoreCase))
basePath += "/";

string fullyQualifiedPath = $"{basePath}{dtmiPath}";

if (fromExpanded)
fullyQualifiedPath = fullyQualifiedPath.Replace(".json", ".expanded.json");

return fullyQualifiedPath;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Azure.Iot.ModelsRepository.Fetchers
{
internal class FetchResult
{
public string Definition { get; set; }
public string Path { get; set; }
public bool FromExpanded => Path.EndsWith("expanded.json", System.StringComparison.InvariantCultureIgnoreCase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Iot.ModelsRepository.Fetchers
{
internal interface IModelFetcher
{
Task<FetchResult> FetchAsync(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default);

FetchResult Fetch(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;

namespace Azure.Iot.ModelsRepository.Fetchers
{
internal class LocalModelFetcher : IModelFetcher
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marked this and the RemoteModelFetcher as internal

{
private readonly bool _tryExpanded;

public LocalModelFetcher(ResolverClientOptions clientOptions)
{
_tryExpanded = clientOptions.DependencyResolution == DependencyResolutionOption.TryFromExpanded;
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
}

public Task<FetchResult> FetchAsync(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default)
{
return Task.FromResult(Fetch(dtmi, repositoryUri, cancellationToken));
}

public FetchResult Fetch(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default)
{
var work = new Queue<string>();

if (_tryExpanded)
work.Enqueue(GetPath(dtmi, repositoryUri, true));

work.Enqueue(GetPath(dtmi, repositoryUri, false));

string fnfError = string.Empty;
while (work.Count != 0 && !cancellationToken.IsCancellationRequested)
{
string tryContentPath = work.Dequeue();
ResolverEventSource.Shared.FetchingModelContent(tryContentPath);

if (File.Exists(tryContentPath))
{
return new FetchResult()
{
Definition = File.ReadAllText(tryContentPath, Encoding.UTF8),
Path = tryContentPath
};
}

ResolverEventSource.Shared.ErrorFetchingModelContent(tryContentPath);
fnfError = string.Format(CultureInfo.InvariantCulture, StandardStrings.ErrorFetchingModelContent, tryContentPath);
}

throw new FileNotFoundException(fnfError);
}

private static string GetPath(string dtmi, Uri repositoryUri, bool expanded = false)
{
string registryPath = repositoryUri.AbsolutePath;
return DtmiConventions.DtmiToQualifiedPath(dtmi, registryPath, expanded);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;
using Azure.Core.Pipeline;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Iot.ModelsRepository.Fetchers
{
internal class RemoteModelFetcher : IModelFetcher
{
private readonly HttpPipeline _pipeline;
private readonly bool _tryExpanded;

public RemoteModelFetcher(ResolverClientOptions clientOptions)
{
_pipeline = CreatePipeline(clientOptions);
_tryExpanded = clientOptions.DependencyResolution == DependencyResolutionOption.TryFromExpanded;
}

public FetchResult Fetch(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<FetchResult> FetchAsync(string dtmi, Uri repositoryUri, CancellationToken cancellationToken = default)
{
Queue<string> work = new Queue<string>();

if (_tryExpanded)
work.Enqueue(GetPath(dtmi, repositoryUri, true));

work.Enqueue(GetPath(dtmi, repositoryUri, false));

string remoteFetchError = string.Empty;
while (work.Count != 0 && !cancellationToken.IsCancellationRequested)
{
string tryContentPath = work.Dequeue();
ResolverEventSource.Shared.FetchingModelContent(tryContentPath);

string content = await EvaluatePathAsync(tryContentPath, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(content))
{
return new FetchResult()
{
Definition = content,
Path = tryContentPath
};
}

ResolverEventSource.Shared.ErrorFetchingModelContent(tryContentPath);
remoteFetchError = string.Format(CultureInfo.InvariantCulture, StandardStrings.ErrorFetchingModelContent, tryContentPath);
}

throw new RequestFailedException(remoteFetchError);
}

private static string GetPath(string dtmi, Uri repositoryUri, bool expanded = false)
{
string absoluteUri = repositoryUri.AbsoluteUri;
return DtmiConventions.DtmiToQualifiedPath(dtmi, absoluteUri, expanded);
}

private async Task<string> EvaluatePathAsync(string path, CancellationToken cancellationToken)
{
Request request = _pipeline.CreateRequest();
request.Method = RequestMethod.Get;
request.Uri = new RequestUriBuilder();
request.Uri.Reset(new Uri(path));

Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);

if (response.Status >= 200 && response.Status <= 299)
{
return await GetContentAsync(response.ContentStream, cancellationToken).ConfigureAwait(false);
}

return null;
azabbasi marked this conversation as resolved.
Show resolved Hide resolved
}

private static async Task<string> GetContentAsync(Stream content, CancellationToken cancellationToken)
{
using (JsonDocument json = await JsonDocument.ParseAsync(content, default, cancellationToken).ConfigureAwait(false))
{
JsonElement root = json.RootElement;
return root.GetRawText();
}
}

private static HttpPipeline CreatePipeline(ResolverClientOptions options)
{
return HttpPipelineBuilder.Build(options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;

namespace Azure.Iot.ModelsRepository
{
internal class ModelMetadata
{
public ModelMetadata(string id, IList<string> extends, IList<string> componentSchemas)
{
Id = id;
Extends = extends;
ComponentSchemas = componentSchemas;
}

public string Id { get; }
public IList<string> Extends { get; }
public IList<string> ComponentSchemas { get; }
public IList<string> Dependencies => Extends.Union(ComponentSchemas).ToList();
}
}
Loading