Skip to content

Commit

Permalink
feat: add basic code and BE setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bhautik-sakhiya committed Nov 21, 2024
1 parent f4125e7 commit 9947cdb
Show file tree
Hide file tree
Showing 25 changed files with 623 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.smartsense.chat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@ConfigurationPropertiesScan
@EnableFeignClients
public class EdcChatApplication {

public static void main(String[] args) {
SpringApplication.run(EdcChatApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.smartsense.chat;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(EdcChatApplication.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.smartsense.chat.dao.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;

import java.util.Date;
import java.util.UUID;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "raw_data_master")
public class BusinessPartner {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
@Column(name = "id", nullable = false, updatable = false, insertable = false)
private final UUID id = UUID.randomUUID();

@Column(name = "name")
private String name;

@Column(name = "bpn")
private String bpn;

@Column(name = "edc_url")
private String edcUrl;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", updatable = false, nullable = false)
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at")
@JsonIgnore
private Date updatedAt;

@PrePersist
void createdAt() {
this.createdAt = new Date();
}

@PreUpdate
void updatedAt() {
this.updatedAt = new Date();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.smartsense.chat.dao.repository;

import com.smartsense.chat.dao.entity.BusinessPartner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface BusinessPartnerRepository extends JpaRepository<BusinessPartner, UUID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.smartsense.chat.edc;

import com.smartsense.chat.edc.operation.PublicUrlHandlerService;
import com.smartsense.chat.edc.operation.TransferProcessService;
import com.smartsense.chat.utils.request.ChatMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URI;

@Service
@RequiredArgsConstructor
@Slf4j
public class EDCService {


private final TransferProcessService transferProcessService;
private final PublicUrlHandlerService publicUrlHandlerService;

public void initProcess(URI edcUri, ChatMessage chatMessage) {
// TODO query catalog
//TODO start negotiation
//TODO get agreement


String agreementId = null;

// Initiate the transfer process
String transferProcessId = transferProcessService.initiateTransfer(edcUri, agreementId);

// sent the message to public url
publicUrlHandlerService.getAuthCodeAndPublicUrl(edcUri, transferProcessId, chatMessage);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.smartsense.chat.edc.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;
import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@FeignClient(name = "edc", url = "http://localhost:8182")
public interface EDCConnectorClient {

@PostMapping(value = "/management/v2/catalog/request", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
Map<String, Object> queryCatalog(URI baseUri,
@RequestBody Map<String, Object> request,
@RequestHeader("X-Api-Key") String auth);

@PostMapping(value = "/management/v2/edrs", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
Map<String, Object> initNegotiation(URI baseUri,
@RequestBody Map<String, Object> request,
@RequestHeader("X-Api-Key") String auth);

@GetMapping(value = "/management/v2/contractnegotiations/{negotiationId}", produces = APPLICATION_JSON_VALUE)
Map<String, Object> getAgreement(URI baseUri,
@PathVariable("negotiationId") String negotiationId,
@RequestHeader("X-Api-Key") String auth);


@PostMapping(value = "/management/v2/edrs/request", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
List<Map<String, Object>> initTransferProcess(URI baseUri,
@RequestBody Map<String, Object> request,
@RequestHeader("X-Api-Key") String auth);

@GetMapping(value = "management/v2/edrs/{transferProcessId}/dataaddress", produces = APPLICATION_JSON_VALUE)
Map<String, Object> getAuthCodeAndPublicUrl(URI baseUri,
@PathVariable("transferProcessId") String transferProcessId,
@RequestHeader("X-Api-Key") String auth);

@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
Map<String, Object> sendMessage(URI baseUri, @RequestBody String message, @RequestHeader("Authorization") String authorization);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartsense.chat.edc.operation;

public class AgreementFetcherService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartsense.chat.edc.operation;

public class ContractNegotiationService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.smartsense.chat.edc.operation;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartsense.chat.edc.client.EDCConnectorClient;
import com.smartsense.chat.utils.request.ChatMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class PublicUrlHandlerService {
private final EDCConnectorClient edc;
private final ObjectMapper mapper;
@Value("${edc.auth.code:password}")
private String authCode;

public void getAuthCodeAndPublicUrl(URI edcUri, String transferProcessId, ChatMessage message) {
try {
log.info("Initiate to get auth code based on transfer process id " + transferProcessId);
Map<String, Object> response = edc.getAuthCodeAndPublicUrl(edcUri, transferProcessId, authCode);
log.info("Auth code and public url response -> {}", response);

// Retrieve public path and authorization code
String publicPath = response.get("tx-auth:refreshEndpoint").toString();
String authorization = response.get("authorization").toString();

// Call the public path with authorization code
callPublicUri(publicPath, mapper.writeValueAsString(message), authorization);

log.info("Initiate to get auth code based on transfer process id {} is done.", transferProcessId);
} catch (Exception ex) {
log.error("Error occurred in get auth code based on transfer process id {} ", transferProcessId, ex);
}
}

private void callPublicUri(String publicPath, String message, String authorization) {
try {
Map<String, Object> publicUriResponse = edc.sendMessage(URI.create(publicPath), message, authorization);
log.info("Received public uri response -> {}", publicUriResponse);
} catch (Exception ex) {
log.error("Error occurred while calling public uri {} and auth code {} ", publicPath, authorization, ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartsense.chat.edc.operation;

public class QueryCatalogService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.smartsense.chat.edc.operation;

import com.smartsense.chat.edc.client.EDCConnectorClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
@Slf4j
public class TransferProcessService {

private final EDCConnectorClient edc;
@Value("${edc.auth.code:password}")
private String authCode;

public String initiateTransfer(URI edcUri, String agreementId) {
try {
log.info("Initiate transfer process for agreement Id {}", agreementId);

// prepare transfer request
Map<String, Object> transferRequest = prepareTransferRequest(agreementId);
// initiate the transfer process
List<Map<String, Object>> transferResponse = edc.initTransferProcess(edcUri, transferRequest, authCode);
log.info("Received transfer response -> {}", transferResponse);

// get the transfer process id from response
String transferProcessId = transferResponse.get(0).get("transferProcessId").toString();
log.info("Transfer process id: {}", transferProcessId);

log.info("Transfer process is complete successfully for agreement Id {}", agreementId);
return transferProcessId;
} catch (Exception ex) {
log.error("Error occurred in transfer process for agreement Id {}", agreementId, ex);
return null;
}
}

private Map<String, Object> prepareTransferRequest(String agreementId) {
Map<String, Object> transferRequest = new HashMap<>();
transferRequest.put("@context", Map.of("@vocab", "https://w3id.org/edc/v0.0.1/ns/"));
transferRequest.put("@type", "QuerySpec");
transferRequest.put("offset", 0);
transferRequest.put("limit", 1);
transferRequest.put("filterExpression", List.of(Map.of("operandLeft", "agreementId",
"operator", "=",
"operandRight", agreementId)));
log.info("Transfer request looks like: {}", transferRequest);
return transferRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.smartsense.chat.edc.settings;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("chat.edc")
public record EDCConfigurations(String authCode,
String assetId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smartsense.chat.service;

import com.smartsense.chat.dao.entity.BusinessPartner;
import com.smartsense.chat.dao.repository.BusinessPartnerRepository;
import com.smartsense.chat.utils.request.BusinessPartnerRequest;
import com.smartsense.chat.web.BusinessPartnerResource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class BusinessPartnerService {

private final BusinessPartnerRepository businessPartnerRepository;


public BusinessPartnerResource createBusinessPartner(BusinessPartnerRequest request) {
log.info("Creating BusinessPartner. name: {}", request.name());
BusinessPartner businessPartner = BusinessPartner.builder()
.name(request.name())
.edcUrl(request.edcUrl())
.bpn(request.bpn())
.build();
businessPartnerRepository.save(businessPartner);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.smartsense.chat.utils.exception;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serial;

@NoArgsConstructor
@Getter
public class BadDataException extends RuntimeException {
@Serial
private static final long serialVersionUID = 5732404099105408974L;

private String code;
private String message;

public BadDataException(String message) {
this.message = message;
}

public BadDataException(String message, String code) {
this.message = message;
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.smartsense.chat.utils.request;

public record BusinessPartnerRequest(String name,
String bpn,
String edcUrl) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.smartsense.chat.utils.request;

public record ChatMessage(String senderBpn,
String receiverBpn,
String message,
String messageId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.smartsense.chat.utils.response;

import java.util.UUID;

public record BusinessPartnerResponse(UUID id,
String name,
String bpn,
String edcUrl) {
}
Loading

0 comments on commit 9947cdb

Please sign in to comment.