|
178 | 178 | import com.cloud.network.vpc.NetworkACL; |
179 | 179 | import com.cloud.network.vpc.PrivateIpVO; |
180 | 180 | import com.cloud.network.vpc.Vpc; |
| 181 | +import com.cloud.network.vpc.VpcGatewayVO; |
181 | 182 | import com.cloud.network.vpc.VpcManager; |
182 | 183 | import com.cloud.network.vpc.VpcVO; |
183 | 184 | import com.cloud.network.vpc.dao.NetworkACLDao; |
184 | 185 | import com.cloud.network.vpc.dao.PrivateIpDao; |
185 | 186 | import com.cloud.network.vpc.dao.VpcDao; |
| 187 | +import com.cloud.network.vpc.dao.VpcGatewayDao; |
186 | 188 | import com.cloud.network.vpc.dao.VpcOfferingDao; |
187 | 189 | import com.cloud.offering.NetworkOffering; |
188 | 190 | import com.cloud.offerings.NetworkOfferingVO; |
@@ -388,6 +390,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C |
388 | 390 | @Inject |
389 | 391 | Ipv6GuestPrefixSubnetNetworkMapDao ipv6GuestPrefixSubnetNetworkMapDao; |
390 | 392 | @Inject |
| 393 | + VpcGatewayDao vpcGatewayDao; |
| 394 | + @Inject |
391 | 395 | AlertManager alertManager; |
392 | 396 | @Inject |
393 | 397 | DomainRouterDao routerDao; |
@@ -1939,24 +1943,17 @@ private Network implementAssociatedNetwork(Long associatedNetworkId, Account cal |
1939 | 1943 | if (domainId != null && associatedNetwork.getDomainId() != domainId) { |
1940 | 1944 | throw new InvalidParameterValueException("The new network and associated network MUST be in same domain"); |
1941 | 1945 | } |
1942 | | - if (cidr != null && associatedNetwork.getCidr() != null && NetUtils.isNetworksOverlap(cidr, associatedNetwork.getCidr())) { |
1943 | | - throw new InvalidParameterValueException("The cidr overlaps with associated network: " + associatedNetwork.getName()); |
1944 | | - } |
1945 | | - List<NetworkDetailVO> associatedNetworks = _networkDetailsDao.findDetails(Network.AssociatedNetworkId, String.valueOf(associatedNetworkId), null); |
1946 | | - for (NetworkDetailVO networkDetailVO : associatedNetworks) { |
1947 | | - NetworkVO associatedNetwork2 = _networksDao.findById(networkDetailVO.getResourceId()); |
1948 | | - if (associatedNetwork2 != null) { |
1949 | | - List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(associatedNetwork2.getId()); |
1950 | | - if (vlans.isEmpty()) { |
1951 | | - continue; |
1952 | | - } |
1953 | | - String startIP2 = vlans.get(0).getIpRange().split("-")[0]; |
1954 | | - String endIP2 = vlans.get(0).getIpRange().split("-")[1]; |
1955 | | - if (StringUtils.isNoneBlank(startIp, startIP2) && NetUtils.ipRangesOverlap(startIp, endIp, startIP2, endIP2)) { |
1956 | | - throw new InvalidParameterValueException("The startIp/endIp overlaps with network: " + associatedNetwork2.getName()); |
1957 | | - } |
| 1946 | + if (cidr != null && associatedNetwork.getCidr() != null) { |
| 1947 | + String[] guestVmCidrPair = associatedNetwork.getCidr().split("\\/"); |
| 1948 | + String[] cidrIpRange = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], Long.valueOf(guestVmCidrPair[1])); |
| 1949 | + if (StringUtils.isNoneBlank(startIp, endIp) && NetUtils.ipRangesOverlap(startIp, endIp, cidrIpRange[0], cidrIpRange[1])) { |
| 1950 | + throw new InvalidParameterValueException(String.format("The IP range (%s-%s) overlaps with cidr of associated network: %s (%s)", |
| 1951 | + startIp, endIp, associatedNetwork.getName(), associatedNetwork.getCidr())); |
1958 | 1952 | } |
1959 | 1953 | } |
| 1954 | + // Check IP range overlap on shared networks and vpc private gateways associated to the same network |
| 1955 | + checkIpRangeOverlapWithAssociatedNetworks(associatedNetworkId, startIp, endIp); |
| 1956 | + |
1960 | 1957 | associatedNetwork = implementedNetworkInCreation(caller, zone, associatedNetwork); |
1961 | 1958 | if (associatedNetwork == null || (associatedNetwork.getState() != Network.State.Implemented && associatedNetwork.getState() != Network.State.Setup)) { |
1962 | 1959 | throw new InvalidParameterValueException("Unable to implement associated network " + associatedNetwork); |
@@ -3065,8 +3062,9 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) { |
3065 | 3062 | if (networkOfferingChanged) { |
3066 | 3063 | throw new InvalidParameterValueException("Cannot specify this network offering change and guestVmCidr at same time. Specify only one."); |
3067 | 3064 | } |
3068 | | - if (!(network.getState() == Network.State.Implemented)) { |
3069 | | - throw new InvalidParameterValueException("The network must be in " + Network.State.Implemented + " state. IP Reservation cannot be applied in " + network.getState() + " state"); |
| 3065 | + if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Allocated) { |
| 3066 | + throw new InvalidParameterValueException(String.format("The network must be in %s or %s state. IP Reservation cannot be applied in %s state", |
| 3067 | + Network.State.Implemented, Network.State.Allocated, network.getState())); |
3070 | 3068 | } |
3071 | 3069 | if (!NetUtils.isValidIp4Cidr(guestVmCidr)) { |
3072 | 3070 | throw new InvalidParameterValueException("Invalid format of Guest VM CIDR."); |
@@ -3125,6 +3123,9 @@ public Network updateGuestNetwork(final UpdateNetworkCmd cmd) { |
3125 | 3123 | } |
3126 | 3124 | } |
3127 | 3125 |
|
| 3126 | + // Check IP range overlap on shared networks and vpc private gateways associated to this network |
| 3127 | + checkIpRangeOverlapWithAssociatedNetworks(networkId, cidrIpRange[0], cidrIpRange[1]); |
| 3128 | + |
3128 | 3129 | // When reservation is applied for the first time, network_cidr will be null |
3129 | 3130 | // Populate it with the actual network cidr |
3130 | 3131 | if (network.getNetworkCidr() == null) { |
@@ -5911,6 +5912,30 @@ private List<Long> convertAccountNamesToAccountIds(final Account caller, final L |
5911 | 5912 | return accountIds; |
5912 | 5913 | } |
5913 | 5914 |
|
| 5915 | + private void checkIpRangeOverlapWithAssociatedNetworks(Long associatedNetworkId, String startIp, String endIp) { |
| 5916 | + List<NetworkDetailVO> associatedNetworks = _networkDetailsDao.findDetails(Network.AssociatedNetworkId, String.valueOf(associatedNetworkId), null); |
| 5917 | + for (NetworkDetailVO networkDetailVO : associatedNetworks) { |
| 5918 | + NetworkVO associatedNetwork2 = _networksDao.findById(networkDetailVO.getResourceId()); |
| 5919 | + if (associatedNetwork2 != null) { |
| 5920 | + List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(associatedNetwork2.getId()); |
| 5921 | + if (vlans.isEmpty()) { |
| 5922 | + VpcGatewayVO vpcGateway = vpcGatewayDao.getVpcGatewayByNetworkId(associatedNetwork2.getId()); |
| 5923 | + if (vpcGateway != null && NetUtils.ipRangesOverlap(startIp, endIp, vpcGateway.getIp4Address(), vpcGateway.getIp4Address())) { |
| 5924 | + throw new InvalidParameterValueException(String.format("The startIp/endIp (%s - %s) overlaps with vpc private gateway %s (%s): ", |
| 5925 | + startIp, endIp, associatedNetwork2.getName(), vpcGateway.getIp4Address())); |
| 5926 | + } |
| 5927 | + continue; |
| 5928 | + } |
| 5929 | + String startIP2 = vlans.get(0).getIpRange().split("-")[0]; |
| 5930 | + String endIP2 = vlans.get(0).getIpRange().split("-")[1]; |
| 5931 | + if (StringUtils.isNoneBlank(startIp, startIP2) && NetUtils.ipRangesOverlap(startIp, endIp, startIP2, endIP2)) { |
| 5932 | + throw new InvalidParameterValueException(String.format("The startIp/endIp (%s - %s) overlaps with network %s (%s - %s)", |
| 5933 | + startIp, endIp, associatedNetwork2.getName(), startIP2, endIP2)); |
| 5934 | + } |
| 5935 | + } |
| 5936 | + } |
| 5937 | + } |
| 5938 | + |
5914 | 5939 | @Override |
5915 | 5940 | public String getConfigComponentName() { |
5916 | 5941 | return NetworkService.class.getSimpleName(); |
|
0 commit comments