Skip to content

Commit

Permalink
feat(policy-api): [eclipse-tractusx#750] return policy only once with…
Browse files Browse the repository at this point in the history
… a list of all assigned business partner numbers
  • Loading branch information
dsmf committed Jul 12, 2024
1 parent b3a8b4f commit 9101730
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 61 deletions.
87 changes: 46 additions & 41 deletions docs/src/api/irs-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ paths:
application/json:
examples:
success:
$ref: '#/components/examples/get-policies-by-id'
$ref: '#/components/examples/get-policy-by-id'
schema:
type: string
description: Returns the policies as list of policies.
Expand Down Expand Up @@ -1560,46 +1560,6 @@ components:
relationships: []
submodels: []
tombstones: []
get-policies-by-id:
value:
- bpn: BPNL1234567890AB
policy:
createdOn: 2024-07-08T12:01:19.4677109+02:00
permissions:
- action: use
constraint:
and: []
or:
- leftOperand: Membership
operator:
'@id': eq
rightOperand: active
- leftOperand: FrameworkAgreement.traceability
operator:
'@id': eq
rightOperand: active
- leftOperand: PURPOSE
operator:
'@id': eq
rightOperand: ID 3.1 Trace
- action: access
constraint:
and: []
or:
- leftOperand: Membership
operator:
'@id': eq
rightOperand: active
- leftOperand: FrameworkAgreement.traceability
operator:
'@id': eq
rightOperand: active
- leftOperand: PURPOSE
operator:
'@id': eq
rightOperand: ID 3.1 Trace
policyId: 13a8523f-c80a-40b8-9e43-faab47fbceaa
validUntil: 2025-07-08T12:01:19.4677109+02:00
get-policies-paged-result:
value:
content:
Expand Down Expand Up @@ -1663,6 +1623,51 @@ components:
unsorted: true
totalElements: 11
totalPages: 2
get-policy-by-id:
value:
bpn:
- BPNL1234567890AB
- BPNL1234567890CD
payload:
'@id': 13a8523f-c80a-40b8-9e43-faab47fbceaa
policy:
createdOn: 2024-07-08T12:01:19.4677109+02:00
permissions:
- action: use
constraint:
and: []
or:
- leftOperand: Membership
operator:
'@id': eq
rightOperand: active
- leftOperand: FrameworkAgreement.traceability
operator:
'@id': eq
rightOperand: active
- leftOperand: PURPOSE
operator:
'@id': eq
rightOperand: ID 3.1 Trace
- action: access
constraint:
and: []
or:
- leftOperand: Membership
operator:
'@id': eq
rightOperand: active
- leftOperand: FrameworkAgreement.traceability
operator:
'@id': eq
rightOperand: active
- leftOperand: PURPOSE
operator:
'@id': eq
rightOperand: ID 3.1 Trace
policyId: 13a8523f-c80a-40b8-9e43-faab47fbceaa
validUntil: 2025-07-08T12:01:19.4677109+02:00
validUntil: 2025-07-08T12:01:19.4677109+02:00
job-handle:
value:
id: 6c311d29-5753-46d4-b32c-19b918ea93b0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyType;
import org.eclipse.tractusx.irs.ess.service.NotificationSummary;
import org.eclipse.tractusx.irs.policystore.models.GetPolicyByIdResponse;
import org.eclipse.tractusx.irs.policystore.models.Payload;
import org.eclipse.tractusx.irs.policystore.models.PolicyWithBpn;
import org.eclipse.tractusx.irs.semanticshub.AspectModel;
import org.eclipse.tractusx.irs.semanticshub.AspectModels;
Expand Down Expand Up @@ -153,7 +155,7 @@ public void createExamples(final Components components) {
components.addExamples("complete-job-list-processing-state", createJobListProcessingState());
components.addExamples("aspect-models-list", createAspectModelsResult());
components.addExamples("get-policies-paged-result", createPageOfPolicies());
components.addExamples("get-policies-by-id", createListOfPolicies());
components.addExamples("get-policy-by-id", createGetPolicyByIdResponse());
}

private Example createPageOfPolicies() {
Expand All @@ -167,17 +169,17 @@ private Example createPageOfPolicies() {
return toExample(page);
}

private Example createListOfPolicies() {
final List<PolicyWithBpn> policyWithBpn = List.of( //
new PolicyWithBpn("BPNL1234567890AB", Policy.builder()
.policyId("13a8523f-c80a-40b8-9e43-faab47fbceaa")
.createdOn(OffsetDateTime.parse(
"2024-07-08T12:01:19.4677109+02:00"))
.validUntil(OffsetDateTime.parse(
"2025-07-08T12:01:19.4677109+02:00"))
.permissions(createPermissions())
.build()));
return toExample(policyWithBpn);
private Example createGetPolicyByIdResponse() {
final OffsetDateTime validUntil = OffsetDateTime.parse("2025-07-08T12:01:19.4677109+02:00");
final String policyId = "13a8523f-c80a-40b8-9e43-faab47fbceaa";
final GetPolicyByIdResponse response = new GetPolicyByIdResponse(validUntil, new Payload(null, policyId,
Policy.builder()
.policyId(policyId)
.createdOn(OffsetDateTime.parse("2024-07-08T12:01:19.4677109+02:00"))
.validUntil(validUntil)
.permissions(createPermissions())
.build()), List.of("BPNL1234567890AB", "BPNL1234567890CD"));
return toExample(response);
}

private List<Permission> createPermissions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.eclipse.tractusx.irs.policystore.common.SearchParameterParser;
import org.eclipse.tractusx.irs.policystore.models.CreatePoliciesResponse;
import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.models.GetPolicyByIdResponse;
import org.eclipse.tractusx.irs.policystore.models.PolicyResponse;
import org.eclipse.tractusx.irs.policystore.models.PolicyWithBpn;
import org.eclipse.tractusx.irs.policystore.models.SearchCriteria;
Expand Down Expand Up @@ -162,6 +163,7 @@ public CreatePoliciesResponse registerAllowedPolicy(@Valid @RequestBody final Cr
return CreatePoliciesResponse.fromPolicy(registeredPolicy);
}


@Operation(operationId = "getPolicyById", summary = "Gets policy by ID.",
security = @SecurityRequirement(name = API_KEY), tags = { POLICY_API_TAG },
description = "Gets policy by ID.")
Expand All @@ -171,7 +173,7 @@ public CreatePoliciesResponse registerAllowedPolicy(@Valid @RequestBody final Cr
schema = @Schema(description = "List of policies",
implementation = List.class),
examples = @ExampleObject(name = "success",
ref = "#/components/examples/get-policies-by-id")),
ref = "#/components/examples/get-policy-by-id")),
}),
@ApiResponse(responseCode = "401", description = UNAUTHORIZED_DESC,
content = { @Content(mediaType = APPLICATION_JSON_VALUE,
Expand All @@ -189,25 +191,25 @@ public CreatePoliciesResponse registerAllowedPolicy(@Valid @RequestBody final Cr
@GetMapping("/policies/{policyId}")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')")
public List<PolicyResponse> getPolicyById(//
public GetPolicyByIdResponse getPolicyById(//
@PathVariable(required = true) //
@ValidPolicyId //
@Parameter(description = "Policy ID.") //
final String policyId //
) {

final Map<String, String[]> parameterMap = this.httpServletRequest.getParameterMap();

final Map<String, List<Policy>> policies = service.getPolicies(null);

// TODO (mfischer): #750: return only one policy and List of BPN as attribute?!
// TODO (mfischer): #750: update insomnia
// TODO (mfischer): #750: update swagger
// TODO (mfischer): #750: add test
return policyPagingService.getPolicyWithBpnStream(policies)
.filter(p -> p.policy().getPolicyId().equals(policyId))
.map(p -> PolicyResponse.from(p.policy(), p.bpn()))
.toList();
// In MinIO we store the policy per BPN.
// Therefore, we can simply take the first one and collect the keys to get all the
// business partner numbers to which the policy is assigned.
.findFirst()
.map(p -> GetPolicyByIdResponse.from(p.policy(), policies.keySet().stream().toList()))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Policy not found"));
}

@Operation(operationId = "getAllowedPoliciesByBpn",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* 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.irs.policystore.models;

import java.time.OffsetDateTime;
import java.util.List;

import lombok.Builder;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;

/**
* Policy representation for get policy by id response
*/
@Builder
public record GetPolicyByIdResponse(OffsetDateTime validUntil, Payload payload, List<String> bpn) {

public static GetPolicyByIdResponse from(final Policy policy, final List<String> bpn) {
return GetPolicyByIdResponse.builder()
.validUntil(policy.getValidUntil())
.payload(Payload.builder()
.policyId(policy.getPolicyId())
.context(Context.getDefault())
.policy(policy)
.build())
.bpn(bpn)
.build();
}

}

0 comments on commit 9101730

Please sign in to comment.