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

feat | refactor code for correction semantic id in pcf submodel #175

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),


## [Unreleased]

### Added
- Correction in semanticId changes in pcf.
- EDC code changes refactor.

### Fixed
- Fix for PCF data sovereignty test

## [2.4.0] - 2024-05-14

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public interface PCFExchangeProxy {

@PutMapping
public ResponseEntity<Object> uploadPcfSubmodel(URI url, @RequestHeader Map<String, String> requestHeader,
@RequestParam(value = "BPN", required = true) String bpnNumber,
@RequestParam(value = "requestId", required = false) String requestId,
@RequestParam(value = "message", required = false) String message, @RequestBody JsonNode pcfData);

@GetMapping
public ResponseEntity<Object> getPcfByProduct(URI url, @RequestHeader Map<String, String> requestHeader,
@RequestParam(value = "BPN", required = true) String bpnNumber,
@RequestParam(value = "requestId", required = true) String requestId, @RequestParam String message);
@RequestParam(value = "requestId", required = true) String requestId,
@RequestParam(value = "message", required = false) String message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.tractusx.sde.common.exception.NoDataFoundException;
import org.eclipse.tractusx.sde.common.model.PagingResponse;
import org.eclipse.tractusx.sde.common.utils.JsonObjectUtility;
import org.eclipse.tractusx.sde.common.utils.LogUtil;
import org.eclipse.tractusx.sde.common.utils.PolicyOperationUtil;
import org.eclipse.tractusx.sde.pcfexchange.enums.PCFRequestStatusEnum;
import org.eclipse.tractusx.sde.pcfexchange.enums.PCFTypeEnum;
Expand All @@ -49,16 +50,19 @@ public class AsyncPushPCFDataForApproveRequest {

private final ProxyRequestInterface proxyRequestInterface;

public void pushPCFDataForApproveRequest(List<JsonObject> jsonObjectList, PolicyModel policy) {
public void pushPCFDataForApproveRequest(List<JsonObject> originalObjectList, PolicyModel policy) {

List<String> accessBPNList = PolicyOperationUtil.getAccessBPNList(policy);

List<String> productList = jsonObjectList.stream()
List<JsonObject> csvObjectList= originalObjectList.stream()
.map(obj-> obj.get("csv").getAsJsonObject())
.toList();

List<String> productList = csvObjectList.stream()
.map(ele -> JsonObjectUtility.getValueFromJsonObject(ele, PRODUCT_ID))
.toList();

markedPCFDataForPendingProviderRequestAsRequested(productList, jsonObjectList);
markedPCFDataForPendingProviderRequestAsRequested(productList, csvObjectList);

PagingResponse pcfData = pcfRepositoryService.getPcfData(
List.of(PCFRequestStatusEnum.PUSHED, PCFRequestStatusEnum.PUSHED_UPDATED_DATA), PCFTypeEnum.PROVIDER, 0,
Expand All @@ -76,7 +80,7 @@ public void pushPCFDataForApproveRequest(List<JsonObject> jsonObjectList, Policy
try {
request.setStatus(PCFRequestStatusEnum.PUSHING_UPDATED_DATA);

JsonObject calculatedPCFValue = jsonObjectList.stream()
JsonObject calculatedPCFValue = originalObjectList.stream()
.filter(ele -> {
ele = ele.get("csv").getAsJsonObject();
return request.getProductId().equals(JsonObjectUtility.getValueFromJsonObject(ele, PRODUCT_ID));
Expand All @@ -101,7 +105,7 @@ public void pushPCFDataForApproveRequest(List<JsonObject> jsonObjectList, Policy

} catch (NoDataFoundException e) {
msg = "Unable to take action on PCF request becasue pcf calculated value does not exist, please provide/upload PCF value for "
+ request.getProductId() + ", requestId " + request.getRequestId();
+ request.getProductId() + ", request Id " + request.getRequestId();
log.error("Async PushPCFDataForApproveRequest" + msg);
throw new NoDataFoundException(msg);
} catch (Exception e) {
Expand All @@ -116,10 +120,10 @@ public void pushPCFDataForApproveRequest(List<JsonObject> jsonObjectList, Policy
}

public void markedPCFDataForPendingProviderRequestAsRequested(List<String> productList,
List<JsonObject> jsonObjectList) {
List<JsonObject> csvObjectList) {

PagingResponse pcfData = pcfRepositoryService
.getPcfData(List.of(PCFRequestStatusEnum.PENDING_DATA_FROM_PROVIDER), PCFTypeEnum.PROVIDER, 0, 100000);
.getPcfData(List.of(PCFRequestStatusEnum.PENDING_DATA_FROM_PROVIDER), PCFTypeEnum.PROVIDER, 0, 1000000);
List<PcfRequestModel> requestList = (List<PcfRequestModel>) pcfData.getItems();

if (!requestList.isEmpty()) {
Expand All @@ -128,21 +132,22 @@ public void markedPCFDataForPendingProviderRequestAsRequested(List<String> produ
String msg = "";
try {

jsonObjectList.stream()
.filter(ele -> request.getProductId()
.equals(JsonObjectUtility.getValueFromJsonObject(ele, PRODUCT_ID)))
.findAny().orElseThrow(() -> new NoDataFoundException(
csvObjectList.stream()
.filter(ele -> request.getProductId().equals(JsonObjectUtility.getValueFromJsonObject(ele, PRODUCT_ID)))
.findAny()
.orElseThrow(() -> new NoDataFoundException(
"No data found for product_id " + request.getProductId()));

pcfRepositoryService.savePcfStatus(request.getRequestId(), PCFRequestStatusEnum.REQUESTED);

} catch (NoDataFoundException e) {
msg = "Unable to markedPCFDataForPendingProviderRequestAsRequested becasue pcf calculated value does not exist "
+ request.getProductId() + ", requestId " + request.getRequestId();
log.error("Async PushPCFDataForApproveRequest" + msg);
throw new NoDataFoundException(msg);
log.warn(LogUtil.encode(msg));
} catch (Exception e) {
pcfRepositoryService.savePcfStatus(request.getRequestId(), PCFRequestStatusEnum.FAILED);
msg = "Unable to markedPCFDataForPendingProviderRequestAsRequested for " + request.getProductId()
+ ", requestId " + request.getRequestId() + ", becasue " + e.getMessage();
log.error(LogUtil.encode(msg));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,26 @@ public String requestForPcfDataExistingOffer(String productId, ConsumerRequest c
false);
});

if (sb.indexOf("Unable to request") != -1)
throw new ValidationException(sb.toString());

return sb.toString();

}

@Override
public Object requestForPcfNotExistDataOffer(PcfRequestModel pcfRequestModel) {

StringBuilder sb = new StringBuilder();
String requestId = UUID.randomUUID().toString();
List<QueryDataOfferModel> pcfExchangeUrlOffers = null;
try {
pcfRepositoryService.savePcfRequestData(requestId, pcfRequestModel.getProductId(),
pcfRequestModel.getBpnNumber(), pcfRequestModel.getMessage(), PCFTypeEnum.CONSUMER,
PCFRequestStatusEnum.SENDING_REQUEST, "");

// 1 fetch EDC connectors and DTR Assets from EDC connectors
List<QueryDataOfferModel> pcfExchangeUrlOffers = edcAssetUrlCacheService
pcfExchangeUrlOffers = edcAssetUrlCacheService
.getPCFExchangeUrlFromTwin(pcfRequestModel.getBpnNumber());

// 2 request for PCF value for non existing sub model and send notification to
Expand All @@ -121,10 +126,13 @@ public Object requestForPcfNotExistDataOffer(PcfRequestModel pcfRequestModel) {
log.error("FeignException requestForPcfNotExistDataOffer: " + errorMsg);
}

if (sb.isEmpty())
throw new ValidationException("Not requested to provider for '" + pcfRequestModel.getProductId()
if (pcfExchangeUrlOffers == null || pcfExchangeUrlOffers.isEmpty())
throw new NoDataFoundException("Not requested to provider for '" + pcfRequestModel.getProductId()
+ "' because there is no PCF exchange endpoint found");

if (sb.indexOf("Unable to request") != -1)
throw new ValidationException(sb.toString());

return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.tractusx.sde.common.configuration.properties.SDEConfigurationProperties;
import org.eclipse.tractusx.sde.common.mapper.JsonObjectMapper;
import org.eclipse.tractusx.sde.common.utils.LogUtil;
import org.eclipse.tractusx.sde.edc.model.edr.EDRCachedByIdResponse;
import org.eclipse.tractusx.sde.edc.model.response.QueryDataOfferModel;
import org.eclipse.tractusx.sde.edc.util.EDCAssetUrlCacheService;
import org.eclipse.tractusx.sde.pcfexchange.enums.PCFRequestStatusEnum;
import org.eclipse.tractusx.sde.pcfexchange.proxy.PCFExchangeProxy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
Expand All @@ -53,46 +53,48 @@ public class ProxyRequestInterface {
private final PCFRepositoryService pcfRepositoryService;
private final EDCAssetUrlCacheService edcAssetUrlCacheService;
private final JsonObjectMapper jsonObjectMapper;

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

@Value(value = "${digital-twins.managed.thirdparty:false}")
private boolean managedThirdParty;
private final SDEConfigurationProperties sdeConfigurationProperties;

@SneakyThrows
public void requestToProviderForPCFValue(String productId, StringBuilder sb, String requestId, String message,
public void requestToProviderForPCFValue(String productId, StringBuilder reponseMap, String requestId, String message,
QueryDataOfferModel dataset, boolean isRequestToNonexistingTwin) {
try {
EDRCachedByIdResponse edrToken = edcAssetUrlCacheService.verifyAndGetToken(dataset.getConnectorId(),
dataset);

if (!reponseMap.isEmpty())
reponseMap.append("\n");

EDRCachedByIdResponse edrToken = edcAssetUrlCacheService.verifyAndGetToken(dataset.getConnectorId(), dataset);
if (edrToken != null) {

if (!sb.isEmpty())
sb.append("\n");
URI pcfpushEnpoint = null;

if (edrToken != null) {

URI pcfpushEnpoint = null;

if(isRequestToNonexistingTwin)
pcfpushEnpoint = new URI(
edrToken.getEndpoint() + SLASH_DELIMETER + PRODUCT_IDS + SLASH_DELIMETER + productId);
else
pcfpushEnpoint = new URI(edrToken.getEndpoint());

Map<String, String> header = new HashMap<>();
header.put("authorization", edrToken.getAuthorization());

// Send request to data provider for PCF value push
pcfExchangeProxy.getPcfByProduct(pcfpushEnpoint, header, manufacturerId,
requestId, message);

sb.append(productId + ": requested for PCF value");
pcfRepositoryService.savePcfStatus(requestId, PCFRequestStatusEnum.REQUESTED);
} else {
sb.append(productId + ": Unable to request for PCF value becasue the EDR token status is null");
log.warn(LogUtil.encode("EDC connector " + dataset.getConnectorOfferUrl() + ":"+ requestId +","+ productId +
"Unable to request for PCF value becasue the EDR token status is null"));
pcfRepositoryService.savePcfStatus(requestId, PCFRequestStatusEnum.FAILED);
if (isRequestToNonexistingTwin)
pcfpushEnpoint = new URI(
edrToken.getEndpoint() + SLASH_DELIMETER + PRODUCT_IDS + SLASH_DELIMETER + productId);
else
pcfpushEnpoint = new URI(edrToken.getEndpoint());

Map<String, String> header = new HashMap<>();
header.put("authorization", edrToken.getAuthorization());
header.put("Edc-Bpn", sdeConfigurationProperties.getManufacturerId());

// Send request to data provider for PCF value push
pcfExchangeProxy.getPcfByProduct(pcfpushEnpoint, header, requestId, message);
reponseMap.append("Successfully requested to provider '"+ dataset.getConnectorOfferUrl()+"' for '"+productId+"' product PCF value");
pcfRepositoryService.savePcfStatus(requestId, PCFRequestStatusEnum.REQUESTED);
} else {
String errorMsg= "Unable to request to provider '"+ dataset.getConnectorOfferUrl()+"' for '"+productId+"' product PCF value becasue the EDR token status is null";
log.error(LogUtil.encode(errorMsg));
pcfRepositoryService.savePcfStatus(requestId, PCFRequestStatusEnum.FAILED);
reponseMap.append(errorMsg);
}
} catch (FeignException e) {
log.error(LogUtil.encode("FeignRequest requestToProviderForPCFValue:" + e.request()));
String error= StringUtils.isBlank(e.contentUTF8()) ? e.getMessage() : e.contentUTF8();
String errorMsg= "Unable to request to provider '"+ dataset.getConnectorOfferUrl()+"' for '"+productId+"' product PCF value beacuse error in remote service execution";
log.error(LogUtil.encode("FeignException requestToProviderForPCFValue: " + errorMsg + ", because: " +error));
reponseMap.append(errorMsg);
}
}

Expand Down Expand Up @@ -135,8 +137,9 @@ private void sendNotification(JsonObject calculatedPCFValue, String productId, S

Map<String, String> header = new HashMap<>();
header.put("authorization", edrToken.getAuthorization());

pcfExchangeProxy.uploadPcfSubmodel(pcfpushEnpoint, header, bpnNumber, requestId, message,
header.put("Edc-Bpn", bpnNumber);

pcfExchangeProxy.uploadPcfSubmodel(pcfpushEnpoint, header, requestId, message,
jsonObjectMapper.gsonObjectToJsonNode(calculatedPCFValue));

sendNotificationStatus = "SUCCESS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ public String getsemanticIdOfModel() {
return this.submodelSchema.get("semantic_id").getAsString();
}

public String getSubmoduleUriPathOfSubmodule() {
public String getUriPathOfSubmodule() {
JsonElement jsonElement = this.submodelSchema.get("submodelUriPath");
return jsonElement == null || jsonElement.isJsonNull() ? "public" : jsonElement.getAsString();
}

public String getDataPlaneUrlOfSubmodule() {
JsonElement jsonElement = this.submodelSchema.get("submodelDataPlaneUrl");
return jsonElement == null || jsonElement.isJsonNull() ? "" : jsonElement.getAsString();
}

public JsonObject getAddOnOfModel() {
return this.submodelSchema.get("addOn").getAsJsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
********************************************************************************/
package org.eclipse.tractusx.sde.common.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -44,4 +45,21 @@ public static List<String> getAccessBPNList(PolicyModel policy) {
public static List<String> getUsageBPNList(PolicyModel policy) {
return getBPNList(policy.getUsagePolicies());
}

public static List<Policies> getStringPolicyAsPolicyList(String policyStr){

List<Policies> policies = new ArrayList<>();

if(StringUtils.isNotBlank(policyStr)) {
String[] split = policyStr.split(";");
for (int i = 0; i < split.length; i++) {
String[] split1 = split[i].split("@");
if (split1.length == 2) {
policies.add(Policies.builder().technicalKey(split1[0]).value(List.of(split1[1])).build());
}
}
}

return policies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.eclipse.tractusx.sde.common.configuration.properties.DigitalTwinConfigurationProperties;
import org.eclipse.tractusx.sde.common.entities.Policies;
import org.eclipse.tractusx.sde.common.entities.PolicyModel;
import org.eclipse.tractusx.sde.common.utils.PolicyOperationUtil;
import org.eclipse.tractusx.sde.common.utils.UUIdGenerator;
import org.eclipse.tractusx.sde.core.utils.ValueReplacerUtility;
import org.eclipse.tractusx.sde.edc.constants.EDCAssetConstant;
import org.eclipse.tractusx.sde.edc.constants.EDCAssetConfigurableConstant;
import org.eclipse.tractusx.sde.edc.entities.request.asset.AssetEntryRequest;
import org.eclipse.tractusx.sde.edc.entities.request.asset.AssetEntryRequestFactory;
import org.eclipse.tractusx.sde.edc.facilitator.CreateEDCAssetFacilator;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class DigitalTwinAssetProvider {
private final CreateEDCAssetFacilator createEDCAssetFacilator;
private final DigitalTwinConfigurationProperties digitalTwinConfigurationProperties;
private final ValueReplacerUtility valueReplacerUtility;
private final EDCAssetConfigurableConstant edcAssetConfigurableConstant;

@PostConstruct
@SneakyThrows
Expand All @@ -77,7 +80,7 @@ private void create(String registryType, String registryAPI) throws JsonProcessi
String assetId = UUIdGenerator.getUuid();

AssetEntryRequest assetEntryRequest = assetFactory.getAssetRequest("", "Digital twin registry information",
assetId, "1", "", "", "", EDCAssetConstant.DATA_CORE_DIGITAL_TWIN_REGISTRY_TYPE);
assetId, "1", "", "", "", edcAssetConfigurableConstant.getAssetPropTypeDigitalTwin());

String baseUrl = digitalTwinConfigurationProperties.getDigitalTwinsHostname() + registryAPI;

Expand Down Expand Up @@ -106,14 +109,22 @@ private void create(String registryType, String registryAPI) throws JsonProcessi
Map<String, String> inputData = new HashMap<>();
inputData.put("baseUrl", baseUrl);
inputData.put("registryType", registryType);
inputData.put("assetType", EDCAssetConstant.DATA_CORE_DIGITAL_TWIN_REGISTRY_TYPE);
inputData.put("assetType", edcAssetConfigurableConstant.getAssetPropTypeDigitalTwin());

ObjectNode requestBody = (ObjectNode) new ObjectMapper().readTree(valueReplacerUtility
.valueReplacerUsingFileTemplate("/edc_request_template/edc_asset_lookup.json", inputData));

if (!edcGateway.assetExistsLookupBasedOnType(requestBody)) {

PolicyModel policy = PolicyModel.builder().accessPolicies(List.of()).usagePolicies(List.of()).build();
List<Policies> accessPolicy = PolicyOperationUtil
.getStringPolicyAsPolicyList(edcAssetConfigurableConstant.getDigitalTwinExchangeAccessPolicy());

List<Policies> usagePolicy = PolicyOperationUtil
.getStringPolicyAsPolicyList(edcAssetConfigurableConstant.getDigitalTwinExchangeUsagePolicy());

PolicyModel policy = PolicyModel.builder().accessPolicies(accessPolicy)
.usagePolicies(usagePolicy)
.build();

Map<String, String> createEDCAsset = createEDCAssetFacilator.createEDCAsset(assetEntryRequest, policy);
log.info("Digital twin " + registryType + " asset creates :" + createEDCAsset.toString());
Expand Down
Loading
Loading