Statistics
| Branch: | Tag: | Revision:

one / src / scheduler / include / VirtualMachineXML.h @ 4697f1ee

History | View | Annotate | Download (9.18 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems                */
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

    
18
#ifndef VM_XML_H_
19
#define VM_XML_H_
20

    
21
#include <sstream>
22

    
23
#include "ObjectXML.h"
24
#include "HostPoolXML.h"
25
#include "Resource.h"
26

    
27
#include "VirtualMachineTemplate.h"
28

    
29
class ImageDatastorePoolXML;
30

    
31
using namespace std;
32

    
33
class VirtualMachineXML : public ObjectXML
34
{
35
public:
36

    
37
    VirtualMachineXML(const string &xml_doc): ObjectXML(xml_doc)
38
    {
39
        init_attributes();
40
    };
41

    
42
    VirtualMachineXML(const xmlNodePtr node): ObjectXML(node)
43
    {
44
        init_attributes();
45
    }
46

    
47
    ~VirtualMachineXML()
48
    {
49
        if (vm_template != 0)
50
        {
51
            delete vm_template;
52
        }
53

    
54
        if (user_template != 0)
55
        {
56
            delete user_template;
57
        }
58
    }
59

    
60
    //--------------------------------------------------------------------------
61
    // Get Methods for VirtualMachineXML class
62
    //--------------------------------------------------------------------------
63

    
64
    int get_oid() const
65
    {
66
        return oid;
67
    };
68

    
69
    int get_uid() const
70
    {
71
        return uid;
72
    };
73

    
74
    int get_gid() const
75
    {
76
        return gid;
77
    };
78

    
79
    int get_hid() const
80
    {
81
        return hid;
82
    };
83

    
84
    int get_dsid() const
85
    {
86
        return dsid;
87
    };
88

    
89
    bool is_resched() const
90
    {
91
        return (resched == 1);
92
    }
93

    
94
    bool is_resume() const
95
    {
96
        return resume;
97
    }
98

    
99
    const string& get_rank()
100
    {
101
        return rank;
102
    };
103

    
104
    const string& get_ds_rank()
105
    {
106
        return ds_rank;
107
    };
108

    
109
    const string& get_requirements()
110
    {
111
        return requirements;
112
    };
113

    
114
    const string& get_ds_requirements()
115
    {
116
        return ds_requirements;
117
    }
118

    
119
    const string& get_cluster_requirements()
120
    {
121
        return cluster_requirements;
122
    };
123

    
124
    void get_requirements (int& cpu, int& memory, long long& disk,
125
        vector<VectorAttribute *> &pci);
126

    
127
    map<int,long long> get_storage_usage();
128

    
129
    /**
130
     * Checks if the VM can be deployed in a public cloud provider
131
     * @return true if the VM can be deployed in a public cloud provider
132
     */
133
    bool is_public_cloud() const
134
    {
135
        return public_cloud;
136
    };
137

    
138
    //--------------------------------------------------------------------------
139
    // Matched Resources Interface
140
    //--------------------------------------------------------------------------
141

    
142
    /**
143
     *  Adds a matching host if it is not equal to the actual one
144
     *    @param oid of the host
145
     */
146
    void add_match_host(int oid)
147
    {
148
        if (( resched == 1 && hid != oid ) || ( resched == 0 ))
149
        {
150
            match_hosts.add_resource(oid);
151
        }
152
    };
153

    
154
    /**
155
     *  Adds a matching datastore
156
     *    @param oid of the datastore
157
     */
158
    void add_match_datastore(int oid)
159
    {
160
        match_datastores.add_resource(oid);
161
    }
162

    
163
    /**
164
     *  Adds a matching cluster
165
     *    @param oid of the cluster
166
     */
167
    void add_match_cluster(int oid)
168
    {
169
        match_clusters.insert(oid);
170
    }
171

    
172
    /**
173
     *  Returns a vector of matched hosts
174
     */
175
    const vector<Resource *> get_match_hosts()
176
    {
177
        return match_hosts.get_resources();
178
    }
179

    
180
    /**
181
     *  Returns a vector of matched datastores
182
     */
183
    const vector<Resource *> get_match_datastores()
184
    {
185
        return match_datastores.get_resources();
186
    }
187

    
188
    /**
189
     *  Returns a vector of matched hosts
190
     */
191
    const set<int>& get_match_clusters()
192
    {
193
        return match_clusters;
194
    }
195

    
196
    /**
197
     *  Sort the matched hosts for the VM
198
     */
199
    void sort_match_hosts()
200
    {
201
        match_hosts.sort_resources();
202
    }
203

    
204
    /**
205
     *  Sort the matched datastores for the VM
206
     */
207
    void sort_match_datastores()
208
    {
209
        match_datastores.sort_resources();
210
    }
211

    
212
    /**
213
     *  Removes the matched hosts
214
     */
215
    void clear_match_hosts()
216
    {
217
        match_hosts.clear();
218
    }
219

    
220
    /**
221
     *  Removes the matched datastores
222
     */
223
    void clear_match_datastores()
224
    {
225
        match_datastores.clear();
226
    }
227

    
228
    /**
229
     * Marks the VM to be only deployed on public cloud hosts
230
     */
231
    void set_only_public_cloud();
232

    
233
    /**
234
     * Returns true is the VM can only be deployed in public cloud hosts
235
     * @return true is the VM can only be deployed in public cloud hosts
236
     */
237
    bool is_only_public_cloud() const;
238

    
239
    //--------------------------------------------------------------------------
240
    // Capacity Interface
241
    //--------------------------------------------------------------------------
242

    
243
    /**
244
     *  Tests if the Image DS have enough free space to host the VM
245
     *    @param img_datastores Image Datastores
246
     *    @param error_msg error reason
247
     *    @return true if the Image Datastores can host the VM
248
     */
249
    bool test_image_datastore_capacity(
250
            ImageDatastorePoolXML * img_dspool, string & error_msg) const;
251

    
252
    /**
253
     *  Tests if the Image DS have enough free space to host the VM
254
     *    @param img_datastores Image Datastores
255
     *    @return true if the Image Datastores can host the VM
256
     */
257
    bool test_image_datastore_capacity(
258
            ImageDatastorePoolXML * img_dspool) const
259
    {
260
        string tmp_st;
261
        return test_image_datastore_capacity(img_dspool, tmp_st);
262
    }
263

    
264
    /**
265
     *  Adds the VM disk requirements to each Image Datastore counter
266
     *    @param img_datastores Image Datastores
267
     */
268
    void add_image_datastore_capacity(ImageDatastorePoolXML * img_dspool);
269

    
270
    //--------------------------------------------------------------------------
271
    // Action Interface
272
    //--------------------------------------------------------------------------
273

    
274
    /**
275
     *  Get the user template of the VM
276
     *    @return the template as a XML string
277
     */
278
    string& get_template(string& xml_str)
279
    {
280
        if (user_template != 0)
281
        {
282
            user_template->to_xml(xml_str);
283
        }
284
        else
285
        {
286
            xml_str = "";
287
        }
288

    
289
        return xml_str;
290
    }
291

    
292
    /**
293
     * Removes (but does not delete) the scheduled actions of the VM
294
     *
295
     * @param attributes to hold the VM actions
296
     */
297
    void get_actions(vector<Attribute *>& attributes) const
298
    {
299
        attributes.clear();
300

    
301
        user_template->remove("SCHED_ACTION", attributes);
302
    }
303

    
304
    /**
305
     * Sets an attribute in the VM Template, it must be allocated in the heap
306
     *
307
     * @param attributes to hold the VM actions
308
     */
309
    void set_attribute(Attribute* att)
310
    {
311
        return user_template->set(att);
312
    }
313

    
314
    /**
315
     *  Checks the action to be performed and returns the corresponding XML-RPC
316
     *  method name.
317
     *    @param action_st, the action to be performed. The XML-RPC name is
318
     *    returned here
319
     *    @return 0 on success.
320
     */
321
    static int parse_action_name(string& action_st);
322

    
323
    /**
324
     *  Function to write a Virtual Machine in an output stream
325
     */
326
    friend ostream& operator<<(ostream& os, VirtualMachineXML& vm);
327

    
328
    /**
329
     * Adds a message to the VM's USER_TEMPLATE/SCHED_MESSAGE attribute
330
     *   @param st Message to set
331
     */
332
    void log(const string &st);
333

    
334
    /**
335
     * Clears the VM's USER_TEMPLATE/SCHED_MESSAGE attribute
336
     * @return true if the template was modified, false if SCHED_MESSAGE did not
337
     * need to be deleted
338
     */
339
    bool clear_log();
340

    
341
protected:
342

    
343
    /**
344
     *  For constructors
345
     */
346
    void init_attributes();
347

    
348
    void init_storage_usage();
349

    
350
    ResourceMatch match_hosts;
351

    
352
    ResourceMatch match_datastores;
353

    
354
    set<int> match_clusters;
355

    
356
    bool only_public_cloud;
357

    
358
    /* ----------------------- VIRTUAL MACHINE ATTRIBUTES ------------------- */
359
    int   oid;
360

    
361
    int   uid;
362
    int   gid;
363

    
364
    int   hid;
365
    int   dsid;
366

    
367
    int   resched;
368
    bool  resume;
369

    
370
    int         memory;
371
    float       cpu;
372
    long long   system_ds_usage;
373

    
374
    map<int,long long> ds_usage;
375

    
376
    bool   public_cloud;
377

    
378
    string rank;
379
    string requirements;
380

    
381
    string ds_requirements;
382
    string ds_rank;
383

    
384
    string cluster_requirements;
385

    
386
    VirtualMachineTemplate * vm_template;   /**< The VM template */
387
    VirtualMachineTemplate * user_template; /**< The VM user template */
388
};
389

    
390
#endif /* VM_XML_H_ */