Skip to content

Commit

Permalink
chore: upgrade to EDC 0.5.1 (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Feb 12, 2024
1 parent 2e12f40 commit 7e32c2d
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 391 deletions.
372 changes: 195 additions & 177 deletions DEPENDENCIES

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/edr-cache-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation(project(":spi:edr-spi"))

testImplementation(testFixtures(project(":spi:edr-spi")))
testImplementation(libs.edc.core.connector)

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
package org.eclipse.tractusx.edc.edr.core;

import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.tractusx.edc.edr.core.defaults.EdrCacheEntryPropertyLookup;
import org.eclipse.tractusx.edc.edr.core.defaults.InMemoryEndpointDataReferenceCache;
import org.eclipse.tractusx.edc.edr.spi.store.EndpointDataReferenceCache;

Expand All @@ -33,14 +37,23 @@ public class EdrCacheCoreExtension implements ServiceExtension {

static final String NAME = "EDR Cache Core";

@Inject
private CriterionOperatorRegistry operatorRegistry;

@Override
public String name() {
return NAME;
}


@Override
public void initialize(ServiceExtensionContext context) {
operatorRegistry.registerPropertyLookup(new EdrCacheEntryPropertyLookup());
}

@Provider(isDefault = true)
public EndpointDataReferenceCache edrCache() {
return new InMemoryEndpointDataReferenceCache();
return new InMemoryEndpointDataReferenceCache(operatorRegistry);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/********************************************************************************
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package org.eclipse.tractusx.edc.edr.core.defaults;

import org.eclipse.edc.spi.query.PropertyLookup;
import org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry;

/**
* This class is almost a 1:1 copy of the {@code CriterionToPredicateConverterImpl} (except for the {@code property()} method) from the {@code control-plane-core} module.
* Pulling in that module is not possible, because that would pull in almost the entire Control Plane
*/
public class EdrCacheEntryPropertyLookup implements PropertyLookup {

public static final String ASSET_ID = "assetId";
public static final String AGREEMENT_ID = "agreementId";
public static final String PROVIDER_ID = "providerId";
public static final String CONTRACT_NEGOTIATION_ID = "contractNegotiationId";
public static final String STATE = "state";

@Override
public Object getProperty(String key, Object object) {
return property(key, object);
}

protected Object property(String key, Object object) {
if (object instanceof EndpointDataReferenceEntry entry) {
return switch (key) {
case ASSET_ID -> entry.getAssetId();
case AGREEMENT_ID -> entry.getAgreementId();
case PROVIDER_ID -> entry.getProviderId();
case CONTRACT_NEGOTIATION_ID -> entry.getContractNegotiationId();
case STATE -> entry.getState();
default -> null;
};
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.edc.spi.entity.StatefulEntity;
import org.eclipse.edc.spi.persistence.Lease;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.spi.result.StoreResult;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
Expand Down Expand Up @@ -57,10 +58,8 @@
*/
public class InMemoryEndpointDataReferenceCache implements EndpointDataReferenceCache {
private static final long DEFAULT_LEASE_TIME_MILLIS = 60_000;
protected final CriterionOperatorRegistry criterionOperatorRegistry;
private final LockManager lockManager;
private final EdrCacheEntryPredicateConverter predicateConverter = new EdrCacheEntryPredicateConverter();


private final Map<String, List<EndpointDataReferenceEntry>> entriesByAssetId;

private final Map<String, EndpointDataReferenceEntry> entriesByEdrId;
Expand All @@ -72,11 +71,13 @@ public class InMemoryEndpointDataReferenceCache implements EndpointDataReference

private final Clock clock;

public InMemoryEndpointDataReferenceCache() {
this(UUID.randomUUID().toString(), Clock.systemUTC(), new ConcurrentHashMap<>());

public InMemoryEndpointDataReferenceCache(CriterionOperatorRegistry criterionOperatorRegistry) {
this(criterionOperatorRegistry, UUID.randomUUID().toString(), Clock.systemUTC(), new ConcurrentHashMap<>());
}

public InMemoryEndpointDataReferenceCache(String lockId, Clock clock, Map<String, Lease> leases) {
public InMemoryEndpointDataReferenceCache(CriterionOperatorRegistry criterionOperatorRegistry, String lockId, Clock clock, Map<String, Lease> leases) {
this.criterionOperatorRegistry = criterionOperatorRegistry;
this.lockId = lockId;
lockManager = new LockManager(new ReentrantReadWriteLock());
entriesByAssetId = new HashMap<>();
Expand All @@ -101,16 +102,6 @@ public StoreResult<EndpointDataReferenceEntry> findByIdAndLease(String transferP
});
}

@Override
public EndpointDataReferenceEntry findById(String correlationId) {
return findByIdAndLease(correlationId).orElse(storeFailure -> null);
}

@Override
public void save(EndpointDataReferenceEntry entity) {
throw new UnsupportedOperationException("Please use save(EndpointDataReferenceEntry, EndpointDataReference) instead!");
}

@Override
@NotNull
public List<EndpointDataReference> referencesForAsset(String assetId, String providerId) {
Expand Down Expand Up @@ -187,15 +178,24 @@ public StoreResult<EndpointDataReferenceEntry> deleteByTransferProcessId(String
});
}

@Override
public EndpointDataReferenceEntry findById(String correlationId) {
return findByIdAndLease(correlationId).orElse(storeFailure -> null);
}

@Override
public @NotNull List<EndpointDataReferenceEntry> nextNotLeased(int max, Criterion... criteria) {
return leaseAndGet(max, criteria);
}

@Override
public void save(EndpointDataReferenceEntry entity) {
throw new UnsupportedOperationException("Please use save(EndpointDataReferenceEntry, EndpointDataReference) instead!");
}

private @NotNull List<EndpointDataReferenceEntry> leaseAndGet(int max, Criterion... criteria) {
return lockManager.writeLock(() -> {
var filterPredicate = Arrays.stream(criteria).map(predicateConverter::convert).reduce(x -> true, Predicate::and);
var filterPredicate = Arrays.stream(criteria).map(criterionOperatorRegistry::toPredicate).reduce(x -> true, Predicate::and);
var entities = entriesByEdrId.values().stream()
.filter(filterPredicate)
.filter(e -> !isLeased(e.getId()))
Expand All @@ -210,7 +210,7 @@ public StoreResult<EndpointDataReferenceEntry> deleteByTransferProcessId(String
private Stream<EndpointDataReferenceEntry> filterBy(List<Criterion> criteria) {
return lockManager.readLock(() -> {
var predicate = criteria.stream()
.map(predicateConverter::convert)
.map(criterionOperatorRegistry::toPredicate)
.reduce(x -> true, Predicate::and);

return entriesByEdrId.values().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.eclipse.tractusx.edc.edr.core.defaults;

import org.eclipse.edc.connector.core.store.CriterionOperatorRegistryImpl;
import org.eclipse.edc.spi.persistence.Lease;
import org.eclipse.tractusx.edc.edr.spi.EndpointDataReferenceCacheTestBase;
import org.eclipse.tractusx.edc.edr.spi.store.EndpointDataReferenceCache;
Expand All @@ -34,7 +35,9 @@ class InMemoryEndpointDataReferenceCacheTest extends EndpointDataReferenceCacheT

@BeforeEach
void setUp() {
cache = new InMemoryEndpointDataReferenceCache(CONNECTOR_NAME, Clock.systemUTC(), leases);
var criterionOperatorRegistry = CriterionOperatorRegistryImpl.ofDefaults();
criterionOperatorRegistry.registerPropertyLookup(new EdrCacheEntryPropertyLookup());
cache = new InMemoryEndpointDataReferenceCache(criterionOperatorRegistry, CONNECTOR_NAME, Clock.systemUTC(), leases);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.iam.TokenParameters;
import org.eclipse.edc.spi.iam.RequestScope;

import java.util.Optional;
import java.util.function.BiFunction;

import static java.lang.String.format;
Expand All @@ -37,18 +36,12 @@ public class SummaryTokenPolicyFunction implements BiFunction<Policy, PolicyCont

@Override
public Boolean apply(Policy policy, PolicyContext context) {
var params = context.getContextData(TokenParameters.Builder.class);
if (params == null) {
throw new EdcException(format("%s not set in policy context", TokenParameters.Builder.class.getName()));
var scopes = context.getContextData(RequestScope.Builder.class);
if (scopes == null) {
throw new EdcException(format("%s not set in policy context", RequestScope.Builder.class.getName()));
}

var scope = params.build().getStringClaim("scope");

var newScope = Optional.ofNullable(scope)
.map(s -> s + " " + CX_SUMMARY_CREDENTIAL)
.orElse(CX_SUMMARY_CREDENTIAL);

params.claims("scope", newScope);
scopes.scope(CX_SUMMARY_CREDENTIAL);
return true;
}
}
Loading

0 comments on commit 7e32c2d

Please sign in to comment.