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

Enables directory to be scanned for preconfigured serialization #87

Merged
merged 18 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
87 changes: 45 additions & 42 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 @@ -66,7 +64,7 @@
<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 @@ -86,47 +84,52 @@
<dependency>
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>model</artifactId>
</dependency>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<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>

<dependency>
<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>
<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
Expand Up @@ -25,9 +25,12 @@

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
Expand All @@ -43,64 +46,91 @@
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.Resource;
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
* @author fried, mateusmolina, despen, witt, jungjan
*
*/
@Component
public class AasEnvironmentPreconfigurationLoader {

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

private ResourceLoader resourceLoader;

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

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

public void loadPreconfiguredEnvironment(AasRepository aasRepository, SubmodelRepository submodelRepository,
ConceptDescriptionRepository conceptDescriptionRepository)
throws IOException, DeserializationException, InvalidFormatException {
for (String filePath : filesToLoad) {

InputStream fileStream = getFileInputStream(filePath);
Environment environment = getEnvironmentFromInputStream(filePath, fileStream);
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 loadEnvironmentFromFile(AasRepository aasRepository, SubmodelRepository submodelRepository,
ConceptDescriptionRepository conceptDescriptionRepository, Environment environment) {
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 void createConceptDescriptionsOnRepositoryFromEnvironment(
ConceptDescriptionRepository conceptDescriptionRepository, Environment 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) {
private void createSubmodelsOnRepositoryFromEnvironment(SubmodelRepository submodelRepository, Environment environment) {
for (Submodel submodel : environment.getSubmodels()) {
submodelRepository.createSubmodel(submodel);
}
Expand All @@ -112,17 +142,16 @@ private void createShellsOnRepositoryFromEnvironment(AasRepository aasRepository
}
}

private Environment getEnvironmentFromInputStream(String filePath, InputStream fileStream)
throws DeserializationException, InvalidFormatException, IOException {
private Environment getEnvironmentFromFile(File file) throws DeserializationException, InvalidFormatException, IOException {
Environment environment = null;
if (isJsonFile(filePath)) {
if (isJsonFile(file.getPath())) {
JsonDeserializer deserializer = new JsonDeserializer();
environment = deserializer.read(fileStream);
} else if (isXmlFile(filePath)) {
environment = deserializer.read(new FileInputStream(file));
} else if (isXmlFile(file.getPath())) {
XmlDeserializer deserializer = new XmlDeserializer();
environment = deserializer.read(fileStream);
} else if (isAasxFile(filePath)) {
AASXDeserializer deserializer = new AASXDeserializer(fileStream);
environment = deserializer.read(new FileInputStream(file));
} else if (isAasxFile(file.getPath())) {
AASXDeserializer deserializer = new AASXDeserializer(new FileInputStream(file));
environment = deserializer.read();
}
return environment;
Expand All @@ -140,13 +169,7 @@ private static boolean isAasxFile(String filePath) {
return filePath.endsWith(".aasx");
}

private InputStream getFileInputStream(String filePath) throws IOException {
Resource resource = resourceLoader.getResource(filePath);
return resource.getInputStream();
}

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

}
9 changes: 9 additions & 0 deletions basyx.aasenvironment/basyx.aasenvironment-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>dataformat-json</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*******************************************************************************
* 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.basyx.digitaltwin.aasenvironment.http;

import java.util.ArrayList;
Expand Down Expand Up @@ -30,12 +55,13 @@ public class DummyAASEnvironmentComponent {
public static final String SUBMODEL_TECHNICAL_DATA_ID = "7A7104BDAB57E184";
public static final String SUBMODEL_OPERATIONAL_DATA_ID = "AC69B1CB44F07935";
public static final String CONCEPT_DESCRIPTION_ID_NOT_INCLUDED_IN_ENV = "IdNotToBeIncludedInSerializedEnv";

@Bean
public AasEnvironmentSerialization createAasEnvironment() {
SubmodelRepository submodelRepository = new InMemorySubmodelRepository(new InMemorySubmodelServiceFactory());
AasRepository aasRepository = new InMemoryAasRepository(new InMemoryAasServiceFactory());
ConceptDescriptionRepository conceptDescriptionRepository = new InMemoryConceptDescriptionRepository(TestAASEnvironmentSerialization.createDummyConceptDescriptions());

for (Submodel submodel : createDummySubmodels()) {
submodelRepository.createSubmodel(submodel);
}
Expand All @@ -55,11 +81,19 @@ private Collection<Submodel> createDummySubmodels() {
}

private Collection<AssetAdministrationShell> createDummyShells() {
AssetAdministrationShell shell1 = new DefaultAssetAdministrationShell.Builder().id(AAS_TECHNICAL_DATA_ID).idShort(AAS_TECHNICAL_DATA_ID)
.assetInformation(new DefaultAssetInformation.Builder().assetKind(AssetKind.INSTANCE).globalAssetID(SUBMODEL_TECHNICAL_DATA_ID).build()).build();
AssetAdministrationShell shell1 = new DefaultAssetAdministrationShell.Builder().id(AAS_TECHNICAL_DATA_ID)
.idShort(AAS_TECHNICAL_DATA_ID)
.assetInformation(new DefaultAssetInformation.Builder().assetKind(AssetKind.INSTANCE)
.globalAssetID(SUBMODEL_TECHNICAL_DATA_ID)
.build())
.build();

AssetAdministrationShell shell2 = new DefaultAssetAdministrationShell.Builder().id(AAS_OPERATIONAL_DATA_ID).idShort(AAS_OPERATIONAL_DATA_ID)
.assetInformation(new DefaultAssetInformation.Builder().assetKind(AssetKind.INSTANCE).globalAssetID(AAS_OPERATIONAL_DATA_ID).build()).build();
AssetAdministrationShell shell2 = new DefaultAssetAdministrationShell.Builder().id(AAS_OPERATIONAL_DATA_ID)
.idShort(AAS_OPERATIONAL_DATA_ID)
.assetInformation(new DefaultAssetInformation.Builder().assetKind(AssetKind.INSTANCE)
.globalAssetID(AAS_OPERATIONAL_DATA_ID)
.build())
.build();
Collection<AssetAdministrationShell> shells = new ArrayList<>();
shells.add(shell1);
shells.add(shell2);
Expand Down
Loading