Statistics
| Branch: | Tag: | Revision:

one / include / Cluster.h @ 621a1869

History | View | Annotate | Download (8.92 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems              */
3
/*                                                                          */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may  */
5
/* not use this file except in compliance with the License. You may obtain  */
6
/* a copy of the License at                                                 */
7
/*                                                                          */
8
/* http://www.apache.org/licenses/LICENSE-2.0                               */
9
/*                                                                          */
10
/* Unless required by applicable law or agreed to in writing, software      */
11
/* distributed under the License is distributed on an "AS IS" BASIS,        */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13
/* See the License for the specific language governing permissions and      */
14
/* limitations under the License.                                           */
15
/* -------------------------------------------------------------------------*/
16

    
17
#ifndef CLUSTER_H_
18
#define CLUSTER_H_
19

    
20
#include "PoolSQL.h"
21
#include "ObjectCollection.h"
22
#include "DatastorePool.h"
23
#include "ClusterTemplate.h"
24

    
25
using namespace std;
26

    
27
/**
28
 *  The Cluster class.
29
 */
30
class Cluster : public PoolObjectSQL
31
{
32
public:
33
    /**
34
     * Returns the DATASTORE_LOCATION for the hosts of the cluster. If not
35
     * defined that in oned.conf is returned.
36
     *
37
     * @param ds_location string to copy the DATASTORE_LOCATION to
38
     * @return DATASTORE_LOCATION
39
     */
40
    string& get_ds_location(string &ds_location);
41

    
42
    // *************************************************************************
43
    // Object Collections (Public)
44
    // *************************************************************************
45

    
46
    /**
47
     *  Adds this host ID to the set.
48
     *    @param id to be added to the cluster
49
     *    @param error_msg Error message, if any
50
     *    @return 0 on success
51
     */
52
    int add_host(int id, string& error_msg)
53
    {
54
        int rc = hosts.add(id);
55

    
56
        if ( rc < 0 )
57
        {
58
            error_msg = "Host ID is already in the cluster set.";
59
        }
60

    
61
        return rc;
62
    }
63

    
64
    /**
65
     *  Deletes this host ID from the set.
66
     *    @param id to be deleted from the cluster
67
     *    @param error_msg Error message, if any
68
     *    @return 0 on success
69
     */
70
    int del_host(int id, string& error_msg)
71
    {
72
        int rc = hosts.del(id);
73

    
74
        if ( rc < 0 )
75
        {
76
            error_msg = "Host ID is not part of the cluster set.";
77
        }
78

    
79
        return rc;
80
    }
81

    
82
    /**
83
     *  Adds this datastore ID to the set.
84
     *    @param id to be added to the cluster
85
     *    @param error_msg Error message, if any
86
     *    @return 0 on success
87
     */
88
    int add_datastore(int id, string& error_msg);
89

    
90
    /**
91
     *  Deletes this datastore ID from the set.
92
     *    @param id to be deleted from the cluster
93
     *    @param error_msg Error message, if any
94
     *    @return 0 on success
95
     */
96
    int del_datastore(int id, string& error_msg);
97

    
98
    /**
99
     *  Returns a copy of the datastore IDs set
100
     */
101
    set<int> get_datastores()
102
    {
103
        return datastores.clone();
104
    };
105

    
106
    /**
107
     *  Returns a system DS for the cluster when none is set at the API level
108
     *    @return the ID of the System
109
     */
110
    static int get_default_system_ds(const set<int>& ds_collection);
111

    
112
    /**
113
     *  Adds this vnet ID to the set.
114
     *    @param id to be added to the cluster
115
     *    @param error_msg Error message, if any
116
     *    @return 0 on success
117
     */
118
    int add_vnet(int id, string& error_msg)
119
    {
120
        int rc = vnets.add(id);
121

    
122
        if ( rc < 0 )
123
        {
124
            error_msg = "Network ID is already in the cluster set.";
125
        }
126

    
127
        return rc;
128
    }
129

    
130
    /**
131
     *  Deletes this vnet ID from the set.
132
     *    @param id to be deleted from the cluster
133
     *    @param error_msg Error message, if any
134
     *    @return 0 on success
135
     */
136
    int del_vnet(int id, string& error_msg)
137
    {
138
        int rc = vnets.del(id);
139

    
140
        if ( rc < 0 )
141
        {
142
            error_msg = "Network ID is not part of the cluster set.";
143
        }
144

    
145
        return rc;
146
    }
147

    
148
    /**
149
     *  Returns a copy of the host IDs set
150
     */
151
    set<int> get_host_ids()
152
    {
153
        return hosts.clone();
154
    }
155

    
156
    /**
157
     *  Returns a copy of the datastore IDs set
158
     */
159
    set<int> get_datastore_ids()
160
    {
161
        return datastores.clone();
162
    }
163

    
164
    /**
165
     *  Returns a copy of the vnet IDs set
166
     */
167
    set<int> get_vnet_ids()
168
    {
169
        return vnets.clone();
170
    }
171

    
172
    /**
173
     *  Get the default reserved capacity for hosts in the cluster. It can be
174
     *  overridden if defined in the host template.
175
     *    @param cpu reserved cpu (in percentage)
176
     *    @param mem reserved mem (in KB)
177
     */
178
    void get_reserved_capacity(long long &cpu, long long& mem)
179
    {
180
        get_template_attribute("RESERVED_CPU", cpu);
181

    
182
        get_template_attribute("RESERVED_MEM", mem);
183
    }
184

    
185
    // *************************************************************************
186
    // DataBase implementation (Public)
187
    // *************************************************************************
188

    
189
    /**
190
     * Function to print the Cluster object into a string in XML format
191
     *  @param xml the resulting XML string
192
     *  @return a reference to the generated string
193
     */
194
    string& to_xml(string& xml) const;
195

    
196
    /**
197
     *  Rebuilds the object from an xml formatted string
198
     *    @param xml_str The xml-formatted string
199
     *
200
     *    @return 0 on success, -1 otherwise
201
     */
202
    int from_xml(const string &xml_str);
203

    
204
    static const char * host_table;
205

    
206
    static const char * datastore_table;
207

    
208
    static const char * network_table;
209

    
210
private:
211

    
212
    // -------------------------------------------------------------------------
213
    // Friends
214
    // -------------------------------------------------------------------------
215

    
216
    friend class ClusterPool;
217

    
218
    // *************************************************************************
219
    // Constructor
220
    // *************************************************************************
221

    
222
    Cluster(int id,
223
            const string& name,
224
            ClusterTemplate*  cl_template);
225

    
226
    virtual ~Cluster(){};
227

    
228
    // *************************************************************************
229
    // Attributes (Private)
230
    // *************************************************************************
231

    
232
    ObjectCollection hosts;
233
    ObjectCollection datastores;
234
    ObjectCollection vnets;
235

    
236
    // *************************************************************************
237
    // DataBase implementation (Private)
238
    // *************************************************************************
239

    
240
    static const char * db_names;
241
    static const char * db_bootstrap;
242
    static const char * table;
243

    
244
    static const char * host_db_names;
245
    static const char * host_db_bootstrap;
246

    
247
    static const char * datastore_db_names;
248
    static const char * datastore_db_bootstrap;
249

    
250
    static const char * network_db_names;
251
    static const char * network_db_bootstrap;
252

    
253
    /**
254
     *  Execute an INSERT or REPLACE Sql query.
255
     *    @param db The SQL DB
256
     *    @param replace Execute an INSERT or a REPLACE
257
     *    @param error_str Returns the error reason, if any
258
     *    @return 0 one success
259
     */
260
    int insert_replace(SqlDB *db, bool replace, string& error_str);
261

    
262
    /**
263
     *  Bootstraps the database table(s) associated to the Cluster
264
     *    @return 0 on success
265
     */
266
    static int bootstrap(SqlDB * db)
267
    {
268
        int rc;
269
        ostringstream oss;
270

    
271
        oss.str(Cluster::db_bootstrap);
272
        rc = db->exec(oss);
273

    
274
        oss.str(Cluster::host_db_bootstrap);
275
        rc += db->exec(oss);
276

    
277
        oss.str(Cluster::datastore_db_bootstrap);
278
        rc += db->exec(oss);
279

    
280
        oss.str(Cluster::network_db_bootstrap);
281
        rc += db->exec(oss);
282

    
283
        return rc;
284
    };
285

    
286
    /**
287
     *  Writes the Cluster in the database.
288
     *    @param db pointer to the db
289
     *    @return 0 on success
290
     */
291
    int insert(SqlDB *db, string& error_str)
292
    {
293
        return insert_replace(db, false, error_str);
294
    }
295

    
296
    /**
297
     *  Writes/updates the Cluster's data fields in the database.
298
     *    @param db pointer to the db
299
     *    @return 0 on success
300
     */
301
    int update(SqlDB *db)
302
    {
303
        string error_str;
304
        return insert_replace(db, true, error_str);
305
    }
306

    
307
    /**
308
     * Checks if all the collections are empty, and therefore this cluster
309
     * can be dropped.
310
     *
311
     * @param error_msg Error message, if any.
312
     * @return 0 if cluster can be dropped, -1 otherwise
313
     */
314
    int check_drop(string& error_msg);
315

    
316
    /**
317
     *  Factory method for cluster templates
318
     */
319
    Template * get_new_template() const
320
    {
321
        return new ClusterTemplate;
322
    }
323
};
324

    
325
#endif /*CLUSTER_H_*/