Skip to content

Commit

Permalink
refactoring: refactored asset reading from smart contract to use EDC …
Browse files Browse the repository at this point in the history
…native methods

Signed-off-by: Julian Legler <julian.legler@tu-berlin.de>
  • Loading branch information
JulianLegler committed Jul 31, 2024
1 parent 9435c42 commit c22f02f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,31 @@ public String getCatalog(FederatedCatalogCacheQuery federatedCatalogCacheQuery)

// iterate over all sources of contractDefinitionResponseDtoGroupedBySource and fetch all contracts for each source

var extendedDebugging = false;


monitor.debug("-------------------------------------------------");
if (extendedDebugging) monitor.debug("-------------------------------------------------");
for (Asset asset : assetList) {
monitor.debug("Asset: " + asset.getId());
monitor.debug("AssetName: " + asset.getName());
monitor.debug("AssetProperties: " + asset.getProperties().toString());
monitor.debug("AssetDataAddress: " + asset.getDataAddress().toString());
if (extendedDebugging) monitor.debug("Asset: " + asset.getId());
if (extendedDebugging) monitor.debug("AssetName: " + asset.getName());
if (extendedDebugging) monitor.debug("AssetProperties: " + asset.getProperties().toString());
if (extendedDebugging) monitor.debug("AssetDataAddress: " + asset.getDataAddress().toString());
}
for (PolicyDefinition policyDefinition : policyDefinitionList) {
monitor.debug("Policy: " + policyDefinition.getId());
monitor.debug("PolicyTarget: " + policyDefinition.getPolicy().getTarget());
if (extendedDebugging) monitor.debug("Policy: " + policyDefinition.getId());
if (extendedDebugging) monitor.debug("PolicyTarget: " + policyDefinition.getPolicy().getTarget());
}
for (ContractDefinition contractDefinition : contractDefinitionList) {
monitor.debug("Contract: " + contractDefinition.getId());
monitor.debug("ContractPolicyId: " + contractDefinition.getContractPolicyId());
monitor.debug("ContractCriteria: " + contractDefinition.getAccessPolicyId());
if (extendedDebugging) monitor.debug("Contract: " + contractDefinition.getId());
if (extendedDebugging) monitor.debug("ContractPolicyId: " + contractDefinition.getContractPolicyId());
if (extendedDebugging) monitor.debug("ContractCriteria: " + contractDefinition.getAccessPolicyId());
}


// iterate over all contracts for a source
assert contractDefinitionList != null;
for (ContractDefinition contract : contractDefinitionList) {

monitor.info(format("[%s] fetching contract %s", this.getClass().getSimpleName(), contract.getId()));
if (extendedDebugging) monitor.debug(format("[%s] fetching contract %s", this.getClass().getSimpleName(), contract.getId()));


// TODO: Refactor - connect everything together
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractOfferMessage;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
Expand All @@ -19,6 +20,8 @@
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Violation;
import org.eclipse.edc.web.spi.exception.InvalidRequestException;
import org.eclipse.edc.web.spi.exception.ValidationFailureException;

Expand Down Expand Up @@ -111,53 +114,6 @@ public ReturnObject sendToSmartContract(String jsonString, String smartContractU
return returnObject;
}



public Asset getAssetWithIdFromSmartContract(String id, String edcInterfaceUrl) {
Asset asset = null;
ObjectMapper mapper = new ObjectMapper();

HttpURLConnection c = null;
try {
URL u = new URL(edcInterfaceUrl + "/asset/" + id);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.connect();
int status = c.getResponseCode();

switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();

return mapper.readValue(sb.toString(), TokenziedAsset.class).getTokenDataAsAsset();
default:
return null;
}

} catch (IOException ex) {
System.out.println(ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
return null;
}

/** Get all contract definitions from the smart contract.
*
* @return List of ContractDefinitionResponseDto
Expand Down Expand Up @@ -231,7 +187,7 @@ public List<ContractDefinition> getAllContractDefinitionsFromSmartContract() {
continue;
}
var jsonContract = jsonLd.expand(tokenizedContract.getTokenData()).getContent();
monitor.debug("Expanded contract definition: " + jsonContract.toString());
//monitor.debug("Expanded contract definition: " + jsonContract.toString());
var contract = transformerRegistry.transform(jsonContract, ContractDefinition.class)
.orElseThrow(InvalidRequestException::new);

Expand Down Expand Up @@ -388,53 +344,16 @@ public HashMap<String, List<ContractOfferMessage>> getAllContractDefinitionsFrom
return null;
}

/*
public static PolicyDefinition getPolicyWithIdFromSmartContract(String id, String edcInterfaceUrl) {
PolicyDefinition policy = null;
ObjectMapper mapper = new ObjectMapper();
HttpURLConnection c = null;
try {
URL u = new URL(edcInterfaceUrl + "/policy/"+id);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
return mapper.readValue(sb.toString(), TokenizedPolicyDefinition.class).getTokenData();
}
} catch (MalformedURLException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
System.out.println(ex);
}
}
private ValidationResult isJsonObjectAnValidAsset(JsonObject assetAsJson) {
if (!assetAsJson.containsKey("@id")) {
monitor.warning("TokenizedAsset does not contain @id");
return ValidationResult.failure(new Violation("TokenizedAsset does not contain @id", "@id", assetAsJson));
}
return null;
}
monitor.debug("Validating asset: " + assetAsJson.getString("@id"));
// It is important to expand the json before validation as otherwise the exact paths in the json does not match the schema
return validatorRegistry.validate(Asset.EDC_ASSET_TYPE, assetAsJson);

*/
}

/** Get all policy definitions from the smart contract and group them by source.
*
Expand All @@ -459,84 +378,56 @@ public List<Asset> getAllAssetsFromSmartContract() {
c.connect();
int status = c.getResponseCode();

switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
if (status != 200 && status != 201) {
monitor.warning("Failed to fetch assets from edc-interface with status code: " + status);
return null;
}

//monitor.debug("Read from edc-interface: " + sb.toString() + " and going on to map it to a list of TokenizedObjects");
List<TokenziedAsset> assetList = mapper.readValue(sb.toString(), new TypeReference<List<TokenziedAsset>>() {});

/*
I first tried to use the normal transformer and validator but it didnt work and i dont know why
So I will just transform them manually, but its the inferior solution
*/
monitor.debug("Read " + assetList.size() + " assets from edc-interface and going on to validate them");
int failedCounter = 0;
for (TokenziedAsset tokenziedAsset : assetList) {
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();

if (tokenziedAsset == null || tokenziedAsset.tokenData == null) {
monitor.warning("TokenizedAsset is null? IPFS file to new?");
continue;
}
if (!tokenziedAsset.tokenData.containsKey("@id")) {
monitor.warning("TokenizedAsset does not contain @id");
continue;
}
if (!tokenziedAsset.tokenData.containsKey("@context")) {
monitor.warning("TokenziedAsset " + tokenziedAsset.tokenData.getString("@id") + " does not contain @context - Skipping");
continue;
}
/*
if(!tokenziedAsset.getTokenData().containsKey("@id")) {
monitor.warning("TokenizedAsset does not contain @id");
continue;
}
monitor.debug("Validating asset: " + tokenziedAsset.getTokenData());
try {
validatorRegistry.validate(EDC_ASSET_TYPE, tokenziedAsset.getTokenData()).orElseThrow(ValidationFailureException::new);
} catch (ValidationFailureException vex) {
monitor.warning("Validation failed for asset with message: " + vex.getMessage());
failedCounter++;
continue;
} catch (ClassCastException cce) {
monitor.warning("We ignore this exception as the validator seems to be buggy: " + cce.getMessage());
}
List<TokenziedAsset> assetList = mapper.readValue(sb.toString(), new TypeReference<>() {
});

*/
try {
var asset = transformerRegistry.transform(jsonLd.expand(tokenziedAsset.tokenData).getContent(), Asset.class)
.orElseThrow(InvalidRequestException::new);
assetResponseList.add(asset);
} catch (InvalidRequestException irex) {
monitor.warning("Transformation failed for asset " + tokenziedAsset.getTokenData().getString("@id") + " with message: " + irex.getMessage());
failedCounter++;
continue;
}
/*
try {
var asset = tokenziedAsset.getTokenDataAsAsset();
assetResponseList.add(asset);
} catch (IllegalArgumentException iae) {
monitor.warning("Transformation failed for asset with message: " + iae.getMessage());
failedCounter++;
continue;
}*/
}
monitor.debug("Read " + assetList.size() + " assets from edc-interface and going on to validate them");
int failedCounter = 0;
for (TokenziedAsset tokenziedAsset : assetList) {

monitor.debug("Validation failed for " + failedCounter + " assets and succeeded for " + assetResponseList.size() + " assets");
if (tokenziedAsset == null || tokenziedAsset.getTokenData(jsonLd) == null) {
monitor.warning("TokenizedAsset is null? IPFS file to new?");
failedCounter++;
continue;
}

var assetAsExpandedJson = tokenziedAsset.getTokenData(jsonLd);

ValidationResult result = isJsonObjectAnValidAsset(assetAsExpandedJson);

if (result.failed()) {
monitor.warning("Validation failed for asset with message in result: " + result.getFailureDetail());
failedCounter++;
continue;
}

try {
var asset = transformerRegistry.transform(jsonLd.expand(tokenziedAsset.getTokenData(jsonLd)).getContent(), Asset.class)
.orElseThrow(InvalidRequestException::new);
assetResponseList.add(asset);
} catch (InvalidRequestException irex) {
monitor.warning("Transformation failed for asset " + tokenziedAsset.getTokenData(jsonLd).getString("@id") + " with message: " + irex.getMessage());
failedCounter++;
continue;
}

return assetResponseList;
default:
return null;
}

monitor.debug("Validation failed for " + failedCounter + " assets and succeeded for " + assetResponseList.size() + " assets");

} catch (MalformedURLException ex) {
System.out.println(ex);
Expand All @@ -551,7 +442,7 @@ public List<Asset> getAllAssetsFromSmartContract() {
}
}
}
return null;
return assetResponseList;

}

Expand Down
Loading

0 comments on commit c22f02f

Please sign in to comment.