2 * Copyright (C) The Arvados Authors. All rights reserved.
4 * SPDX-License-Identifier: AGPL-3.0 OR Apache-2.0
8 package org.arvados.client.facade;
10 import com.google.common.collect.Lists;
11 import org.arvados.client.api.client.CollectionsApiClient;
12 import org.arvados.client.api.client.GroupsApiClient;
13 import org.arvados.client.api.client.KeepWebApiClient;
14 import org.arvados.client.api.client.UsersApiClient;
15 import org.arvados.client.api.model.*;
16 import org.arvados.client.api.model.argument.Filter;
17 import org.arvados.client.api.model.argument.ListArgument;
18 import org.arvados.client.config.FileConfigProvider;
19 import org.arvados.client.config.ConfigProvider;
20 import org.arvados.client.logic.collection.FileToken;
21 import org.arvados.client.logic.collection.ManifestDecoder;
22 import org.arvados.client.logic.keep.FileDownloader;
23 import org.arvados.client.logic.keep.FileUploader;
24 import org.arvados.client.logic.keep.KeepClient;
25 import org.slf4j.Logger;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
32 public class ArvadosFacade {
34 private final ConfigProvider config;
35 private final Logger log = org.slf4j.LoggerFactory.getLogger(ArvadosFacade.class);
36 private CollectionsApiClient collectionsApiClient;
37 private GroupsApiClient groupsApiClient;
38 private UsersApiClient usersApiClient;
39 private FileDownloader fileDownloader;
40 private FileUploader fileUploader;
41 private static final String PROJECT = "project";
42 private static final String SUBPROJECT = "sub-project";
44 public ArvadosFacade(ConfigProvider config) {
49 public ArvadosFacade() {
50 this.config = new FileConfigProvider();
54 private void setFacadeFields() {
55 collectionsApiClient = new CollectionsApiClient(config);
56 groupsApiClient = new GroupsApiClient(config);
57 usersApiClient = new UsersApiClient(config);
58 KeepClient keepClient = new KeepClient(config);
59 ManifestDecoder manifestDecoder = new ManifestDecoder();
60 KeepWebApiClient keepWebApiClient = new KeepWebApiClient(config);
61 fileDownloader = new FileDownloader(keepClient, manifestDecoder, collectionsApiClient, keepWebApiClient);
62 fileUploader = new FileUploader(keepClient, collectionsApiClient, config);
66 * This method downloads single file from collection using Arvados Keep-Web.
67 * File is saved on a drive in specified location and returned.
69 * @param filePathName path to the file in collection. If requested file is stored
70 * directly in collection (not within its subdirectory) this
71 * would be just the name of file (ex. 'file.txt').
72 * Otherwise full file path must be passed (ex. 'folder/file.txt')
73 * @param collectionUuid uuid of collection containing requested file
74 * @param pathToDownloadFolder path to location in which file should be saved.
75 * Passed location must be a directory in which file of
76 * that name does not already exist.
77 * @return downloaded file
79 public File downloadFile(String filePathName, String collectionUuid, String pathToDownloadFolder) {
80 return fileDownloader.downloadSingleFileUsingKeepWeb(filePathName, collectionUuid, pathToDownloadFolder);
84 * This method downloads all files from collection.
85 * Directory named by collection uuid is created in specified location,
86 * files are saved on a drive in this directory and list with downloaded
89 * @param collectionUuid uuid of collection from which files are downloaded
90 * @param pathToDownloadFolder path to location in which files should be saved.
91 * New folder named by collection uuid, containing
92 * downloaded files, is created in this location.
93 * Passed location must be a directory in which folder
94 * of that name does not already exist.
95 * @param usingKeepWeb if set to true files will be downloaded using Keep Web.
96 * If set to false files will be downloaded using Keep Server API.
97 * @return list containing downloaded files
99 public List<File> downloadCollectionFiles(String collectionUuid, String pathToDownloadFolder, boolean usingKeepWeb) {
101 return fileDownloader.downloadFilesFromCollectionUsingKeepWeb(collectionUuid, pathToDownloadFolder);
102 return fileDownloader.downloadFilesFromCollection(collectionUuid, pathToDownloadFolder);
106 * Lists all FileTokens (objects containing information about files) for
107 * specified collection.
108 * Information in each FileToken includes file path, name, size and position
111 * @param collectionUuid uuid of collection for which FileTokens are listed
112 * @return list containing FileTokens for each file in specified collection
114 public List<FileToken> listFileInfoFromCollection(String collectionUuid) {
115 return fileDownloader.listFileInfoFromCollection(collectionUuid);
119 * Creates and uploads new collection containing passed files.
120 * Created collection has a default name and is uploaded to user's 'Home' project.
122 * @see ArvadosFacade#upload(List, String, String)
123 * @param files list of files to be uploaded within new collection
124 * @return collection object mapped from JSON that is returned from server after successful upload
126 public Collection upload(List<File> files) {
127 return upload(files, null, null);
131 * Creates and uploads new collection containing a single file.
132 * Created collection has a default name and is uploaded to user's 'Home' project.
134 * @see ArvadosFacade#upload(List, String, String)
135 * @param file file to be uploaded
136 * @return collection object mapped from JSON that is returned from server after successful upload
138 public Collection upload(File file) {
139 return upload(Collections.singletonList(file), null, null);
143 * Uploads new collection with specified name and containing selected files
144 * to an existing project.
146 * @param sourceFiles list of files to be uploaded within new collection
147 * @param collectionName name for the newly created collection.
148 * Collection with that name cannot be already created
149 * in specified project. If null is passed
150 * then collection name is set to default, containing
151 * phrase 'New Collection' and a timestamp.
152 * @param projectUuid uuid of the project in which created collection is to be included.
153 * If null is passed then collection is uploaded to user's 'Home' project.
154 * @return collection object mapped from JSON that is returned from server after successful upload
156 public Collection upload(List<File> sourceFiles, String collectionName, String projectUuid) {
157 return fileUploader.upload(sourceFiles, collectionName, projectUuid);
161 * Uploads a file to a specified collection.
163 * @see ArvadosFacade#uploadToExistingCollection(List, String)
164 * @param file file to be uploaded to existing collection. Filenames must be unique
165 * in comparison with files already existing within collection.
166 * @param collectionUUID UUID of collection to which files should be uploaded
167 * @return collection object mapped from JSON that is returned from server after successful upload
169 public Collection uploadToExistingCollection(File file, String collectionUUID) {
170 return fileUploader.uploadToExistingCollection(Collections.singletonList(file), collectionUUID);
174 * Uploads multiple files to an existing collection.
176 * @param files list of files to be uploaded to existing collection.
177 * File names must be unique - both within passed list and
178 * in comparison with files already existing within collection.
179 * @param collectionUUID UUID of collection to which files should be uploaded
180 * @return collection object mapped from JSON that is returned from server after successful upload
182 public Collection uploadToExistingCollection(List<File> files, String collectionUUID) {
183 return fileUploader.uploadToExistingCollection(files, collectionUUID);
187 * Creates and uploads new empty collection to specified project.
189 * @param collectionName name for the newly created collection.
190 * Collection with that name cannot be already created
191 * in specified project.
192 * @param projectUuid uuid of project that will contain uploaded empty collection.
193 * To select home project pass current user's uuid from getCurrentUser()
194 * @return collection object mapped from JSON that is returned from server after successful upload
195 * @see ArvadosFacade#getCurrentUser()
197 public Collection createEmptyCollection(String collectionName, String projectUuid) {
198 Collection collection = new Collection();
199 collection.setOwnerUuid(projectUuid);
200 collection.setName(collectionName);
201 return collectionsApiClient.create(collection);
205 * Returns current user information based on Api Token provided via configuration
207 * @return user object mapped from JSON that is returned from server based on provided Api Token.
208 * It contains information about user who has this token assigned.
210 public User getCurrentUser() {
211 return usersApiClient.current();
215 * Gets uuid of current user based on api Token provided in configuration and uses it to list all
216 * projects that this user owns in Arvados.
218 * @return GroupList containing all groups that current user is owner of.
219 * @see ArvadosFacade#getCurrentUser()
221 public GroupList showGroupsOwnedByCurrentUser() {
222 ListArgument listArgument = ListArgument.builder()
223 .filters(Arrays.asList(
224 Filter.of("owner_uuid", Filter.Operator.LIKE, getCurrentUser().getUuid()),
225 Filter.of("group_class", Filter.Operator.IN, Lists.newArrayList(PROJECT, SUBPROJECT)
228 GroupList groupList = groupsApiClient.list(listArgument);
229 log.debug("Groups owned by user:");
230 groupList.getItems().forEach(m -> log.debug(m.getUuid() + " -- " + m.getName()));
236 * Gets uuid of current user based on api Token provided in configuration and uses it to list all
237 * projects that this user has read access to in Arvados.
239 * @return GroupList containing all groups that current user has read access to.
241 public GroupList showGroupsAccessibleByCurrentUser() {
242 ListArgument listArgument = ListArgument.builder()
243 .filters(Collections.singletonList(
244 Filter.of("group_class", Filter.Operator.IN, Lists.newArrayList(PROJECT, SUBPROJECT)
247 GroupList groupList = groupsApiClient.list(listArgument);
248 log.debug("Groups accessible by user:");
249 groupList.getItems().forEach(m -> log.debug(m.getUuid() + " -- " + m.getName()));
255 * Filters all collections from selected project and returns list of those that contain passed String in their name.
256 * Operator "LIKE" is used so in order to obtain certain collection it is sufficient to pass just part of its name.
257 * Returned collections in collectionList are ordered by date of creation (starting from oldest one).
259 * @param collectionName collections containing this param in their name will be returned.
260 * Passing a wildcard is possible - for example passing "a%" searches for
261 * all collections starting with "a".
262 * @param projectUuid uuid of project in which will be searched for collections with given name. To search home
263 * project provide user uuid (from getCurrentUser())
264 * @return object CollectionList containing all collections matching specified name criteria
265 * @see ArvadosFacade#getCurrentUser()
267 public CollectionList getCollectionsFromProjectByName(String collectionName, String projectUuid) {
268 ListArgument listArgument = ListArgument.builder()
269 .filters(Arrays.asList(
270 Filter.of("owner_uuid", Filter.Operator.LIKE, projectUuid),
271 Filter.of("name", Filter.Operator.LIKE, collectionName)
273 .order(Collections.singletonList("created_at"))
276 return collectionsApiClient.list(listArgument);
280 * Gets project details by uuid.
282 * @param projectUuid uuid of project
283 * @return Group object containing information about project
285 public Group getProjectByUuid(String projectUuid) {
286 Group project = groupsApiClient.get(projectUuid);
287 log.debug("Retrieved " + project.getName() + " with UUID: " + project.getUuid());
292 * Creates new project that will be a subproject of "home" for current user.
294 * @param projectName name for the newly created project
295 * @return Group object containing information about created project
296 * (mapped from JSON returned from server after creating the project)
298 public Group createNewProject(String projectName) {
299 Group project = new Group();
300 project.setName(projectName);
301 project.setGroupClass(PROJECT);
302 Group createdProject = groupsApiClient.create(project);
303 log.debug("Project " + createdProject.getName() + " created with UUID: " + createdProject.getUuid());
304 return createdProject;
308 * Deletes collection with specified uuid.
310 * @param collectionUuid uuid of collection to be deleted. User whose token is provided in configuration
311 * must be authorized to delete such collection.
312 * @return collection object with deleted collection (mapped from JSON returned from server after deleting the collection)
314 public Collection deleteCollection(String collectionUuid) {
315 Collection deletedCollection = collectionsApiClient.delete(collectionUuid);
316 log.debug("Collection: " + collectionUuid + " deleted.");
317 return deletedCollection;