7676import com .cloud .utils .Pair ;
7777import com .cloud .utils .db .EntityManager ;
7878import com .cloud .utils .net .Ip ;
79+ import com .cloud .vm .DomainRouterVO ;
7980import com .cloud .vm .Nic ;
8081import com .cloud .vm .NicProfile ;
8182import com .cloud .vm .NicVO ;
8283import com .cloud .vm .VMInstanceVO ;
8384import com .cloud .vm .VirtualMachine ;
85+ import com .cloud .vm .dao .DomainRouterDao ;
8486import com .cloud .vm .dao .NicDao ;
8587import com .cloud .vm .dao .VMInstanceDao ;
8688import org .apache .cloudstack .engine .orchestration .service .NetworkOrchestrationService ;
@@ -118,6 +120,8 @@ public class NetworkModelImplTest {
118120 @ Mock
119121 private NicDao nicDao ;
120122 @ Mock
123+ private DomainRouterDao routerDao ;
124+ @ Mock
121125 private ServiceOfferingDao serviceOfferingDao ;
122126 @ Mock
123127 private ConfigurationManager configMgr ;
@@ -596,12 +600,12 @@ public void getNetworkRate_routerPublicWithGuestSibling_returnsGuestNetworkOffer
596600 VMInstanceVO vm = mock (VMInstanceVO .class );
597601 when (vm .getType ()).thenReturn (VirtualMachine .Type .DomainRouter );
598602 when (vmInstanceDao .findById (vmId )).thenReturn (vm );
603+ when (routerDao .findById (vmId )).thenReturn (null );
599604 NicVO guestNic = mock (NicVO .class );
600605 when (guestNic .getNetworkId ()).thenReturn (guestNetworkId );
601606 when (nicDao .listByVmId (vmId )).thenReturn (List .of (guestNic ));
602607 NetworkVO guestNetwork = mock (NetworkVO .class );
603608 when (guestNetwork .getTrafficType ()).thenReturn (TrafficType .Guest );
604- when (guestNetwork .getVpcId ()).thenReturn (null );
605609 when (guestNetwork .getNetworkOfferingId ()).thenReturn (guestOfferingId );
606610 when (_networksDao .findById (guestNetworkId )).thenReturn (guestNetwork );
607611 when (configMgr .getNetworkOfferingNetworkRate (guestOfferingId , dataCenterId )).thenReturn (80 );
@@ -616,27 +620,45 @@ public void getNetworkRate_routerPublicWithoutGuestSibling_fallsBackToNetworkOff
616620 VMInstanceVO vm = mock (VMInstanceVO .class );
617621 when (vm .getType ()).thenReturn (VirtualMachine .Type .DomainRouter );
618622 when (vmInstanceDao .findById (vmId )).thenReturn (vm );
623+ when (routerDao .findById (vmId )).thenReturn (null );
619624 when (nicDao .listByVmId (vmId )).thenReturn (Collections .emptyList ());
620625 mockNetworkOffering (networkOfferingId );
621626 when (configMgr .getNetworkOfferingNetworkRate (networkOfferingId , dataCenterId )).thenReturn (33 );
622627
623628 assertEquals (Integer .valueOf (33 ), networkModel .getNetworkRate (networkId , vmId ));
624629 }
625630
631+ @ Test
632+ public void getNetworkRate_routerPublicWithVpcRouter_returnsVpcOfferingRateWithoutNicLookup () {
633+ long networkId = 1L , vmId = 12L , dataCenterId = 2L , vpcId = 7L , vpcOfferingId = 70L ;
634+ mockNetwork (networkId , 99L , dataCenterId , TrafficType .Public );
635+ VMInstanceVO vm = mock (VMInstanceVO .class );
636+ when (vm .getType ()).thenReturn (VirtualMachine .Type .DomainRouter );
637+ when (vmInstanceDao .findById (vmId )).thenReturn (vm );
638+ DomainRouterVO router = mock (DomainRouterVO .class );
639+ when (router .getVpcId ()).thenReturn (vpcId );
640+ when (routerDao .findById (vmId )).thenReturn (router );
641+ VpcVO vpc = mock (VpcVO .class );
642+ when (vpc .getVpcOfferingId ()).thenReturn (vpcOfferingId );
643+ when (vpcDao .findById (vpcId )).thenReturn (vpc );
644+ when (configMgr .getVpcOfferingNetworkRate (vpcOfferingId , dataCenterId )).thenReturn (10 );
645+
646+ // Resolved purely from the router's own vpc_id - the guest NIC does not need to exist
647+ // in the nics table yet, matching the state during initial VR deployment.
648+ assertEquals (Integer .valueOf (10 ), networkModel .getNetworkRate (networkId , vmId ));
649+ Mockito .verify (nicDao , Mockito .never ()).listByVmId (Mockito .anyLong ());
650+ }
651+
626652 @ Test
627653 public void getNetworkRate_routerPublicWithVpcGuestSibling_returnsVpcOfferingRate () {
628- long networkId = 1L , vmId = 12L , dataCenterId = 2L , guestNetworkId = 5L , vpcId = 7L , vpcOfferingId = 70L ;
654+ long networkId = 1L , vmId = 12L , dataCenterId = 2L , vpcId = 7L , vpcOfferingId = 70L ;
629655 mockNetwork (networkId , 99L , dataCenterId , TrafficType .Public );
630656 VMInstanceVO vm = mock (VMInstanceVO .class );
631657 when (vm .getType ()).thenReturn (VirtualMachine .Type .DomainRouter );
632658 when (vmInstanceDao .findById (vmId )).thenReturn (vm );
633- NicVO guestNic = mock (NicVO .class );
634- when (guestNic .getNetworkId ()).thenReturn (guestNetworkId );
635- when (nicDao .listByVmId (vmId )).thenReturn (List .of (guestNic ));
636- NetworkVO guestNetwork = mock (NetworkVO .class );
637- when (guestNetwork .getTrafficType ()).thenReturn (TrafficType .Guest );
638- when (guestNetwork .getVpcId ()).thenReturn (vpcId );
639- when (_networksDao .findById (guestNetworkId )).thenReturn (guestNetwork );
659+ DomainRouterVO router = mock (DomainRouterVO .class );
660+ when (router .getVpcId ()).thenReturn (vpcId );
661+ when (routerDao .findById (vmId )).thenReturn (router );
640662 VpcVO vpc = mock (VpcVO .class );
641663 when (vpc .getVpcOfferingId ()).thenReturn (vpcOfferingId );
642664 when (vpcDao .findById (vpcId )).thenReturn (vpc );
@@ -653,15 +675,17 @@ public void getNetworkRate_routerPublicWithVpcGuestSiblingButMissingVpc_fallsBac
653675 VMInstanceVO vm = mock (VMInstanceVO .class );
654676 when (vm .getType ()).thenReturn (VirtualMachine .Type .DomainRouter );
655677 when (vmInstanceDao .findById (vmId )).thenReturn (vm );
678+ DomainRouterVO router = mock (DomainRouterVO .class );
679+ when (router .getVpcId ()).thenReturn (vpcId );
680+ when (routerDao .findById (vmId )).thenReturn (router );
681+ when (vpcDao .findById (vpcId )).thenReturn (null );
656682 NicVO guestNic = mock (NicVO .class );
657683 when (guestNic .getNetworkId ()).thenReturn (guestNetworkId );
658684 when (nicDao .listByVmId (vmId )).thenReturn (List .of (guestNic ));
659685 NetworkVO guestNetwork = mock (NetworkVO .class );
660686 when (guestNetwork .getTrafficType ()).thenReturn (TrafficType .Guest );
661- when (guestNetwork .getVpcId ()).thenReturn (vpcId );
662687 when (guestNetwork .getNetworkOfferingId ()).thenReturn (guestOfferingId );
663688 when (_networksDao .findById (guestNetworkId )).thenReturn (guestNetwork );
664- when (vpcDao .findById (vpcId )).thenReturn (null );
665689 when (configMgr .getNetworkOfferingNetworkRate (guestOfferingId , dataCenterId )).thenReturn (90 );
666690
667691 assertEquals (Integer .valueOf (90 ), networkModel .getNetworkRate (networkId , vmId ));
0 commit comments