201201import com .cloud .utils .net .MacAddress ;
202202import com .cloud .utils .net .NetUtils ;
203203import com .cloud .vm .DomainRouterVO ;
204+ import com .cloud .vm .Nic ;
204205import com .cloud .vm .NicProfile ;
205206import com .cloud .vm .NicVO ;
206207import com .cloud .vm .ReservationContext ;
@@ -1565,7 +1566,10 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterV
15651566 String defaultDns1 = null ;
15661567 String defaultDns2 = null ;
15671568
1568- for (NicProfile nic : profile .getNics ()) {
1569+
1570+ Iterator <NicProfile > it = profile .getNics ().iterator ();
1571+ while (it .hasNext ()) {
1572+ NicProfile nic = it .next ();
15691573 int deviceId = nic .getDeviceId ();
15701574 buf .append (" eth" ).append (deviceId ).append ("ip=" ).append (nic .getIp4Address ());
15711575 buf .append (" eth" ).append (deviceId ).append ("mask=" ).append (nic .getNetmask ());
@@ -1600,6 +1604,10 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterV
16001604 buf .append (" localgw=" ).append (dest .getPod ().getGateway ());
16011605 }
16021606 }
1607+ } else {
1608+ //Remove public and guest nics from the profile
1609+ s_logger .debug ("Removing nic of type " + nic .getTrafficType () + " from virtual machine profile " + profile .getVirtualMachine ());
1610+ it .remove ();
16031611 }
16041612 }
16051613
@@ -1893,15 +1901,6 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
18931901 ReservationContext context ) {
18941902 DomainRouterVO router = profile .getVirtualMachine ();
18951903
1896- //Get guest nic info
1897- List <NicProfile > routerNics = profile .getNics ();
1898- List <Network > guestNetworks = new ArrayList <Network >();
1899- for (NicProfile routerNic : routerNics ) {
1900- if (routerNic .getTrafficType () == TrafficType .Guest ) {
1901- guestNetworks .add (_networkMgr .getNetwork (routerNic .getNetworkId ()));
1902- }
1903- }
1904-
19051904 boolean result = true ;
19061905
19071906 Answer answer = cmds .getAnswer ("checkSsh" );
@@ -1917,6 +1916,23 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
19171916 if (result == false ) {
19181917 return result ;
19191918 }
1919+
1920+ //Get guest nic info
1921+ Map <Nic , Network > guestNics = new HashMap <Nic , Network >();
1922+ Map <Nic , Network > publicNics = new HashMap <Nic , Network >();
1923+ List <Network > guestNetworks = new ArrayList <Network >();
1924+
1925+ List <? extends Nic > routerNics = _nicDao .listByVmId (profile .getId ());
1926+ for (Nic routerNic : routerNics ) {
1927+ Network network = _networkMgr .getNetwork (routerNic .getNetworkId ());
1928+ if (network .getTrafficType () == TrafficType .Guest ) {
1929+ guestNics .put (routerNic , network );
1930+ guestNetworks .add (network );
1931+ } else if (network .getTrafficType () == TrafficType .Public ) {
1932+ publicNics .put (routerNic , network );
1933+ }
1934+ }
1935+
19201936 answer = cmds .getAnswer ("getDomRVersion" );
19211937 if (answer != null && answer instanceof GetDomRVersionAnswer ) {
19221938 GetDomRVersionAnswer versionAnswer = (GetDomRVersionAnswer )answer ;
@@ -1931,6 +1947,30 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
19311947 } else {
19321948 result = false ;
19331949 }
1950+
1951+ try {
1952+ //add router to public and guest networks
1953+ for (Nic publicNic : publicNics .keySet ()) {
1954+ Network publicNtwk = publicNics .get (publicNic );
1955+ if (!addRouterToPublicNetwork (router , publicNtwk , _ipAddressDao .findByIpAndSourceNetworkId (publicNtwk .getId (),
1956+ publicNic .getIp4Address ()))) {
1957+ s_logger .warn ("Failed to plug nic " + publicNic + " to router " + router );
1958+ return false ;
1959+ }
1960+ }
1961+
1962+ for (Nic guestNic : guestNics .keySet ()) {
1963+ Network guestNtwk = guestNics .get (guestNic );
1964+ if (!addRouterToGuestNetwork (router , guestNtwk , false )) {
1965+ s_logger .warn ("Failed to plug nic " + guestNic + " to router " + router );
1966+ return false ;
1967+ }
1968+ }
1969+ } catch (Exception ex ) {
1970+ s_logger .warn ("Failed to plug nic for router " + router + " due to exception " , ex );
1971+ return false ;
1972+ }
1973+
19341974
19351975 return result ;
19361976 }
@@ -3051,7 +3091,7 @@ protected boolean setupGuestNetwork(Network network, VirtualRouter router, boole
30513091 }
30523092
30533093 NicVO nic = _nicDao .findByInstanceIdAndNetworkId (network .getId (), router .getId ());
3054- NicProfile nicProfile = new NicProfile (nic , network , nic .getBroadcastUri (), nic .getIsolationUri (), null ,
3094+ NicProfile nicProfile = new NicProfile (nic , network , nic .getBroadcastUri (), nic .getIsolationUri (), _networkMgr . getNetworkRate ( network . getId (), router . getId ()) ,
30553095 _networkMgr .isSecurityGroupSupportedInNetwork (network ), _networkMgr .getNetworkTag (router .getHypervisorType (), network ));
30563096
30573097 SetupGuestNetworkCommand setupCmd = new SetupGuestNetworkCommand (dhcpRange , networkDomain , isRedundant , priority ,
@@ -3086,12 +3126,6 @@ public boolean addRouterToGuestNetwork(VirtualRouter router, Network network, bo
30863126 return false ;
30873127 }
30883128
3089- //Check if router is already a part of the Guest network
3090- if (_networkMgr .isVmPartOfNetwork (router .getId (), network .getId ())) {
3091- s_logger .debug ("Router " + router + " is already part of the Guest network " + network );
3092- return true ;
3093- }
3094-
30953129 //Add router to the Guest network
30963130 boolean result = true ;
30973131 try {
@@ -3155,29 +3189,23 @@ public boolean removeRouterFromGuestNetwork(VirtualRouter router, Network networ
31553189 return result ;
31563190 }
31573191
3158- protected boolean addRouterToPublicNetwork (VirtualRouter router , Network publicNetwork , IpAddress sourceNatIp )
3192+ protected boolean addRouterToPublicNetwork (VirtualRouter router , Network publicNetwork , IpAddress publicIpAddr )
31593193 throws ConcurrentOperationException ,ResourceUnavailableException , InsufficientCapacityException {
31603194
31613195 if (publicNetwork .getTrafficType () != TrafficType .Public ) {
31623196 s_logger .warn ("Network " + publicNetwork + " is not of type " + TrafficType .Public );
31633197 return false ;
31643198 }
31653199
3166- //Check if router is already a part of the Public network
3167- if (_networkMgr .isVmPartOfNetwork (router .getId (), publicNetwork .getId ())) {
3168- s_logger .debug ("Router " + router + " is already part of the Public network " + publicNetwork );
3169- return true ;
3170- }
3171-
31723200 //Add router to the Public network
31733201 boolean result = true ;
31743202 try {
31753203
31763204 NicProfile publicNic = _itMgr .addVmToNetwork (router , publicNetwork );
31773205 //setup public network
31783206 if (publicNic != null ) {
3179- if (sourceNatIp != null ) {
3180- IPAddressVO ipVO = _ipAddressDao .findById (sourceNatIp .getId ());
3207+ if (publicIpAddr != null ) {
3208+ IPAddressVO ipVO = _ipAddressDao .findById (publicIpAddr .getId ());
31813209 PublicIp publicIp = new PublicIp (ipVO , _vlanDao .findById (ipVO .getVlanId ()),
31823210 NetUtils .createSequenceBasedMacAddress (ipVO .getMacAddress ()));
31833211 result = setupPublicNetwork (publicNetwork , router , false , publicIp );
@@ -3242,10 +3270,11 @@ protected boolean removeRouterFromPublicNetwork(VirtualRouter router, Network pu
32423270 return result ;
32433271 }
32443272
3245- protected boolean setupPublicNetwork (Network network , VirtualRouter router , boolean add , PublicIp sourceNatIp )
3273+ protected boolean setupPublicNetwork (Network network , VirtualRouter router , boolean add , PublicIp ipAddress )
32463274 throws ConcurrentOperationException , ResourceUnavailableException {
32473275
32483276 List <PublicIp > publicIps = new ArrayList <PublicIp >(1 );
3277+ publicIps .add (ipAddress );
32493278 Commands cmds = new Commands (OnError .Stop );
32503279 createAssociateIPCommands (router , publicIps , cmds , 0 );
32513280 sendCommandsToRouter (router , cmds );
@@ -3254,7 +3283,7 @@ protected boolean setupPublicNetwork(Network network, VirtualRouter router, bool
32543283 IpAssocAnswer ipAssocAnswer = cmds .getAnswer (IpAssocAnswer .class );
32553284 String setup = add ? "set" : "destroy" ;
32563285 if (!(ipAssocAnswer != null && ipAssocAnswer .getResult ())) {
3257- s_logger .warn ("Unable to " + setup + " guest network on router " + router );
3286+ s_logger .warn ("Unable to " + setup + " public network on router " + router );
32583287 result = false ;
32593288 }
32603289
0 commit comments