Skip to content

Commit

Permalink
Merge pull request #182 from catenax-ng/support_externalsubjectid
Browse files Browse the repository at this point in the history
Update shell for reflect specific assets IDs information
  • Loading branch information
adityagajbhiye9 authored Aug 28, 2023
2 parents 1c9d16c + b280023 commit 0da0ffa
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class DigitalTwinsFacilitator {

@Value(value = "${digital-twins.api:/api/v3.0}")
private String dtApiUri;

@Value(value = "${manufacturerId}")
public String manufacturerId;

Expand All @@ -63,7 +63,8 @@ public List<String> shellLookup(ShellLookupRequest request) throws ServiceExcept
}

@SneakyThrows
public List<String> shellLookupFromDDTR(ShellLookupRequest request, String ddtrUrl, String edcBPN) throws ServiceException {
public List<String> shellLookupFromDDTR(ShellLookupRequest request, String ddtrUrl, String edcBPN)
throws ServiceException {

URI dtURL = (ddtrUrl == null || ddtrUrl.length() <= 0) ? getDtURL(digitalTwinsHost) : getDtURL(ddtrUrl);

Expand Down Expand Up @@ -145,6 +146,18 @@ public ShellDescriptorResponse createShellDescriptor(ShellDescriptorRequest requ
return responseBody;
}

public void updateShellSpecificAssetIdentifiers(String shellId, List<Object> specificAssetIds) {

digitalTwinsFeignClient.deleteShellSpecificAttributes(getDtURL(digitalTwinsHost),
encodeShellIdBase64Utf8(shellId), manufacturerId);

ResponseEntity<List<Object>> registerSubmodel = digitalTwinsFeignClient.createShellSpecificAttributes(
getDtURL(digitalTwinsHost), encodeShellIdBase64Utf8(shellId), manufacturerId, specificAssetIds);
if (registerSubmodel.getStatusCode() != HttpStatus.CREATED) {
log.error("Error in shell SpecificAssetIdentifiers deletion: " + registerSubmodel.toString());
}
}

public void createSubModel(String shellId, CreateSubModelRequest request) {

request.setDescription(List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.eclipse.tractusx.sde.common.constants.CommonConstants.MANUFACTURER_PART_ID;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -124,31 +125,32 @@ public List<Endpoint> prepareDtEndpoint(String shellId, String submodelIdentific
}

@SneakyThrows
private ArrayList<Object> getSpecificAssetIds(Map<String, String> specificAssetIds, List<String> bpns) {
public List<Object> getSpecificAssetIds(Map<String, String> specificAssetIds, List<String> bpns) {

ArrayList<Object> specificIdentifiers = new ArrayList<>();
List<Object> specificIdentifiers = new ArrayList<>();

List<Keys> keyList = bpnKeyRefrence(bpns);

specificAssetIds.entrySet().stream().forEach(entry -> {

List<String> list = publicReadableSpecificAssetIDs.get(entry.getKey());
ExternalSubjectId externalSubjectId = null;

if (list != null && (list.contains("*") || list.contains(entry.getValue()))) {

externalSubjectId = ExternalSubjectId.builder()
.type("ExternalReference")
.keys(List.of(Keys.builder().type("GlobalReference").value(PUBLIC_READABLE).build()))
.build();
specificIdentifiers.add(new KeyValuePair(entry.getKey(), entry.getValue(), externalSubjectId));
}
else {
if (bpns!=null && !bpns.isEmpty()) {
for (String bpn : bpns) {
externalSubjectId = ExternalSubjectId.builder()
.type("ExternalReference")
.keys(List.of(Keys.builder().type("GlobalReference").value(bpn).build()))
.build();
specificIdentifiers.add(new KeyValuePair(entry.getKey(), entry.getValue(), externalSubjectId));
}
if (keyList != null && !keyList.isEmpty()) {

externalSubjectId = ExternalSubjectId.builder()
.type("ExternalReference").keys(keyList)
.build();
specificIdentifiers.add(new KeyValuePair(entry.getKey(), entry.getValue(), externalSubjectId));

} else {
Map<String, Object> map = new HashMap<>();
map.put("name", entry.getKey());
Expand All @@ -161,6 +163,15 @@ private ArrayList<Object> getSpecificAssetIds(Map<String, String> specificAssetI
return specificIdentifiers;
}

private List<Keys> bpnKeyRefrence(List<String> bpns) {
if (bpns != null && !(bpns.size() == 1 && bpns.contains(manufacturerId))) {
return bpns.stream()
.map(bpn -> Keys.builder().type("GlobalReference").value(bpn).build())
.toList();
}
return Collections.emptyList();
}

private String encodedUrl(String format) {
return format.replace(":", "%3A");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
package org.eclipse.tractusx.sde.digitaltwins.gateways.external;

import java.net.URI;
import java.util.List;

import org.eclipse.tractusx.sde.common.model.KeycloakJWTTokenResponse;
import org.eclipse.tractusx.sde.digitaltwins.entities.request.CreateSubModelRequest;
import org.eclipse.tractusx.sde.digitaltwins.entities.request.ShellDescriptorRequest;
import org.eclipse.tractusx.sde.digitaltwins.entities.request.ShellLookupQueryRequest;
import org.eclipse.tractusx.sde.digitaltwins.entities.response.ShellDescriptorResponse;
import org.eclipse.tractusx.sde.digitaltwins.entities.response.ShellLookupResponse;
import org.eclipse.tractusx.sde.digitaltwins.entities.response.SubModelListResponse;
Expand All @@ -46,33 +46,40 @@ public interface DigitalTwinsFeignClient {
@PostMapping
KeycloakJWTTokenResponse readAuthToken(URI url, @RequestBody MultiValueMap<String, Object> body);

@DeleteMapping(path = "/shell-descriptors/{aasIdentifier}/submodel-descriptors/{submodelIdentifier}")
ResponseEntity<Object> deleteSubmodelfromShellById(URI url, @PathVariable("aasIdentifier") String shellId,
@PathVariable("submodelIdentifier") String submodelIdentifier);

@PostMapping(path = "/shell-descriptors")
ResponseEntity<ShellDescriptorResponse> createShellDescriptor(URI url, @RequestBody ShellDescriptorRequest request);


@GetMapping(path = "/shell-descriptors/{aasIdentifier}")
ResponseEntity<ShellDescriptorResponse> getShellDescriptorByShellId(URI url,
@PathVariable("aasIdentifier") String shellId);

@DeleteMapping(path = "/shell-descriptors/{aasIdentifier}")
ResponseEntity<Void> deleteShell(URI url, @PathVariable("assetIds") String shellId);

@PostMapping(path = "/shell-descriptors/{aasIdentifier}/submodel-descriptors")
ResponseEntity<String> createSubModel(URI url, @PathVariable("aasIdentifier") String shellId,
@RequestBody CreateSubModelRequest request);

@GetMapping(path = "/shell-descriptors/{aasIdentifier}/submodel-descriptors")
ResponseEntity<SubModelListResponse> getSubModels(URI digitalTwinsHost,
@PathVariable("aasIdentifier") String shellId);

@DeleteMapping(path = "/shell-descriptors/{aasIdentifier}/submodel-descriptors/{submodelIdentifier}")
ResponseEntity<Object> deleteSubmodelfromShellById(URI url, @PathVariable("aasIdentifier") String shellId,
@PathVariable("submodelIdentifier") String submodelIdentifier);

@GetMapping(path = "/lookup/shells")
ResponseEntity<ShellLookupResponse> shellLookup(URI url, @RequestParam String assetIds,
@RequestHeader("Edc-Bpn") String edcBpn);

@DeleteMapping(path = "/lookup/shells/{assetIds}")
ResponseEntity<Void> deleteShell(URI url, @PathVariable("assetIds") String shellId);

@PostMapping(path = "/lookup/shells/query")
ResponseEntity<ShellLookupResponse> shellLookupByQuery(URI url, @RequestBody ShellLookupQueryRequest request);
@PostMapping(path = "/lookup/shells/{assetIds}")
ResponseEntity<List<Object>> createShellSpecificAttributes(URI url, @PathVariable("assetIds") String shellId,
@RequestHeader("Edc-Bpn") String edcBpn, @RequestBody List<Object> specificAssetIds);

@DeleteMapping(path = "/lookup/shells/{assetIds}")
ResponseEntity<Void> deleteShellSpecificAttributes(URI url, @PathVariable("assetIds") String shellId,
@RequestHeader("Edc-Bpn") String edcBpn);


}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ private Batch doRun(Batch batch) throws CsvHandlerDigitalTwinUseCaseException {
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);

digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(getSpecificAssetIds(batch), batch.getBpnNumbers()));

logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ private PartAsPlanned doRun(PartAsPlanned partAsPlannedAspect) throws CsvHandler
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);

digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(getSpecificAssetIds(partAsPlannedAspect), partAsPlannedAspect.getBpnNumbers()));

logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ private PartSiteInformationAsPlanned doRun(PartSiteInformationAsPlanned partSite
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);

digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(getSpecificAssetIds(partSiteInformationAsPlannedAspect),
partSiteInformationAsPlannedAspect.getBpnNumbers()));

logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ private Aspect doRun(Aspect aspect) throws CsvHandlerDigitalTwinUseCaseException
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);
digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(getSpecificAssetIds(aspect), aspect.getBpnNumbers()));
logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ private SingleLevelBoMAsPlanned doRun(SingleLevelBoMAsPlanned singleLevelBoMAsPl
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);

digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(
getSpecificAssetIdsForSingleLevel(singleLevelBoMAsPlannedAspect),
singleLevelBoMAsPlannedAspect.getBpnNumbers()));

logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down Expand Up @@ -145,14 +151,22 @@ private Map<String, String> getSpecificAssetIds(PartAsPlanned partAsPlannedAspec
}

private ShellLookupRequest getShellLookupRequest(SingleLevelBoMAsPlanned singleLevelBoMAsPlannedAspect) {

ShellLookupRequest shellLookupRequest = new ShellLookupRequest();
shellLookupRequest.addLocalIdentifier(CommonConstants.ASSET_LIFECYCLE_PHASE, CommonConstants.AS_PLANNED);
shellLookupRequest.addLocalIdentifier(CommonConstants.MANUFACTURER_PART_ID,
singleLevelBoMAsPlannedAspect.getParentManufacturerPartId());
shellLookupRequest.addLocalIdentifier(CommonConstants.MANUFACTURER_ID, digitalTwinsUtility.getManufacturerId());
getSpecificAssetIdsForSingleLevel(singleLevelBoMAsPlannedAspect).entrySet().stream()
.forEach(entry -> shellLookupRequest.addLocalIdentifier(entry.getKey(), entry.getValue()));

return shellLookupRequest;
}

private Map<String, String> getSpecificAssetIdsForSingleLevel(SingleLevelBoMAsPlanned singleLevelBoMAsPlannedAspect) {
Map<String, String> specificIdentifiers = new HashMap<>();
specificIdentifiers.put(CommonConstants.ASSET_LIFECYCLE_PHASE, CommonConstants.AS_PLANNED);
specificIdentifiers.put(CommonConstants.MANUFACTURER_PART_ID,
singleLevelBoMAsPlannedAspect.getParentManufacturerPartId());
specificIdentifiers.put(CommonConstants.MANUFACTURER_ID, digitalTwinsUtility.getManufacturerId());
return specificIdentifiers;
}

private ShellDescriptorRequest getShellDescriptorRequest(PartAsPlanned partAsPlannedAspect) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ private SingleLevelUsageAsBuilt doRun(SingleLevelUsageAsBuilt aspectSingleLevelU
} else if (shellIds.size() == 1) {
logDebug(String.format("Shell id found for '%s'", shellLookupRequest.toJsonString()));
shellId = shellIds.stream().findFirst().orElse(null);

digitalTwinsFacilitator.updateShellSpecificAssetIdentifiers(shellId,
digitalTwinsUtility.getSpecificAssetIds(
getSpecificAssetIdsForSingleLevel(aspectSingleLevelUsageAsBuilt),
aspectSingleLevelUsageAsBuilt.getBpnNumbers()));

logDebug(String.format("Shell id '%s'", shellId));
} else {
throw new CsvHandlerDigitalTwinUseCaseException(
Expand Down Expand Up @@ -143,18 +149,29 @@ private String createShellDescriptor(SingleLevelUsageAsBuilt aspectSingleLevelUs

private ShellLookupRequest getShellLookupRequest(SingleLevelUsageAsBuilt aspectSingleLevelUsageAsBuilt) {
ShellLookupRequest shellLookupRequest = new ShellLookupRequest();
shellLookupRequest.addLocalIdentifier(CommonConstants.PART_INSTANCE_ID,

getSpecificAssetIdsForSingleLevel(aspectSingleLevelUsageAsBuilt).entrySet().stream()
.forEach(entry -> shellLookupRequest.addLocalIdentifier(entry.getKey(), entry.getValue()));

return shellLookupRequest;
}

private Map<String, String> getSpecificAssetIdsForSingleLevel(
SingleLevelUsageAsBuilt aspectSingleLevelUsageAsBuilt) {

Map<String, String> specificIdentifiers = new HashMap<>();

specificIdentifiers.put(CommonConstants.PART_INSTANCE_ID,
aspectSingleLevelUsageAsBuilt.getParentPartInstanceId());
shellLookupRequest.addLocalIdentifier(CommonConstants.MANUFACTURER_PART_ID,
specificIdentifiers.put(CommonConstants.MANUFACTURER_PART_ID,
aspectSingleLevelUsageAsBuilt.getParentManufacturerPartId());
shellLookupRequest.addLocalIdentifier(CommonConstants.MANUFACTURER_ID, digitalTwinsUtility.getManufacturerId());
specificIdentifiers.put(CommonConstants.MANUFACTURER_ID, digitalTwinsUtility.getManufacturerId());

if (aspectSingleLevelUsageAsBuilt.hasOptionalParentIdentifier()) {
shellLookupRequest.addLocalIdentifier(aspectSingleLevelUsageAsBuilt.getParentOptionalIdentifierKey(),
specificIdentifiers.put(aspectSingleLevelUsageAsBuilt.getParentOptionalIdentifierKey(),
aspectSingleLevelUsageAsBuilt.getParentOptionalIdentifierValue());
}

return shellLookupRequest;
return specificIdentifiers;
}

private Map<String, String> getSpecificAssetIds(Aspect aspect) {
Expand Down

0 comments on commit 0da0ffa

Please sign in to comment.