1 /*==============================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #ifndef _h_vdb_config_model_
28 #define _h_vdb_config_model_
29 
30 #include <kfg/config.h>
31 #include <kfg/properties.h>
32 #include <kfg/repository.h>
33 #include <kfg/ngc.h>
34 #include <kfs/directory.h>
35 #include <vfs/manager.h>
36 #include <vfs/path.h>
37 
38 #include <string>
39 
40 #include "util.hpp"
41 
42 enum ESetRootState {
43     eSetRootState_OK,            // successfully changed repository root
44     eSetRootState_NotChanged,    // the new path is the same as the old one
45     eSetRootState_NotUnique,   // there is another repository with the same root
46     eSetRootState_MkdirFail,     // failed to make new repository directory
47     eSetRootState_NewPathEmpty,  // new repository directory path is not empty
48     eSetRootState_NewDirNotEmpty,// new repository directory is not empty
49     eSetRootState_NewNotDir,     // new repository exists and is not a directory
50     eSetRootState_OldNotEmpty,   // old repository is not empty
51     eSetRootState_Error,         // some unusual error happened
52 };
53 
54 /*
55     a c++ class the encapsulates the connection to
56 */
57 
58 class vdbconf_model
59 {
60     public :
61         static const int32_t kPublicRepoId;
62         static const int32_t kInvalidRepoId;
63 
64         vdbconf_model( CKConfig & config );
65         ~vdbconf_model( void );
66 
67         // ----------------------------------------------------------------
68         std::string native_to_internal( const std::string &s ) const;
69         std::string internal_to_native( const std::string &s ) const;
70 
get_config_changed(void) const71         bool get_config_changed( void ) const { return _config.IsUpdated(); }
72 
73         // ----------------------------------------------------------------
74 
75         bool is_http_proxy_enabled( void ) const;
76 
77         void set_http_proxy_enabled( bool enabled );
78 
79         std::string get_http_proxy_path( void ) const;
80 
81         void set_http_proxy_path(const std::string &path);
82 
83         bool has_http_proxy_env_higher_priority( void ) const;
84         void set_http_proxy_env_higher_priority( bool value );
85 
86         // ----------------------------------------------------------------
87         bool is_remote_enabled( void ) const;
88         void set_remote_enabled( bool enabled );
89 
90         // ----------------------------------------------------------------
91         bool does_site_repo_exist( void ) const;
92 
93         bool is_site_enabled( void ) const;
94         void set_site_enabled( bool enabled );
95 
96         // ----------------------------------------------------------------
97         bool allow_all_certs( void ) const;
98         void set_allow_all_certs( bool enabled );
99 
100         // ----------------------------------------------------------------
101         /* THIS IS NEW AND NOT YET IMPLEMENTED IN CONFIG: global cache on/off !!! */
102         bool is_global_cache_enabled( void ) const;
103 
104         void set_global_cache_enabled( bool enabled );
105 
106   // ----------------------------- //
107   // ADD DEFINE IF YOU NEED IT !!! //
108   // ----------------------------- //
is_user_enabled(void) const109         bool is_user_enabled( void ) const
110         {
111             bool res = true;
112 #ifdef ALLOW_USER_REPOSITORY_DISABLING
113             if ( _config_valid ) KConfig_Get_User_Access_Enabled( _config, &res );
114 #endif
115             return res;
116         }
set_user_enabled(bool enabled)117         void set_user_enabled( bool enabled )
118         {
119 #ifdef ALLOW_USER_REPOSITORY_DISABLING
120             if ( _config_valid ) KConfig_Set_User_Access_Enabled( _config, enabled );
121 #endif
122         }
123 
124         // ----------------------------------------------------------------
125         bool is_user_cache_enabled( void ) const;
126         void set_user_cache_enabled( bool enabled );
127 
128         // ----------------------------------------------------------------
129         uint32_t get_repo_count( void ) const;
130 
131 
132         /* Returns:
133          *  kInvalidRepoId if not found,
134          *  kPublicRepoId for the user public repository
135          *  protected repository id otherwise
136          */
137         int32_t get_repo_id( const std::string & repo_name ) const;
138 
139         std::string get_repo_name( uint32_t id ) const;
140         std::string get_repo_description( const std::string & repo_name ) const;
141 
is_protected_repo_enabled(uint32_t id) const142         bool is_protected_repo_enabled( uint32_t id ) const
143         {
144             bool res = true;
145 #ifdef ALLOW_USER_REPOSITORY_DISABLING
146             if ( _config_valid ) KConfigGetProtectedRepositoryEnabledById( _config, id, &res );
147 #endif
148             return res;
149         }
set_protected_repo_enabled(uint32_t id,bool enabled)150         void set_protected_repo_enabled( uint32_t id, bool enabled )
151         {
152 #ifdef ALLOW_USER_REPOSITORY_DISABLING
153             if ( _config_valid ) KConfigSetProtectedRepositoryEnabledById( _config, id, enabled );
154 #endif
155         }
156 
157         bool is_protected_repo_cached( uint32_t id ) const;
158 
159         void set_protected_repo_cached( uint32_t id, bool enabled );
160 
161         bool does_repo_exist( const char * repo_name );
162 
163         std::string get_repo_location( uint32_t id ) const;
164 
165         ESetRootState set_repo_location( uint32_t id,
166             bool flushOld, const std::string &path, bool reuseNew );
167 
168 /* ----------------------------------------------------------------
169  * flushOld repository
170  * reuseNew repository: whether to refuse to change location on existing newPath
171  */
172         ESetRootState change_repo_location(bool flushOld,
173             const std::string &newPath, bool reuseNew, int32_t repoId);
174 
175         ESetRootState prepare_repo_directory(const std::string &newPath,
176             bool reuseNew = false);
177 
178         // ----------------------------------------------------------------
179 
180         std::string get_public_location( void ) const;
181 
182         ESetRootState set_public_location( bool flushOld, std::string &path, bool reuseNew );
183         void unset_public_repo_location( void );
184 
185         bool is_user_public_enabled( void ) const;
186         void set_user_public_enabled( bool enabled );
187 
188         bool is_user_public_cached( void ) const;
189 
190         void set_user_public_cached( bool enabled );
191 
192         // ----------------------------------------------------------------
193         std::string get_current_dir( void ) const;
194         std::string get_home_dir( void ) const;
195 
196         std::string get_user_default_dir( void ) const;
197         std::string get_ngc_root( std::string &base, const KNgcObj * ngc ) const;
198 
199         void set_user_default_dir( const char * new_default_dir );
200 
201         // ----------------------------------------------------------------
202         bool import_ngc( const std::string &native_location,
203             const KNgcObj *ngc, uint32_t permissions,
204             uint32_t * result_flags );
205 
206         bool get_id_of_ngc_obj( const KNgcObj *ngc, uint32_t * id );
207 
208         bool mkdir(const KNgcObj *ngc);
209 
210         bool does_path_exist( std::string &path );
211 
212         /* does prefetch download ETL to output directory or cache? */
213         bool does_prefetch_download_to_cache(void) const;
214         void set_prefetch_download_to_cache(bool download_to_cache);
215 
216         /* does user agree to accept charges? */
217         bool does_user_accept_aws_charges( void ) const;
218         bool does_user_accept_gcp_charges( void ) const;
219         void set_user_accept_aws_charges( bool accepts_charges );
220         void set_user_accept_gcp_charges( bool accepts_charges );
221 
222         /* does user agree to report cloud instance identity? */
223         bool report_cloud_instance_identity( void ) const;
224         void set_report_cloud_instance_identity( bool report_identity );
225 
226         /* preferred temporary cache location */
227         std::string get_temp_cache_location(void) const;
228         void set_temp_cache_location(const std::string & path);
229 
230         /* user-pay for GCP, a file name */
231         std::string get_gcp_credential_file_location(void) const;
232         void set_gcp_credential_file_location(const std::string & path);
233 
234         /* user-pay for AWS, can be a directory or a file */
235         std::string get_aws_credential_file_location(void) const;
236         void set_aws_credential_file_location(const std::string & path);
237 
238         /* "default" if not present or empty */
239         std::string get_aws_profile(void) const;
240         void set_aws_profile(const std::string & name);
241 
242         /* how much memory to use for caching */
243         uint32_t get_cache_amount_in_MB( void ) const;
244         void set_cache_amount_in_MB( uint32_t value );
245 
246         /* guid related functions */
247         std::string get_guid( void ) const;
248         void check_guid( void );
249 
250         /* full-quality related functions */
251         bool get_full_quality( void ) const;
252         void set_full_quality( bool b );
253 
254         // ----------------------------------------------------------------
255         bool commit( void );
256         void reload( void ); // throws on error
257         void set_defaults( void );
258         std::string get_dflt_import_path_start_dir( void );
259 
260     private :
261         CKConfig & _config;
262 
263         KDirectory * _dir;
264         const KRepositoryMgr *_mgr;
265         VFSManager *_vfs_mgr;
266 
267         ESetRootState x_ChangeRepoLocation(const std::string &native_newPath,
268             bool reuseNew, int32_t repoId, bool flushOld = false);
269 };
270 
271 #endif
272