Revision 0dbb3330

View differences:

include/Image.h
221 221
        string& name,
222 222
        vector<const Attribute*>& values) const
223 223
    {
224
        return image_template.get(name,values);
224
        return image_template->get(name,values);
225 225
    };
226 226

  
227 227
    /**
......
235 235
        vector<const Attribute*>& values) const
236 236
    {
237 237
        string str=name;
238
        return image_template.get(str,values);
238
        return image_template->get(str,values);
239 239
    };
240 240

  
241 241
    /**
......
248 248
        string&         value) const
249 249
    {
250 250
        string str=name;
251
        image_template.get(str,value);
251
        image_template->get(str,value);
252 252
    }
253 253

  
254 254
    /**
......
261 261
        int&            value) const
262 262
    {
263 263
        string str=name;
264
        image_template.get(str,value);
264
        image_template->get(str,value);
265 265
    }
266 266

  
267 267
    /**
......
270 270
     */
271 271
    int remove_template_attribute(SqlDB * db, const string&   name)
272 272
    {
273
        return image_template.remove_attribute(db, name);
273
        return image_template->remove_attribute(db, name);
274 274
    }
275 275

  
276 276
private:
......
332 332
    /**
333 333
     *  The Image template, holds the Image attributes.
334 334
     */
335
    ImageTemplate    image_template;
335
    ImageTemplate *  image_template;
336 336

  
337 337

  
338 338
    // *************************************************************************
......
382 382
    // Constructor
383 383
    // *************************************************************************
384 384

  
385
    Image(int id=-1);
385
    Image(int uid=-1, ImageTemplate *img_template = 0);
386 386

  
387 387
    virtual ~Image();
388 388

  
include/ImagePool.h
55 55
     *                  -2 in case of template parse failure
56 56
     */
57 57
    int allocate (
58
        int            uid,
59
        const  string& stemplate,
60
        int *          oid);
58
        int             uid,
59
        ImageTemplate * img_template,
60
        int *           oid);
61 61

  
62 62
    /**
63 63
     *  Function to get a Image from the pool, if the object is not in memory
......
133 133
    {
134 134
        SingleAttribute * sattr = new SingleAttribute(name,value);
135 135

  
136
        return image->image_template.replace_attribute(db,sattr);
136
        return image->image_template->replace_attribute(db,sattr);
137 137
    }
138 138

  
139 139
    /** Delete an image attribute in the template (Image MUST be locked)
......
145 145
        Image *       image,
146 146
        const string& name)
147 147
    {
148
        return image->image_template.remove_attribute(db, name);
148
        return image->image_template->remove_attribute(db, name);
149 149
    }
150 150

  
151 151
    /**
include/VirtualNetwork.h
195 195
        string& name,
196 196
        vector<const Attribute*>& values) const
197 197
    {
198
        return vn_template.get(name,values);
198
        return vn_template->get(name,values);
199 199
    };
200 200

  
201 201
    /**
......
209 209
        vector<const Attribute*>& values) const
210 210
    {
211 211
        string str=name;
212
        return vn_template.get(str,values);
212
        return vn_template->get(str,values);
213 213
    };
214 214

  
215 215
    /**
......
222 222
        string&         value) const
223 223
    {
224 224
        string str=name;
225
        vn_template.get(str,value);
225
        vn_template->get(str,value);
226 226
    }
227 227

  
228 228
    /**
......
235 235
        int&            value) const
236 236
    {
237 237
        string str=name;
238
        vn_template.get(str,value);
238
        vn_template->get(str,value);
239 239
    }
240 240

  
241 241
private:
......
293 293
    /**
294 294
     *  The Virtual Network template, holds the VNW attributes.
295 295
     */
296
    VirtualNetworkTemplate  vn_template;
296
    VirtualNetworkTemplate * vn_template;
297 297

  
298 298
    // *************************************************************************
299 299
    // DataBase implementation (Private)
......
355 355
        int               rc;
356 356

  
357 357
        sattr = new SingleAttribute(name,value);
358
        rc    = vn_template.replace_attribute(db,sattr);
358
        rc    = vn_template->replace_attribute(db,sattr);
359 359

  
360 360
        if (rc != 0)
361 361
        {
......
371 371
    // Constructor
372 372
    //**************************************************************************
373 373

  
374
    VirtualNetwork();
374
    VirtualNetwork(VirtualNetworkTemplate * _vn_template = 0);
375 375

  
376 376
    ~VirtualNetwork();
377 377

  
......
428 428
    {
429 429
        int rc;
430 430

  
431
        rc =  vn_template.drop(db);
431
        rc =  vn_template->drop(db);
432 432

  
433 433
        rc += leases->drop(db);
434 434

  
include/VirtualNetworkPool.h
40 40
    ~VirtualNetworkPool(){};
41 41

  
42 42
    /**
43
     *  Function to allocate a new VN object
43
     *  Function to allocate a new VNET object
44 44
     *    @param uid user identifier
45
     *    @param stemplate a string describing the VN
45
     *    @param vn_template a VirtualNetworkTemplate describing the VNET
46 46
     *    @param oid the id assigned to the VM (output)
47
     *    @return oid on success, -1 error inserting in DB,-2 error parsing
48
     *     the template, -3 wrong attributes in template
47
     *    @return oid on success, -1 error
49 48
     */
50 49
    int allocate (
51 50
        int     uid,
52
        const  string& stemplate,
51
        VirtualNetworkTemplate * vn_template,
53 52
        int *  oid);
54 53

  
55 54
    /**
src/image/Image.cc
32 32
/* Image :: Constructor/Destructor                                           */
33 33
/* ************************************************************************ */
34 34

  
35
Image::Image(int _uid):
35
Image::Image(int _uid, ImageTemplate * _image_template):
36 36
        PoolObjectSQL(-1),
37 37
        uid(_uid),
38 38
        name(""),
......
41 41
        source(""),
42 42
        state(INIT),
43 43
        running_vms(0)
44
        {};
44
{
45
    if (_image_template != 0)
46
    {
47
        image_template = _image_template;
48
    }
49
    else
50
    {
51
        image_template = new ImageTemplate;
52
    }
53
};
45 54

  
46
Image::~Image(){};
55
Image::~Image()
56
{
57
    if (image_template != 0)
58
    {
59
        delete image_template;
60
    }
61
};
47 62

  
48 63
/* ************************************************************************ */
49 64
/* Image :: Database Access Functions                                        */
......
93 108

  
94 109
    running_vms = atoi(values[RUNNING_VMS]);
95 110

  
96
    image_template.id  = oid;
111
    image_template->id  = oid;
97 112

  
98 113
    return 0;
99 114
}
......
123 138

  
124 139
    // Get the template
125 140

  
126
    rc = image_template.select(db);
141
    rc = image_template->select(db);
127 142

  
128 143
    if ( rc != 0 )
129 144
    {
......
182 197
    // ------------ PUBLIC --------------------
183 198

  
184 199
    get_template_attribute("PUBLIC", public_attr);
185
    image_template.erase("PUBLIC");
200
    image_template->erase("PUBLIC");
186 201

  
187 202
    transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
188 203
        (int(*)(int))toupper);
......
198 213
        SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
199 214
                                          ImagePool::default_dev_prefix());
200 215

  
201
        image_template.set(dev_att);
216
        image_template->set(dev_att);
202 217
    }
203 218

  
204 219
    // ------------ SOURCE (path to store the image)--------------------
......
212 227

  
213 228
    // ------------ INSERT THE TEMPLATE --------------------
214 229

  
215
    if ( image_template.id == -1 )
230
    if ( image_template->id == -1 )
216 231
    {
217
        image_template.id = oid;
232
        image_template->id = oid;
218 233
    }
219 234

  
220 235
    state = DISABLED;
221 236

  
222
    rc = image_template.insert(db);
237
    rc = image_template->insert(db);
223 238

  
224 239
    if ( rc != 0 )
225 240
    {
......
234 249

  
235 250
    if ( rc != 0 )
236 251
    {
237
        image_template.drop(db);
252
        image_template->drop(db);
238 253

  
239 254
        return rc;
240 255
    }
......
374 389
        return -1;
375 390
    }
376 391

  
377
    image_template.drop(db);
392
    image_template->drop(db);
378 393

  
379 394
    oss << "DELETE FROM " << table << " WHERE oid=" << oid;
380 395

  
......
424 439
            "<SOURCE>"         << source      << "</SOURCE>"      <<
425 440
            "<STATE>"          << state       << "</STATE>"       <<
426 441
            "<RUNNING_VMS>"    << running_vms << "</RUNNING_VMS>" <<
427
            image_template.to_xml(template_xml)                   <<
442
            image_template->to_xml(template_xml)                   <<
428 443
        "</IMAGE>";
429 444

  
430 445
    xml = oss.str();
......
452 467
        "STATE       = "    << state       << endl <<
453 468
        "RUNNING_VMS = "    << running_vms << endl <<
454 469
        "TEMPLATE"          << endl
455
                            << image_template.to_str(template_str)
470
                            << image_template->to_str(template_str)
456 471
                            << endl;
457 472

  
458 473
    str = os.str();
src/image/ImagePool.cc
89 89

  
90 90
int ImagePool::allocate (
91 91
        int            uid,
92
        const  string& stemplate,
92
        ImageTemplate* img_template,
93 93
        int *          oid)
94 94
{
95
    int     rc;
96 95
    Image * img;
97

  
98 96
    string  name;
99
    char *  error_msg;
100 97

  
101 98
    // ---------------------------------------------------------------------
102 99
    // Build a new Image object
103 100
    // ---------------------------------------------------------------------
104
    img = new Image(uid);
105

  
106
    // ---------------------------------------------------------------------
107
    // Parse template
108
    // ---------------------------------------------------------------------
109
    rc = img->image_template.parse(stemplate, &error_msg);
110

  
111
    if ( rc != 0 )
112
    {
113
        ostringstream oss;
114
        oss << "ImagePool template parse error: " << error_msg;
115
        NebulaLog::log("IMG", Log::ERROR, oss);
116

  
117
        free(error_msg);
118
        delete img;
119

  
120
        return -1;
121
    }
101
    img = new Image(uid,img_template);
122 102

  
123 103
    img->get_template_attribute("NAME", name);
124 104

  
125 105
    // ---------------------------------------------------------------------
126 106
    // Insert the Object in the pool
127 107
    // ---------------------------------------------------------------------
128

  
129 108
    *oid = PoolSQL::allocate(img);
130 109

  
131
    if ( *oid == -1 )
132
    {
133
        return -1;
134
    }
135

  
136 110
    // ---------------------------------------------------------------------
137 111
    // Add the image name to the map of image_names
138 112
    // ---------------------------------------------------------------------
139

  
140
    image_names.insert(make_pair(name, *oid));
113
    if ( *oid != -1 )
114
    {
115
        image_names.insert(make_pair(name, *oid));
116
    }
141 117

  
142 118
    return *oid;
143 119
}
src/rm/RequestManagerAllocate.cc
70 70
    //--------------------------------------------------------------------------
71 71
    //   Authorize this request
72 72
    //--------------------------------------------------------------------------
73

  
74 73
    vm_template = new VirtualMachineTemplate;
75 74

  
76 75
    rc = vm_template->parse(str_template,&error_msg);
......
129 128
    //--------------------------------------------------------------------------
130 129
    //   Allocate the VirtualMAchine
131 130
    //--------------------------------------------------------------------------
132

  
133 131
    rc = vmpool->allocate(uid,vm_template,&vid,false);
134 132

  
135 133
    if ( rc < 0 )
src/rm/RequestManagerImageAllocate.cc
29 29
    xmlrpc_c::value *   const  retval)
30 30
{
31 31
    string              session;
32
    string              image_template;
32
    string              str_template;
33

  
34
    ImageTemplate *     img_template;
33 35

  
34 36
    int                 iid;
35 37
    int                 uid;
36 38
    int                 rc;
39
    char *              error_msg = 0;
37 40

  
38 41
    ostringstream       oss;
39 42

  
......
42 45
    vector<xmlrpc_c::value> arrayData;
43 46
    xmlrpc_c::value_array * arrayresult;
44 47

  
45

  
46 48
    NebulaLog::log("ReM",Log::DEBUG,"ImageAllocate invoked");
47 49

  
48
    session        = xmlrpc_c::value_string(paramList.getString(0));
49
    image_template = xmlrpc_c::value_string(paramList.getString(1));
50
    image_template += "\n";
51

  
50
    session      = xmlrpc_c::value_string(paramList.getString(0));
51
    str_template = xmlrpc_c::value_string(paramList.getString(1));
52
    str_template += "\n";
52 53

  
53
    // First, we need to authenticate the user
54
    //--------------------------------------------------------------------------
55
    //   Authorize this request
56
    //--------------------------------------------------------------------------
54 57
    uid = ImageAllocate::upool->authenticate(session);
55 58

  
56 59
    if ( uid == -1 )
......
58 61
        goto error_authenticate;
59 62
    }
60 63

  
61
    rc = ImageAllocate::ipool->allocate(uid,image_template,&iid);
64
    //--------------------------------------------------------------------------
65
    //   Authorize this request
66
    //--------------------------------------------------------------------------
67
    img_template = new ImageTemplate;
68

  
69
    rc = img_template->parse(str_template,&error_msg);
70

  
71
    if ( rc != 0 )
72
    {
73
        goto error_parse;
74
    }
75

  
76
    if ( uid != 0 )
77
    {
78
        AuthRequest ar(uid);
79
        string      t64;
80

  
81
        ar.add_auth(AuthRequest::IMAGE,
82
                    img_template->to_xml(t64),
83
                    AuthRequest::CREATE,
84
                    uid,
85
                    false);
86

  
87
        if (UserPool::authorize(ar) == -1)
88
        {
89
            goto error_authorize;
90
        }
91
    }
92

  
93
    //--------------------------------------------------------------------------
94
    //   Allocate the Image
95
    //--------------------------------------------------------------------------
96
    rc = ImageAllocate::ipool->allocate(uid,img_template,&iid);
62 97

  
63 98
    if ( rc < 0 )
64 99
    {
......
81 116
    oss.str(authenticate_error(method_name));
82 117
    goto error_common;
83 118

  
119
error_authorize:
120
    oss.str(authorization_error(method_name, "CREATE", "IMAGE", uid, -1));
121
    delete img_template;
122
    goto error_common;
123

  
124
error_parse:
125
    oss.str(action_error(method_name, "PARSE", "IMAGE TEMPLATE",-2,rc));
126
    if (error_msg != 0)
127
    {
128
        oss << "Reason: " << error_msg;
129
        free(error_msg);
130
    }
131

  
132
    delete img_template;
133
    goto error_common;
134

  
84 135
error_allocate:
85 136
    oss.str(action_error(method_name, "CREATE", "IMAGE", -2, rc));
86 137
    goto error_common;
src/rm/RequestManagerVirtualNetworkAllocate.cc
27 27
    xmlrpc_c::value *   const  retval)
28 28
{
29 29
    string              session;
30
    string              username;
31
    string              password;
32 30
    string              name;
33
    string              stemplate;
31
    string              str_template;
32

  
33
    VirtualNetworkTemplate * vn_template;
34 34

  
35 35
    int                 nid;
36 36
    int                 uid;
37 37
    int                 rc;
38

  
39
    User *              user;
38
    char *              error_msg = 0;
40 39

  
41 40
    ostringstream       oss;
42
    
41

  
43 42
    const string        method_name = "VirtualNetworkAllocate";
44 43

  
45 44
    /*   -- RPC specific vars --  */
......
49 48
    NebulaLog::log("ReM",Log::DEBUG,"VirtualNetworkAllocate method invoked");
50 49

  
51 50
    // Get the parameters & host
52
    session   = xmlrpc_c::value_string(paramList.getString(0));
53
    stemplate = xmlrpc_c::value_string(paramList.getString(1));
51
    session      = xmlrpc_c::value_string(paramList.getString(0));
52
    str_template = xmlrpc_c::value_string(paramList.getString(1));
53

  
54
    //--------------------------------------------------------------------------
55
    //   Authorize this request
56
    //--------------------------------------------------------------------------
57
    uid = VirtualNetworkAllocate::upool->authenticate(session);
54 58

  
55
    if ( User::split_secret(session,username,password) != 0 )
59
    if ( uid == -1 )
56 60
    {
57 61
        goto error_authenticate;
58 62
    }
59 63

  
60
    // Now let's get the user
61
    user = VirtualNetworkAllocate::upool->get(username,true);
64
    //--------------------------------------------------------------------------
65
    //   Authorize this request
66
    //--------------------------------------------------------------------------
67
    vn_template = new VirtualNetworkTemplate;
62 68

  
63
    if ( user == 0 )
69
    rc = vn_template->parse(str_template,&error_msg);
70

  
71
    if ( rc != 0 )
64 72
    {
65
        goto error_get_user;
73
        goto error_parse;
66 74
    }
67 75

  
68
    uid = user->get_uid();
76
    if ( uid != 0 )
77
    {
78
        AuthRequest ar(uid);
79
        string      t64;
80

  
81
        ar.add_auth(AuthRequest::NET,
82
                    vn_template->to_xml(t64),
83
                    AuthRequest::CREATE,
84
                    uid,
85
                    false);
86

  
87
        if (UserPool::authorize(ar) == -1)
88
        {
89
            goto error_authorize;
90
        }
91
    }
69 92

  
70
    user->unlock();
71
    
72
    rc = vnpool->allocate(uid,stemplate,&nid);
93
    //--------------------------------------------------------------------------
94
    //   Allocate the Virtual Network
95
    //--------------------------------------------------------------------------
96
    rc = vnpool->allocate(uid,vn_template,&nid);
73 97

  
74 98
    if ( rc < 0 )
75 99
    {
......
92 116
    oss.str(authenticate_error(method_name));
93 117
    goto error_common;
94 118

  
95
error_get_user:
96
    oss.str(get_error(method_name, "USER", -1));
119
error_authorize:
120
    oss.str(authorization_error(method_name, "CREATE", "VNET", uid, -1));
121
    delete vn_template;
97 122
    goto error_common;
98
    
123

  
124
error_parse:
125
    oss.str(action_error(method_name, "PARSE", "VNET TEMPLATE",-2,rc));
126
    if (error_msg != 0)
127
    {
128
        oss << "Reason: " << error_msg;
129
        free(error_msg);
130
    }
131

  
132
    delete vn_template;
133
    goto error_common;
134

  
99 135
error_vn_allocate:
100 136
    oss.str(action_error(method_name, "CREATE", "NET", -2, rc));
101 137
    goto error_common;
src/vnm/VirtualNetwork.cc
29 29
/* Virtual Network :: Constructor/Destructor                                  */
30 30
/* ************************************************************************** */
31 31

  
32
VirtualNetwork::VirtualNetwork():
32
VirtualNetwork::VirtualNetwork(VirtualNetworkTemplate *_vn_template):
33 33
                PoolObjectSQL(-1),
34 34
                name(""),
35 35
                uid(-1),
36 36
                bridge(""),
37 37
                type(UNINITIALIZED),
38
                leases(0){};
38
                leases(0)
39
{
40
    if (_vn_template != 0)
41
    {
42
        vn_template = _vn_template;
43
    }
44
    else
45
    {
46
        vn_template = new VirtualNetworkTemplate;
47
    }
48
};
39 49

  
40 50
/* -------------------------------------------------------------------------- */
41 51
/* -------------------------------------------------------------------------- */
......
46 56
    {
47 57
        delete leases;
48 58
    }
59

  
60
    if (vn_template != 0)
61
    {
62
        delete vn_template;
63
    }
49 64
}
50 65

  
51 66
/* ************************************************************************** */
......
89 104
    public_vnet = atoi(values[PUBLIC]);
90 105

  
91 106
    // Virtual Network template ID is the Network ID
92
    vn_template.id = oid;
107
    vn_template->id = oid;
93 108

  
94 109
    return 0;
95 110
}
......
128 143
    }
129 144

  
130 145
    //Get the template
131
    rc = vn_template.select(db);
146
    rc = vn_template->select(db);
132 147

  
133 148
    if (rc != 0)
134 149
    {
......
315 330

  
316 331
    public_vnet = (pub == "YES");
317 332

  
318
    vn_template.erase("PUBLIC");
333
    vn_template->erase("PUBLIC");
319 334

  
320 335
    // ------------ INSERT THE TEMPLATE --------------------
321 336

  
322
    if ( vn_template.id == -1 )
337
    if ( vn_template->id == -1 )
323 338
    {
324
        vn_template.id = oid;
339
        vn_template->id = oid;
325 340
    }
326 341

  
327
    rc = vn_template.insert(db);
342
    rc = vn_template->insert(db);
328 343

  
329 344
    if ( rc != 0 )
330 345
    {
......
425 440

  
426 441
error_update:
427 442
    ose << "Can not update Virtual Network id " << oid;
428
    vn_template.drop(db);
443
    vn_template->drop(db);
429 444
    goto error_common;
430 445

  
431 446
error_addr:
......
506 521
    ostringstream   oss;
507 522
    int             rc;
508 523

  
509
    vn_template.drop(db);
524
    vn_template->drop(db);
510 525

  
511 526
    if ( leases != 0 )
512 527
    {
......
558 573
            "<TYPE>"    << type         << "</TYPE>" <<
559 574
            "<BRIDGE>"  << bridge       << "</BRIDGE>" <<
560 575
            "<PUBLIC>"  << public_vnet  << "</PUBLIC>" <<
561
            vn_template.to_xml(template_xml);
576
            vn_template->to_xml(template_xml);
562 577
    if (leases)
563 578
    {
564 579
        os << leases->to_xml(leases_xml);
......
596 611
    os << "Bridge            : " << bridge      << endl;
597 612
    os << "Public            : " << public_vnet << endl << endl;
598 613

  
599
    os << "....: Template :...." << vn_template.to_str(template_str) << endl << endl;
614
    os << "....: Template :...." << vn_template->to_str(template_str) << endl <<
615
endl;
600 616

  
601 617
    if (leases)
602 618
    {
src/vnm/VirtualNetworkPool.cc
71 71

  
72 72
int VirtualNetworkPool::allocate (
73 73
    int            uid,
74
    const  string& stemplate,
74
    VirtualNetworkTemplate * vn_template,
75 75
    int *          oid)
76 76
{
77 77
    VirtualNetwork *    vn;
78
    char *              error_msg;
79
    int                 rc;
80 78

  
81
    // Build a new Virtual Network object
82
    vn = new VirtualNetwork();
79
    vn = new VirtualNetwork(vn_template);
83 80

  
84 81
    vn->uid = uid;
85 82

  
86
    rc = vn->vn_template.parse(stemplate,&error_msg);
87

  
88
    if ( rc != 0 )
89
    {
90
        ostringstream oss;
91

  
92
        oss << error_msg;
93
        NebulaLog::log("VNM", Log::ERROR, oss);
94
        free(error_msg);
95

  
96
        delete vn;
97

  
98
        return -1;
99
    }
100

  
101
    // Insert the VN in the pool so we have a valid OID
102

  
103 83
    *oid = PoolSQL::allocate(vn);
104 84

  
105 85
    return *oid;

Also available in: Unified diff