Skip to content

Commit

Permalink
Merge pull request #747 from poojapatel23/chore/IRS-739_add_value_for…
Browse files Browse the repository at this point in the history
…_submodel_url_for_IRS_version_4.5.2

IRS-739_add_value_for_submodel_url_for_IRS_version_4.5.2
  • Loading branch information
ds-jhartmann authored Jul 5, 2024
2 parents 1d7c946 + 6ab3780 commit 500e366
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 84 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed submodel request path by introducing configuration property `irs-edc-client.submodel.submodel-suffix` which will
be appended to the href URL.
- Fixed validation of json-schemas - IRS is creating tombstone instead collecting Submodel payload, when it not passes validation of schema #522

## [4.5.2] - 2024-02-22
## Changed
- Updated IRS OpenAPI version to 4.5.2
Expand Down
1 change: 1 addition & 0 deletions charts/irs-helm/templates/configmap-spring-app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ data:
submodel:
request-ttl: {{ tpl .Values.edc.submodel.request.ttl . | default "PT10M" | quote }}
urn-prefix: {{ tpl (.Values.edc.submodel.urnprefix | default "/urn") . | quote }}
submodel-suffix: {{ tpl (.Values.edc.submodel.suffix | default "/$value") . | quote }}
catalog:
acceptedPolicies:
Expand Down
1 change: 1 addition & 0 deletions charts/irs-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ edc:
request:
ttl: PT10M # Requests to dataplane will time out after this duration (see https://en.wikipedia.org/wiki/ISO_8601#Durations)
urnprefix: /urn
suffix: /$value
catalog:
# IRS will only negotiate contracts for offers with a policy as defined in the allowedNames list.
# If a requested asset does not provide one of these policies, a tombstone will be created and this node will not be processed.
Expand Down
12 changes: 3 additions & 9 deletions irs-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,9 @@
<version>${irs-registry-client.version}</version>
</dependency>
<dependency>
<groupId>net.jimblackler</groupId>
<artifactId>jsonschemafriend</artifactId>
<version>${jsonschemafriend.version}</version>
<exclusions>
<exclusion>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</exclusion>
</exclusions>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${json-schema-validator.version}</version>
</dependency>
<!-- Update jsoup manually to avoid a vulnerability; can be removed after jsonschemafriend updates their dependency -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
********************************************************************************/
package org.eclipse.tractusx.irs.services.validation;

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

import com.networknt.schema.InputFormat;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaException;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import lombok.extern.slf4j.Slf4j;
import net.jimblackler.jsonschemafriend.GenerationException;
import net.jimblackler.jsonschemafriend.Schema;
import net.jimblackler.jsonschemafriend.SchemaStore;
import net.jimblackler.jsonschemafriend.Validator;
import org.eclipse.tractusx.irs.data.JsonParseException;
import org.eclipse.tractusx.irs.util.JsonUtil;
import org.springframework.stereotype.Service;

/**
Expand All @@ -41,17 +42,7 @@
@Service
@Slf4j
public class JsonValidatorService {
private final SchemaStore schemaStore = new SchemaStore();
private final JsonUtil parser;

/**
* Creates a new validation service
*
* @param parser the parser to use for the JSON
*/
public JsonValidatorService(final JsonUtil parser) {
this.parser = parser;
}
private final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);

/**
* Validate the payload against the schema.
Expand All @@ -61,17 +52,13 @@ public JsonValidatorService(final JsonUtil parser) {
* @return the validation result, containing the validation errors if applicable
*/
public ValidationResult validate(final String jsonSchema, final String jsonPayload) throws InvalidSchemaException {
log.debug("Trying to validate JSON ({}) with schema ({})", jsonPayload, jsonSchema);
log.trace("Trying to validate JSON ({}) with schema ({})", jsonPayload, jsonSchema);

final Schema schema = loadSchema(jsonSchema);
final JsonSchema schemaFromString = loadSchema(jsonSchema);

final Validator validator = new Validator();
try {
final Object payload = parser.fromString(jsonPayload, Object.class);

final List<String> errors = new ArrayList<>();
validator.validate(schema, payload, validationError -> errors.add(validationError.getMessage()));
return createValidationResult(errors);
final Set<ValidationMessage> errors = schemaFromString.validate(jsonPayload, InputFormat.JSON);
return createValidationResult(errors.stream().map(ValidationMessage::getMessage).toList());

} catch (final IllegalStateException | JsonParseException e) {
log.warn("Unable to validate JSON payload ({})", jsonPayload, e);
Expand All @@ -92,10 +79,10 @@ private ValidationResult createValidationResult(final List<String> errors) {
}
}

private Schema loadSchema(final String jsonSchema) throws InvalidSchemaException {
private JsonSchema loadSchema(final String jsonSchema) throws InvalidSchemaException {
try {
return schemaStore.loadSchemaJson(jsonSchema);
} catch (final GenerationException e) {
return factory.getSchema(jsonSchema);
} catch (final JsonSchemaException e) {
throw new InvalidSchemaException("Cannot load JSON schema for validation", e);
}
}
Expand Down
1 change: 1 addition & 0 deletions irs-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ irs-edc-client:
submodel:
request-ttl: ${EDC_SUBMODEL_REQUEST_TTL:PT10M} # How long to wait for an async EDC submodel retrieval to finish, ISO 8601 Duration
urn-prefix: ${EDC_SUBMODEL_URN_PREFIX:/urn} # A prefix used to identify URNs correctly in the submodel endpoint address
submodel-suffix: "/$value"
timeout:
read: PT90S # HTTP read timeout for the submodel client
connect: PT90S # HTTP connect timeout for the submodel client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.eclipse.tractusx.irs.testing.wiremock.SubmodelFacadeWiremockSupport;

public class WiremockSupport {
public static final String SUBMODEL_SUFFIX = "/\\$value";
public static EndpointDataReference createEndpointDataReference(final String contractAgreementId) {
final EDRAuthCode edrAuthCode = EDRAuthCode.builder()
.cid(contractAgreementId)
Expand Down Expand Up @@ -113,7 +114,7 @@ static void verifyNegotiationCalls(final int times) {
}

static void successfulDataRequests(final String assetId, final String fileName) {
stubFor(get(urlPathMatching(DtrWiremockSupport.DATAPLANE_PUBLIC_PATH + "/" + assetId)).willReturn(
stubFor(get(urlPathMatching(DtrWiremockSupport.DATAPLANE_PUBLIC_PATH + "/" + assetId+ SUBMODEL_SUFFIX)).willReturn(
responseWithStatus(200).withBodyFile(fileName)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
import java.nio.file.Path;
import java.util.Objects;

import org.eclipse.tractusx.irs.util.JsonUtil;
import org.junit.jupiter.api.Test;

class JsonValidatorServiceTest {

private final JsonValidatorService testee = new JsonValidatorService(new JsonUtil());
private final JsonValidatorService testee = new JsonValidatorService();

@Test
void shouldValidateAssemblyPartRelationship() throws Exception {
Expand Down
47 changes: 10 additions & 37 deletions irs-api/src/test/resources/__files/assemblyPartRelationship.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
{
"quantity": {
"quantityNumber": 0.11,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#kilogram",
"lexicalValue": "kilogram"
}
"measurementUnit": "unit:kilogram"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -17,10 +14,7 @@
{
"quantity": {
"quantityNumber": 0.1204,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#kilogram",
"lexicalValue": "kilogram"
}
"measurementUnit": "unit:kilogram"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -30,10 +24,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -43,10 +34,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -56,10 +44,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -79,7 +64,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": null
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -89,10 +74,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -102,10 +84,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -115,10 +94,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand All @@ -128,10 +104,7 @@
{
"quantity": {
"quantityNumber": 1,
"measurementUnit": {
"datatypeURI": "urn:bamm:io.openmanufacturing:meta-model:1.0.0#piece",
"lexicalValue": "piece"
}
"measurementUnit": "unit:piece"
},
"lifecycleContext": "AsBuilt",
"createdOn": "2022-02-03T14:48:54.709Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public static class SubmodelConfig {
private Duration requestTtl;

private String urnPrefix;

private String submodelSuffix;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ public class EdcSubmodelFacade {

private final EdcSubmodelClient client;

private final EdcConfiguration config;

@SuppressWarnings("PMD.PreserveStackTrace")
public SubmodelDescriptor getSubmodelPayload(final String connectorEndpoint, final String submodelDataplaneUrl,
final String assetId) throws EdcClientException {
try {
return client.getSubmodelPayload(connectorEndpoint, submodelDataplaneUrl, assetId).get();
final String fullSubmodelDataplaneUrl = submodelDataplaneUrl + config.getSubmodel().getSubmodelSuffix();
log.debug("Requesting Submodel for URL: '{}'", fullSubmodelDataplaneUrl);
return client.getSubmodelPayload(connectorEndpoint, fullSubmodelDataplaneUrl, assetId).get();
} catch (InterruptedException e) {
log.debug("InterruptedException occurred.", e);
Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.tractusx.irs.edc.client.exceptions.EdcClientException;
import org.eclipse.tractusx.irs.edc.client.model.SubmodelDescriptor;
import org.eclipse.tractusx.irs.edc.client.model.notification.EdcNotificationResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -58,6 +59,12 @@ class EdcSubmodelFacadeTest {
@Mock
private EdcSubmodelClient client;

private final EdcConfiguration config = new EdcConfiguration();

@BeforeEach
public void beforeEach() {
this.testee = new EdcSubmodelFacade(client, config);
}
@Nested
@DisplayName("getSubmodelRawPayload")
class GetSubmodelRawPayloadTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void setUp() {
final EdcSubmodelClient client = new EdcSubmodelClientImpl(config, negotiationService, dataPlaneClient,
endpointDataReferenceStorage,
pollingService, retryRegistry, catalogFacade, endpointDataReferenceCacheService);
testee = new EdcSubmodelFacade(client);
testee = new EdcSubmodelFacade(client, config);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public EndpointDataForConnectorsService endpointDataForConnectorsService(final E
}

@Bean
public EdcSubmodelFacade edcSubmodelFacade(final EdcSubmodelClient client) {
return new EdcSubmodelFacade(client);
public EdcSubmodelFacade edcSubmodelFacade(final EdcSubmodelClient client, final EdcConfiguration config) {
return new EdcSubmodelFacade(client, config);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.concurrent.ExecutionException;

import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.EdcSubmodelClient;
import org.eclipse.tractusx.irs.edc.client.EdcSubmodelFacade;
import org.eclipse.tractusx.irs.edc.client.exceptions.EdcClientException;
Expand All @@ -43,7 +44,7 @@
import org.springframework.web.client.RestTemplate;

class DefaultConfigurationTest {

private final EdcConfiguration edcConfiguration = new EdcConfiguration();
private final DefaultConfiguration testee = new DefaultConfiguration();
private final String descriptorTemplate = "descriptor/{aasIdentifier}";
private final String shellLookupTemplate = "shell?{assetIds}";
Expand All @@ -70,7 +71,7 @@ void decentralDigitalTwinRegistryService() {
@Test
void edcSubmodelFacade() {
final EdcSubmodelClient facadeMock = mock(EdcSubmodelClient.class);
final EdcSubmodelFacade edcSubmodelFacade = testee.edcSubmodelFacade(facadeMock);
final EdcSubmodelFacade edcSubmodelFacade = testee.edcSubmodelFacade(facadeMock,edcConfiguration);

assertThat(edcSubmodelFacade).isNotNull();
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<bc-jdk18on.version>1.76</bc-jdk18on.version>
<wiremock-standalone.version>3.3.1</wiremock-standalone.version>
<jsoup.version>1.16.1</jsoup.version>
<jsonschemafriend.version>0.12.0</jsonschemafriend.version>
<json-schema-validator.version>1.4.0</json-schema-validator.version>
<commons-io.version>2.14.0</commons-io.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<okhttp.version>4.10.0</okhttp.version>
Expand Down

0 comments on commit 500e366

Please sign in to comment.