Skip to content

Commit

Permalink
feat: first JSON-LD context for management api (#4256)
Browse files Browse the repository at this point in the history
* feat: first JSON-LD context for management api

* pr remarks

* chore: deps file

* chore: pr suggestions
  • Loading branch information
wolf4ood authored Jun 14, 2024
1 parent 40c67e8 commit aa431ac
Show file tree
Hide file tree
Showing 10 changed files with 993 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.3.6, A
maven/mavencentral/com.github.docker-java/docker-java-transport/3.3.6, Apache-2.0, approved, #7942
maven/mavencentral/com.github.java-json-tools/btf/1.3, Apache-2.0 OR LGPL-3.0-or-later, approved, #2721
maven/mavencentral/com.github.java-json-tools/jackson-coreutils-equivalence/1.0, LGPL-3.0 OR Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.java-json-tools/jackson-coreutils/2.0, Apache-2.0 OR LGPL-3.0-or-later, approved, #2719
maven/mavencentral/com.github.java-json-tools/jackson-coreutils/2.0, Apache-2.0 AND LGPL-2.1-or-later AND LGPL-3.0-only AND (Apache-2.0 AND GPL-1.0-or-later AND LGPL-3.0-only) AND Apache-2.0 AND LGPL-3.0-only, restricted, #15186
maven/mavencentral/com.github.java-json-tools/json-patch/1.13, Apache-2.0 OR LGPL-3.0-or-later, approved, CQ23929
maven/mavencentral/com.github.java-json-tools/json-schema-core/1.2.14, Apache-2.0 OR LGPL-3.0-or-later, approved, #2722
maven/mavencentral/com.github.java-json-tools/json-schema-validator/2.2.14, Apache-2.0 OR LGPL-3.0-or-later, approved, CQ20779
Expand Down Expand Up @@ -96,7 +96,7 @@ maven/mavencentral/commons-beanutils/commons-beanutils/1.8.3, Apache-2.0, approv
maven/mavencentral/commons-beanutils/commons-beanutils/1.9.4, Apache-2.0, approved, CQ12654
maven/mavencentral/commons-codec/commons-codec/1.11, Apache-2.0 AND BSD-3-Clause, approved, CQ15971
maven/mavencentral/commons-codec/commons-codec/1.15, Apache-2.0 AND BSD-3-Clause AND LicenseRef-Public-Domain, approved, CQ22641
maven/mavencentral/commons-collections/commons-collections/3.2.2, Apache-2.0, approved, CQ10385
maven/mavencentral/commons-collections/commons-collections/3.2.2, Apache-2.0, approved, #15185
maven/mavencentral/commons-io/commons-io/2.11.0, Apache-2.0, approved, CQ23745
maven/mavencentral/commons-logging/commons-logging/1.1.1, Apache-2.0, approved, CQ1907
maven/mavencentral/commons-logging/commons-logging/1.2, Apache-2.0, approved, CQ10162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private JsonValue createContext(String scope) {

// Compute the additional context IRI defined for * and the input scope
var contexts = Stream.concat(contextsForScope(JsonLd.DEFAULT_SCOPE), contextsForScope(scope))
.collect(Collectors.toSet());
.collect(Collectors.toCollection(LinkedHashSet::new));

var contextObject = builder.build();
// if not empty we build a JsonArray
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

plugins {
`java-library`
}

dependencies {
api(project(":spi:common:json-ld-spi"))

implementation(project(":core:common:lib:transform-lib"))
implementation(project(":core:control-plane:control-plane-transform"))

testImplementation(project(":spi:common:edr-store-spi"))
testImplementation(project(":core:common:connector-core"))
testImplementation(project(":core:control-plane:control-plane-transform"))
testImplementation(project(":core:common:junit"))
testImplementation(project(":core:common:lib:json-ld-lib"))
testImplementation(project(":extensions:common:json-ld"))
testImplementation(project(":extensions:common:http:jetty-core"))
testImplementation(project(":extensions:common:http:jersey-core"))
testImplementation(project(":extensions:common:api:management-api-configuration"))
testImplementation(project(":extensions:control-plane:api:management-api:contract-definition-api"))
testImplementation(project(":extensions:control-plane:api:management-api:contract-negotiation-api"))
testImplementation(project(":extensions:control-plane:api:management-api:policy-definition-api"))
testImplementation(project(":extensions:control-plane:api:management-api:transfer-process-api"))
testImplementation(project(":extensions:control-plane:api:management-api:secrets-api"))
testImplementation(project(":extensions:control-plane:api:management-api:edr-cache-api"))
}

edcBuild {
swagger {
apiGroup.set("management-api")
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.management.jsonld;

import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import static java.lang.String.format;

public class ManagementApiJsonLdContextExtension implements ServiceExtension {

public static final String EDC_CONNECTOR_MANAGEMENT_CONTEXT = "https://w3id.org/edc/connector/management/v0.0.1";
public static final String EDC_CONNECTOR_MANAGEMENT_SCOPE = "MANAGEMENT_API";

private static final String PREFIX = "document/";
private static final Map<String, String> FILES = Map.of(
EDC_CONNECTOR_MANAGEMENT_CONTEXT, PREFIX + "management-context-v1.jsonld");

@Inject
private JsonLd jsonLdService;

@Inject
private Monitor monitor;

@Override
public void initialize(ServiceExtensionContext context) {
jsonLdService.registerContext(EDC_CONNECTOR_MANAGEMENT_CONTEXT, EDC_CONNECTOR_MANAGEMENT_SCOPE);
FILES.entrySet().stream().map(this::mapToUri)
.forEach(result -> result.onSuccess(entry -> jsonLdService.registerCachedDocument(entry.getKey(), entry.getValue()))
.onFailure(failure -> monitor.warning("Failed to register cached json-ld document: " + failure.getFailureDetail())));
}

private Result<Map.Entry<String, URI>> mapToUri(Map.Entry<String, String> fileEntry) {
return getResourceUri(fileEntry.getValue())
.map(file1 -> Map.entry(fileEntry.getKey(), file1));
}

@NotNull
private Result<URI> getResourceUri(String name) {
var uri = getClass().getClassLoader().getResource(name);
if (uri == null) {
return Result.failure(format("Cannot find resource %s", name));
}

try {
return Result.success(uri.toURI());
} catch (URISyntaxException e) {
return Result.failure(format("Cannot read resource %s: %s", name, e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
#
# Contributors:
# Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
#
#

org.eclipse.edc.connector.api.management.jsonld.ManagementApiJsonLdContextExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"@context": {
"@version": 1.1,
"edc": "https://w3id.org/edc/v0.0.1/ns/",
"Asset": "edc:Asset",
"PolicyDefinition": "edc:PolicyDefinition",
"DataAddress": {
"@id": "edc:DataAddress",
"@context": {
"type": "edc:type"
}
},
"ContractDefinition": "edc:ContractDefinition",
"Criterion": "edc:Criterion",
"ContractRequest": "edc:ContractRequest",
"QuerySpec": "edc:QuerySpec",
"ContractNegotiation": {
"@id": "edc:ContractNegotiation",
"@context": {
"type": "edc:type"
}
},
"CallbackAddress": "edc:CallbackAddress",
"NegotiationState": "edc:NegotiationState",
"TerminateNegotiation": "edc:TerminateNegotiation",
"ContractAgreement": "edc:ContractAgreement",
"TransferRequest": "edc:TransferRequest",
"TransferState": "edc:TransferState",
"TransferProcess": {
"@id": "edc:TransferProcess",
"@context": {
"type": "edc:type"
}
},
"TerminateTransfer": "edc:TerminateTransfer",
"SuspendTransfer": "edc:SuspendTransfer",
"Secret": "edc:Secret",
"EndpointDataReferenceEntry": "edc:EndpointDataReferenceEntry",
"properties": {
"@id": "edc:properties",
"@context": {
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
}
},
"privateProperties": {
"@id": "edc:privateProperties",
"@context": {
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
}
},
"dataAddress": {
"@id": "edc:dataAddress",
"@context": {
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
}
},
"createdAt": "edc:createdAt",
"accessPolicyId": "edc:accessPolicyId",
"contractPolicyId": "edc:contractPolicyId",
"assetsSelector": {
"@id": "edc:assetsSelector",
"@container": "@set"
},
"operandLeft": "edc:operandLeft",
"operator": "edc:operator",
"operandRight": "edc:operandRight",
"limit": "edc:limit",
"offset": "edc:offset",
"filterExpression": {
"@id": "edc:filterExpression",
"@container": "@set"
},
"sortOrder": "edc:sortOrder",
"sortField": "edc:sortField",
"counterPartyAddress": "edc:counterPartyAddress",
"protocol": "edc:protocol",
"callbackAddresses": {
"@id": "edc:callbackAddresses",
"@container": "@set"
},
"providerId": "edc:providerId",
"policy": {
"@id": "edc:policy",
"@context": [
"http://www.w3.org/ns/odrl.jsonld",
{
"uid": null,
"type": null
}
]
},
"counterPartyId": "edc:counterPartyId",
"state": "edc:state",
"errorDetail": "edc:errorDetail",
"contractAgreementId": "edc:contractAgreementId",
"uri": "edc:uri",
"transactional": "edc:transactional",
"events": {
"@id": "edc:events",
"@container": "@set"
},
"reason": "edc:reason",
"assetId": "edc:assetId",
"consumerId": "edc:consumerId",
"contractSigningDate": "edc:contractSigningDate",
"contractId": "edc:contractId",
"dataDestination": "edc:dataDestination",
"correlationId": "edc:correlationId",
"stateTimestamp": "edc:stateTimestamp",
"value": "edc:value",
"transferProcessId": "edc:transferProcessId",
"contractNegotiationId": "edc:contractNegotiationId",
"agreementId": "edc:agreementId",
"inForceDate": "edc:inForceDate"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.connector.api.management.jsonld.serde;

import org.eclipse.edc.connector.api.management.jsonld.ManagementApiJsonLdContextExtension;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.eclipse.edc.connector.api.management.jsonld.ManagementApiJsonLdContextExtension.EDC_CONNECTOR_MANAGEMENT_CONTEXT;
import static org.eclipse.edc.connector.api.management.jsonld.ManagementApiJsonLdContextExtension.EDC_CONNECTOR_MANAGEMENT_SCOPE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@ExtendWith(DependencyInjectionExtension.class)
class ManagementApiJsonLdContextExtensionTest {

private final JsonLd jsonLd = mock();
private ManagementApiJsonLdContextExtension extension;

@BeforeEach
void setup(ServiceExtensionContext context) {
context.registerService(JsonLd.class, jsonLd);
}

@Test
void initialize(ManagementApiJsonLdContextExtension extension, ServiceExtensionContext context) {
extension.initialize(context);
verify(jsonLd).registerContext(EDC_CONNECTOR_MANAGEMENT_CONTEXT, EDC_CONNECTOR_MANAGEMENT_SCOPE);
}
}
Loading

0 comments on commit aa431ac

Please sign in to comment.