Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
adapting for compute use case
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Sep 7, 2020
1 parent e9dafb0 commit 435b0f1
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 6 deletions.
40 changes: 40 additions & 0 deletions src/main/java/io/keyko/nevermined/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
import io.keyko.nevermined.models.service.Service;
import io.keyko.nevermined.models.service.types.ComputingService;
import io.reactivex.Flowable;

Expand Down Expand Up @@ -194,6 +195,18 @@ public interface AssetsAPI {
*/
Flowable<OrderResult> order(DID did, int serviceDefinitionId) throws OrderException;

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did of the DDO
* @return OrderResult
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
OrderResult orderDirect(DID did) throws OrderException, ServiceException, EscrowRewardException;


/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
Expand All @@ -206,6 +219,33 @@ public interface AssetsAPI {
*/
OrderResult orderDirect(DID did, int serviceDefinitionId) throws OrderException, ServiceException, EscrowRewardException;


/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did of the DDO
* @param serviceTypes service type to order
* @return OrderResult
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
OrderResult orderDirect(DID did, Service.ServiceTypes serviceTypes) throws OrderException, ServiceException, EscrowRewardException;


/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did of the DDO
* @param serviceDefinitionId the service definition id
* @param serviceTypes service type to order
* @return OrderResult
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
OrderResult orderDirect(DID did, int serviceDefinitionId, Service.ServiceTypes serviceTypes) throws OrderException, ServiceException, EscrowRewardException;

/**
* Executes a remote service associated with an asset and serviceAgreementId
* @param agreementId the agreement id
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/keyko/nevermined/api/impl/AssetsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
import io.keyko.nevermined.models.service.Service;
import io.keyko.nevermined.models.service.types.ComputingService;
import io.reactivex.Flowable;

Expand Down Expand Up @@ -136,6 +137,19 @@ public Flowable<OrderResult> order(DID did, int serviceDefinitionId) throws Orde
return neverminedManager.purchaseAssetFlowable(did, serviceDefinitionId);
}

public OrderResult orderDirect(DID did) throws OrderException, ServiceException, EscrowRewardException {
return neverminedManager.purchaseAssetDirect(did);
}

public OrderResult orderDirect(DID did, Service.ServiceTypes serviceTypes) throws OrderException, ServiceException, EscrowRewardException {
return neverminedManager.purchaseAssetDirect(did, serviceTypes);
}

public OrderResult orderDirect(DID did, int serviceDefinitionId, Service.ServiceTypes serviceTypes) throws OrderException, ServiceException, EscrowRewardException {
return neverminedManager.purchaseAssetDirect(did, serviceDefinitionId, serviceTypes);
}


public OrderResult orderDirect(DID did, int serviceDefinitionId) throws OrderException, ServiceException, EscrowRewardException {
return neverminedManager.purchaseAssetDirect(did, serviceDefinitionId);
}
Expand Down
61 changes: 57 additions & 4 deletions src/main/java/io/keyko/nevermined/manager/NeverminedManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ public boolean isConditionFulfilled(String serviceAgreementId, Condition.Conditi
return false;
}

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did
* @return true if the asset was purchased successfully, if not false
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
public OrderResult purchaseAssetDirect(DID did)
throws OrderException, ServiceException, EscrowRewardException {
return purchaseAssetDirect(did, -1, Service.ServiceTypes.ACCESS);
}

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
Expand All @@ -349,6 +363,37 @@ public boolean isConditionFulfilled(String serviceAgreementId, Condition.Conditi
*/
public OrderResult purchaseAssetDirect(DID did, int serviceIndex)
throws OrderException, ServiceException, EscrowRewardException {
return purchaseAssetDirect(did, serviceIndex, null);
}

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did
* @param serviceType Service to purchase
* @return true if the asset was purchased successfully, if not false
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
public OrderResult purchaseAssetDirect(DID did, Service.ServiceTypes serviceType)
throws OrderException, ServiceException, EscrowRewardException {
return purchaseAssetDirect(did, -1, serviceType);
}

/**
* Purchases an Asset represented by a DID. It implies to initialize a Service Agreement between publisher and consumer
*
* @param did the did
* @param serviceIndex the index of the service
* @param serviceType Service to purchase
* @return true if the asset was purchased successfully, if not false
* @throws OrderException OrderException
* @throws ServiceException ServiceException
* @throws EscrowRewardException EscrowRewardException
*/
public OrderResult purchaseAssetDirect(DID did, int serviceIndex, Service.ServiceTypes serviceType)
throws OrderException, ServiceException, EscrowRewardException {

String serviceAgreementId = ServiceAgreementHandler.generateSlaId();
OrderResult orderResult;
Expand All @@ -361,11 +406,19 @@ public OrderResult purchaseAssetDirect(DID did, int serviceIndex)
throw new OrderException("Error processing Order with DID " + did.getDid(), e);
}

Service service = ddo.getService(serviceIndex);
Service service;
if (serviceIndex >= 0)
service = ddo.getService(serviceIndex);
else if (serviceType.toString().equals(Service.ServiceTypes.COMPUTE)) {
service = ddo.getComputeService();
serviceIndex = service.index;
} else {
service = ddo.getAccessService();
serviceIndex = service.index;
}

try {
// Step 1. We initialize the Service Agreement

final boolean isInitialized = initializeServiceAgreementDirect(ddo, serviceIndex, serviceAgreementId);
if (!isInitialized) {
throw new ServiceAgreementException(serviceAgreementId, "Service Agreement not Initialized");
Expand Down Expand Up @@ -402,7 +455,7 @@ public OrderResult purchaseAssetDirect(DID did, int serviceIndex)
// Step 2. We fulfull the Lock Reward (we make the payment)
this.fulfillLockReward(ddo, serviceIndex, eventServiceAgreementId);
final boolean isFulfilled = isConditionFulfilled(serviceAgreementId, Condition.ConditionTypes.lockReward);
orderResult = new OrderResult(serviceAgreementId, isFulfilled, false);
orderResult = new OrderResult(serviceAgreementId, isFulfilled, false, serviceIndex);

} catch (LockRewardFulfillException e) {
this.fulfillEscrowReward(ddo, serviceIndex, serviceAgreementId);
Expand Down Expand Up @@ -472,7 +525,7 @@ else if (service.type.equals(Service.ServiceTypes.COMPUTE.toString()))
return conditionFulilledEvent;
}
})
.map(event -> new OrderResult(serviceAgreementId, true, false))
.map(event -> new OrderResult(serviceAgreementId, true, false, serviceIndex))
// TODO timout of the condition
.timeout(120, TimeUnit.SECONDS)
.onErrorReturn(throwable -> {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/keyko/nevermined/models/DDO.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,16 @@ public AccessService getAccessService() {
return null;
}

@JsonIgnore
public ComputingService getComputeService() {
for (Service service : services) {
if (service.type.equals(Service.ServiceTypes.COMPUTE.toString())) {
return (ComputingService) service;
}
}
return null;
}


@JsonIgnore
public static DDO cleanFileUrls(DDO ddo) {
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/io/keyko/nevermined/models/asset/OrderResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
public class OrderResult extends AbstractModel implements FromJsonToModel {

private String serviceAgreementId;
private String executionId;
private Boolean accessFullfilled = false;
private Boolean refund = false;
private int serviceIndex = -1;

public OrderResult(String serviceAgreementId, Boolean accessFullfilled, Boolean refund) {

public OrderResult(String serviceAgreementId, Boolean accessFullfilled, Boolean refund, int serviceIndex) {
setServiceAgreementId(serviceAgreementId);
this.accessFullfilled = accessFullfilled;
this.refund = refund;
this.serviceIndex = serviceIndex;
}

public OrderResult(String serviceAgreementId, Boolean accessFullfilled, Boolean refund) {
this(serviceAgreementId, accessFullfilled, refund, -1);
}

public String getServiceAgreementId() {
Expand All @@ -40,4 +46,20 @@ public Boolean isRefund() {
public void setRefund(Boolean refund) {
this.refund = refund;
}

public int getServiceIndex() {
return serviceIndex;
}

public void setServiceIndex(int serviceIndex) {
this.serviceIndex = serviceIndex;
}

public String getExecutionId() {
return executionId;
}

public void setExecutionId(String executionId) {
this.executionId = executionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.keyko.nevermined.exceptions.DIDFormatException;
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.asset.AssetMetadata;
import jnr.ffi.annotations.In;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down Expand Up @@ -37,6 +41,19 @@ public static class Stage {
@JsonProperty
public Output output;

public static List<Input> parseInputs(String stringInputs) {
final String[] inputs = stringInputs.split(",");
List listInputs = new ArrayList();
for (String entry: inputs) {
try {
Input input = new Input();
input.id = new DID(entry);
listInputs.add(input);
} catch (DIDFormatException e) {
}
}
return listInputs;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
Expand All @@ -61,6 +78,16 @@ public static class Container {
@JsonProperty
public String checksum;

public static Container parseString(String containerLine) {
final String[] containerTokens = containerLine.split(":");
Container container = new Container();
container.image = containerTokens[0];
if (containerTokens.length>1)
container.tag = containerTokens[1];
else
container.tag = "latest";
return container;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
Expand All @@ -73,6 +100,8 @@ public static class Input {
@JsonProperty
public DID id;



}

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down

0 comments on commit 435b0f1

Please sign in to comment.