Statistics
| Branch: | Tag: | Revision:

one / src / host / Host.cc @ 6329d8b3

History | View | Annotate | Download (7.78 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 <limits.h>
18
#include <string.h>
19

    
20
#include <iostream>
21
#include <sstream>
22

    
23
#include "Host.h"
24
#include "NebulaLog.h"
25

    
26
/* ************************************************************************ */
27
/* Host :: Constructor/Destructor                                           */
28
/* ************************************************************************ */
29

    
30
Host::Host(
31
    int     id,
32
    int     cluster_id,
33
    const string& _hostname,
34
    const string& _im_mad_name,
35
    const string& _vmm_mad_name,
36
    const string& _tm_mad_name):
37
        PoolObjectSQL(id,_hostname,-1,cluster_id,table),
38
        state(INIT),
39
        im_mad_name(_im_mad_name),
40
        vmm_mad_name(_vmm_mad_name),
41
        tm_mad_name(_tm_mad_name),
42
        last_monitored(0)
43
{
44
    obj_template = new HostTemplate;        
45
}
46

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

    
55
/* ************************************************************************ */
56
/* Host :: Database Access Functions                                        */
57
/* ************************************************************************ */
58

    
59
const char * Host::table = "host_pool";
60

    
61
const char * Host::db_names = "oid, name, body, state, last_mon_time, cid";
62

    
63
const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool ("
64
    "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, "
65
    "last_mon_time INTEGER, cid INTEGER, UNIQUE(name))";
66

    
67
/* ------------------------------------------------------------------------ */
68
/* ------------------------------------------------------------------------ */
69

    
70
int Host::insert(SqlDB *db, string& error_str)
71
{
72
    int rc;
73

    
74
    rc = insert_replace(db, false);
75

    
76
    if ( rc != 0 )
77
    {
78
        error_str = "Error inserting Host in DB.";
79
    }
80

    
81
    return rc;
82
}
83

    
84
/* ------------------------------------------------------------------------ */
85
/* ------------------------------------------------------------------------ */
86

    
87
int Host::update(SqlDB *db)
88
{
89
    int    rc;
90

    
91
    rc = insert_replace(db, true);
92

    
93
    return rc;
94
}
95

    
96
/* ------------------------------------------------------------------------ */
97
/* ------------------------------------------------------------------------ */
98

    
99
int Host::insert_replace(SqlDB *db, bool replace)
100
{
101
    ostringstream   oss;
102

    
103
    int    rc;
104
    string xml_body;
105

    
106
    char * sql_hostname;
107
    char * sql_xml;
108

    
109
   // Update the Host
110

    
111
    sql_hostname = db->escape_str(name.c_str());
112

    
113
    if ( sql_hostname == 0 )
114
    {
115
        goto error_hostname;
116
    }
117

    
118
    sql_xml = db->escape_str(to_xml(xml_body).c_str());
119

    
120
    if ( sql_xml == 0 )
121
    {
122
        goto error_body;
123
    }
124

    
125
    if(replace)
126
    {
127
        oss << "REPLACE";
128
    }
129
    else
130
    {
131
        oss << "INSERT";
132
    }
133

    
134
    // Construct the SQL statement to Insert or Replace
135

    
136
    oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
137
        <<          oid                 << ","
138
        << "'" <<   sql_hostname        << "',"
139
        << "'" <<   sql_xml             << "',"
140
        <<          state               << ","
141
        <<          last_monitored      << ","
142
        <<          gid                 << ")";
143

    
144
    rc = db->exec(oss);
145

    
146
    db->free_str(sql_hostname);
147
    db->free_str(sql_xml);
148

    
149
    return rc;
150

    
151
error_body:
152
    db->free_str(sql_hostname);
153
error_hostname:
154
    return -1;
155
}
156

    
157
/* ------------------------------------------------------------------------ */
158
/* ------------------------------------------------------------------------ */
159

    
160
int Host::update_info(string &parse_str)
161
{
162
    char *  error_msg;
163
    int     rc;
164

    
165
    rc = obj_template->parse(parse_str, &error_msg);
166

    
167
    if ( rc != 0 )
168
    {
169
        NebulaLog::log("ONE", Log::ERROR, error_msg);
170

    
171
        free(error_msg);
172
        return -1;
173
    }
174

    
175
    get_template_attribute("TOTALCPU",host_share.max_cpu);
176
    get_template_attribute("TOTALMEMORY",host_share.max_mem);
177

    
178
    get_template_attribute("FREECPU",host_share.free_cpu);
179
    get_template_attribute("FREEMEMORY",host_share.free_mem);
180

    
181
    get_template_attribute("USEDCPU",host_share.used_cpu);
182
    get_template_attribute("USEDMEMORY",host_share.used_mem);
183

    
184
    return 0;
185
}
186

    
187
/* ************************************************************************ */
188
/* Host :: Misc                                                             */
189
/* ************************************************************************ */
190

    
191
ostream& operator<<(ostream& os, Host& host)
192
{
193
    string host_str;
194

    
195
    os << host.to_xml(host_str);
196

    
197
    return os;
198
}
199

    
200
/* ------------------------------------------------------------------------ */
201
/* ------------------------------------------------------------------------ */
202

    
203
string& Host::to_xml(string& xml) const
204
{
205
    string template_xml;
206
    string share_xml;
207
    ostringstream   oss;
208

    
209
    oss <<
210
    "<HOST>"
211
       "<ID>"            << oid                  << "</ID>"            <<
212
       "<NAME>"          << name                << "</NAME>"          <<
213
       "<STATE>"         << state          << "</STATE>"         <<
214
       "<IM_MAD>"        << im_mad_name    << "</IM_MAD>"        <<
215
       "<VM_MAD>"        << vmm_mad_name   << "</VM_MAD>"        <<
216
       "<TM_MAD>"        << tm_mad_name    << "</TM_MAD>"        <<
217
       "<LAST_MON_TIME>" << last_monitored << "</LAST_MON_TIME>" <<
218
       "<CID>"           << gid            << "</CID>"       <<
219
       host_share.to_xml(share_xml)  <<
220
       obj_template->to_xml(template_xml) <<
221
    "</HOST>";
222

    
223
    xml = oss.str();
224

    
225
    return xml;
226
}
227

    
228
/* ------------------------------------------------------------------------ */
229
/* ------------------------------------------------------------------------ */
230

    
231
int Host::from_xml(const string& xml)
232
{
233
    vector<xmlNodePtr> content;
234
    
235
    int int_state;
236
    int rc = 0;
237

    
238
    // Initialize the internal XML object
239
    update_from_str(xml);
240

    
241
    // Get class base attributes
242
    rc += xpath(oid, "/HOST/ID", -1);
243
    rc += xpath(name, "/HOST/NAME", "not_found");
244
    rc += xpath(int_state, "/HOST/STATE", 0);
245

    
246
    rc += xpath(im_mad_name, "/HOST/IM_MAD", "not_found");
247
    rc += xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found");
248
    rc += xpath(tm_mad_name, "/HOST/TM_MAD", "not_found");
249

    
250
    rc += xpath(last_monitored, "/HOST/LAST_MON_TIME", 0);
251
    rc += xpath(gid, "/HOST/CID", 0);
252

    
253
    state = static_cast<HostState>( int_state );
254

    
255
    // Get associated classes
256
    ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
257

    
258
    if( content.size() < 1 )
259
    {
260
        return -1;
261
    }
262

    
263
    rc += host_share.from_xml_node( content[0] );
264

    
265
    content.clear();
266
    
267
    ObjectXML::get_nodes("/HOST/TEMPLATE", content);
268

    
269
    if( content.size() < 1 )
270
    {
271
        return -1;
272
    }
273

    
274
    rc += obj_template->from_xml_node( content[0] );
275

    
276
    if (rc != 0)
277
    {
278
        return -1;
279
    }
280

    
281
    return 0;
282
}