Statistics
| Branch: | Tag: | Revision:

one / include / VMTemplate.h @ d0973401

History | View | Annotate | Download (7.67 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2017, 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
#ifndef VMTEMPLATE_H_
18
#define VMTEMPLATE_H_
19

    
20
#include "PoolObjectSQL.h"
21
#include "VirtualMachineTemplate.h"
22

    
23
/* -------------------------------------------------------------------------- */
24
/* -------------------------------------------------------------------------- */
25

    
26
/**
27
 *  The VMTemplate class.
28
 */
29
class VMTemplate : public PoolObjectSQL
30
{
31
public:
32

    
33
    /**
34
     * Function to print the VMTemplate object into a string in XML format
35
     *  @param xml the resulting XML string
36
     *  @return a reference to the generated string
37
     */
38
    string& to_xml(string& xml) const;
39

    
40

    
41
    /**
42
     * Function to print the VMTemplate object into a string in XML format
43
     *  @param xml the resulting XML string
44
     *  @param tmpl a template to replace the internal obj_template. It is only
45
     *  used to create the resulting xml string, the internal obj_template is
46
     *  not altered
47
     *  @return a reference to the generated string
48
     */
49
    string& to_xml(string& xml, const Template* tmpl) const;
50

    
51
    // ------------------------------------------------------------------------
52
    // Template Contents
53
    // ------------------------------------------------------------------------
54

    
55
    /**
56
     *  Factory method for virtual machine templates
57
     */
58
    Template * get_new_template() const
59
    {
60
        return new VirtualMachineTemplate;
61
    }
62

    
63
    /**
64
     *  Returns a copy of the VirtualMachineTemplate
65
     *    @return A copy of the VirtualMachineTemplate
66
     */
67
    VirtualMachineTemplate * clone_template() const
68
    {
69
        return new VirtualMachineTemplate(
70
                *(static_cast<VirtualMachineTemplate *>(obj_template)));
71
    };
72

    
73
    /**
74
     * Returns a copy of the DISK attributes of this template, the attributes
75
         * are copied and must be freed by the calling function.
76
         *   @param a vector to store the disks.
77
     */
78
    void clone_disks(vector<VectorAttribute *>& disks)
79
    {
80
                vector<const VectorAttribute *> _disks;
81

    
82
                obj_template->get("DISK", _disks);
83

    
84
                for (vector<const VectorAttribute *>::const_iterator i = _disks.begin();
85
                                i != _disks.end() ; ++i)
86
                {
87
                        disks.push_back(new VectorAttribute(*i));
88
                }
89
    }
90

    
91
    /**
92
     * Replaces the current DISK attributes with the given ones. The objects
93
     * must NOT be deleted by the calling function, and should be obtained
94
     * through a clone_disks() call.
95
     *   @param disks a vector with the new disks
96
     *
97
     */
98
    void replace_disks(vector<VectorAttribute *>& disks)
99
    {
100
        obj_template->erase("DISK");
101

    
102
        obj_template->set(disks);
103
    }
104

    
105
    /**
106
    *  This method check if sched_action is malformed or not
107
        *
108
    *    @return false if the attribute is malformed
109
    */
110
    bool parse_sched_action()
111
    {
112
        bool malformed = false;
113
        string action = "";
114
        VectorAttribute *attr = nullptr;
115
        vector<const VectorAttribute *> _sched_actions;
116
        obj_template->get("SCHED_ACTION",_sched_actions);
117

    
118
        for (vector<const VectorAttribute *>::const_iterator i = _sched_actions.begin();
119
                                i != _sched_actions.end() && !malformed ; ++i)
120
                {
121
                    attr = new VectorAttribute(*i);
122
            if ( attr->vector_value("DONE") != "" || attr->vector_value("MESSAGE") != "" )
123
            {
124
                malformed = true;
125
            }
126
                }
127
        return !malformed;
128
    }
129

    
130
    // ------------------------------------------------------------------------
131
    // Virtual Router
132
    // ------------------------------------------------------------------------
133

    
134
    /**
135
     * Returns true if this Template is a Virtual Router Template
136
     * @return true if this Template is a Virtual Router Template
137
     */
138
    bool is_vrouter();
139

    
140
private:
141
    // -------------------------------------------------------------------------
142
    // Friends
143
    // -------------------------------------------------------------------------
144

    
145
    friend class VMTemplatePool;
146

    
147
    // -------------------------------------------------------------------------
148
    // VMTemplate Attributes
149
    // -------------------------------------------------------------------------
150

    
151
    /**
152
     *  Registration time
153
     */
154
    time_t      regtime;
155

    
156
    // *************************************************************************
157
    // DataBase implementation (Private)
158
    // *************************************************************************
159

    
160
    /**
161
     *  Execute an INSERT or REPLACE Sql query.
162
     *    @param db The SQL DB
163
     *    @param replace Execute an INSERT or a REPLACE
164
     *    @param error_str Returns the error reason, if any
165
     *    @return 0 one success
166
     */
167
    int insert_replace(SqlDB *db, bool replace, string& error_str);
168

    
169
    /**
170
     *  Execute this method after update the template.
171
     *    @param error Returns the error reason, if any
172
     *    @return 0 one success
173
     */
174
    int post_update_template(string& error);
175

    
176
    /**
177
     *  Bootstraps the database table(s) associated to the VMTemplate
178
     *    @return 0 on success
179
     */
180
    static int bootstrap(SqlDB * db)
181
    {
182
        ostringstream oss(VMTemplate::db_bootstrap);
183

    
184
        return db->exec_local_wr(oss);
185
    };
186

    
187
    /**
188
     *  Rebuilds the object from an xml formatted string
189
     *    @param xml_str The xml-formatted string
190
     *
191
     *    @return 0 on success, -1 otherwise
192
     */
193
    int from_xml(const string &xml_str);
194

    
195
protected:
196

    
197
    // *************************************************************************
198
    // Constructor
199
    // *************************************************************************
200
    VMTemplate(int id,
201
               int uid,
202
               int gid,
203
               const string& uname,
204
               const string& gname,
205
               int umask,
206
               VirtualMachineTemplate * _template_contents);
207

    
208
    ~VMTemplate();
209

    
210
    // *************************************************************************
211
    // DataBase implementation
212
    // *************************************************************************
213

    
214
    static const char * db_names;
215

    
216
    static const char * db_bootstrap;
217

    
218
    static const char * table;
219

    
220
    /**
221
     *  Writes the VMTemplate in the database.
222
     *    @param db pointer to the db
223
     *    @param error_str Returns the error reason, if any
224
     *    @return 0 on success
225
     */
226
    int insert(SqlDB *db, string& error_str);
227

    
228
    /**
229
     *  Writes/updates the VMTemplate data fields in the database.
230
     *    @param db pointer to the db
231
     *    @return 0 on success
232
     */
233
    int update(SqlDB *db)
234
    {
235
        string err;
236
        return insert_replace(db, true, err);
237
    };
238
};
239

    
240
#endif /*VMTEMPLATE_H_*/