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

Commit

Permalink
Added ability to fetch compute logs in the asset api
Browse files Browse the repository at this point in the history
- Added tests
- Updated version of common-utils
  • Loading branch information
r-marques committed Oct 7, 2020
1 parent 5908342 commit 39b4503
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<final-name>nevermined-sdk</final-name>

<web3j.version>4.5.18</web3j.version>
<common-utils.version>0.3.1</common-utils.version>
<common-utils.version>0.3.2</common-utils.version>

<nevermined.contracts.version>0.4.1</nevermined.contracts.version>
<ocean.secretstore.version>0.1.4</ocean.secretstore.version>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/keyko/nevermined/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
Expand Down Expand Up @@ -357,5 +358,15 @@ public interface AssetsAPI {
*/
Boolean getPermissions(DID did, String subjectAddress) throws DDOException;

/**
* Get the logs for the compute job with executionId and serviceAgreementId
* @param serviceAgreementId The service agreement id for the compute service
* @param executionId The execution id of the compute job
* @param consumerAddress The address of the consumer that executed the compute job
* @return a list of compute logs
* @throws ServiceException ServiceException
*/
List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException;

}
7 changes: 7 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 @@ -10,6 +10,7 @@
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
Expand Down Expand Up @@ -68,6 +69,12 @@ public DDO createComputeService(AssetMetadata metadata, ProviderConfig providerC
return neverminedManager.registerComputeService(metadata, providerConfig, computingProvider);
}

@Override
public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException {
return neverminedManager.getComputeLogs(serviceAgreementId, executionId, consumerAddress, providerConfig);
}

@Override
public DDO resolve(DID did) throws DDOException {
return neverminedManager.resolveDID(did);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/keyko/nevermined/external/GatewayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -435,4 +436,43 @@ private static String getExecutionId(String bodyResponse) throws IOException {
return responseMap.get("workflowId");
}

/**
* Calls a gateway endpoint to get the compute logs
*
* @param serviceEndpoint the gateway service endpoint
* @param consumerAddress the address of the consumer of the compute to the data job
* @param signature the signature of the executionId
* @return a list of compute logs.
* @throws ServiceException ServiceException
*/
public static List<ComputeLogs> getComputeLogs(String serviceEndpoint, String consumerAddress, String signature)
throws ServiceException {

HttpResponse response;
List<ComputeLogs> logs;
Map<String, String> headers = new HashMap<>();
headers.put(HEADER_CONSUMER_ADDRESS, consumerAddress);
headers.put(HEADER_SIGNATURE, signature);

try {
response = HttpHelper.httpClientGet(serviceEndpoint, headers);
} catch (HttpException e) {
log.error("Exception getting the compute logs: " + e.getMessage());
throw new ServiceException("Exception getting the compute logs", e);
}

if (response.getStatusCode() != 200 && response.getStatusCode() != 201) {
log.error("Unable to get the compute logs: " + response.toString());
throw new ServiceException("Unable to get logs: " + response.toString());
}

try {
logs = ComputeLogs.fromJSON(new TypeReference<>() {}, response.getBody());
} catch (IOException e) {
log.error("Exception parsing the compute logs: " + e.getMessage());
throw new ServiceException("Unable to parse logs", e);
}
return logs;
}

}
26 changes: 26 additions & 0 deletions src/main/java/io/keyko/nevermined/manager/NeverminedManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.keyko.nevermined.models.Order;
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.gateway.ExecuteService;
import io.keyko.nevermined.models.service.*;
import io.keyko.nevermined.models.service.types.*;
Expand Down Expand Up @@ -1111,6 +1112,31 @@ public GatewayService.ServiceExecutionResult executeComputeService(String agreem

}

/**
* Generates the service endpoint and signature and calls the gateway to get the logs of a compute job
*
* @param serviceAgreementId The agreement id for the compute to the data
* @param executionId The id of the compute job
* @param consumerAddress The address of the consumer of the compute to the data job
* @param providerConfig The configuration of the provider.
* @return A list of log lines.
* @throws ServiceException Service Exception
*/
public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException {
String serviceEndpoint = providerConfig.getAccessEndpoint()
.replace("/access", "/compute/logs/");
serviceEndpoint += serviceAgreementId + "/" + executionId;
String signature;
try {
signature = generateSignature(executionId);
} catch (IOException | CipherException e) {
log.error("Exception generating signature: ", e.getMessage());
throw new ServiceException("Unable to generate signature", e);
}
return GatewayService.getComputeLogs(serviceEndpoint, consumerAddress, signature);
}


// TODO: to be implemented
public Order getOrder(String orderId) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/keyko/nevermined/models/gateway/ComputeLogs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.keyko.nevermined.models.gateway;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.keyko.nevermined.models.AbstractModel;
import io.keyko.nevermined.models.FromJsonToModel;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ComputeLogs extends AbstractModel implements FromJsonToModel {
@JsonProperty
public String content;

@JsonProperty
public String podName;
}
11 changes: 11 additions & 0 deletions src/test/java/io/keyko/nevermined/api/AssetsApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.service.ProviderConfig;
import io.keyko.nevermined.models.service.Service;
import io.keyko.nevermined.models.service.types.ComputingService;
Expand Down Expand Up @@ -291,6 +292,16 @@ public void ownerDownload() throws Exception {
assertFalse(shouldntBeDownloaded);
}

// Ignore test until e2e compute elements are automated
@Ignore
@Test
public void testComputeLogs() throws Exception {
List<ComputeLogs> computeLogs = neverminedAPI.getAssetsAPI()
.getComputeLogs("9bc29134bb0b466a8ca9e171e649b5a647f2fc098c894a6ea0eaf4ec2ba4e535",
"nevermined-compute-df4g7", neverminedAPI.getMainAccount().address,
providerConfig);
assertNotNull(computeLogs);
}

// TODO: Automate the Compute use cases e2e
// Ignoring test until the e2e compute components are automated
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/io/keyko/nevermined/models/ComputeLogsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.keyko.nevermined.models;

import com.fasterxml.jackson.core.type.TypeReference;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class ComputeLogsTest {

@Test
public void fromJsonTest() throws IOException {
String logsJson = "[{\"content\":\"line1\",\"podName\":\"configurator\"},{\"content\":\"line2\",\"podName\":\"configurator\"}]";
List<ComputeLogs> computeLogs = ComputeLogs.fromJSON(new TypeReference<>() {
}, logsJson);

assertEquals("configurator", computeLogs.get(0).podName);
assertEquals("line1", computeLogs.get(0).content);
assertEquals("configurator", computeLogs.get(1).podName);
assertEquals("line2", computeLogs.get(1).content);
}
}

0 comments on commit 39b4503

Please sign in to comment.