Skip to content

Commit

Permalink
Merge 82b3976 into 93a1ba7
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wiedemann authored Jul 5, 2024
2 parents 93a1ba7 + 82b3976 commit 053109a
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pre-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ jobs:
mvn clean package -Dquarkus.package.type=native -Dquarkus.container-image.build=true -Dquarkus.container-image.tag=${{ needs.generate-version.outputs.version }} -Dquarkus.container-image.registry="${{ env.REGISTRY }}" -Dquarkus.container-image.group="${{ env.REPOSITORY }}"
- name: Test image
run: |
mvn integration-test -Pk8s-it -DskipTests -Dquarkus.container-image.tag=${{ needs.generate-version.outputs.version }} -Dquarkus.container-image.registry="${{ env.REGISTRY }}" -Dquarkus.container-image.group="${{ env.REPOSITORY }}"
run: |
mvn integration-test -Pk8s-it -DskipTests -Dquarkus.container-image.tag=${{ needs.generate-version.outputs.version }} -Dquarkus.container-image.registry="${{ env.REGISTRY }}" -Dquarkus.container-image.group="${{ env.REPOSITORY }}"
- name: Push
run: |
Expand Down
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
3 changes: 3 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 @@ -78,10 +79,12 @@
| leftOperand | ngsi-ld:<property>_observedAt | # | retrieves the observedAt of the property The method should be concretized in the mapping.json, to match a concrete property. |
| leftOperand | ngsi-ld:<property>_modifiedAt | # | retrieves the modifiedAt of the property The method should be concretized in the mapping.json, to match a concrete property. |
| leftOperand | ngsi-ld:<relationship> | # | retrieves the object of the relationship, only applies to properties of type "Relationship". The method should be concretized in the mapping.json, to match a concrete property. |
| action | ngsild:create | is_creation(request) | Check if the given request is a creation |

## tmf

| ODRL Class | ODRL Key | Rego-Method | Description |
| --- | --- | --- | --- |
| 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 |
28 changes: 15 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
<version.org.jboss.resteasy>6.2.8.Final</version.org.jboss.resteasy>
<version.org.openapitools.maven-generator-plugin>7.4.0</version.org.openapitools.maven-generator-plugin>
<version.org.testcontainers>1.19.6</version.org.testcontainers>
<version.io.quarkiverse.openapi.generator>2.4.1</version.io.quarkiverse.openapi.generator>
<version.org.projectlombok.lombok>1.18.30</version.org.projectlombok.lombok>
<version.com.google.guava>33.1.0-jre</version.com.google.guava>
<version.org.apache.commons.commons-compress>1.26.1</version.org.apache.commons.commons-compress>
<version.org.keycloak>24.0.2</version.org.keycloak>
<version.org.awaitility>4.2.1</version.org.awaitility>
<version.org.mock-server.mockserver-client-java>5.15.0</version.org.mock-server.mockserver-client-java>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -79,30 +86,25 @@
<groupId>io.quarkus.qute</groupId>
<artifactId>qute-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator-server</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
<version>2.4.1</version>
<version>${version.io.quarkiverse.openapi.generator}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<version>${version.org.projectlombok.lombok}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.1.0-jre</version>
<version>${version.com.google.guava}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.1</version>
<version>${version.org.apache.commons.commons-compress}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -135,7 +137,7 @@
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.1</version>
<version>${version.org.awaitility}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -153,19 +155,19 @@
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>24.0.2</version>
<version>${version.org.keycloak}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services</artifactId>
<version>24.0.2</version>
<version>${version.org.keycloak}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>5.15.0</version>
<version>${version.org.mock-server.mockserver-client-java}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
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
19 changes: 19 additions & 0 deletions src/main/java/org/fiware/odrl/PolicyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.fiware.odrl.rego.PolicyWrapper;
import org.fiware.odrl.rego.RegoPolicy;

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

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
Expand Down Expand Up @@ -65,6 +67,23 @@ 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
26 changes: 23 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,11 @@
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 lombok.extern.slf4j.Slf4j;
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 @@ -10,17 +15,32 @@
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@ApplicationScoped
@Slf4j
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()));
try {
log.warn("The entity {}", objectMapper.writeValueAsString(policyEntity));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return new PolicyWrapper(
new OdrlPolicy(policyEntity.getOdrl().getPolicy()),
new RegoPolicy(policyEntity.getRego().getPolicy()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.fiware.odrl.mapping;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.fiware.odrl.model.Policy;

/**
* @author <a href="https://github.com/wistefan">Stefan Wiedemann</a>
*/
@RegisterForReflection(targets = {Policy.class})
public class PolicyReflectionConfiguration {
}
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
17 changes: 15 additions & 2 deletions src/main/java/org/fiware/odrl/rego/PersistentPolicyRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.fiware.odrl.rego;

import com.google.common.collect.ImmutableMap;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
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 +15,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 +58,6 @@ private String getUniqueId() {
return generatedId;
}

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

Expand All @@ -64,8 +67,18 @@ 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
@Transactional
public void deletePolicy(String id) {
PolicyEntity.findByPolicyId(id).map(policyEntity -> policyEntity.id).ifPresent(PolicyEntity::deleteById);
log.warn("Try to delete {}", id);
PolicyEntity.findByPolicyId(id)
.ifPresent(PanacheEntityBase::delete);
}
}
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
Loading

0 comments on commit 053109a

Please sign in to comment.