Revision d03ed158

View differences:

include/Zone.h
22 22

  
23 23
using namespace std;
24 24

  
25
class ZoneServers;
26

  
25 27
/**
26 28
 *  The Zone class.
27 29
 */
......
45 47
    int from_xml(const string &xml_str);
46 48

  
47 49
private:
48

  
49 50
    // -------------------------------------------------------------------------
50 51
    // Friends
51 52
    // -------------------------------------------------------------------------
52

  
53 53
    friend class ZonePool;
54 54

  
55
    // *************************************************************************
55
    // -------------------------------------------------------------------------
56 56
    // Constructor
57
    // *************************************************************************
58

  
57
    // -------------------------------------------------------------------------
59 58
    Zone(int id, Template* zone_template);
60 59

  
61 60
    ~Zone();
62 61

  
63
    // *************************************************************************
64
    // DataBase implementation (Private)
65
    // *************************************************************************
62
    // -------------------------------------------------------------------------
63
    // Zone servers
64
    // -------------------------------------------------------------------------
65
    Template servers_template;
66

  
67
    ZoneServers * servers;
66 68

  
69
    // -------------------------------------------------------------------------
70
    // DataBase implementation (Private)
71
    // -------------------------------------------------------------------------
67 72
    static const char * db_names;
68 73

  
69 74
    static const char * db_bootstrap;
include/ZoneServer.h
1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2016, 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 ZONE_SERVER_H_
18
#define ZONE_SERVER_H_
19

  
20
#include <queue>
21
#include <set>
22

  
23
#include "ExtendedAttribute.h"
24

  
25
/**
26
 * The VirtualMachine DISK attribute
27
 */
28
class ZoneServer : public ExtendedAttribute
29
{
30
public:
31
    ZoneServer(VectorAttribute *va, int id):ExtendedAttribute(va, id){};
32

  
33
    virtual ~ZoneServer(){};
34

  
35
    int init(string& error)
36
    {
37
        return 0;
38
    }
39

  
40
    int get_id() const
41
    {
42
        return ExtendedAttribute::get_id();
43
    }
44

  
45
private:
46

  
47
};
48

  
49

  
50
/**
51
 *  Set of Zone servers
52
 */
53
class ZoneServers : public ExtendedAttributeSet
54
{
55
public:
56
    /* ---------------------------------------------------------------------- */
57
    /* Constructor and Initialization functions                               */
58
    /* ---------------------------------------------------------------------- */
59
    /**
60
     *  Creates the ZoneServers set from a zone template with SERVER=[...]
61
     *  attributes
62
     *    @param tmpl template with SERVER
63
     */
64
    ZoneServers(Template * tmpl):ExtendedAttributeSet(false), next_id(-1)
65
    {
66
        std::vector<VectorAttribute *> vas;
67

  
68
        tmpl->get(SERVER_NAME, vas);
69

  
70
        init_attribute_map(SERVER_ID_NAME, vas);
71

  
72
        for ( zone_iterator it = begin() ; it != end() ; ++it )
73
        {
74
            string error;
75

  
76
            int i = (*it)->get_id();
77

  
78
            if ( i > next_id )
79
            {
80
                next_id = i;
81
            }
82

  
83
            (*it)->init(error);
84
        }
85

  
86
        next_id += 1;
87
    };
88

  
89
    /**
90
     *  Creates an empty zone server set
91
     */
92
    ZoneServers():ExtendedAttributeSet(false){};
93

  
94
    virtual ~ZoneServers(){};
95

  
96
    /* ---------------------------------------------------------------------- */
97
    /* Iterators                                                              */
98
    /* ---------------------------------------------------------------------- */
99
    class ZoneIterator : public AttributeIterator
100
    {
101
    public:
102
        ZoneIterator():AttributeIterator(){};
103
        ZoneIterator(const AttributeIterator& dit):AttributeIterator(dit){};
104
        virtual ~ZoneIterator(){};
105

  
106
        ZoneServer * operator*() const
107
        {
108
            return static_cast<ZoneServer *>(map_it->second);
109
        }
110
    };
111

  
112
    ZoneIterator begin()
113
    {
114
        ZoneIterator it(ExtendedAttributeSet::begin());
115
        return it;
116
    }
117

  
118
    ZoneIterator end()
119
    {
120
        ZoneIterator it(ExtendedAttributeSet::end());
121
        return it;
122
    }
123

  
124
    typedef class ZoneIterator zone_iterator;
125

  
126
    /* ---------------------------------------------------------------------- */
127
    /* ZoneServer interface                                                   */
128
    /* ---------------------------------------------------------------------- */
129
    /**
130
     * Returns the SERVER attribute for a zone server
131
     *   @param disk_id of the DISK
132
     *   @return pointer to the attribute ir null if not found
133
     */
134
    ZoneServer * get_server(int id) const
135
    {
136
        return static_cast<ZoneServer *>(get_attribute(id));
137
    }
138

  
139
    int add_server(VectorAttribute * va, string& error)
140
    {
141
        ZoneServer * server = new ZoneServer(va, next_id);
142

  
143
        if ( server->init(error) != 0 )
144
        {
145
            delete server;
146

  
147
            return -1;
148
        }
149

  
150
        add_attribute(server, next_id);
151

  
152
        next_id += 1;
153

  
154
        return 0;
155
    };
156

  
157

  
158
protected:
159

  
160
    ExtendedAttribute * attribute_factory(VectorAttribute * va, int id) const
161
    {
162
        return new ZoneServer(va, id);
163
    };
164

  
165
private:
166

  
167
    static const char * SERVER_NAME; //"SERVER"
168

  
169
    static const char * SERVER_ID_NAME; //"ID"
170

  
171
    int next_id;
172
};
173

  
174
#endif  /*ZONE_SERVER_H_*/
175

  
src/zone/Zone.cc
15 15
/* ------------------------------------------------------------------------ */
16 16

  
17 17
#include "Zone.h"
18
#include "ZoneServer.h"
18 19

  
19 20
/* ------------------------------------------------------------------------ */
20 21

  
......
28 29
    "gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, "
29 30
    "UNIQUE(name))";
30 31

  
32
const char * ZoneServers::SERVER_NAME    = "SERVER";
33

  
34
const char * ZoneServers::SERVER_ID_NAME = "ID";
35

  
31 36
/* -------------------------------------------------------------------------- */
32 37
/* -------------------------------------------------------------------------- */
33 38

  
34 39
Zone::Zone(int id, Template* zone_template):
35
        PoolObjectSQL(id, ZONE, "", -1, -1, "", "", table)
40
        PoolObjectSQL(id, ZONE, "", -1, -1, "", "", table), servers_template(
41
                false, '=', "SERVER_POOL"), servers(0)
36 42
{
37 43
    if (zone_template != 0)
38 44
    {
......
50 56
Zone::~Zone()
51 57
{
52 58
    delete obj_template;
59

  
60
    delete servers;
53 61
};
54 62

  
55 63
/* ************************************************************************ */
......
63 71

  
64 72
    ostringstream oss;
65 73

  
66
    // ---------------------------------------------------------------------
74
    // -------------------------------------------------------------------------
67 75
    // Check default attributes
68
    // ---------------------------------------------------------------------
76
    // -------------------------------------------------------------------------
69 77

  
70 78
    erase_template_attribute("NAME", name);
71 79

  
......
82 90
        goto error_endpoint;
83 91
    }
84 92

  
85
    // ------------------------------------------------------------------------
93
    remove_template_attribute("SERVER");
94

  
95
    // -------------------------------------------------------------------------
86 96
    // Insert the Zone
87
    // ------------------------------------------------------------------------
97
    // -------------------------------------------------------------------------
88 98

  
89 99
    rc = insert_replace(db, false, error_str);
90 100

  
......
192 202

  
193 203
string& Zone::to_xml(string& xml) const
194 204
{
195
    ostringstream   oss;
196
    string          template_xml;
205
    ostringstream oss;
206

  
207
    string template_xml;
208
    string server_xml;
197 209

  
198 210
    oss <<
199 211
    "<ZONE>"    <<
200 212
        "<ID>"   << oid  << "</ID>"   <<
201 213
        "<NAME>" << name << "</NAME>" <<
202 214
        obj_template->to_xml(template_xml) <<
215
        servers_template.to_xml(server_xml) <<
203 216
    "</ZONE>";
204 217

  
205 218
    xml = oss.str();
......
207 220
    return xml;
208 221
}
209 222

  
210
/* ------------------------------------------------------------------------ */
211
/* ------------------------------------------------------------------------ */
223
/* -------------------------------------------------------------------------- */
224
/* -------------------------------------------------------------------------- */
212 225

  
213 226
int Zone::from_xml(const string& xml)
214 227
{
......
222 235
    rc += xpath(oid, "/ZONE/ID",   -1);
223 236
    rc += xpath(name,"/ZONE/NAME", "not_found");
224 237

  
225
    // Get associated classes
238
    // -------------------------------------------------------------------------
239
    // Zone template
240
    // -------------------------------------------------------------------------
226 241
    ObjectXML::get_nodes("/ZONE/TEMPLATE", content);
227 242

  
228 243
    if (content.empty())
......
230 245
        return -1;
231 246
    }
232 247

  
233
    // Template contents
234 248
    rc += obj_template->from_xml_node(content[0]);
235 249

  
236 250
    ObjectXML::free_nodes(content);
237 251
    content.clear();
238 252

  
253
    // -------------------------------------------------------------------------
254
    // Zone Server template
255
    // -------------------------------------------------------------------------
256
    ObjectXML::get_nodes("/ZONE/SERVER_POOL", content);
257

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

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

  
265
    ObjectXML::free_nodes(content);
266
    content.clear();
267

  
239 268
    if (rc != 0)
240 269
    {
241 270
        return -1;
......
245 274
    set_user(0,"");
246 275
    set_group(0,"");
247 276

  
277
    servers = new ZoneServers(&servers_template);
278

  
248 279
    return 0;
249 280
}
250 281

  
251
/* ------------------------------------------------------------------------ */
252
/* ------------------------------------------------------------------------ */
282
/* -------------------------------------------------------------------------- */
283
/* -------------------------------------------------------------------------- */
253 284

  
254 285
int Zone::post_update_template(string& error)
255 286
{
......
261 292
        replace_template_attribute("ENDPOINT", "-");
262 293
    }
263 294

  
295
    remove_template_attribute("SERVER");
296

  
264 297
    return 0;
265 298
}
266 299

  
267
/* ------------------------------------------------------------------------ */
268
/* ------------------------------------------------------------------------ */
300
/* -------------------------------------------------------------------------- */
301
/* -------------------------------------------------------------------------- */

Also available in: Unified diff