Statistics
| Branch: | Tag: | Revision:

one / include / PoolObjectSQL.h @ c7625b48

History | View | Annotate | Download (10 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
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 POOL_OBJECT_SQL_H_
18
#define POOL_OBJECT_SQL_H_
19

    
20
#include "ObjectSQL.h"
21
#include "ObjectXML.h"
22
#include "Template.h"
23
#include <pthread.h>
24
#include <string.h>
25

    
26
using namespace std;
27

    
28
/**
29
 * PoolObject class. Provides a SQL backend interface for Pool components. Each
30
 * object is identified with and unique OID
31
 *
32
 * Note: The PoolObject provides a synchronization mechanism (mutex). This
33
 * implementation assumes that the mutex IS LOCKED when the class destructor
34
 * is called.
35
 */
36
class PoolObjectSQL : public ObjectSQL, public ObjectXML
37
{
38
public:
39

    
40
    PoolObjectSQL(int id, const string& _name, int _uid,
41
                  int _gid, const char *_table)
42
            :ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),gid(_gid),
43
             valid(true),public_obj(0),obj_template(0),table(_table)
44
    {
45
        pthread_mutex_init(&mutex,0);
46
    };
47

    
48
    virtual ~PoolObjectSQL()
49
    {
50
        pthread_mutex_unlock(&mutex);
51

    
52
        pthread_mutex_destroy(&mutex);
53
    };
54

    
55
    /* --------------------------------------------------------------------- */
56

    
57
    int get_oid() const
58
    {
59
        return oid;
60
    };
61

    
62
    const string& get_name() const
63
    {
64
        return name;
65
    };
66

    
67
    int get_uid()
68
    {
69
        return uid;
70
    };
71

    
72
    /**
73
     *  Returns true if the image is public
74
     *     @return true if the image is public
75
     */
76
    bool isPublic()
77
    {
78
        return (public_obj == 1);
79
    };
80

    
81
    // TODO: Check if uid == -1?
82
    // What happens with user, is the user his owner, or uid = -1?
83
    int set_uid(int _uid)
84
    {
85
        uid = _uid;
86
        return 0;
87
    }
88

    
89
    int get_gid()
90
    {
91
        return gid;
92
    };
93

    
94
    int set_gid(int _gid)
95
    {
96
        if( gid == -1 )
97
        {
98
            return -1;
99
        }
100

    
101
        gid = _gid;
102
        return 0;
103
    };
104

    
105
    /* --------------------------------------------------------------------- */
106

    
107
    /**
108
     *  Check if the object is valid
109
     *    @return true if object is valid
110
     */
111
    const bool& isValid() const
112
    {
113
       return valid;
114
    };
115

    
116
    /**
117
     *  Set the object valid flag
118
     *  @param _valid new valid flag
119
     */
120
    void set_valid(const bool _valid)
121
    {
122
        valid = _valid;
123
    };
124

    
125
    /**
126
     *  Function to lock the object
127
     */
128
    void lock()
129
    {
130
        pthread_mutex_lock(&mutex);
131
    };
132

    
133
    /**
134
     *  Function to unlock the object
135
     */
136
    void unlock()
137
    {
138
        pthread_mutex_unlock(&mutex);
139
    };
140

    
141
    /**
142
     * Function to print the object into a string in XML format
143
     * base64 encoded
144
     *  @param xml the resulting XML string
145
     *  @return a reference to the generated string
146
     */
147
    virtual string& to_xml64(string &xml64);
148

    
149
    /**
150
     * Function to print the object into a string in XML format
151
     *  @param xml the resulting XML string
152
     *  @return a reference to the generated string
153
     */
154
    virtual string& to_xml(string& xml) const = 0;
155

    
156
    /**
157
     *  Rebuilds the object from an xml formatted string
158
     *    @param xml_str The xml-formatted string
159
     *
160
     *    @return 0 on success, -1 otherwise
161
     */
162
    virtual int from_xml(const string &xml_str) = 0;
163

    
164
    // ------------------------------------------------------------------------
165
    // Template
166
    // ------------------------------------------------------------------------
167

    
168
    /**
169
     *  Gets the values of a template attribute
170
     *    @param name of the attribute
171
     *    @param values of the attribute
172
     *    @return the number of values
173
     */
174
    int get_template_attribute(
175
        string& name,
176
        vector<const Attribute*>& values) const
177
    {
178
        return obj_template->get(name,values);
179
    };
180

    
181
    /**
182
     *  Gets the values of a template attribute
183
     *    @param name of the attribute
184
     *    @param values of the attribute
185
     *    @return the number of values
186
     */
187
    int get_template_attribute(
188
        const char *name,
189
        vector<const Attribute*>& values) const
190
    {
191
        string str=name;
192
        return obj_template->get(str,values);
193
    };
194

    
195
    /**
196
     *  Gets a string based attribute (single)
197
     *    @param name of the attribute
198
     *    @param value of the attribute (a string), will be "" if not defined or
199
     *    not a single attribute
200
     */
201
    void get_template_attribute(
202
        const char *    name,
203
        string&         value) const
204
    {
205
        string str=name;
206
        obj_template->get(str,value);
207
    }
208

    
209
    /**
210
     *  Gets an int based attribute (single)
211
     *    @param name of the attribute
212
     *    @param value of the attribute (an int), will be 0 if not defined or
213
     *    not a single attribute
214
     */
215
    void get_template_attribute(
216
        const char *    name,
217
        int&            value) const
218
    {
219
        string str=name;
220
        obj_template->get(str,value);
221
    }
222

    
223
    /**
224
     *  Adds a new attribute to the template (replacing it if
225
     *  already defined), the object's mutex SHOULD be locked
226
     *    @param name of the new attribute
227
     *    @param value of the new attribute
228
     *    @return 0 on success
229
     */
230
    int replace_template_attribute(
231
        const string& name,
232
        const string& value)
233
    {
234
        SingleAttribute * sattr = new SingleAttribute(name,value);
235

    
236
        obj_template->erase(sattr->name());
237

    
238
        obj_template->set(sattr);
239

    
240
        return 0;
241
    }
242

    
243
    /**
244
     *  Generates a XML string for the template of the Object
245
     *    @param xml the string to store the XML description.
246
     */
247
    void template_to_xml(string &xml) const
248
    {
249
        obj_template->to_xml(xml);
250
    }
251

    
252
    /**
253
     *  Removes an Image attribute
254
     *    @param name of the attribute
255
     */
256
    int remove_template_attribute(const string& name)
257
    {
258
        return obj_template->erase(name);
259
    }
260

    
261
    /**
262
     *  Sets an error message for the VM in the template
263
     *    @param message
264
     *    @return 0 on success
265
     */
266
    void set_template_error_message(const string& message);
267

    
268
    /**
269
     *  Factory method for templates, it should be implemented
270
     *  by classes that uses templates
271
     *    @return a new template
272
     */
273
    virtual Template * get_new_template()
274
    {
275
        return 0;
276
    }
277

    
278
    /**
279
     *  Replace template for this object
280
     *    @param tmpl string representation of the template
281
     */
282
    int replace_template(const string& tmpl_str, string& error);
283

    
284
protected:
285

    
286
    /**
287
     *  Callback function to unmarshall a PoolObjectSQL
288
     *    @param num the number of columns read from the DB
289
     *    @param names the column names
290
     *    @param vaues the column values
291
     *    @return 0 on success
292
     */
293
    int select_cb(void *nil, int num, char **values, char **names)
294
    {
295
        if ( (!values[0]) || (num != 1) )
296
        {
297
            return -1;
298
        }
299

    
300
        return from_xml(values[0]);
301
    };
302

    
303
    /**
304
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
305
     *    @param db pointer to the db
306
     *    @return 0 on success
307
     */
308
    virtual int select(SqlDB *db);
309

    
310
    /**
311
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
312
     *    @param db pointer to the db
313
     *    @return 0 on success
314
     */
315
    virtual int select(SqlDB *db, const string& _name, int _uid);
316

    
317
    /**
318
     *  Drops object from the database
319
     *    @param db pointer to the db
320
     *    @return 0 on success
321
     */
322
    virtual int drop(SqlDB *db);
323

    
324
    /**
325
     *  Function to output a pool object into a stream in XML format
326
     *    @param oss the output stream
327
     *    @param num the number of columns read from the DB
328
     *    @param names the column names
329
     *    @param vaues the column values
330
     *    @return 0 on success
331
     */
332
    static int dump(ostringstream& oss, int num, char **values, char **names)
333
    {
334
        if ( (!values[0]) || (num != 1) )
335
        {
336
            return -1;
337
        }
338

    
339
        oss << values[0];
340
        return 0;
341
    };
342

    
343
    /**
344
     *  The object's unique ID
345
     */
346
    int     oid;
347

    
348
    /**
349
     *  The object's name
350
     */
351
    string  name;
352

    
353
    /**
354
     *  Object's owner, set it to -1 if owner is not used
355
     */
356
    int     uid;
357

    
358
    /**
359
     *  Object's group, set it to -1 if group is not used
360
     */
361
    int     gid;
362

    
363
    /**
364
     *  The contents of this object are valid
365
     */
366
    bool    valid;
367

    
368
    /**
369
     *  Set if the object is public
370
     */
371
    int     public_obj;
372

    
373
    /**
374
     *  Template for this object, will be allocated if needed
375
     */
376
    Template * obj_template;
377

    
378
private:
379

    
380
    /**
381
     *  The PoolSQL, friend to easily manipulate its Objects
382
     */
383
    friend class PoolSQL;
384

    
385
    /**
386
     * The mutex for the PoolObject. This implementation assumes that the mutex
387
     * IS LOCKED when the class destructor is called.
388
     */
389
    pthread_mutex_t mutex;
390

    
391
    /**
392
     *  Pointer to the SQL table for the PoolObjectSQL
393
     */
394
    const char * table;
395

    
396
    /**
397
     *  Name for the error messages attribute
398
     */
399
    static const char * error_attribute_name;
400
};
401

    
402
#endif /*POOL_OBJECT_SQL_H_*/