Statistics
| Branch: | Tag: | Revision:

one / include / PoolObjectSQL.h @ d42f3d33

History | View | Annotate | Download (10.1 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. Object should be updated
280
     *  after calling this method
281
     *    @param tmpl string representation of the template
282
     */
283
    int replace_template(const string& tmpl_str, string& error);
284

    
285
protected:
286

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
379
private:
380

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

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

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

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

    
403
#endif /*POOL_OBJECT_SQL_H_*/