globalconfiguration.h 7.6 KB
Newer Older
1
2
3
//================================================================================
// Name        : globalconfiguration.h
// Author      : Alessio Netti
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright   : Leibniz Supercomputing Centre
// Description : Common functionality for reading in configuration files.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2019-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
Alessio Netti's avatar
Alessio Netti committed
17
//
18
19
20
21
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
Alessio Netti's avatar
Alessio Netti committed
22
//
23
24
25
26
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//================================================================================
Alessio Netti's avatar
Alessio Netti committed
27

28
29
30
31
32
33
34
/**
 * @defgroup globalconf Global Configuration
 * @ingroup common
 *
 * @brief Groups all definitions related to configuring DCDB applications.
 */

Alessio Netti's avatar
Alessio Netti committed
35
36
37
38
39
40
41
42
43
#ifndef PROJECT_GLOBALCONFIGURATION_H
#define PROJECT_GLOBALCONFIGURATION_H

#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/algorithm/string.hpp>
#include "logging.h"
44
#include "mqttchecker.h"
Alessio Netti's avatar
Alessio Netti committed
45

46
47
48
49
50
51
52
53
#define DEFAULT_LOGLEVEL 3
#define DEFAULT_THREADS 8
#define DEFAULT_TEMPDIR "./"
#define DEFAULT_CASSANDRAHOST "127.0.0.1"
#define DEFAULT_CASSANDRAPORT "9042"
#define DEFAULT_CASSANDRATTL 0


54
55
class RESTHttpsServer;

56
57
58
59
60
/**
 * @brief Wrapper class for plugin-specific settings
 *
 * @ingroup globalconf
 */
Alessio Netti's avatar
Alessio Netti committed
61
62
63
64
class pluginSettings_t {
public:
    pluginSettings_t() {}
    std::string mqttPrefix = "";
65
    std::string	tempdir = DEFAULT_TEMPDIR;
66
    bool autoPublish = false;
Alessio Netti's avatar
Alessio Netti committed
67
68
69
    unsigned int cacheInterval = 900000;
};

70
71
72
73
74
/**
 * @brief Wrapper class for REST API server-related settings
 *
 * @ingroup globalconf
 */
75
class serverSettings_t {
Alessio Netti's avatar
Alessio Netti committed
76
public:
77
    serverSettings_t() {}
78
    bool enabled = false;
79
80
81
82
    std::string host = "";/**< Host name/IP address to listen on */
    std::string port = "8000";/**< Port to listen on */
    std::string certificate = "";/**< Certificate chain file in PEM format */
    std::string privateKey = "";/**< Private key file in PEM format */
Alessio Netti's avatar
Alessio Netti committed
83
84
};

85
86
87
88
89
/**
 * @brief Wrapper class for data analytics-related settings
 *
 * @ingroup globalconf
 */
Alessio Netti's avatar
Alessio Netti committed
90
91
92
93
class analyticsSettings_t {
public:
    analyticsSettings_t() {}
    std::string hierarchy = "";
94
    std::string filter = "";
Alessio Netti's avatar
Alessio Netti committed
95
    std::string jobFilter = "";
96
    std::string jobMatch = "";
97
    std::string jobIdFilter = "";
98
    std::string jobDomainId = "default";
Alessio Netti's avatar
Alessio Netti committed
99
100
};

Alessio Netti's avatar
Alessio Netti committed
101
/**
102
103
104
105
 * @brief     Parses a host:port string and returns the host
 *
 * @param str The input string to be parsed
 * @return    The host-related part of the string
Alessio Netti's avatar
Alessio Netti committed
106
 * 
107
 * @ingroup globalconf
Alessio Netti's avatar
Alessio Netti committed
108
109
110
111
112
113
114
 */
static std::string parseNetworkHost(std::string str) {
    size_t pos = str.find(":");
    if (pos != std::string::npos)
        str.erase(pos);
    return str;
}
Alessio Netti's avatar
Alessio Netti committed
115

Alessio Netti's avatar
Alessio Netti committed
116
/**
117
118
119
120
 * @brief     Parses a host:port string and returns the port
 *
 * @param str The input string to be parsed
 * @return    The port-related part of the string
Alessio Netti's avatar
Alessio Netti committed
121
 * 
122
 * @ingroup globalconf
Alessio Netti's avatar
Alessio Netti committed
123
124
125
126
127
128
129
130
131
132
133
 */
static std::string parseNetworkPort(std::string str) {
    size_t pos = str.find(":");
    if (pos != std::string::npos)
        str = str.substr(pos+1);
    else
        str = "";
    return str;
}

/**
134
135
136
137
 * @brief   Converts the input string to a boolean
 *
 * @param s The input string
 * @return  The corresponding boolean value
Alessio Netti's avatar
Alessio Netti committed
138
 * 
139
 * @ingroup globalconf
Alessio Netti's avatar
Alessio Netti committed
140
141
142
143
 */
static bool to_bool(const std::string& s) {
    return s=="true" || s=="on";
}
Alessio Netti's avatar
Alessio Netti committed
144

145
146
147
148
149
150
151
152
153
154
155
156
/**
 * @brief   Converts the input boolean to a string
 *
 * @param b The input boolean
 * @return  The corresponding string value
 * 
 * @ingroup globalconf
 */
static std::string bool_to_str(const bool& b) { 
    return (b ? "true" : "false"); 
}

Alessio Netti's avatar
Alessio Netti committed
157
/**
158
159
160
161
162
163
164
165
166
167
168
 * @brief This class contains the logic to parse and store configurations in
 *        the form of BOOST INFO trees. The configuration parameters which are
 *        parsed here are only those shared between several components of DCDB,
 *        i.e. dcdbpusher and collectagent. Further specific parameters can be
 *        parsed by extending this class appropriately.
 *
 * @details All configuration parameters are freely accessible as public
 *          members of the class. While encapsulation properties are lost, this
 *          makes the codes considerably lighter and more readable.
 *
 * @ingroup globalconf
Alessio Netti's avatar
Alessio Netti committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 */
class GlobalConfiguration {
public:
    /**
     * @brief               Class constructor
     * 
     * @param cfgFilePath   Path to the directory containing the configuration file 
     * @param cfgFileName   Name of the configuration file
     */
    GlobalConfiguration(const std::string& cfgFilePath, const std::string& cfgFileName);
    
    /**
     * @brief               Default constructor
     */
    GlobalConfiguration() {}

185
186
187
188
189
    /**
     * @brief               Default destructor
     */
    virtual ~GlobalConfiguration() {}

Alessio Netti's avatar
Alessio Netti committed
190
191
192
193
194
    /**
     * @brief               Reads in the config from the specified file
     * 
     * @return              True if successful, false otherwise 
     */
195
    void readConfig();
Alessio Netti's avatar
Alessio Netti committed
196
    
197
198
199
200
201
202
203
204
205
    /**
     * @brief Reads user credentials from the config file and accordingly adds
     *        users to a RESTHttpsServer.
     *
     * @param server    The Rest API server where to add the users.
     * @return  True on success, false otherwise.
     */
    bool readRestAPIUsers(RESTHttpsServer* server);

Alessio Netti's avatar
Alessio Netti committed
206
207
208
    // Global configuration members directly accessible from the class
    bool validateConfig = false;
    bool daemonize = false;
209
    int statisticsInterval = 60;
210
    std::string statisticsMqttPart;
211
    uint64_t threads = DEFAULT_THREADS;
212
    int logLevelFile = -1;
213
    int logLevelCmd = DEFAULT_LOGLEVEL;
Alessio Netti's avatar
Alessio Netti committed
214
    analyticsSettings_t analyticsSettings;
215
    serverSettings_t restAPISettings;
Alessio Netti's avatar
Alessio Netti committed
216
    pluginSettings_t pluginSettings;
217
218
219
    std::string cfgFilePath;
    std::string cfgFileName;

Alessio Netti's avatar
Alessio Netti committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
protected:
    /**
     * @brief               Virtual method to integrate additional configuration blocks
     * 
     *                      Additional configuration blocks are defined as nodes in the INFO configuration tree. These
     *                      are distinct from the global and the predefined blocks.
     * 
     * @param cfg           Reference to a BOOST iptree object
     */
    virtual void readAdditionalBlocks(boost::property_tree::iptree& cfg) {}
    
    /**
     * @brief               Virtual method to integrate additional configuration members
     * 
     *                      The configuration members defined here will be parsed within the "global" block of the
     *                      configuration, and not in separate blocks.
     * 
     * @param global        The BOOST iptree node containing the configuration parameter
     * @return              True if the parameter was recognized, false otherwise
     */
    virtual bool readAdditionalValues(boost::property_tree::iptree::value_type &global) { return false; }
    
    // Logger object
    boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};

#endif //PROJECT_GLOBALCONFIGURATION_H