Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature/integ-aas4j…
Browse files Browse the repository at this point in the history
…-ser
  • Loading branch information
mdanish98 committed Sep 22, 2023
2 parents bbea994 + b0bcff0 commit 9ad0e44
Show file tree
Hide file tree
Showing 161 changed files with 5,810 additions and 1,174 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ local.properties
.settings/
.loadpath
.recommenders
META-INF/*
**/META-INF/*

# External tool builders
.externalToolBuilders/
Expand Down
10 changes: 7 additions & 3 deletions basyx.aasenvironment/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ The Aggregated Swagger UI for the endpoint is available at:
http://{host}:{port}/swagger-ui/index.html

For a configuration example, see [application.properties](./basyx.aasenvironment.component/src/main/resources/application.properties)
The Health Endpoint an
d CORS Documentation can be found [here](../docs/Readme.md).
The Health Endpoint and CORS Documentation can be found [here](../docs/Readme.md).

Right now, no additional input parameters modifying the output (e.g., cursor, serializationModifier) are supported.
## Preconfiguration of AAS Environments
The AAS Environment Component supports the preconfiguration of AAS Environments (e.g., XML, JSON, AASX) via the _basyx.environment_ parameter.

The feature supports both preconfiguring explicit files (e.g., file:myDevice.aasx) as well as directories (e.g., file:myDirectory) that will be recursively scanned for serialized environments.

For examples, see [application.properties](./basyx.aasenvironment.component/src/main/resources/application.properties)
92 changes: 76 additions & 16 deletions basyx.aasenvironment/basyx.aasenvironment-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -30,14 +28,43 @@
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasservice-core</artifactId>
<artifactId>basyx.submodelrepository-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasservice-core</artifactId>
<artifactId>basyx.submodelservice-core</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.submodelservice-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasservice-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.conceptdescriptionrepository-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasrepository-backend-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.submodelrepository-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasrepository-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.conceptdescriptionrepository-core</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>dataformat-json</artifactId>
Expand All @@ -55,21 +82,54 @@
<artifactId>dataformat-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.submodelservice-core</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>model</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.http</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<scope>test</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<usedDependencies>
<dependency>groupId:artifactId</dependency>
</usedDependencies>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasenvironment.preconfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXDeserializer;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlDeserializer;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.ConceptDescription;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.conceptdescriptionrepository.ConceptDescriptionRepository;
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.file.RecursiveDirectoryScanner;
import org.springframework.stereotype.Component;

/**
* Loader for AAS environment pre-configuration
*
* @author fried, mateusmolina, despen, witt, jungjan
*
*/
@Component
public class AasEnvironmentPreconfigurationLoader {

@Value("${basyx.environment:#{null}}")
private List<String> pathsToLoad;

private ResourceLoader resourceLoader;

@Autowired
public AasEnvironmentPreconfigurationLoader(ResourceLoader resourceLoader, List<String> pathsToLoad) {
this.resourceLoader = resourceLoader;
this.pathsToLoad = pathsToLoad;
}

public boolean shouldLoadPreconfiguredEnvironment() {
return pathsToLoad != null;
}

public void loadPreconfiguredEnvironment(AasRepository aasRepository, SubmodelRepository submodelRepository, ConceptDescriptionRepository conceptDescriptionRepository)
throws IOException, DeserializationException, InvalidFormatException {
List<File> files = resolveFiles(pathsToLoad);
for (File file : files) {
Environment environment = getEnvironmentFromFile(file);
loadEnvironmentFromFile(aasRepository, submodelRepository, conceptDescriptionRepository, environment);
}
}

private List<File> resolveFiles(List<String> paths) throws IOException {
ArrayList<File> files = new ArrayList<>();

for (String path : paths) {
resolvePathAndAddFilesToList(files, path);
}
return files;
}

private void resolvePathAndAddFilesToList(ArrayList<File> files, String path) throws IOException {
if (!getFile(path).isFile()) {
List<File> filesFromDir = extractFilesToLoadFromEnvironmentDirectory(path);
files.addAll(filesFromDir);
} else {
files.add(getFile(path));
}
}

private File getFile(String filePath) throws IOException {
return resourceLoader.getResource(filePath)
.getFile();
}

private void loadEnvironmentFromFile(AasRepository aasRepository, SubmodelRepository submodelRepository, ConceptDescriptionRepository conceptDescriptionRepository, Environment environment) {
if (isEnvironmentLoaded(environment)) {
createShellsOnRepositoryFromEnvironment(aasRepository, environment);
createSubmodelsOnRepositoryFromEnvironment(submodelRepository, environment);
createConceptDescriptionsOnRepositoryFromEnvironment(conceptDescriptionRepository, environment);
}
}

private List<File> extractFilesToLoadFromEnvironmentDirectory(String directoryToLoad) throws IllegalArgumentException, IOException {
File rootDirectory = getFile(directoryToLoad);
RecursiveDirectoryScanner directoryScanner = new RecursiveDirectoryScanner();

List<File> potentialEnvironments = directoryScanner.listFiles(rootDirectory);
return potentialEnvironments.stream()
.filter(file -> isAasxFile(file.getPath()) || isJsonFile(file.getPath()) || isXmlFile(file.getPath()))
.collect(Collectors.toList());
}

private void createConceptDescriptionsOnRepositoryFromEnvironment(ConceptDescriptionRepository conceptDescriptionRepository, Environment environment) {
for (ConceptDescription conceptDescription : environment.getConceptDescriptions()) {
conceptDescriptionRepository.createConceptDescription(conceptDescription);
}
}

private void createSubmodelsOnRepositoryFromEnvironment(SubmodelRepository submodelRepository, Environment environment) {
for (Submodel submodel : environment.getSubmodels()) {
submodelRepository.createSubmodel(submodel);
}
}

private void createShellsOnRepositoryFromEnvironment(AasRepository aasRepository, Environment environment) {
for (AssetAdministrationShell aas : environment.getAssetAdministrationShells()) {
aasRepository.createAas(aas);
}
}

private Environment getEnvironmentFromFile(File file) throws DeserializationException, InvalidFormatException, IOException {
Environment environment = null;
if (isJsonFile(file.getPath())) {
JsonDeserializer deserializer = new JsonDeserializer();
environment = deserializer.read(new FileInputStream(file));
} else if (isXmlFile(file.getPath())) {
XmlDeserializer deserializer = new XmlDeserializer();
environment = deserializer.read(new FileInputStream(file));
} else if (isAasxFile(file.getPath())) {
AASXDeserializer deserializer = new AASXDeserializer(new FileInputStream(file));
environment = deserializer.read();
}
return environment;
}

private static boolean isJsonFile(String filePath) {
return filePath.endsWith(".json");
}

private static boolean isXmlFile(String filePath) {
return filePath.endsWith(".xml");
}

private static boolean isAasxFile(String filePath) {
return filePath.endsWith(".aasx");
}

private boolean isEnvironmentLoaded(Environment environment) {
return environment != null;
}
}
Loading

0 comments on commit 9ad0e44

Please sign in to comment.