Skip to content

Commit

Permalink
Implements InMemory AASX file server (#84)
Browse files Browse the repository at this point in the history
* Implements InMemory AASX file server

- Adds core
- Adds backend-inmemory
- Adds component

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>

* Deletes Readme.md file

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>

* Refractors code

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>

* Improves formatting

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>

* Review comments changes

* Changes for review comments to use IOUtils for cheking the inputstream data

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>

* Changes for review comments

* Review comment changes

* Removing IOUtils

* Changes to AASXFileServerSuite

* Changes for Review Comments

* Changes for the review comments

* Applies BaSyx formatter and minor corrections

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Uncomment other modules from parent pom

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

* Resolves review remarks

Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>

---------

Signed-off-by: Chaithra Kandur Bhagavanthaiah <[email protected]>
Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
Co-authored-by: Mohammad Ghazanfar Ali Danish <[email protected]>
  • Loading branch information
KBChaithra and mdanish98 authored Nov 8, 2023
1 parent 6ed2f55 commit 664073b
Show file tree
Hide file tree
Showing 23 changed files with 1,531 additions and 3 deletions.
40 changes: 40 additions & 0 deletions basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<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>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasxfileserver</artifactId>
<version>${revision}</version>
</parent>

<artifactId>basyx.aasxfileserver-backend-inmemory</artifactId>
<name>AASX File Server Backend InMemory</name>

<dependencies>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasxfileserver-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasxfileserver-core</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*******************************************************************************
* 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.aasxfileserver;

import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import org.eclipse.digitaltwin.basyx.aasxfileserver.model.Package;
import org.eclipse.digitaltwin.basyx.aasxfileserver.model.PackageDescription;
import org.eclipse.digitaltwin.basyx.aasxfileserver.model.PackagesBody;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;

/**
* In-Memory implementation of the {@link AASXFileServer}
*
* @author chaithra
*
*/
public class InMemoryAASXFileServer implements AASXFileServer {

private Map<String, Package> packageMap = new LinkedHashMap<>();
private AtomicInteger packageId = new AtomicInteger(0);

private String aasxFileServerName;

/**
* Creates the InMemoryAASXFileServer
*
*/
public InMemoryAASXFileServer() {
}

/**
* Creates the InMemoryAASXFileServer
*
* @param aasxRepositoryName
* Name of the CDRepository
*/
public InMemoryAASXFileServer(String aasxFileServerName) {
this.aasxFileServerName = aasxFileServerName;
}

@Override
public Collection<PackageDescription> getAllAASXPackageIds(String shellId) {
Collection<PackageDescription> packageDescriptions = packageMap.values().stream().map(Package::getPackageDescription).collect(Collectors.toList());

if (shellId == null || shellId.isBlank())
return packageDescriptions;

return packageDescriptions.stream().filter(packageDesc -> containsShellId(packageDesc, shellId)).collect(Collectors.toList());
}

@Override
public InputStream getAASXByPackageId(String packageId) throws ElementDoesNotExistException {
throwIfAASXPackageIdDoesNotExist(packageId);

return packageMap.get(packageId).getPackagesBody().getFile();
}

@Override
public void updateAASXByPackageId(String packageId, List<String> shellIds, InputStream file, String filename) throws ElementDoesNotExistException {

throwIfAASXPackageIdDoesNotExist(packageId);

updateAASXPackage(packageId, shellIds, file, filename);
}

@Override
public PackageDescription createAASXPackage(List<String> shellIds, InputStream file, String fileName) {

String newpackageId = String.valueOf(packageId.incrementAndGet());

PackageDescription packageDescription = createPackageDescription(shellIds, newpackageId);

createPackage(shellIds, file, fileName, newpackageId, packageDescription);

return packageDescription;
}

@Override
public void deleteAASXByPackageId(String packageId) throws ElementDoesNotExistException {
throwIfAASXPackageIdDoesNotExist(packageId);

packageMap.remove(packageId);
}

@Override
public String getName() {
return aasxFileServerName == null ? AASXFileServer.super.getName() : aasxFileServerName;
}

private PackageDescription createPackageDescription(List<String> shellIds, String newPackageId) {
PackageDescription packageDescription = new PackageDescription();
packageDescription.packageId(newPackageId);
packageDescription.aasIds(shellIds);

return packageDescription;
}

private PackagesBody createPackagesBody(List<String> shellIds, InputStream file, String fileName) {
PackagesBody packagesBody = new PackagesBody();
packagesBody.aasIds(shellIds);
packagesBody.file(file);
packagesBody.fileName(fileName);

return packagesBody;
}

private void createPackage(List<String> shellIds, InputStream file, String fileName, String newPackageId, PackageDescription packageDescription) {
PackagesBody packagesBody = createPackagesBody(shellIds, file, fileName);

Package aasxPackage = new Package(newPackageId, packageDescription, packagesBody);

packageMap.put(newPackageId, aasxPackage);
}

private void updateAASXPackage(String packageId, List<String> shellIds, InputStream file, String filename) {
Package aasxPackage = this.packageMap.get(packageId);

updatePackagesBody(shellIds, file, filename, aasxPackage.getPackagesBody());

aasxPackage.getPackageDescription().setAasIds(shellIds);
}

private void updatePackagesBody(List<String> shellIds, InputStream file, String filename, PackagesBody packagesBody) {
packagesBody.setAasIds(shellIds);
packagesBody.setFileName(filename);
packagesBody.setFile(file);
}

private void throwIfAASXPackageIdDoesNotExist(String id) {

if (!packageMap.containsKey(id))
throw new ElementDoesNotExistException(id);
}

private boolean containsShellId(PackageDescription packageDesc, String shellId) {
return packageDesc.getAasIds().stream().anyMatch(aasId -> aasId.equals(shellId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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.aasxfileserver;

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

/**
* AASXFileServer factory returning an in-memory backend {@link AASXFileServer}
*
* @author schnicke, chaithra
*/
@Component
@ConditionalOnExpression("'${basyx.backend}'.equals('InMemory')")
public class InMemoryAASXFileServerFactory implements AASXFileServerFactory {

@Override
public AASXFileServer create() {
return new InMemoryAASXFileServer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* 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.aasxfileserver;

import static org.junit.Assert.assertEquals;

import org.eclipse.digitaltwin.basyx.aasxfileserver.core.AASXFileServerSuite;
import org.junit.Test;

/**
* Tests the {@link InMemoryAASXFileServer}
*
* @author chaithra
*
*/
public class TestInMemoryAASXFileServer extends AASXFileServerSuite {

private static final String CONFIGURED_AASX_SERVER_NAME = "configured-aasx-server-name";

@Override
protected AASXFileServer getAASXFileServer() {
return new InMemoryAASXFileServer();
}

@Test
public void getConfiguredInMemoryAASXFileServer() {
AASXFileServer server = new InMemoryAASXFileServer(CONFIGURED_AASX_SERVER_NAME);

assertEquals(CONFIGURED_AASX_SERVER_NAME, server.getName());
}

}
34 changes: 34 additions & 0 deletions basyx.aasxfileserver/basyx.aasxfileserver-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<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>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasxfileserver</artifactId>
<version>${revision}</version>
</parent>

<artifactId>basyx.aasxfileserver-core</artifactId>
<name>AASX File Server Core</name>

<dependencies>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.core</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>model</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Loading

0 comments on commit 664073b

Please sign in to comment.