-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: avoid potential unnecessary store accesses on dataset resol…
…ution (#4513) * refactor: avoid potential unnecessary store accesses on dataset resolution * pr remarks
- Loading branch information
Showing
16 changed files
with
512 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...n/java/org/eclipse/edc/connector/controlplane/catalog/ContractDefinitionResolverImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2024 Cofinity-X | ||
* | ||
* 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: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.connector.controlplane.catalog; | ||
|
||
import org.eclipse.edc.connector.controlplane.catalog.spi.ContractDefinitionResolver; | ||
import org.eclipse.edc.connector.controlplane.catalog.spi.ResolvedContractDefinitions; | ||
import org.eclipse.edc.connector.controlplane.contract.spi.offer.store.ContractDefinitionStore; | ||
import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition; | ||
import org.eclipse.edc.connector.controlplane.policy.spi.PolicyDefinition; | ||
import org.eclipse.edc.connector.controlplane.policy.spi.store.PolicyDefinitionStore; | ||
import org.eclipse.edc.policy.engine.spi.PolicyContextImpl; | ||
import org.eclipse.edc.policy.engine.spi.PolicyEngine; | ||
import org.eclipse.edc.policy.model.Policy; | ||
import org.eclipse.edc.spi.agent.ParticipantAgent; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.Result; | ||
|
||
import java.util.HashMap; | ||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
import static org.eclipse.edc.connector.controlplane.catalog.CatalogCoreExtension.CATALOG_SCOPE; | ||
|
||
/** | ||
* Determines the contract definitions applicable to a {@link ParticipantAgent} by evaluating the access control and | ||
* usage policies associated with a set of assets as defined by {@link ContractDefinition}s. On the distinction between | ||
* access control and usage policy, see {@link ContractDefinition}. | ||
*/ | ||
public class ContractDefinitionResolverImpl implements ContractDefinitionResolver { | ||
private final PolicyEngine policyEngine; | ||
private final PolicyDefinitionStore policyStore; | ||
private final ContractDefinitionStore definitionStore; | ||
|
||
public ContractDefinitionResolverImpl(ContractDefinitionStore contractDefinitionStore, PolicyEngine policyEngine, PolicyDefinitionStore policyStore) { | ||
definitionStore = contractDefinitionStore; | ||
this.policyEngine = policyEngine; | ||
this.policyStore = policyStore; | ||
} | ||
|
||
@Override | ||
public ResolvedContractDefinitions resolveFor(ParticipantAgent agent) { | ||
var policies = new HashMap<String, Policy>(); | ||
var definitions = definitionStore.findAll(QuerySpec.max()) | ||
.filter(definition -> { | ||
var policyContext = PolicyContextImpl.Builder.newInstance().additional(ParticipantAgent.class, agent).build(); | ||
var accessResult = Optional.of(definition.getAccessPolicyId()) | ||
.map(policyId -> policies.computeIfAbsent(policyId, | ||
key -> Optional.ofNullable(policyStore.findById(key)) | ||
.map(PolicyDefinition::getPolicy) | ||
.orElse(null)) | ||
) | ||
.map(policy -> policyEngine.evaluate(CATALOG_SCOPE, policy, policyContext)) | ||
.orElse(Result.failure(format("Policy %s not found", definition.getAccessPolicyId()))); | ||
|
||
return accessResult.succeeded(); | ||
}) | ||
.toList(); | ||
|
||
return new ResolvedContractDefinitions(definitions, policies); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.