Statistics
| Branch: | Tag: | Revision:

one / include / PoolObjectSQL.h @ 5483a1ba

History | View | Annotate | Download (8.86 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,const char *_table)
41
            :ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),
42
             valid(true),obj_template(0),table(_table)
43
    {
44
        pthread_mutex_init(&mutex,0);
45
    };
46

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

    
51
        pthread_mutex_destroy(&mutex);
52
    };
53

    
54
    /* --------------------------------------------------------------------- */
55

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

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

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

    
71
    /* --------------------------------------------------------------------- */
72

    
73
    /**
74
     *  Check if the object is valid
75
     *    @return true if object is valid
76
     */
77
    const bool& isValid() const
78
    {
79
       return valid;
80
    };
81

    
82
    /**
83
     *  Set the object valid flag
84
     *  @param _valid new valid flag
85
     */
86
    void set_valid(const bool _valid)
87
    {
88
        valid = _valid;
89
    };
90

    
91
    /**
92
     *  Function to lock the object
93
     */
94
    void lock()
95
    {
96
        pthread_mutex_lock(&mutex);
97
    };
98

    
99
    /**
100
     *  Function to unlock the object
101
     */
102
    void unlock()
103
    {
104
        pthread_mutex_unlock(&mutex);
105
    };
106

    
107
    /**
108
     * Function to print the object into a string in XML format
109
     * base64 encoded
110
     *  @param xml the resulting XML string
111
     *  @return a reference to the generated string
112
     */
113
    virtual string& to_xml64(string &xml64);
114

    
115
    /**
116
     * Function to print the object into a string in XML format
117
     *  @param xml the resulting XML string
118
     *  @return a reference to the generated string
119
     */
120
    virtual string& to_xml(string& xml) const = 0;
121

    
122
    /**
123
     *  Rebuilds the object from an xml formatted string
124
     *    @param xml_str The xml-formatted string
125
     *
126
     *    @return 0 on success, -1 otherwise
127
     */
128
    virtual int from_xml(const string &xml_str) = 0;
129

    
130
    // ------------------------------------------------------------------------
131
    // Template
132
    // ------------------------------------------------------------------------
133

    
134
    /**
135
     *  Gets the values of a template attribute
136
     *    @param name of the attribute
137
     *    @param values of the attribute
138
     *    @return the number of values
139
     */
140
    int get_template_attribute(
141
        string& name,
142
        vector<const Attribute*>& values) const
143
    {
144
        return obj_template->get(name,values);
145
    };
146

    
147
    /**
148
     *  Gets the values of a template attribute
149
     *    @param name of the attribute
150
     *    @param values of the attribute
151
     *    @return the number of values
152
     */
153
    int get_template_attribute(
154
        const char *name,
155
        vector<const Attribute*>& values) const
156
    {
157
        string str=name;
158
        return obj_template->get(str,values);
159
    };
160

    
161
    /**
162
     *  Gets a string based attribute (single)
163
     *    @param name of the attribute
164
     *    @param value of the attribute (a string), will be "" if not defined or
165
     *    not a single attribute
166
     */
167
    void get_template_attribute(
168
        const char *    name,
169
        string&         value) const
170
    {
171
        string str=name;
172
        obj_template->get(str,value);
173
    }
174

    
175
    /**
176
     *  Gets an int based attribute (single)
177
     *    @param name of the attribute
178
     *    @param value of the attribute (an int), will be 0 if not defined or
179
     *    not a single attribute
180
     */
181
    void get_template_attribute(
182
        const char *    name,
183
        int&            value) const
184
    {
185
        string str=name;
186
        obj_template->get(str,value);
187
    }
188

    
189
    /**
190
     *  Adds a new attribute to the template (replacing it if
191
     *  already defined), the object's mutex SHOULD be locked
192
     *    @param name of the new attribute
193
     *    @param value of the new attribute
194
     *    @return 0 on success
195
     */
196
    int replace_template_attribute(
197
        const string& name,
198
        const string& value)
199
    {
200
        SingleAttribute * sattr = new SingleAttribute(name,value);
201

    
202
        obj_template->erase(sattr->name());
203

    
204
        obj_template->set(sattr);
205

    
206
        return 0;
207
    }
208

    
209
    /**
210
     *  Generates a XML string for the template of the Object
211
     *    @param xml the string to store the XML description.
212
     */
213
    void template_to_xml(string &xml) const
214
    {
215
        obj_template->to_xml(xml);
216
    }
217

    
218
    /**
219
     *  Removes an Image attribute
220
     *    @param name of the attribute
221
     */
222
    int remove_template_attribute(const string& name)
223
    {
224
        return obj_template->erase(name);
225
    }
226

    
227
    /**
228
     *  Sets an error message for the VM in the template
229
     *    @param message
230
     *    @return 0 on success
231
     */
232
    void set_template_error_message(const string& message);
233

    
234
protected:
235

    
236
    /**
237
     *  Callback function to unmarshall a PoolObjectSQL
238
     *    @param num the number of columns read from the DB
239
     *    @param names the column names
240
     *    @param vaues the column values
241
     *    @return 0 on success
242
     */
243
    int select_cb(void *nil, int num, char **values, char **names)
244
    {
245
        if ( (!values[0]) || (num != 1) )
246
        {
247
            return -1;
248
        }
249

    
250
        return from_xml(values[0]);
251
    };
252

    
253
    /**
254
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
255
     *    @param db pointer to the db
256
     *    @return 0 on success
257
     */
258
    virtual int select(SqlDB *db);
259

    
260
    /**
261
     *  Reads the PoolObjectSQL (identified by its OID) from the database.
262
     *    @param db pointer to the db
263
     *    @return 0 on success
264
     */
265
    virtual int select(SqlDB *db, const string& _name, int _uid);
266

    
267
    /**
268
     *  Drops object from the database
269
     *    @param db pointer to the db
270
     *    @return 0 on success
271
     */
272
    virtual int drop(SqlDB *db);
273

    
274
    /**
275
     *  Function to output a pool object into a stream in XML format
276
     *    @param oss the output stream
277
     *    @param num the number of columns read from the DB
278
     *    @param names the column names
279
     *    @param vaues the column values
280
     *    @return 0 on success
281
     */
282
    static int dump(ostringstream& oss, int num, char **values, char **names)
283
    {
284
        if ( (!values[0]) || (num != 1) )
285
        {
286
            return -1;
287
        }
288

    
289
        oss << values[0];
290
        return 0;
291
    };
292

    
293
    /**
294
     *  The object's unique ID
295
     */
296
    int     oid;
297

    
298
    /**
299
     *  The object's name
300
     */
301
    string  name;
302

    
303
    /**
304
     *  Object's owner, set it to -1 if owner is not used
305
     */
306
    int     uid;
307

    
308
    /**
309
     *  The contents of this object are valid
310
     */
311
    bool    valid;
312

    
313
    /**
314
     *  Template for this object, will be allocated if needed
315
     */
316
    Template * obj_template;
317

    
318
private:
319

    
320
    /**
321
     *  The PoolSQL, friend to easily manipulate its Objects
322
     */
323
    friend class PoolSQL;
324

    
325
    /**
326
     * The mutex for the PoolObject. This implementation assumes that the mutex
327
     * IS LOCKED when the class destructor is called.
328
     */
329
    pthread_mutex_t mutex;
330

    
331
    /**
332
     *  Pointer to the SQL table for the PoolObjectSQL
333
     */
334
    const char * table;
335

    
336
    /**
337
     *  Name for the error messages attribute
338
     */
339
    static const char * error_attribute_name;
340
};
341

    
342
#endif /*POOL_OBJECT_SQL_H_*/