Skip to content

Commit

Permalink
Derivative Redeem after Order Termination
Browse files Browse the repository at this point in the history
  • Loading branch information
Dabami committed Oct 19, 2022
1 parent 68dc054 commit 52db46d
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public ResponseEntity<Boolean> publishProductOrder(
licenseTerms
);

OrderOfferMapping orderOfferMapping = new OrderOfferMapping(request.getOfferDid(), request.getOrderDid());
orderOfferMappingRepository.save(orderOfferMapping);

return ResponseEntity.ok().body(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class PublishProductOrderRequest {
@NotNull private ProductOrder productOrder;
private Map<String, Invitation> invitations;
private Collection<VerifiableCredential> verifiableCredentials;
@NotBlank private String did;
@NotBlank private String orderDid;
@NotBlank private String offerDid;
private String supplierDid;
private TimePeriod validFor;

Expand Down Expand Up @@ -47,15 +48,23 @@ public PublishProductOrderRequest setVerifiableCredentials(
return this;
}

public String getDid() {
return did;
public String getOrderDid() {
return orderDid;
}

public PublishProductOrderRequest setDid(String did) {
this.did = did;
public PublishProductOrderRequest setOrderDid(String orderDid) {
this.orderDid = orderDid;
return this;
}

public String getOfferDid() {
return offerDid;
}

public void setOfferDid(String offerDid) {
this.offerDid = offerDid;
}

public String getSupplierDid() {
return supplierDid;
}
Expand All @@ -79,6 +88,6 @@ public ProductOrderDetails toOrderDetails() {
.setProductOrder(productOrder)
.setSupplierDid(supplierDid)
.setValidFor(validFor)
.setOrderDid(did);
.setOrderDid(orderDid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package eu._5gzorro.manager.api.model.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.*;
import java.util.UUID;

@Entity
@Table(name = "order_offer_mappings")
public class OrderOfferMapping {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonIgnore
private UUID id;

@JsonProperty("offerDid")
private String offerDid;

@JsonProperty("orderDid")
private String orderDid;

public OrderOfferMapping() {
}

@JsonCreator
public OrderOfferMapping(@JsonProperty("offerDid") String offerDid,
@JsonProperty("orderDid") String orderDid) {
this.offerDid = offerDid;
this.orderDid = orderDid;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getOfferDid() {
return offerDid;
}

public void setOfferDid(String offerDid) {
this.offerDid = offerDid;
}

public String getOrderDid() {
return orderDid;
}

public void setOrderDid(String orderDid) {
this.orderDid = orderDid;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eu._5gzorro.manager.api.repository;

import eu._5gzorro.manager.api.model.entity.OrderOfferMapping;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface OrderOfferMappingRepository extends JpaRepository<OrderOfferMapping, UUID> {

@NotNull
Optional<OrderOfferMapping> findById(@NotNull UUID id);

@Query("SELECT oom FROM OrderOfferMapping oom WHERE oom.orderDid = ?1")
Optional<OrderOfferMapping> findByOrderDid(String orderDid);

@NotNull
List<OrderOfferMapping> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package eu._5gzorro.manager.api.service.kafka;

import eu._5gzorro.manager.api.model.entity.OrderOfferMapping;
import eu._5gzorro.manager.api.repository.OrderOfferMappingRepository;
import eu._5gzorro.manager.domain.events.ProductOrderUpdateEvent;
import eu._5gzorro.manager.service.DerivativeSpectokenDriver;
import eu._5gzorro.manager.service.ProductOrderDriver;
import io.reactivex.rxjava3.disposables.Disposable;
import org.slf4j.Logger;
Expand All @@ -12,6 +15,7 @@

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Optional;

@ConditionalOnProperty("spring.kafka.enabled")
@Service
Expand All @@ -22,13 +26,18 @@ public class ProductOrderKafkaService extends AbstractProducer<ProductOrderUpdat
private String productOrderTopic;

private final ProductOrderDriver driver;
private final DerivativeSpectokenDriver derivativeSpectokenDriver;

private Disposable kafkaPublishDisposable;

private final OrderOfferMappingRepository orderOfferMappingRepository;

public ProductOrderKafkaService(
KafkaTemplate<String, ProductOrderUpdateEvent> kafkaTemplate, ProductOrderDriver driver) {
KafkaTemplate<String, ProductOrderUpdateEvent> kafkaTemplate, ProductOrderDriver driver, DerivativeSpectokenDriver derivativeSpectokenDriver, OrderOfferMappingRepository orderOfferMappingRepository) {
super(kafkaTemplate);
this.driver = driver;
this.derivativeSpectokenDriver = derivativeSpectokenDriver;
this.orderOfferMappingRepository = orderOfferMappingRepository;
}

@PostConstruct
Expand All @@ -44,6 +53,11 @@ public void setup() {
productOrderTopic,
productOrderUpdateEvent.getDeduplicationId(),
productOrderUpdateEvent);

if (productOrderUpdateEvent.isDeleted() && productOrderUpdateEvent.isSpectrum()) {
Optional<OrderOfferMapping> optionalOrderOfferMapping = orderOfferMappingRepository.findByOrderDid(productOrderUpdateEvent.getDid());
optionalOrderOfferMapping.ifPresent(orderOfferMapping -> derivativeSpectokenDriver.redeemDerivativeSpectoken(orderOfferMapping.getOfferDid(), productOrderUpdateEvent.getSellerName()));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ public Observable<ProductOrderUpdateEvent> productOrderObservable() {
updateWrapper -> {
ProductOrder productOrder = updateWrapper.getProductOrder();

// if (updateWrapper.getUpdateType().equals(OrderUpdateType.END)) {
// rpcClient.startFlowDynamic(RedeemDerivativeSpecTokenFlow.class, productOrder.getOfferDid())
// }

ObjectMapper objectMapper = new ObjectMapper();
ThreeTenModule module = new ThreeTenModule();
module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
Expand All @@ -203,7 +199,9 @@ public Observable<ProductOrderUpdateEvent> productOrderObservable() {
.setDid(productOrder.getOfferDid())
.setInvitations(productOrder.getDidInvitations())
.setIdentifier(productOrder.getLinearId().getId().toString())
.setDeleted(updateWrapper.getUpdateType().equals(OrderUpdateType.END));
.setDeleted(updateWrapper.getUpdateType().equals(OrderUpdateType.END))
.setSpectrum(OfferType.SPECTRUM.equals(productOrder.getOfferType()))
.setSellerName(productOrder.getSeller().getName().toString());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import eu._5gzorro.manager.dlt.corda.flows.spectoken.CreateDerivativeSpecTokenTypeFromOfferFlow;
import eu._5gzorro.manager.dlt.corda.flows.spectoken.GetDerivativeSpecTokensFlow;
import eu._5gzorro.manager.dlt.corda.flows.spectoken.IssueDerivativeSpecTokenToHolderFlow;
import eu._5gzorro.manager.dlt.corda.flows.spectoken.RedeemDerivativeSpecTokenFlow;
import eu._5gzorro.manager.dlt.corda.models.types.OfferType;
import eu._5gzorro.manager.dlt.corda.service.rpc.NodeRPC;
import eu._5gzorro.manager.dlt.corda.service.rpc.RPCSyncService;
import eu._5gzorro.manager.dlt.corda.states.DerivativeSpecTokenType;
import eu._5gzorro.manager.domain.ProductOfferDetails;
import eu._5gzorro.manager.domain.events.enums.OrderUpdateType;
import eu._5gzorro.manager.domain.events.enums.UpdateType;
import eu._5gzorro.manager.service.DerivativeSpectokenDriver;
import eu._5gzorro.manager.service.identity.DIDToDLTIdentityService;
Expand Down Expand Up @@ -95,6 +98,12 @@ public List<GetDerivativeSpectokenResponse> getDerivativeSpectokens() throws Exe
return derivativeSpectokens;
}

@Override
public void redeemDerivativeSpectoken(String offerDid, String sellerName) {
Party seller = rpcClient.wellKnownPartyFromX500Name(CordaX500Name.parse(sellerName));
rpcClient.startFlowDynamic(RedeemDerivativeSpecTokenFlow.class, offerDid, seller);
}

private Party findRegulatorNode() {
return regulatorNodeNames.stream()
.map(CordaX500Name::parse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ProductOrderUpdateEvent {
private OrderUpdateType updateType;
private String deduplicationId;
private boolean deleted;
private boolean spectrum;
private String sellerName;

public String getIdentifier() {
return identifier;
Expand Down Expand Up @@ -91,6 +93,24 @@ public boolean isDeleted() {
return deleted;
}

public boolean isSpectrum() {
return spectrum;
}

public ProductOrderUpdateEvent setSpectrum(boolean spectrum) {
this.spectrum = spectrum;
return this;
}

public String getSellerName() {
return sellerName;
}

public ProductOrderUpdateEvent setSellerName(String sellerName) {
this.sellerName = sellerName;
return this;
}

@Override
public String toString() {
return "ProductOrderUpdateEvent{" +
Expand All @@ -102,6 +122,8 @@ public String toString() {
", updateType=" + updateType +
", deduplicationId='" + deduplicationId + '\'' +
", deleted=" + deleted +
", spectrum=" + spectrum +
", seller=" + sellerName +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


import eu._5gzorro.manager.domain.ProductOfferDetails;
import eu._5gzorro.manager.domain.events.enums.OrderUpdateType;
import eu._5gzorro.tm_forum.models.spectoken.GetDerivativeSpectokenResponse;
import net.corda.core.identity.Party;

import javax.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -19,4 +21,5 @@ public interface DerivativeSpectokenDriver {

List<GetDerivativeSpectokenResponse> getDerivativeSpectokens() throws ExecutionException, InterruptedException;

void redeemDerivativeSpectoken(String offerDid, String sellerName);
}

0 comments on commit 52db46d

Please sign in to comment.