Statistics
| Branch: | Tag: | Revision:

one / src / vm_template / VMTemplate.cc @ d2538409

History | View | Annotate | Download (7.22 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
#include "VMTemplate.h"
18

    
19
#define TO_UPPER(S) transform(S.begin(),S.end(),S.begin(),(int(*)(int))toupper)
20

    
21

    
22
/* ************************************************************************ */
23
/* VMTemplate :: Constructor/Destructor                                     */
24
/* ************************************************************************ */
25

    
26
VMTemplate::VMTemplate(int id,
27
                       int _uid,
28
                       int _gid,
29
                       VirtualMachineTemplate * _template_contents):
30
        PoolObjectSQL(id,"",_uid,_gid,table),
31
        regtime(time(0))
32
{
33
    if (_template_contents != 0)
34
    {
35
        obj_template = _template_contents;
36
    }
37
    else
38
    {
39
        obj_template = new VirtualMachineTemplate;
40
    }
41
}
42

    
43
/* ------------------------------------------------------------------------ */
44
/* ------------------------------------------------------------------------ */
45

    
46
VMTemplate::~VMTemplate()
47
{
48
    if ( obj_template != 0 )
49
    {
50
        delete obj_template;
51
    }
52
}
53

    
54
/* ************************************************************************ */
55
/* VMTemplate :: Database Access Functions                                  */
56
/* ************************************************************************ */
57

    
58
const char * VMTemplate::table = "template_pool";
59

    
60
const char * VMTemplate::db_names = "oid, name, body, uid, gid, public";
61

    
62
const char * VMTemplate::db_bootstrap =
63
    "CREATE TABLE IF NOT EXISTS template_pool (oid INTEGER PRIMARY KEY, "
64
    "name VARCHAR(256), body TEXT, uid INTEGER, gid INTEGER, public INTEGER)";
65

    
66
/* ------------------------------------------------------------------------ */
67
/* ------------------------------------------------------------------------ */
68

    
69
int VMTemplate::insert(SqlDB *db, string& error_str)
70
{
71
    int             rc;
72
    ostringstream   oss;
73
    string          public_attr;
74

    
75

    
76
    // ---------------------------------------------------------------------
77
    // Check default attributes
78
    // ---------------------------------------------------------------------
79

    
80
    // ------------ NAME & TEMPLATE_ID --------------------
81
    oss << oid;
82

    
83
    replace_template_attribute("TEMPLATE_ID",oss.str()); 
84

    
85
    get_template_attribute("NAME", name);
86

    
87
    if ( name.empty() == true )
88
    {
89
        oss.str("");
90
        oss << "template-" << oid;
91
        name = oss.str();
92
    }
93

    
94

    
95
    // ------------ PUBLIC --------------------
96

    
97
    get_template_attribute("PUBLIC", public_attr);
98

    
99
    obj_template->erase("PUBLIC");
100

    
101
    TO_UPPER(public_attr);
102

    
103
    public_obj = (public_attr == "YES");
104

    
105
    // ------------------------------------------------------------------------
106
    // Insert the Template
107
    // ------------------------------------------------------------------------
108

    
109
    rc = insert_replace(db, false);
110

    
111
    if ( rc != 0 )
112
    {
113
        error_str = "Error inserting Template in DB.";
114
    }
115

    
116
    return rc;
117
}
118

    
119
/* ------------------------------------------------------------------------ */
120
/* ------------------------------------------------------------------------ */
121

    
122
int VMTemplate::insert_replace(SqlDB *db, bool replace)
123
{
124
    ostringstream   oss;
125

    
126
    int    rc;
127
    string xml_body;
128

    
129
    char * sql_name;
130
    char * sql_xml;
131

    
132
   // Update the Object
133

    
134
    sql_name = db->escape_str(name.c_str());
135

    
136
    if ( sql_name == 0 )
137
    {
138
        goto error_name;
139
    }
140

    
141
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
142

    
143
    if ( sql_xml == 0 )
144
    {
145
        goto error_body;
146
    }
147

    
148
    if(replace)
149
    {
150
        oss << "REPLACE";
151
    }
152
    else
153
    {
154
        oss << "INSERT";
155
    }
156

    
157
    // Construct the SQL statement to Insert or Replace
158

    
159
    oss <<" INTO " << table <<" ("<< db_names <<") VALUES ("
160
        <<            oid        << ","
161
        << "'"     << sql_name   << "',"
162
        << "'"     << sql_xml    << "',"
163
        <<            uid        << ","
164
        <<            gid        << ","
165
        <<            public_obj << ")";
166

    
167
    rc = db->exec(oss);
168

    
169
    db->free_str(sql_name);
170
    db->free_str(sql_xml);
171

    
172
    return rc;
173

    
174
error_body:
175
    db->free_str(sql_name);
176
error_name:
177
    return -1;
178
}
179

    
180
/* ************************************************************************ */
181
/* VMTemplate :: Misc                                                       */
182
/* ************************************************************************ */
183

    
184
ostream& operator<<(ostream& os, VMTemplate& vmTemplate)
185
{
186
    string xml_str;
187

    
188
    os << vmTemplate.to_xml(xml_str);
189

    
190
    return os;
191
}
192

    
193
/* ------------------------------------------------------------------------ */
194
/* ------------------------------------------------------------------------ */
195

    
196
string& VMTemplate::to_xml(string& xml) const
197
{
198
    ostringstream   oss;
199
    string          template_xml;
200

    
201
    oss << "<VMTEMPLATE>"
202
            << "<ID>"       << oid        << "</ID>"
203
            << "<UID>"      << uid        << "</UID>"
204
            << "<GID>"      << gid        << "</GID>"
205
            << "<NAME>"     << name       << "</NAME>"
206
            << "<PUBLIC>"   << public_obj << "</PUBLIC>"
207
            << "<REGTIME>"  << regtime    << "</REGTIME>"
208
            << obj_template->to_xml(template_xml)
209
        << "</VMTEMPLATE>";
210

    
211
    xml = oss.str();
212

    
213
    return xml;
214
}
215

    
216
/* ------------------------------------------------------------------------ */
217
/* ------------------------------------------------------------------------ */
218

    
219
int VMTemplate::from_xml(const string& xml)
220
{
221
    vector<xmlNodePtr> content;
222
    int rc = 0;
223

    
224
    // Initialize the internal XML object
225
    update_from_str(xml);
226

    
227
    // Get class base attributes
228
    rc += xpath(oid,        "/VMTEMPLATE/ID",      -1);
229
    rc += xpath(uid,        "/VMTEMPLATE/UID",     -1);
230
    rc += xpath(gid,        "/VMTEMPLATE/GID",     -1);
231
    rc += xpath(name,       "/VMTEMPLATE/NAME",    "not_found");
232
    rc += xpath(public_obj, "/VMTEMPLATE/PUBLIC",  0);
233
    rc += xpath(regtime,    "/VMTEMPLATE/REGTIME", 0);
234

    
235
    // Get associated classes
236
    ObjectXML::get_nodes("/VMTEMPLATE/TEMPLATE", content);
237

    
238
    if( content.size() < 1 )
239
    {
240
        return -1;
241
    }
242

    
243
    // Template contents
244
    rc += obj_template->from_xml_node(content[0]);
245

    
246
    if (rc != 0)
247
    {
248
        return -1;
249
    }
250

    
251
    return 0;
252
}