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)
124 public Collection upload(List<File> files) {
125 return upload(files, null, null);
129 * Creates and uploads new collection containing a single file.
130 * Created collection has a default name and is uploaded to user's 'Home' project.
132 * @see ArvadosFacade#upload(List, String, String)
134 public Collection upload(File file) {
135 return upload(Collections.singletonList(file), null, null);
139 * Uploads new collection with specified name and containing selected files
140 * to an existing project.
142 * @param sourceFiles list of files to be uploaded within new collection
143 * @param collectionName name for the newly created collection.
144 * Collection with that name cannot be already created
145 * in specified project. If null is passed
146 * then collection name is set to default, containing
147 * phrase 'New Collection' and a timestamp.
148 * @param projectUuid uuid of the project in which created collection is to be included.
149 * If null is passed then collection is uploaded to user's 'Home' project.
150 * @return collection object mapped from JSON that is returned from server after successful upload
152 public Collection upload(List<File> sourceFiles, String collectionName, String projectUuid) {
153 return fileUploader.upload(sourceFiles, collectionName, projectUuid);
157 * Uploads a file to a specified collection.
159 * @see ArvadosFacade#uploadToExistingCollection(List, String)
161 public Collection uploadToExistingCollection(File file, String collectionUUID) {
162 return fileUploader.uploadToExistingCollection(Collections.singletonList(file), collectionUUID);
166 * Uploads multiple files to an existing collection.
168 * @param files list of files to be uploaded to existing collection.
169 * File names must be unique - both within passed list and
170 * in comparison with files already existing within collection.
171 * @param collectionUUID UUID of collection to which files should be uploaded
172 * @return collection object mapped from JSON that is returned from server after successful upload
174 public Collection uploadToExistingCollection(List<File> files, String collectionUUID) {
175 return fileUploader.uploadToExistingCollection(files, collectionUUID);
179 * Creates and uploads new empty collection to specified project.
181 * @param collectionName name for the newly created collection.
182 * Collection with that name cannot be already created
183 * in specified project.
184 * @param projectUuid uuid of project that will contain uploaded empty collection.
185 * To select home project pass current user's uuid from getCurrentUser()
186 * @return collection object mapped from JSON that is returned from server after successful upload
187 * @see ArvadosFacade#getCurrentUser()
189 public Collection createEmptyCollection(String collectionName, String projectUuid) {
190 Collection collection = new Collection();
191 collection.setOwnerUuid(projectUuid);
192 collection.setName(collectionName);
193 return collectionsApiClient.create(collection);
197 * Returns current user information based on Api Token provided via configuration
199 * @return user object mapped from JSON that is returned from server based on provided Api Token.
200 * It contains information about user who has this token assigned.
202 public User getCurrentUser() {
203 return usersApiClient.current();
207 * Gets uuid of current user based on api Token provided in configuration and uses it to list all
208 * projects that this user owns in Arvados.
210 * @return GroupList containing all groups that current user is owner of.
211 * @see ArvadosFacade#getCurrentUser()
213 public GroupList showGroupsOwnedByCurrentUser() {
214 ListArgument listArgument = ListArgument.builder()
215 .filters(Arrays.asList(
216 Filter.of("owner_uuid", Filter.Operator.LIKE, getCurrentUser().getUuid()),
217 Filter.of("group_class", Filter.Operator.IN, Lists.newArrayList(PROJECT, SUBPROJECT)
220 GroupList groupList = groupsApiClient.list(listArgument);
221 log.debug("Groups owned by user:");
222 groupList.getItems().forEach(m -> log.debug(m.getUuid() + " -- " + m.getName()));
228 * Gets uuid of current user based on api Token provided in configuration and uses it to list all
229 * projects that this user has read access to in Arvados.
231 * @return GroupList containing all groups that current user has read access to.
233 public GroupList showGroupsAccessibleByCurrentUser() {
234 ListArgument listArgument = ListArgument.builder()
235 .filters(Collections.singletonList(
236 Filter.of("group_class", Filter.Operator.IN, Lists.newArrayList(PROJECT, SUBPROJECT)
239 GroupList groupList = groupsApiClient.list(listArgument);
240 log.debug("Groups accessible by user:");
241 groupList.getItems().forEach(m -> log.debug(m.getUuid() + " -- " + m.getName()));
247 * Filters all collections from selected project and returns list of those that contain passed String in their name.
248 * Operator "LIKE" is used so in order to obtain certain collection it is sufficient to pass just part of its name.
249 * Returned collections in collectionList are ordered by date of creation (starting from oldest one).
251 * @param collectionName collections containing this param in their name will be returned.
252 * Passing a wildcard is possible - for example passing "a%" searches for
253 * all collections starting with "a".
254 * @param projectUuid uuid of project in which will be searched for collections with given name. To search home
255 * project provide user uuid (from getCurrentUser())
256 * @return object CollectionList containing all collections matching specified name criteria
257 * @see ArvadosFacade#getCurrentUser()
259 public CollectionList getCollectionsFromProjectByName(String collectionName, String projectUuid) {
260 ListArgument listArgument = ListArgument.builder()
261 .filters(Arrays.asList(
262 Filter.of("owner_uuid", Filter.Operator.LIKE, projectUuid),
263 Filter.of("name", Filter.Operator.LIKE, collectionName)
265 .order(Collections.singletonList("created_at"))
268 return collectionsApiClient.list(listArgument);
272 * Creates new project that will be a subproject of "home" for current user.
274 * @param projectName name for the newly created project
275 * @return Group object containing information about created project
276 * (mapped from JSON returned from server after creating the project)
278 public Group createNewProject(String projectName) {
279 Group project = new Group();
280 project.setName(projectName);
281 project.setGroupClass(PROJECT);
282 Group createdProject = groupsApiClient.create(project);
283 log.debug("Project " + createdProject.getName() + " created with UUID: " + createdProject.getUuid());
284 return createdProject;
288 * Deletes collection with specified uuid.
290 * @param collectionUuid uuid of collection to be deleted. User whose token is provided in configuration
291 * must be authorized to delete such collection.
292 * @return collection object with deleted collection (mapped from JSON returned from server after deleting the collection)
294 public Collection deleteCollection(String collectionUuid) {
295 Collection deletedCollection = collectionsApiClient.delete(collectionUuid);
296 log.debug("Collection: " + collectionUuid + " deleted.");
297 return deletedCollection;