Skip to content

Commit

Permalink
Fixes AASX upload/preconfig ignoring thumbnail (#259)
Browse files Browse the repository at this point in the history
* Fixes AASX upload/preconfig ignoring thumbnail

Signed-off-by: Mohammad Ghazanfar Ali Danish <ghazanfar.danish@iese.fraunhofer.de>

* Updates RBAC rules for thumbnails

Signed-off-by: Mohammad Ghazanfar Ali Danish <ghazanfar.danish@iese.fraunhofer.de>

* Addresses review remarks

Signed-off-by: Mohammad Ghazanfar Ali Danish <ghazanfar.danish@iese.fraunhofer.de>

---------

Signed-off-by: Mohammad Ghazanfar Ali Danish <ghazanfar.danish@iese.fraunhofer.de>
  • Loading branch information
mdanish98 committed Apr 11, 2024
1 parent 3ec6d72 commit 5d66131
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.Resource;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEnvironment;
import org.eclipse.digitaltwin.basyx.aasenvironment.AasEnvironment;
import org.eclipse.digitaltwin.basyx.aasenvironment.ConceptDescriptionIdCollector;
Expand Down Expand Up @@ -113,13 +114,14 @@ public byte[] createAASXAASEnvironmentSerialization(@Valid List<String> aasIds,

public void loadEnvironment(CompleteEnvironment completeEnvironment) {
Environment environment = completeEnvironment.getEnvironment();
List<InMemoryFile> relatedFiles = completeEnvironment.getRelatedFiles();

if (environment == null)
return;

checker.assertNoDuplicateIds(environment);

createShellsOnRepositoryFromEnvironment(environment);
createShellsOnRepositoryFromEnvironment(environment, relatedFiles);
createSubmodelsOnRepositoryFromEnvironment(environment, completeEnvironment.getRelatedFiles());
createConceptDescriptionsOnRepositoryFromEnvironment(environment);
}
Expand Down Expand Up @@ -179,15 +181,55 @@ private InMemoryFile getAssociatedInMemoryFile(List<InMemoryFile> relatedFiles,
return inMemoryFile.get();
}

private void createShellsOnRepositoryFromEnvironment(Environment environment) {
private void createShellsOnRepositoryFromEnvironment(Environment environment, List<InMemoryFile> relatedFiles) {
IdentifiableRepository<AssetAdministrationShell> repo = new DelegatingIdentifiableRepository<AssetAdministrationShell>(aasRepository::getAas, aasRepository::updateAas, aasRepository::createAas);
IdentifiableUploader<AssetAdministrationShell> uploader = new IdentifiableUploader<>(repo);
for (AssetAdministrationShell shell : environment.getAssetAdministrationShells()) {
boolean success = uploader.upload(shell);
setThumbnail(shell.getId(), relatedFiles);
logSuccess("shell", shell.getId(), success);
}
}

private void setThumbnail(String shellId, List<InMemoryFile> relatedFiles) {
if (relatedFiles == null || relatedFiles.isEmpty())
return;

Resource thumbnailResource = aasRepository.getAssetInformation(shellId).getDefaultThumbnail();

if (thumbnailResource == null || !isValidPath(thumbnailResource.getPath()) || !isValidContentType(thumbnailResource.getContentType())) {
logger.info("Could not find thumbnail resource for aas {}", shellId);
return;
}

String thumbnailPath = thumbnailResource.getPath();
String thumbnailContentType = thumbnailResource.getContentType();

Optional<InMemoryFile> optionalInMemoryFile = relatedFiles.stream().filter(file -> file.getPath().equals(thumbnailPath)).findAny();

if (optionalInMemoryFile.isEmpty()) {
logger.info("Thumbnail file specified at path {} for aas {} could not be found.", thumbnailPath, shellId);
return;
}

byte[] thumbnailContent = optionalInMemoryFile.get().getFileContent();

if (thumbnailContent.length == 0) {
logger.info("Thumbnail content for aas {} is empty.", thumbnailPath);
return;
}

aasRepository.setThumbnail(shellId, getFileName(thumbnailPath), thumbnailContentType, new ByteArrayInputStream(thumbnailContent));
}

private boolean isValidContentType(String contentType) {
return contentType != null && !contentType.isBlank();
}

private boolean isValidPath(String path) {
return path != null && !path.isBlank();
}

private void createSubmodelsOnRepository(List<Submodel> submodels) {
IdentifiableRepository<Submodel> repo = new DelegatingIdentifiableRepository<Submodel>(submodelRepository::getSubmodel, submodelRepository::updateSubmodel, submodelRepository::createSubmodel);
IdentifiableUploader<Submodel> uploader = new IdentifiableUploader<>(repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@
"aasId": "*"
}
},
{
"role": "basyx-uploader",
"action": "UPDATE",
"targetInformation": {
"@type": "aas",
"aasId": "*"
}
},
{
"role": "basyx-uploader",
"action": "READ",
Expand Down Expand Up @@ -493,6 +501,14 @@
"aasId": "http://customer.com/aas/9175_7013_7091_9168"
}
},
{
"role": "basyx-uploader-two",
"action": "UPDATE",
"targetInformation": {
"@type": "aas",
"aasId": "http://customer.com/aas/9175_7013_7091_9168"
}
},
{
"role": "basyx-uploader-two",
"action": "READ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -141,6 +142,16 @@ public void aasxFromDirectoryLoadedSubmodelsInRepository() {
assertNotNull(submodel2);
}

@Test
public void getThumbnail() throws IOException {
File file = aasRepo.getThumbnail("http://customer.com/aas/9175_7013_7091_9168");

InputStream expectedContent = getClass().getClassLoader().getResourceAsStream("testFiles/verwaltungsschale-detail-part1.png");
InputStream actualContent = new FileInputStream(file);

assertTrue(IOUtils.contentEquals(expectedContent, actualContent));
}

@Test
public void jsonFromDirectoryLoadedShellsInRepository() {
AssetAdministrationShell shell1 = aasRepo.getAas("technical-data-shell-id-json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResult;
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResultPagingMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -186,14 +187,10 @@ public ResponseEntity<Void> deleteThumbnailAasRepository(Base64UrlEncodedIdentif

@Override
public ResponseEntity<Resource> getThumbnailAasRepository(Base64UrlEncodedIdentifier aasIdentifier) {
try {
FileInputStream fileInputStream = new FileInputStream(aasRepository.getThumbnail(aasIdentifier.getIdentifier()));
Resource resource = new InputStreamResource(fileInputStream);
return new ResponseEntity<Resource>(resource, HttpStatus.OK);
} catch (FileNotFoundException e) {
return new ResponseEntity<Resource>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Resource resource = new FileSystemResource(aasRepository.getThumbnail(aasIdentifier.getIdentifier()));

return new ResponseEntity<>(resource, HttpStatus.OK);
}

@Override
public ResponseEntity<Void> putThumbnailAasRepository(Base64UrlEncodedIdentifier aasIdentifier, String fileName, @Valid MultipartFile file) {
Expand Down

0 comments on commit 5d66131

Please sign in to comment.