Skip to content

Commit

Permalink
Merge a9636b9 into 73d8646
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wiedemann authored Jun 21, 2024
2 parents 73d8646 + a9636b9 commit a1fee72
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 10 deletions.
33 changes: 33 additions & 0 deletions api/odrl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ paths:
schema:
type: string
description: location of the created policy
get:
parameters:
- $ref: '#/components/parameters/Page'
- $ref: '#/components/parameters/PageSize'
operationId: getPolicies
summary: Get the policies in the ODRL-Format
responses:
'200':
description: Successfully retrieved the policis.
content:
application/json:
schema:
$ref: '#/components/schemas/PolicyList'
'404':
description: No such policy exists
/policy/{id}:
put:
parameters:
Expand Down Expand Up @@ -86,6 +101,20 @@ components:
required: true
schema:
$ref: '#/components/schemas/Uid'
Page:
name: page
in: query
required: false
schema:
type: integer
default: 0
PageSize:
name: pageSize
in: query
required: false
schema:
type: integer
default: 25
schemas:
Policy:
type: object
Expand All @@ -96,6 +125,10 @@ components:
type: string
rego:
type: string
PolicyList:
type: array
items:
$ref: '#/components/schemas/Policy'
Uid:
type: string
format: https://datatracker.ietf.org/doc/html/rfc3987
Expand Down
2 changes: 2 additions & 0 deletions doc/REGO.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
| --- | --- | --- | --- |
| leftOperand | vc:role | role(verifiable_credential,organization_id) | retrieves the roles from the credential, that target the current organization |
| leftOperand | vc:currentParty | current_party(credential) | the current (organization)party, |
| leftOperand | vc:type | types(verifiable_credential) | the type(s) of the current credential |
| assignee | odrl:any | is_any | allows for any user |

## ngsild
Expand All @@ -85,3 +86,4 @@
| --- | --- | --- | --- |
| leftOperand | tmf:lifecycleStatus | life_cycle_status(entity) | return the lifeCycleStatus of a given entity |
| leftOperand | tmf:resource | resource_type(http_part) | retrieves the type of the resource from the path |
| action | tmf:create | is_creation(request) | Check if the given request is a creation |
1 change: 1 addition & 0 deletions src/main/java/org/fiware/odrl/BundleResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/fiware/odrl/PolicyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
Expand All @@ -16,7 +17,10 @@
import org.fiware.odrl.rego.PolicyWrapper;
import org.fiware.odrl.rego.RegoPolicy;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
Expand Down Expand Up @@ -65,6 +69,22 @@ public Response deletePolicyById(String id) {
return Response.noContent().build();
}


@Override
public Response getPolicies(Integer page, Integer pageSize) {
List<Policy> policyList = policyRepository
.getPolicies(Optional.ofNullable(page).orElse(0), Optional.ofNullable(pageSize).orElse(25))
.entrySet()
.stream()
.map(policyEntry -> new Policy()
.id(policyEntry.getKey())
.odrl(policyEntry.getValue().odrl().policy())
.rego(policyEntry.getValue().rego().policy())).toList();

return Response.ok(policyList).build();
}


@Override
public Response getPolicyById(String id) {
return policyRepository
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/fiware/odrl/mapping/EntityMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.fiware.odrl.mapping;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.fiware.odrl.persistence.Policy;
import org.fiware.odrl.persistence.PolicyEntity;
import org.fiware.odrl.rego.OdrlPolicy;
import org.fiware.odrl.rego.PolicyWrapper;
Expand All @@ -12,15 +16,24 @@
@ApplicationScoped
public class EntityMapper {

@Inject
private ObjectMapper objectMapper;

public PolicyEntity map(String id, PolicyWrapper policyWrapper) {
Policy rego = new Policy();
rego.setPolicy(policyWrapper.rego().policy());
Policy odrl = new Policy();
odrl.setPolicy(policyWrapper.odrl().policy());
PolicyEntity policy = new PolicyEntity();
policy.setPolicyId(id);
policy.setRego(policyWrapper.rego().policy());
policy.setOdrl(policyWrapper.odrl().policy());
policy.setRego(rego);
policy.setOdrl(odrl);
return policy;
}

public PolicyWrapper map(PolicyEntity policyEntity) {
return new PolicyWrapper(new OdrlPolicy(policyEntity.getOdrl()), new RegoPolicy(policyEntity.getRego()));
return new PolicyWrapper(
new OdrlPolicy(policyEntity.getOdrl().getPolicy()),
new RegoPolicy(policyEntity.getRego().getPolicy()));
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/fiware/odrl/persistence/Policy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fiware.odrl.persistence;

import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Data;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@RegisterForReflection
@Data
public class Policy {
private String policy;
}
13 changes: 9 additions & 4 deletions src/main/java/org/fiware/odrl/persistence/PolicyEntity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.fiware.odrl.persistence;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Lob;
import lombok.Data;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.util.Optional;

Expand All @@ -17,11 +21,12 @@ public class PolicyEntity extends PanacheEntity {
public static final String TABLE_NAME = "policy_entity";

private String policyId;
@Lob
private String odrl;
@Lob
private String rego;

@JdbcTypeCode(SqlTypes.JSON)
private Policy odrl;

@JdbcTypeCode(SqlTypes.JSON)
private Policy rego;

public static Optional<PolicyEntity> findByPolicyId(String policyId) {
return Optional.ofNullable(find("policyId", policyId).firstResult());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.fiware.odrl.rego;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.panache.common.Page;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
Expand All @@ -12,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
Expand Down Expand Up @@ -54,7 +59,6 @@ private String getUniqueId() {
return generatedId;
}

@Transactional
public Map<String, PolicyWrapper> getPolicies() {
Map<String, PolicyWrapper> policies = new HashMap<>();

Expand All @@ -64,6 +68,13 @@ public Map<String, PolicyWrapper> getPolicies() {
return ImmutableMap.copyOf(policies);
}

public Map<String, PolicyWrapper> getPolicies(int page, int pageSize) {
PanacheQuery<PolicyEntity> policyEntities = PolicyEntity.findAll();
List<PolicyEntity> policyEntityList = policyEntities.page(Page.of(page, pageSize)).list();

return policyEntityList.stream().collect(Collectors.toMap(PolicyEntity::getPolicyId, e -> entityMapper.map(e), (e1, e2) -> e1));
}

@Override
public void deletePolicy(String id) {
PolicyEntity.findByPolicyId(id).map(policyEntity -> policyEntity.id).ifPresent(PolicyEntity::deleteById);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/fiware/odrl/rego/PolicyRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fiware.odrl.rego;

import jakarta.transaction.Transactional;

import java.util.Map;
import java.util.Optional;
import java.util.Random;
Expand Down Expand Up @@ -29,5 +31,7 @@ default String generatePolicyId() {

Map<String, PolicyWrapper> getPolicies();

Map<String, PolicyWrapper> getPolicies(int page, int pageSize);

void deletePolicy(String id);
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/pap
quarkus.datasource.jdbc.max-size=13
quarkus.hibernate-orm.database.generation=update
quarkus.hibernate-orm.database.generation.create-schemas=true

Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
"regoPackage": "dome.action as dome_action",
"regoMethod": "dome_action.is_set_published(helper.http_part)"
}
},
"tmf": {
"create": {
"regoPackage": "tmf.action as tmf_action",
"regoMethod": "tmf_action.is_creation(helper.http_part)"
}
}
},
"operator": {
Expand Down Expand Up @@ -132,6 +138,10 @@
"currentParty": {
"regoPackage": "vc.leftOperand as vc_lo",
"regoMethod": "vc_lo.current_party(helper.verifiable_credential)"
},
"type": {
"regoPackage": "vc.leftOperand as vc_lo",
"regoMethod": "vc_lo.types(helper.verifiable_credential)"
}
},
"ngsi-ld": {
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/rego-resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ rego/vc/leftOperand.rego
rego/vc/assignee.rego
rego/ngsi-ld/leftOperand.rego
rego/tmf/leftOperand.rego
rego/dome/leftOperand.rego
rego/dome/action.rego
rego/odrl/operand.rego
rego/odrl/rightOperand.rego
rego/odrl/operator.rego
rego/odrl/leftOperand.rego
rego/odrl/target.rego
rego/odrl/action.rego
rego/odrl/assignee.rego
rego/utils/kong.rego
rego/utils/apisix.rego
rego/vc/leftOperand.rego
rego/vc/assignee.rego
rego/ngsi-ld/leftOperand.rego
rego/tmf/leftOperand.rego
rego/tmf/action.rego
7 changes: 7 additions & 0 deletions src/main/resources/rego/tmf/action.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tmf.action

import rego.v1

## tmf:create
# Check if the given request is a creation
is_creation(request) if request.method == "POST"
6 changes: 5 additions & 1 deletion src/main/resources/rego/vc/leftOperand.rego
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ role(verifiable_credential,organization_id) := r if {

## vc:currentParty
# the current (organization)party,
current_party(credential) := credential.issuer
current_party(credential) := credential.issuer

## vc:type
# the type(s) of the current credential
types(verifiable_credential) := verifiable_credential.type

0 comments on commit a1fee72

Please sign in to comment.