Skip to content

Commit

Permalink
feat: add support for membership policy creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhautik-sakhiya committed Dec 3, 2024
1 parent b874caa commit 85f0579
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
5 changes: 3 additions & 2 deletions edc-chat-app-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ successful chat flow:
- a Postgres database to store other Business Partners' information and chat history.
- a simple UI to send messages to and receive messages from other Business Partners.

**NOTE:** When the backend application starts, it will create the Asset, Policy, and Contract Definition. However, if
the assetId as an **edc-chat-app** is already in the edc database, it’ll skip this process.

## Packages Overview

The backend application consists of the below packages:
Expand Down Expand Up @@ -69,8 +72,6 @@ server. The [WebSocketConfig.java](src/main/java/com/smartsense/chat/config/WebS
| `AGREEMENT_RETRY_LIMIT` | Max retry check for contract to be finalized |
| `APP_HOST_URL` | Application host url to add in asset |
- Run the application by executing the following command:
```bash
./gradlew bootRun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import com.smartsense.chat.edc.client.EDCConnectorClient;
import com.smartsense.chat.edc.settings.AppConfig;
import com.smartsense.chat.service.ChatMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -58,7 +57,7 @@ private Map<String, Object> prepareNegotiationRequest(String receiverDspUrl, Str
negotiationRequest.put("edc:counterPartyAddress", receiverDspUrl);
negotiationRequest.put("edc:protocol", "dataspace-protocol-http");
negotiationRequest.put("edc:counterPartyId", receiverBpnL);
negotiationRequest.put("edc:policy", prepareNegotiationPolicy(receiverBpnL, offerId));
negotiationRequest.put("edc:policy", prepareNegotiationMembershipPolicy(receiverBpnL, offerId));
log.info("Negotiation request looks like: {}", negotiationRequest);
return negotiationRequest;
}
Expand All @@ -72,4 +71,15 @@ private Map<String, Object> prepareNegotiationPolicy(String receiverBpnL, String
negotiationPolicy.put("assigner", receiverBpnL);
return negotiationPolicy;
}

private Map<String, Object> prepareNegotiationMembershipPolicy(String receiverBpnL, String offerId) {
Map<String, Object> negotiationPolicy = new HashMap<>();
negotiationPolicy.put("@id", offerId);
negotiationPolicy.put("@type", "Offer");
negotiationPolicy.put("permission", List.of(Map.of("action", "use",
"constraint", Map.of("and", List.of(Map.of("leftOperand", "Membership", "operator", "eq", "rightOperand", "active"))))));
negotiationPolicy.put("target", config.edc().assetId());
negotiationPolicy.put("assigner", receiverBpnL);
return negotiationPolicy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import com.smartsense.chat.edc.client.EDCConnectorClient;
import com.smartsense.chat.edc.settings.AppConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
Expand All @@ -25,7 +24,7 @@ public class PolicyCreationService {

public void createPolicy() {
log.info("Policy creation process is started...");
edc.createPolicy(config.edc().edcUri(), cretePolicyRequest(), config.edc().authCode());
edc.createPolicy(config.edc().edcUri(), createMemberShipPolicyRequest(), config.edc().authCode());
log.info("Policy creation process is completed...");
}

Expand All @@ -38,4 +37,15 @@ private Map<String, Object> cretePolicyRequest() {
policyCreation.put("policy", Map.of("@type", "Set", "permission", List.of(Map.of("action", "use"))));
return policyCreation;
}

private Map<String, Object> createMemberShipPolicyRequest() {
Map<String, Object> policyCreation = new HashMap<>();
policyCreation.put("@context", List.of("https://w3id.org/tractusx/policy/v1.0.0", "http://www.w3.org/ns/odrl.jsonld",
Map.of("@vocab", "https://w3id.org/edc/v0.0.1/ns/", "dct", "https://purl.org/dc/terms/")));
policyCreation.put("@type", "PolicyDefinitionRequestDto");
policyCreation.put("@id", config.edc().policyId());
policyCreation.put("policy", Map.of("@type", "Set", "permission", List.of(Map.of("action", "use",
"constraint", Map.of("and", List.of(Map.of("leftOperand", "Membership", "operator", "eq", "rightOperand", "active")))))));
return policyCreation;
}
}

0 comments on commit 85f0579

Please sign in to comment.