Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add private properties to policy definition entity #3571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;

import java.util.Map;

import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;


@Extension(value = PolicyDefinitionApiExtension.NAME)
Expand All @@ -53,6 +55,9 @@ public class PolicyDefinitionApiExtension implements ServiceExtension {
@Inject
private JsonObjectValidatorRegistry validatorRegistry;

@Inject
private TypeManager typeManager;

@Override
public String name() {
return NAME;
Expand All @@ -62,7 +67,8 @@ public String name() {
public void initialize(ServiceExtensionContext context) {
var jsonBuilderFactory = Json.createBuilderFactory(Map.of());
transformerRegistry.register(new JsonObjectToPolicyDefinitionTransformer());
transformerRegistry.register(new JsonObjectFromPolicyDefinitionTransformer(jsonBuilderFactory));
var mapper = typeManager.getMapper(JSON_LD);
transformerRegistry.register(new JsonObjectFromPolicyDefinitionTransformer(jsonBuilderFactory, mapper));

validatorRegistry.register(EDC_POLICY_DEFINITION_TYPE, PolicyDefinitionValidator.instance());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.eclipse.edc.connector.api.management.policy.transform;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.policy.spi.PolicyDefinition;
Expand All @@ -29,11 +30,13 @@

public class JsonObjectFromPolicyDefinitionTransformer extends AbstractJsonLdTransformer<PolicyDefinition, JsonObject> {

private final ObjectMapper mapper;
private final JsonBuilderFactory jsonFactory;

public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory) {
public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory, ObjectMapper jsonLdMapper) {
super(PolicyDefinition.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = jsonLdMapper;
}

@Override
Expand All @@ -46,6 +49,11 @@ public JsonObjectFromPolicyDefinitionTransformer(JsonBuilderFactory jsonFactory)

var policy = context.transform(input.getPolicy(), JsonObject.class);
objectBuilder.add(EDC_POLICY_DEFINITION_POLICY, policy);
if (!input.getPrivateProperties().isEmpty()) {
var privatePropBuilder = jsonFactory.createObjectBuilder();
transformProperties(input.getPrivateProperties(), privatePropBuilder, mapper, context);
objectBuilder.add(PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES, privatePropBuilder);
}

return objectBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_POLICY;
import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES;

public class JsonObjectToPolicyDefinitionTransformer extends AbstractJsonLdTransformer<JsonObject, PolicyDefinition> {

Expand All @@ -42,6 +43,11 @@ public JsonObjectToPolicyDefinitionTransformer() {
private void transformProperty(String key, JsonValue value, PolicyDefinition.Builder builder, TransformerContext context) {
if (key.equals(EDC_POLICY_DEFINITION_POLICY)) {
transformArrayOrObject(value, Policy.class, builder::policy, context);
} else if (key.equals(EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES)) {
var props = value.asJsonArray().getJsonObject(0);
visitProperties(props, (k, val) -> transformProperty(k, val, builder, context));
} else {
builder.privateProperty(key, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.eclipse.edc.connector.policy.spi.PolicyDefinition.EDC_POLICY_DEFINITION_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -35,7 +36,7 @@

class JsonObjectFromPolicyDefinitionTransformerTest {

private final JsonObjectFromPolicyDefinitionTransformer transformer = new JsonObjectFromPolicyDefinitionTransformer(Json.createBuilderFactory(emptyMap()));
private final JsonObjectFromPolicyDefinitionTransformer transformer = new JsonObjectFromPolicyDefinitionTransformer(Json.createBuilderFactory(emptyMap()), createObjectMapper());
private final TransformerContext context = mock(TransformerContext.class);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ entity edc_policydefinitions {
target: string
type: string
extensible_properties: string <<json>>
private_properties: string <<json>>
}
@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CREATE TABLE IF NOT EXISTS edc_policydefinitions
assignee VARCHAR,
target VARCHAR,
policy_type VARCHAR NOT NULL,
private_properties JSON,
PRIMARY KEY (policy_id)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public String getInsertTemplate() {
.column(getTargetColumn())
.column(getTypeColumn())
.column(getCreatedAtColumn())
.jsonColumn(getPrivatePropertiesColumn())
.insertInto(getPolicyTable());
}

Expand All @@ -55,6 +56,7 @@ public String getUpdateTemplate() {
.column(getAssigneeColumn())
.column(getTargetColumn())
.column(getTypeColumn())
.jsonColumn(getPrivatePropertiesColumn())
.update(getPolicyTable(), getPolicyIdColumn());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@ default String getCreatedAtColumn() {
return "created_at";
}

default String getPrivatePropertiesColumn() {
return "private_properties";
}

SqlQueryStatement createQuery(QuerySpec querySpec);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.spi.entity.Entity;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -39,7 +42,9 @@
public class PolicyDefinition extends Entity {
public static final String EDC_POLICY_DEFINITION_TYPE = EDC_NAMESPACE + "PolicyDefinition";
public static final String EDC_POLICY_DEFINITION_POLICY = EDC_NAMESPACE + "policy";
public static final String EDC_POLICY_DEFINITION_PRIVATE_PROPERTIES = EDC_NAMESPACE + "privateProperties";
private Policy policy;
private final Map<String, Object> privateProperties = new HashMap<>();

private PolicyDefinition() {
}
Expand All @@ -50,7 +55,7 @@ public Policy getPolicy() {

@Override
public int hashCode() {
return Objects.hash(Objects.hash(id), policy.hashCode());
return Objects.hash(Objects.hash(id), policy.hashCode(), privateProperties);
}

@Override
Expand All @@ -69,6 +74,15 @@ public String getUid() {
return id;
}

@NotNull
public Map<String, Object> getPrivateProperties() {
return privateProperties;
}

public Object getPrivateProperty(String key) {
return privateProperties.get(key);
}

@JsonPOJOBuilder(withPrefix = "")
public static final class Builder extends Entity.Builder<PolicyDefinition, Builder> {

Expand All @@ -86,6 +100,17 @@ public Builder policy(Policy policy) {
return this;
}

public Builder privateProperty(String key, Object value) {
entity.privateProperties.put(key, value);
return this;
}

public Builder privateProperties(Map<String, Object> privateProperties) {
Objects.requireNonNull(privateProperties);
entity.privateProperties.putAll(privateProperties);
return this;
}

@Override
public Builder self() {
return this;
Expand Down