Skip to content
Permalink
Browse files
Fetching child files and directories of a parent resource through met…
…adata api
  • Loading branch information
DImuthuUpe committed Aug 16, 2020
1 parent b71efaf commit 2b4539dfc5799523112c33b3977635a31c23a08c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 16 deletions.
@@ -76,7 +76,6 @@ public static final class SyncRPCRequestBuilder {
private String agentId;
private String method;
private Map<String, String> parameters = new HashMap<>();
private String returnAddress;
private String messageId;

private SyncRPCRequestBuilder() {
@@ -101,11 +100,6 @@ public SyncRPCRequestBuilder withParameter(String key, String value) {
return this;
}

public SyncRPCRequestBuilder withReturnAddress(String returnAddress) {
this.returnAddress = returnAddress;
return this;
}

public SyncRPCRequestBuilder withMessageId(String messageId) {
this.messageId = messageId;
return this;
@@ -116,7 +110,6 @@ public SyncRPCRequest build() {
syncRPCRequest.setAgentId(agentId);
syncRPCRequest.setMethod(method);
syncRPCRequest.setParameters(parameters);
syncRPCRequest.setReturnAddress(returnAddress);
syncRPCRequest.setMessageId(messageId);
return syncRPCRequest;
}
@@ -64,6 +64,21 @@ public String resolveRPCRequest(SyncRPCRequest request) throws Exception {
return mapper.writeValueAsString(fileResourceMetadata);
}
break;
case "getChildFileResourceMetadata":
resourceId = request.getParameters().get("resourceId");
resourceType = request.getParameters().get("resourceType");
resourceToken = request.getParameters().get("resourceToken");
String childPath = request.getParameters().get("childPath");
mftAuthorizationToken = request.getParameters().get("mftAuthorizationToken");

metadataCollectorOp = MetadataCollectorResolver.resolveMetadataCollector(resourceType);
if (metadataCollectorOp.isPresent()) {
MetadataCollector metadataCollector = metadataCollectorOp.get();
metadataCollector.init(resourceServiceHost, resourceServicePort, secretServiceHost, secretServicePort);
FileResourceMetadata fileResourceMetadata = metadataCollector.getFileResourceMetadata(resourceId, childPath, resourceToken);
return mapper.writeValueAsString(fileResourceMetadata);
}
break;
case "getDirectoryResourceMetadata":
resourceId = request.getParameters().get("resourceId");
resourceType = request.getParameters().get("resourceType");
@@ -78,6 +93,21 @@ public String resolveRPCRequest(SyncRPCRequest request) throws Exception {
return mapper.writeValueAsString(dirResourceMetadata);
}
break;
case "getChildDirectoryResourceMetadata":
resourceId = request.getParameters().get("resourceId");
resourceType = request.getParameters().get("resourceType");
resourceToken = request.getParameters().get("resourceToken");
childPath = request.getParameters().get("childPath");
mftAuthorizationToken = request.getParameters().get("mftAuthorizationToken");

metadataCollectorOp = MetadataCollectorResolver.resolveMetadataCollector(resourceType);
if (metadataCollectorOp.isPresent()) {
MetadataCollector metadataCollector = metadataCollectorOp.get();
metadataCollector.init(resourceServiceHost, resourceServicePort, secretServiceHost, secretServicePort);
DirectoryResourceMetadata dirResourceMetadata = metadataCollector.getDirectoryResourceMetadata(resourceId, childPath, resourceToken);
return mapper.writeValueAsString(dirResourceMetadata);
}
break;
}
logger.error("Unknown method type specified {}", request.getMethod());
throw new Exception("Unknown method " + request.getMethod());
@@ -144,19 +144,33 @@ public void getResourceAvailability(ResourceAvailabilityRequest request, StreamO
}
}

/**
* Fetches metadata for a specified file resource. This has 2 modes
* 1. Fetch the metadata of the exact file pointed in the resourceId. This assumes resourceId is an id of a file resource
* 2. Fetch the metadata of a child directory in the parent directory. To do this, childPath should be provided explicitly
* and resourceId is the parent resource id. Here resource id should be an id of directory resource
*/
@Override
public void getFileResourceMetadata(FetchResourceMetadataRequest request, StreamObserver<FileMetadataResponse> responseObserver) {

try {
SyncRPCResponse rpcResponse = agentRPCClient.sendSyncRequest(SyncRPCRequest.SyncRPCRequestBuilder.builder()
SyncRPCRequest.SyncRPCRequestBuilder requestBuilder = SyncRPCRequest.SyncRPCRequestBuilder.builder()
.withAgentId(request.getTargetAgentId())
.withMessageId(UUID.randomUUID().toString())
.withMethod("getFileResourceMetadata")
.withParameter("resourceId", request.getResourceId())
.withParameter("resourceType", request.getResourceType())
.withParameter("resourceToken", request.getResourceToken())
.withParameter("mftAuthorizationToken", request.getMftAuthorizationToken())
.build());
.withParameter("mftAuthorizationToken", request.getMftAuthorizationToken());

if (request.getChildPath().isEmpty()) {
requestBuilder.withMethod("getFileResourceMetadata");
} else {
// If a childPath is specified, look for child directories in the given parent resource id
requestBuilder.withMethod("getChildFileResourceMetadata");
requestBuilder.withParameter("childPath", request.getChildPath());
}

SyncRPCResponse rpcResponse = agentRPCClient.sendSyncRequest(requestBuilder.build());

switch (rpcResponse.getResponseStatus()) {
case SUCCESS:
@@ -178,18 +192,33 @@ public void getFileResourceMetadata(FetchResourceMetadataRequest request, Stream
}
}

/**
* Fetches metadata for a specified directory resource. This method assumes that the resourceId is an id of
* a directory resource. This has 2 modes
* 1. Fetch the metadata of the exact directory pointed in the resourceId
* 2. Fetch the metadata of a child directory in the parent directory. To do this, childPath should be provided explicitly
* and resourceId is the parent resource id.
*/
@Override
public void getDirectoryResourceMetadata(FetchResourceMetadataRequest request, StreamObserver<DirectoryMetadataResponse> responseObserver) {
try {
SyncRPCResponse rpcResponse = agentRPCClient.sendSyncRequest(SyncRPCRequest.SyncRPCRequestBuilder.builder()
SyncRPCRequest.SyncRPCRequestBuilder requestBuilder = SyncRPCRequest.SyncRPCRequestBuilder.builder()
.withAgentId(request.getTargetAgentId())
.withMessageId(UUID.randomUUID().toString())
.withMethod("getDirectoryResourceMetadata")
.withParameter("resourceId", request.getResourceId())
.withParameter("resourceType", request.getResourceType())
.withParameter("resourceToken", request.getResourceToken())
.withParameter("mftAuthorizationToken", request.getMftAuthorizationToken())
.build());
.withParameter("mftAuthorizationToken", request.getMftAuthorizationToken());

if (request.getChildPath().isEmpty()) {
requestBuilder.withMethod("getDirectoryResourceMetadata");
} else {
// If a childPath is specified, look for child directories in the given parent resource id
requestBuilder.withMethod("getChildDirectoryResourceMetadata");
requestBuilder.withParameter("childPath", request.getChildPath());
}

SyncRPCResponse rpcResponse = agentRPCClient.sendSyncRequest(requestBuilder.build());

switch (rpcResponse.getResponseStatus()) {
case SUCCESS:
@@ -81,7 +81,8 @@ message FetchResourceMetadataRequest {
string resourceBackend = 4;
string resourceCredentialBackend = 5;
string targetAgentId = 7;
string mftAuthorizationToken = 8;
string childPath = 8; // if the child entities of the parent resource are required, set this field
string mftAuthorizationToken = 9;
}

service MFTApiService {
@@ -10,6 +10,7 @@ public class SCPExample {
public static void main(String args[]) throws Exception {
MFTApiServiceGrpc.MFTApiServiceBlockingStub client = MFTApiClient.buildClient("localhost", 7004);

// File metadata
long startTime = System.currentTimeMillis();
FileMetadataResponse fileResourceMetadata = client.getFileResourceMetadata(FetchResourceMetadataRequest.newBuilder()
.setResourceId("remote-ssh-resource2")
@@ -22,6 +23,7 @@ public static void main(String args[]) throws Exception {
System.out.println(fileResourceMetadata);
System.out.println("Time for processing : " + (endTime - startTime) + " ms");

// Directory metadata
startTime = System.currentTimeMillis();
DirectoryMetadataResponse directoryMetadataResponse = client.getDirectoryResourceMetadata(FetchResourceMetadataRequest.newBuilder()
.setResourceId("remote-ssh-dir-resource")
@@ -34,5 +36,35 @@ public static void main(String args[]) throws Exception {
System.out.println("Directory metadata response ");
System.out.println(directoryMetadataResponse);
System.out.println("Time for processing : " + (endTime - startTime) + " ms");

// Child file inside parent directoru
startTime = System.currentTimeMillis();
fileResourceMetadata = client.getFileResourceMetadata(FetchResourceMetadataRequest.newBuilder()
.setResourceId("remote-ssh-dir-resource") // Parent directory resource id
.setResourceType("SCP")
.setResourceToken("local-ssh-cred")
.setTargetAgentId("agent0")
.setChildPath("/tmp/10mb.txt") // Child file path
.build());
endTime = System.currentTimeMillis();
System.out.println("Child file metadata response ");
System.out.println(fileResourceMetadata);
System.out.println("Time for processing : " + (endTime - startTime) + " ms");

// Child directory inside parent directoru
startTime = System.currentTimeMillis();
directoryMetadataResponse = client.getDirectoryResourceMetadata(FetchResourceMetadataRequest.newBuilder()
.setResourceId("remote-ssh-dir-resource") // Parent directory resource id
.setResourceType("SCP")
.setResourceToken("local-ssh-cred")
.setTargetAgentId("agent0")
.setChildPath("/tmp/hsperfdata_root") // Child directory path
.build());
endTime = System.currentTimeMillis();

System.out.println("Child directory metadata response ");
System.out.println(directoryMetadataResponse);
System.out.println("Time for processing : " + (endTime - startTime) + " ms");

}
}

0 comments on commit 2b4539d

Please sign in to comment.