@@ -668,19 +668,38 @@ public void createVpcAssociatePublicIPCommands(final VirtualRouter router, final
668668 for (final Map .Entry <String , ArrayList <PublicIpAddress >> vlanAndIp : vlanIpMap .entrySet ()) {
669669 final List <PublicIpAddress > ipAddrList = vlanAndIp .getValue ();
670670
671+ // Source nat ip address should always be sent first
672+ Collections .sort (ipAddrList , new Comparator <PublicIpAddress >() {
673+ @ Override
674+ public int compare (final PublicIpAddress o1 , final PublicIpAddress o2 ) {
675+ final boolean s1 = o1 .isSourceNat ();
676+ final boolean s2 = o2 .isSourceNat ();
677+ return s1 ^ s2 ? s1 ^ true ? 1 : -1 : 0 ;
678+ }
679+ });
680+
681+
671682 // Get network rate - required for IpAssoc
672683 final Integer networkRate = _networkModel .getNetworkRate (ipAddrList .get (0 ).getNetworkId (), router .getId ());
673684 final Network network = _networkModel .getNetwork (ipAddrList .get (0 ).getNetworkId ());
674685
675686 final IpAddressTO [] ipsToSend = new IpAddressTO [ipAddrList .size ()];
676687 int i = 0 ;
688+ boolean firstIP = true ;
677689
678690 for (final PublicIpAddress ipAddr : ipAddrList ) {
679691 final boolean add = ipAddr .getState () == IpAddress .State .Releasing ? false : true ;
692+ boolean sourceNat = ipAddr .isSourceNat ();
693+ /* enable sourceNAT for the first ip of the public interface
694+ * For additional public subnet source nat rule needs to be added for vm to reach ips in that subnet
695+ */
696+ if (firstIP ) {
697+ sourceNat = true ;
698+ }
680699
681700 final String macAddress = vlanMacAddress .get (BroadcastDomainType .getValue (BroadcastDomainType .fromString (ipAddr .getVlanTag ())));
682701
683- final IpAddressTO ip = new IpAddressTO (ipAddr .getAccountId (), ipAddr .getAddress ().addr (), add , false , ipAddr . isSourceNat () , BroadcastDomainType .fromString (ipAddr .getVlanTag ()).toString (), ipAddr .getGateway (),
702+ final IpAddressTO ip = new IpAddressTO (ipAddr .getAccountId (), ipAddr .getAddress ().addr (), add , firstIP , sourceNat , BroadcastDomainType .fromString (ipAddr .getVlanTag ()).toString (), ipAddr .getGateway (),
684703 ipAddr .getNetmask (), macAddress , networkRate , ipAddr .isOneToOneNat ());
685704
686705 ip .setTrafficType (network .getTrafficType ());
@@ -690,6 +709,12 @@ public void createVpcAssociatePublicIPCommands(final VirtualRouter router, final
690709 sourceNatIpAdd = new Pair <IpAddressTO , Long >(ip , ipAddr .getNetworkId ());
691710 addSourceNat = add ;
692711 }
712+
713+ //for additional public subnet on delete it is not sure which ip is set to first ip. So on delete we
714+ //want to set sourcenat to true for all ips to delete source nat rules.
715+ if (!firstIP || add ) {
716+ firstIP = false ;
717+ }
693718 }
694719 final IpAssocVpcCommand cmd = new IpAssocVpcCommand (ipsToSend );
695720 cmd .setAccessDetail (NetworkElementCommand .ROUTER_IP , _routerControlHelper .getRouterControlIp (router .getId ()));
0 commit comments