Revision f8615d6e include/AddressRange.h

View differences:

include/AddressRange.h
35 35
{
36 36
public:
37 37

  
38
    AddressRange(unsigned int _id):id(_id),next(0),used_addr(0){};
39

  
40 38
    virtual ~AddressRange(){};
41 39

  
42 40
    // *************************************************************************
......
103 101
     *            SIZE = 1024,
104 102
     *            ULA_PREFIX    = "fd00:0:0:1::",
105 103
     *            GLOBAL_PREFIX = "2001::"]
104
     *
105
     * NOTE: This function is part of the AddressRange interface. The AR
106
     * implementation may contact an external IPAM to complete or validate
107
     * the AR allocation request.
106 108
     */
107
    int from_vattr(VectorAttribute * attr, string& error_msg);
109
    virtual int from_vattr(VectorAttribute * attr, string& error_msg) = 0;
108 110

  
109 111
    /**
110 112
     *  Builds an Address Range from a vector attribute stored in the DB
......
276 278
     */
277 279
    unsigned int get_used_addr() const
278 280
    {
279
        return used_addr;
281
        return allocated.size();
280 282
    }
281 283

  
282 284
    /**
......
284 286
     */
285 287
    unsigned int get_free_addr() const
286 288
    {
287
        return size - used_addr;
289
        return size - allocated.size();
288 290
    }
289 291

  
290 292
    /**
......
331 333
            string&             error_msg);
332 334

  
333 335
    /**
336
     *  Helper function to initialize restricte attributes of an AddressRange
337
     */
338
    static void set_restricted_attributes(vector<const SingleAttribute *>& ras);
339

  
340
    /**
334 341
     *  Get the security groups for this AR.
335 342
     *    @return a reference to the security group set
336 343
     */
......
345 352
     */
346 353
    friend int AddressRangePool::add_ar(AddressRange * ar);
347 354

  
348
    static void set_restricted_attributes(vector<const SingleAttribute *>& rattrs);
355
protected:
356
    /**
357
     *  Base constructor it cannot be called directly but from the
358
     *  AddressRange factory constructor.
359
     */
360
    AddressRange(unsigned int _id):id(_id){};
361

  
362
    /**
363
     * Builds the AddressRange from its vector attribute representation
364
     */
365
    int from_attr(VectorAttribute * attr, string& error_msg);
366

  
367
    /* ---------------------------------------------------------------------- */
368
    /* Implementation specific address management interface                   */
369
    /* ---------------------------------------------------------------------- */
370
    /**
371
     *  Sets the given range of addresses (by index) as used
372
     *    @param index the first address to set as used
373
     *    @param sz number of addresses to set
374
     *    @param msg describing the error if any
375
     *
376
     *    @return 0 if success
377
     */
378
    virtual int allocate_addr(unsigned int index, unsigned int sz, string& msg) = 0;
379
    /**
380
     *  Gets a range of free addresses
381
     *    @param index the first address in the range
382
     *    @param size number of addresses requested in the range
383
     *    @param msg describing the error if any
384
     *
385
     *    @return 0 if success
386
     */
387
    virtual int get_addr(unsigned int& index, unsigned int sz, string& msg) = 0;
388

  
389
    /**
390
     *  Sets the given address (by index) as free
391
     *    @param index of the address
392
     *    @param msg describing the error if any
393
     *
394
     *    @return 0 if success
395
     */
396
    virtual int free_addr(unsigned int index, string& msg) = 0;
397

  
398
    /* ---------------------------------------------------------------------- */
399
    /* Allocated addresses                                                    */
400
    /* ---------------------------------------------------------------------- */
401
    /**
402
     *  Map to store the allocated address indexed by the address index relative
403
     *  to the mac/ip values. It contains also the type and id of the object
404
     *  owning the address.
405
     *
406
     *              +--------------------+--------------------+
407
     *  index ----> | ObjectType(32bits) | Object ID (32bits) |
408
     *              +--------------------+--------------------+
409
     *
410
     *  Address = First Address + index
411
     */
412
    map<unsigned int, long long> allocated;
349 413

  
350 414
private:
351 415
    /* ---------------------------------------------------------------------- */
......
399 463
    /* ---------------------------------------------------------------------- */
400 464
    /* NIC setup functions                                                    */
401 465
    /* ---------------------------------------------------------------------- */
466

  
467
    /**
468
     *  Check if the given MAC is valid for this address range by verifying:
469
     *    - Correct : notation
470
     *    - Part of the AR
471
     *
472
     *    @param index of the MAC in the AR
473
     *    @param mac_s string representation of the MAC in : notation
474
     *    @param check_free apart from previous checks
475
     *
476
     *    @return true if the MAC is valid
477
     */
478
    bool is_valid_mac(unsigned int& index, const string& mac_s, bool check_free);
479

  
480
    /**
481
     *  Check if the given IP is valid for this address range by verifying:
482
     *    - AR is of type IP4 or IP4_6
483
     *    - Correct . notation
484
     *    - Part of the AR
485
     *
486
     *    @param index of the IP in the AR
487
     *    @param ip_s string representation of the IP in . notation
488
     *    @param check_free apart from previous checks
489
     *
490
     *    @return true if the IP is valid
491
     */
492
    bool is_valid_ip(unsigned int& index, const string& ip_s, bool check_free);
493

  
402 494
    /**
403 495
     *  Writes MAC address to the given NIC attribute
404 496
     *    @param addr_index internal index for the lease
......
450 542
    /**
451 543
     *  Adds a new allocated address to the map. Updates the ALLOCATED attribute
452 544
     */
453
    void allocate_addr(PoolObjectSQL::ObjectType ot, int obid,
545
    void set_allocated_addr(PoolObjectSQL::ObjectType ot, int obid,
454 546
        unsigned int addr_index);
455 547

  
456 548
    /**
549
     *  Sets the address lease as used and fills a NIC attribute with the
550
     *  configuration parameters from the address range.
551
     *    @param index of the lease in the address range
552
     *    @param ot the type of the object allocating the address
553
     *    @param obid the id of the object
554
     *    @param nic the VM NIC attribute
555
     *    @param inherit attributes to be added to the NIC attribute
556
     *    @return 0 if success
557
     */
558
    void allocate_by_index(unsigned int index,
559
        PoolObjectSQL::ObjectType ot,
560
        int                       obid,
561
        VectorAttribute*          nic,
562
        const vector<string>&     inherit);
563

  
564
    /**
457 565
     *  Frees an address from the map. Updates the ALLOCATED attribute
458 566
     */
459
    int free_addr(PoolObjectSQL::ObjectType ot, int obid,
567
    int free_allocated_addr(PoolObjectSQL::ObjectType ot, int obid,
460 568
        unsigned int addr_index);
461 569

  
462 570
    /**
......
529 637
    set<int> security_groups;
530 638

  
531 639
    /**
532
     *  The Address Range attributes as a Template VectorAttribute. This is
533
     *  used to generate XML or a template representation of the AR.
640
     *  The name of the IPAM driver (internal for OpenNebula built-in)
534 641
     */
535
    VectorAttribute * attr;
642
    string ipam_mad;
536 643

  
537
    /* ---------------------------------------------------------------------- */
538
    /* Allocated address & control                                            */
539
    /* ---------------------------------------------------------------------- */
540 644
    /**
541
     *  Map to store the allocated address indexed by the address index relative
542
     *  to the mac/ip values. It contains also the type and id of the object
543
     *  owning the address ObjectType(32bits) | Object ID (32)
645
     *  The Address Range attributes as a Template VectorAttribute. This is
646
     *  used to generate XML or a template representation of the AR.
544 647
     */
545
    map<unsigned int, long long> allocated;
546

  
547
    unsigned int next;
548

  
549
    unsigned int used_addr;
648
    VectorAttribute * attr;
550 649

  
551 650
    /* ---------------------------------------------------------------------- */
552 651
    /* Restricted Attributes                                                  */

Also available in: Unified diff