Revision 74aa41ba

View differences:

include/Image.h
204 204
     *      prefix + (d + index) :   hdd, hde, hdf...
205 205
     * @param disk attribute for the VM template
206 206
     */
207
    void disk_attribute(VectorAttribute ** disk, int index);
207
    void disk_attribute(VectorAttribute * disk, int index);
208 208

  
209 209
    // ------------------------------------------------------------------------
210 210
    // Template
include/ImagePool.h
190 190
            return -1;
191 191
        }
192 192

  
193
        img->disk_attribute(&disk,index);
193
        img->disk_attribute(disk,index);
194 194

  
195 195
        img->unlock();
196 196

  
include/VirtualNetwork.h
73 73
     */
74 74
    bool is_public()
75 75
    {
76
        return public_vnet;
76
        return (public_vnet == 1);
77 77
    };
78 78

  
79 79
    /**
......
144 144
     */
145 145
    string& to_xml(string& xml) const;
146 146

  
147
    /**
148
     * Modifies the given nic attribute adding the following attributes:
149
     *  * IP:  leased from network
150
     *  * MAC: leased from network
151
     *  * BRIDGE: for this virtual network
152
     *  @param nic attribute for the VM template
153
     *  @param vid of the VM getting the lease
154
     *  @return 0 on success
155
     */
156
    int nic_attribute(VectorAttribute * nic, int vid);
157

  
147 158
private:
148 159

  
149 160
    // -------------------------------------------------------------------------
include/VirtualNetworkPool.h
81 81
    //--------------------------------------------------------------------------
82 82

  
83 83
    /**
84
     *  Generates a NIC attribute for VM templates using the VirtualNetwork
85
     *  metadata
86
     *    @param nic the nic attribute to be generated
87
     *    @param vid of the VM requesting the lease
88
     *    @return 0 on success, -1 error, -2 not using the pool
89
     */
90
    int nic_attribute(VectorAttribute * nic, int vid)
91
    {
92
        string           network;
93
        VirtualNetwork * vnet;
94

  
95
        network = nic->vector_value("NETWORK");
96

  
97
        if (network.empty())
98
        {
99
            return -2;
100
        }
101

  
102
        vnet = get(network,true);
103

  
104
        if (vnet == 0)
105
        {
106
            return -1;
107
        }
108

  
109
        int rc = vnet->nic_attribute(nic,vid);
110

  
111
        vnet->unlock();
112

  
113
        return rc;
114
    }
115

  
116
    /**
84 117
     *  Updates the template of a VN, adding a new attribute (replacing it if
85 118
     *  already defined), the VN's mutex SHOULD be locked
86 119
     *    @param vn pointer to the virtual network object
src/image/Image.cc
459 459
/* ------------------------------------------------------------------------ */
460 460
/* ------------------------------------------------------------------------ */
461 461

  
462
void Image::disk_attribute(VectorAttribute ** disk, int index)
462
void Image::disk_attribute(VectorAttribute * disk, int index)
463 463
{
464 464
    string  overwrite;
465 465
    string  saveas;
466 466
    string  name;
467 467
    string  bus;
468 468

  
469
    name      = (*disk)->vector_value("NAME");
470
    overwrite = (*disk)->vector_value("OVERWRITE");
471
    saveas    = (*disk)->vector_value("SAVE_AS");
472
    bus       = (*disk)->vector_value("BUS");
469
    name      = disk->vector_value("NAME");
470
    overwrite = disk->vector_value("OVERWRITE");
471
    saveas    = disk->vector_value("SAVE_AS");
472
    bus       = disk->vector_value("BUS");
473 473

  
474 474
    string template_bus;
475 475
    string prefix;
......
480 480
   //---------------------------------------------------------------------------
481 481
   //                       NEW DISK ATTRIBUTES
482 482
   //---------------------------------------------------------------------------
483
    VectorAttribute * new_disk = new VectorAttribute("DISK");
484 483

  
485
    new_disk->replace("NAME",name);
486
    new_disk->replace("OVERWRITE",overwrite);
487
    new_disk->replace("SAVE_AS",saveas);
484
    map<string,string> new_disk;
488 485

  
489
    new_disk->replace("SOURCE", source);
486
    new_disk.insert(make_pair("NAME",name));
487
    new_disk.insert(make_pair("OVERWRITE",overwrite));
488
    new_disk.insert(make_pair("SAVE_AS",saveas));
489

  
490
    new_disk.insert(make_pair("SOURCE", source));
490 491

  
491 492
    if (bus.empty())
492 493
    {
493 494
        if (!template_bus.empty())
494 495
        {
495
            new_disk->replace("BUS",template_bus);
496
            new_disk.insert(make_pair("BUS",template_bus));
496 497
        }
497 498
    }
498 499
    else
499 500
    {
500
        new_disk->replace("BUS",bus);
501
        new_disk.insert(make_pair("BUS",bus));
501 502
    }
502 503

  
503 504
   //---------------------------------------------------------------------------
......
511 512
    {
512 513
        case OS:
513 514
        case DATABLOCK:
514
          new_disk->replace("TYPE","DISK");
515
          new_disk->replace("READONLY","NO");
515
          new_disk.insert(make_pair("TYPE","DISK"));
516
          new_disk.insert(make_pair("READONLY","NO"));
516 517

  
517 518
          if (overwrite == "YES")
518 519
          {
519
              new_disk->replace("CLONE","NO");
520
              new_disk->replace("SAVE","YES");
520
              new_disk.insert(make_pair("CLONE","NO"));
521
              new_disk.insert(make_pair("SAVE","YES"));
521 522
          }
522 523
          else if (saveas == "YES")
523 524
          {
524
              new_disk->replace("CLONE","YES");
525
              new_disk->replace("SAVE","YES");
525
              new_disk.insert(make_pair("CLONE","YES"));
526
              new_disk.insert(make_pair("SAVE","YES"));
526 527
          }
527 528
          else
528 529
          {
529
              new_disk->replace("CLONE","YES");
530
              new_disk->replace("SAVE","NO");
530
              new_disk.insert(make_pair("CLONE","YES"));
531
              new_disk.insert(make_pair("SAVE","NO"));
531 532
          }
532 533
        break;
533 534

  
534 535
        case CDROM:
535
          new_disk->replace("TYPE","CDROM");
536
          new_disk->replace("READONLY","YES");
536
          new_disk.insert(make_pair("TYPE","CDROM"));
537
          new_disk.insert(make_pair("READONLY","YES"));
537 538

  
538
          new_disk->replace("CLONE","YES");
539
          new_disk->replace("SAVE","NO");
539
          new_disk.insert(make_pair("CLONE","YES"));
540
          new_disk.insert(make_pair("SAVE","NO"));
540 541
        break;
541 542
    }
542 543

  
......
559 560
        break;
560 561

  
561 562
    }
563
    new_disk.insert(make_pair("TARGET", prefix));
562 564

  
563
    new_disk->replace("TARGET", prefix);
564

  
565
    delete (*disk);
566
    (*disk) = new_disk;
565
    disk->replace(new_disk);
567 566
}
568 567

  
569 568
/* ------------------------------------------------------------------------ */
src/vm/VirtualMachine.cc
594 594
    int                        num_nics, rc;
595 595
    vector<Attribute  * >      nics;
596 596
    VirtualNetworkPool       * vnpool;
597
    VirtualNetwork           * vn;
598 597
    VectorAttribute          * nic;
599
    map<string,string>         new_nic;
600

  
601
    string                     ip;
602
    string                     mac;
603
    string                     bridge;
604
    string                     network;
605
    string                     model;
606

  
607
    ostringstream              vnid;
608 598

  
609 599
    // Set the networking attributes.
610 600

  
......
613 603

  
614 604
    num_nics   = vm_template.get("NIC",nics);
615 605

  
616
    for(int i=0; i<num_nics; i++,vnid.str(""))
606
    for(int i=0; i<num_nics; i++)
617 607
    {
618
   	 	nic = dynamic_cast<VectorAttribute * >(nics[i]);
608
        nic = dynamic_cast<VectorAttribute * >(nics[i]);
619 609

  
620 610
        if ( nic == 0 )
621 611
        {
622 612
            continue;
623 613
        }
624 614

  
625
        network = nic->vector_value("NETWORK");
626

  
627
        if ( network.empty() )
628
        {
629
            continue;
630
        }
631

  
632
        vn = vnpool->get(network,true);
633

  
634
        if ( vn == 0 )
635
        {
636
            return -1;
637
        }
638

  
639
        if ( vn->get_uid() != uid && vn->get_uid() != 0 && uid != 0)
640
        {
641
            ostringstream ose;
642
            ose << "Owner " << uid << " of the VM doesn't have ownership of Virtual Network "
643
                << vn->get_uid();
644
            NebulaLog::log("VMM", Log::ERROR, ose);
645
            return -1;
646
        }
647

  
648

  
649
        ip = nic->vector_value("IP");
650

  
651
        if (ip.empty())
652
        {
653
        	rc = vn->get_lease(oid, ip, mac, bridge);
654
        }
655
        else
656
        {
657
        	rc = vn->set_lease(oid, ip, mac, bridge);
658
        }
659

  
660
        vn->unlock();
615
        rc = vnpool->nic_attribute(nic, oid);
661 616

  
662
        if ( rc != 0 )
617
        if (rc == -1)
663 618
        {
664 619
            return -1;
665 620
        }
666

  
667
        vnid << vn->get_oid();
668

  
669
        new_nic.insert(make_pair("NETWORK",network));
670
        new_nic.insert(make_pair("MAC"    ,mac));
671
        new_nic.insert(make_pair("BRIDGE" ,bridge));
672
        new_nic.insert(make_pair("VNID"   ,vnid.str()));
673
        new_nic.insert(make_pair("IP"     ,ip));
674

  
675
        model = nic->vector_value("MODEL");
676

  
677
        if ( !model.empty() )
678
        {
679
            new_nic.insert(make_pair("MODEL",model));
680
        }
681

  
682
        nic->replace(new_nic);
683

  
684
        new_nic.erase(new_nic.begin(),new_nic.end());
685 621
    }
686 622

  
687 623
    return 0;
src/vnm/VirtualNetwork.cc
547 547

  
548 548
/* -------------------------------------------------------------------------- */
549 549
/* -------------------------------------------------------------------------- */
550

  
551
int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid)
552
{
553
    int rc;
554

  
555
    string  network;
556
    string  model;
557
    string  ip;
558
    string  mac;
559

  
560
    ostringstream  vnid;
561

  
562
    map<string,string> new_nic;
563

  
564
    network = nic->vector_value("NETWORK");
565
    model   = nic->vector_value("MODEL");
566
    ip      = nic->vector_value("IP");
567
    vnid   << oid;
568

  
569
    //--------------------------------------------------------------------------
570
    //                       GET NETWORK LEASE
571
    //--------------------------------------------------------------------------
572

  
573
    if (ip.empty())
574
    {
575
        rc = leases->get(vid,ip,mac);
576
    }
577
    else
578
    {
579
        rc = leases->set(vid,ip,mac);
580
    }
581

  
582
    if ( rc != 0 )
583
    {
584
        return -1;
585
    }
586

  
587
    //--------------------------------------------------------------------------
588
    //                       NEW NIC ATTRIBUTES
589
    //--------------------------------------------------------------------------
590

  
591
    new_nic.insert(make_pair("NETWORK",network));
592
    new_nic.insert(make_pair("MAC"    ,mac));
593
    new_nic.insert(make_pair("BRIDGE" ,bridge));
594
    new_nic.insert(make_pair("VNID"   ,vnid.str()));
595
    new_nic.insert(make_pair("IP"     ,ip));
596

  
597
    if (!model.empty())
598
    {
599
        new_nic.insert(make_pair("MODEL",model));
600
    }
601

  
602
    nic->replace(new_nic);
603

  
604
    return 0;
605
}
606

  
607
/* -------------------------------------------------------------------------- */
608
/* -------------------------------------------------------------------------- */

Also available in: Unified diff