-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit - ModelsRepository Client (#20033)
- Loading branch information
Showing
94 changed files
with
5,625 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
19 changes: 19 additions & 0 deletions
19
sdk/modelsrepository/azure-iot-modelsrepository/CHANGELOG.md
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,19 @@ | ||
# Release History | ||
|
||
## 1.0.0-beta.1 (Unreleased) | ||
|
||
### New features | ||
|
||
- N/A | ||
|
||
### Breaking changes | ||
|
||
- N/A | ||
|
||
### Added | ||
|
||
- N/A | ||
|
||
### Fixes and improvements | ||
|
||
- N/A |
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,36 @@ | ||
# Azure IoT Models Repository client library for Java | ||
|
||
## Getting started | ||
|
||
The complete Microsoft Azure SDK can be downloaded from the [Microsoft Azure downloads][microsoft_sdk_download] page, and it ships with support for building deployment packages, integrating with tooling, rich command line tooling, and more. | ||
|
||
For the best development experience, developers should use the official Microsoft Maven packages for libraries. Maven packages will be regularly updated with new functionality and hotfixes. | ||
|
||
### Include the Package | ||
|
||
### Prerequisites | ||
|
||
### Authenticate the Client | ||
|
||
## Key concepts | ||
|
||
## Troubleshooting | ||
|
||
## Next steps | ||
|
||
## Contributing | ||
|
||
This project welcomes contributions and suggestions. | ||
Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. | ||
For details, visit <https://cla.microsoft.com.>. | ||
|
||
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). | ||
Simply follow the instructions provided by the bot. | ||
You will only need to do this once across all repos using our CLA. | ||
|
||
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. | ||
For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments. | ||
|
||
<!-- LINKS --> | ||
[microsoft_sdk_download]: https://azure.microsoft.com/downloads/?sdk=net | ||
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ |
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,14 @@ | ||
# Builds the azure-iot-modelsrepository project | ||
# The output will be in the target directory. | ||
# You can use the generated azure-iot-modelsrepository-x-sources.jar to upload to the API review tool at https://apiview.dev/ | ||
|
||
param ( | ||
[Parameter(Mandatory = $false)] | ||
[switch] $skipTests | ||
) | ||
|
||
if ($skipTests) { | ||
mvn install -DskipTests | ||
} else { | ||
mvn install | ||
} |
147 changes: 147 additions & 0 deletions
147
sdk/modelsrepository/azure-iot-modelsrepository/pom.xml
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,147 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-client-sdk-parent</artifactId> | ||
<version>1.7.0</version> <!-- {x-version-update;com.azure:azure-client-sdk-parent;current} --> | ||
<relativePath>../../parents/azure-client-sdk-parent</relativePath> | ||
</parent> | ||
|
||
<groupId>com.azure</groupId> | ||
<artifactId>azure-iot-modelsrepository</artifactId> | ||
<version>1.0.0-beta.1</version> <!-- {x-version-update;com.azure:azure-iot-modelsrepository;current} --> | ||
|
||
<name>Microsoft Azure client library for Models Repository</name> | ||
<description>This package contains the Microsoft Azure IoT ModelsRepository client library.</description> | ||
<url>https://github.com/Azure/azure-sdk-for-java</url> | ||
|
||
<distributionManagement> | ||
<site> | ||
<id>azure-java-build-docs</id> | ||
<url>${site.url}/site/${project.artifactId}</url> | ||
</site> | ||
</distributionManagement> | ||
|
||
<scm> | ||
<url>scm:git:https://github.com/Azure/azure-sdk-for-java</url> | ||
<connection>scm:git:git@github.com:Azure/azure-sdk-for-java.git</connection> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-core</artifactId> | ||
<version>1.13.0</version> <!-- {x-version-update;com.azure:azure-core;dependency} --> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-core-http-netty</artifactId> | ||
<version>1.8.0</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} --> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-core-test</artifactId> | ||
<version>1.5.3</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-identity</artifactId> | ||
<version>1.2.3</version> <!-- {x-version-update;com.azure:azure-identity;dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure</groupId> | ||
<artifactId>azure-core-http-okhttp</artifactId> | ||
<version>1.5.0</version> <!-- {x-version-update;com.azure:azure-core-http-okhttp;dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>5.6.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>5.6.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<version>5.6.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.16.1</version> <!-- {x-version-update;org.assertj:assertj-core;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
<version>1.3</version> <!-- {x-version-update;commons-cli:commons-cli;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<testResources> | ||
<testResource> | ||
<directory>src/samples/resources</directory> | ||
</testResource> | ||
<testResource> | ||
<directory>src/test/resources</directory> | ||
</testResource> | ||
</testResources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-enforcer-plugin;external_dependency} --> | ||
<configuration> | ||
<rules> | ||
<bannedDependencies> | ||
<includes> | ||
<include>com.fasterxml.jackson.core:jackson-annotations:[2.12.1]</include> <!-- {x-include-update;com.fasterxml.jackson.core:jackson-annotations;external_dependency} --> | ||
</includes> | ||
</bannedDependencies> | ||
</rules> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<profiles> | ||
<profile> | ||
<id>java-lts</id> | ||
<activation> | ||
<jdk>[11,)</jdk> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} --> | ||
<configuration> | ||
<argLine> | ||
--add-exports com.azure.core/com.azure.core.implementation.http=ALL-UNNAMED | ||
--add-opens com.azure.iot.modelsrepository/com.azure.iot.modelsrepository=ALL-UNNAMED | ||
</argLine> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
128 changes: 128 additions & 0 deletions
128
...re-iot-modelsrepository/src/main/java/com/azure/iot/modelsrepository/DtmiConventions.java
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,128 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.iot.modelsrepository; | ||
|
||
import com.azure.core.util.UrlBuilder; | ||
import com.azure.iot.modelsrepository.implementation.ModelsRepositoryConstants; | ||
import com.azure.iot.modelsrepository.implementation.StatusStrings; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Locale; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* DtmiConventions implements the core aspects of the IoT model repo conventions | ||
* which includes DTMI validation and calculating a URI path from a DTMI. | ||
*/ | ||
public final class DtmiConventions { | ||
|
||
private DtmiConventions() { } | ||
|
||
/** | ||
* A DTMI has three components: scheme, path, and version. | ||
* Scheme and path are separated by a colon. Path and version are separated by a semicolon i.e. scheme : path ; version. | ||
* The scheme is the string literal "dtmi" in lowercase. The path is a sequence of one or more segments, separated by colons. | ||
* The version is a sequence of one or more digits. Each path segment is a non-empty string containing only letters, digits, and undersc | ||
* The first character may not be a digit, and the last character may not be an underscore. | ||
* The version length is limited to nine digits, because the number 999,999,999 fits in a 32-bit signed integer value. | ||
* The first digit may not be zero, so there is no ambiguity regarding whether version 1 matches version 01 since the latter is invalid. | ||
*/ | ||
private static final Pattern VALID_DTMI_PATTERN = Pattern.compile("^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}$"); | ||
|
||
/** | ||
* Indicates whether a given string DTMI value is well-formed. | ||
* | ||
* @param dtmi DigitalTwin Model Id | ||
* @return True if dtmi has a valid format. False otherwise. | ||
*/ | ||
public static boolean isValidDtmi(String dtmi) { | ||
if (dtmi == null || dtmi.isEmpty()) { | ||
return false; | ||
} | ||
|
||
return VALID_DTMI_PATTERN.matcher(dtmi).matches(); | ||
} | ||
|
||
/** | ||
* Generates the model URI. | ||
* | ||
* @param dtmi DigitalTwin Model Id. | ||
* @param repositoryUri The repository uri | ||
* @param expanded Is model from precomputed values | ||
* @return The model uri | ||
* Will throw an {@link IllegalArgumentException} if the provided dtmi is not valid. | ||
*/ | ||
public static URI getModelUri(String dtmi, URI repositoryUri, boolean expanded) { | ||
String dtmiPath = dtmiToPath(dtmi); | ||
|
||
if (expanded) { | ||
dtmiPath = dtmiPath.replace(ModelsRepositoryConstants.JSON_EXTENSION, ModelsRepositoryConstants.JSON_EXPANDED_EXTENSION); | ||
} | ||
|
||
UrlBuilder urlBuilder = new UrlBuilder(); | ||
urlBuilder.setHost(repositoryUri.getHost()); | ||
urlBuilder.setScheme(repositoryUri.getScheme()); | ||
urlBuilder.setPath(repositoryUri.getPath()); | ||
urlBuilder.setQuery(repositoryUri.getQuery()); | ||
|
||
if (repositoryUri.getPort() > 0) { | ||
urlBuilder.setPort(repositoryUri.getPort()); | ||
} | ||
|
||
String path = urlBuilder.getPath(); | ||
|
||
if (path != null && !path.endsWith("/")) { | ||
urlBuilder.setPath(path + "/"); | ||
} | ||
|
||
if (urlBuilder.getPath() == null) { | ||
urlBuilder.setPath(dtmiPath); | ||
} else { | ||
urlBuilder.setPath(urlBuilder.getPath() + dtmiPath); | ||
} | ||
|
||
try { | ||
return new URI(urlBuilder.toString()); | ||
} catch (Exception e) { | ||
// No exceptions will be thrown as the input is a valid URI format. | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Converts a string to {@link URI} | ||
* | ||
* @param uri String format of the path | ||
* @return {@link URI} representation of the path/uri . | ||
*/ | ||
public static URI convertToUri(String uri) { | ||
try { | ||
return new URI(uri); | ||
} catch (URISyntaxException ex) { | ||
try { | ||
Path path = Paths.get(uri).normalize(); | ||
return new File(path.toAbsolutePath().toString()).toURI(); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalArgumentException("Invalid uri format", e); | ||
} | ||
} | ||
} | ||
|
||
static String dtmiToPath(String dtmi) { | ||
if (!isValidDtmi(dtmi)) { | ||
throw new IllegalArgumentException(String.format(StatusStrings.INVALID_DTMI_FORMAT_S, dtmi)); | ||
} | ||
|
||
return dtmi | ||
.toLowerCase(Locale.getDefault()) | ||
.replaceAll(":", "/") | ||
.replaceAll(";", "-") | ||
+ ModelsRepositoryConstants.JSON_EXTENSION; | ||
} | ||
} |
Oops, something went wrong.