1616// under the License.
1717package com .cloud .network ;
1818
19+ import static org .apache .commons .lang .StringUtils .isNotBlank ;
20+
1921import java .net .Inet6Address ;
2022import java .net .InetAddress ;
2123import java .net .URI ;
@@ -995,6 +997,46 @@ private void checkSharedNetworkCidrOverlap(Long zoneId, long physicalNetworkId,
995997 }
996998 }
997999
1000+ private void validateRouterIps (String routerIp , String routerIpv6 , String startIp , String endIp , String gateway ,
1001+ String netmask , String startIpv6 , String endIpv6 , String ip6Cidr ) {
1002+ if (isNotBlank (routerIp )) {
1003+ if (startIp != null && endIp == null ) {
1004+ endIp = startIp ;
1005+ }
1006+ if (!NetUtils .isValidIp4 (routerIp )) {
1007+ throw new CloudRuntimeException ("Router IPv4 IP provided is of incorrect format" );
1008+ }
1009+ if (isNotBlank (startIp ) && isNotBlank (endIp )) {
1010+ if (!NetUtils .isIpInRange (routerIp , startIp , endIp )) {
1011+ throw new CloudRuntimeException ("Router IPv4 IP provided is not within the specified range: " + startIp + " - " + endIp );
1012+ }
1013+ } else {
1014+ String cidr = NetUtils .ipAndNetMaskToCidr (gateway , netmask );
1015+ if (!NetUtils .isIpWithInCidrRange (routerIp , cidr )) {
1016+ throw new CloudRuntimeException ("Router IP provided in not within the network range" );
1017+ }
1018+ }
1019+ }
1020+ if (isNotBlank (routerIpv6 )) {
1021+ if (startIpv6 != null && endIpv6 == null ) {
1022+ endIpv6 = startIpv6 ;
1023+ }
1024+ if (!NetUtils .isValidIp6 (routerIpv6 )) {
1025+ throw new CloudRuntimeException ("Router IPv6 address provided is of incorrect format" );
1026+ }
1027+ if (isNotBlank (startIpv6 ) && isNotBlank (endIpv6 )) {
1028+ String ipv6Range = startIpv6 + "-" + endIpv6 ;
1029+ if (!NetUtils .isIp6InRange (routerIpv6 , ipv6Range )) {
1030+ throw new CloudRuntimeException ("Router IPv6 address provided is not within the specified range: " + startIpv6 + " - " + endIpv6 );
1031+ }
1032+ } else {
1033+ if (!NetUtils .isIp6InNetwork (routerIpv6 , ip6Cidr )) {
1034+ throw new CloudRuntimeException ("Router IPv6 address provided is not with the network range" );
1035+ }
1036+ }
1037+ }
1038+ }
1039+
9981040 @ Override
9991041 @ DB
10001042 @ ActionEvent (eventType = EventTypes .EVENT_NETWORK_CREATE , eventDescription = "creating network" )
@@ -1007,11 +1049,13 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
10071049 String networkDomain = cmd .getNetworkDomain ();
10081050 String vlanId = null ;
10091051 boolean bypassVlanOverlapCheck = false ;
1052+ String routerIp = null ;
1053+ String routerIpv6 = null ;
10101054 if (cmd instanceof CreateNetworkCmdByAdmin ) {
10111055 vlanId = ((CreateNetworkCmdByAdmin )cmd ).getVlan ();
1012- }
1013- if (cmd instanceof CreateNetworkCmdByAdmin ) {
10141056 bypassVlanOverlapCheck = ((CreateNetworkCmdByAdmin )cmd ).getBypassVlanOverlapCheck ();
1057+ routerIp = ((CreateNetworkCmdByAdmin )cmd ).getRouterIp ();
1058+ routerIpv6 = ((CreateNetworkCmdByAdmin )cmd ).getRouterIpv6 ();
10151059 }
10161060
10171061 String name = cmd .getNetworkName ();
@@ -1105,6 +1149,15 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
11051149 throw new InvalidParameterValueException ("Only Admins can create network with guest type " + GuestType .Shared );
11061150 }
11071151
1152+ if (ntwkOff .getGuestType () != GuestType .Shared && (isNotBlank (routerIp ) || isNotBlank (routerIpv6 ))) {
1153+ throw new InvalidParameterValueException ("Router IP can be specified only for Shared networks" );
1154+ }
1155+
1156+ if (ntwkOff .getGuestType () == GuestType .Shared && !_networkModel .isProviderForNetworkOffering (Provider .VirtualRouter , networkOfferingId )
1157+ && (isNotBlank (routerIp ) || isNotBlank (routerIpv6 ))) {
1158+ throw new InvalidParameterValueException ("Virtual Router is not a supported provider for the Shared network, hence router ip should not be provided" );
1159+ }
1160+
11081161 // Check if the network is domain specific
11091162 if (aclType == ACLType .Domain ) {
11101163 // only Admin can create domain with aclType=Domain
@@ -1216,6 +1269,8 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
12161269 }
12171270 }
12181271
1272+ validateRouterIps (routerIp , routerIpv6 , startIP , endIP , gateway , netmask , startIPv6 , endIPv6 , ip6Cidr );
1273+
12191274 if (isolatedPvlan != null && (zone .getNetworkType () != NetworkType .Advanced || ntwkOff .getGuestType () != Network .GuestType .Shared )) {
12201275 throw new InvalidParameterValueException ("Can only support create Private VLAN network with advance shared network!" );
12211276 }
@@ -1293,7 +1348,7 @@ && areServicesSupportedByNetworkOffering(ntwkOff.getId(), Service.SourceNat))) {
12931348
12941349 Network network = commitNetwork (networkOfferingId , gateway , startIP , endIP , netmask , networkDomain , vlanId , bypassVlanOverlapCheck , name , displayText , caller , physicalNetworkId , zoneId , domainId ,
12951350 isDomainSpecific , subdomainAccess , vpcId , startIPv6 , endIPv6 , ip6Gateway , ip6Cidr , displayNetwork , aclId , isolatedPvlan , ntwkOff , pNtwk , aclType , owner , cidr ,
1296- createVlan , externalId );
1351+ createVlan , externalId , routerIp , routerIpv6 );
12971352
12981353 // if the network offering has persistent set to true, implement the network
12991354 if (ntwkOff .getIsPersistent ()) {
@@ -1326,7 +1381,7 @@ private Network commitNetwork(final Long networkOfferingId, final String gateway
13261381 final String vlanId , final Boolean bypassVlanOverlapCheck , final String name , final String displayText , final Account caller , final Long physicalNetworkId , final Long zoneId , final Long domainId ,
13271382 final boolean isDomainSpecific , final Boolean subdomainAccessFinal , final Long vpcId , final String startIPv6 , final String endIPv6 , final String ip6Gateway ,
13281383 final String ip6Cidr , final Boolean displayNetwork , final Long aclId , final String isolatedPvlan , final NetworkOfferingVO ntwkOff , final PhysicalNetwork pNtwk ,
1329- final ACLType aclType , final Account ownerFinal , final String cidr , final boolean createVlan , final String externalId ) throws InsufficientCapacityException , ResourceAllocationException {
1384+ final ACLType aclType , final Account ownerFinal , final String cidr , final boolean createVlan , final String externalId , String routerIp , String routerIpv6 ) throws InsufficientCapacityException , ResourceAllocationException {
13301385 try {
13311386 Network network = Transaction .execute (new TransactionCallbackWithException <Network , Exception >() {
13321387 @ Override
@@ -1381,7 +1436,7 @@ public Network doInTransaction(TransactionStatus status) throws InsufficientCapa
13811436 }
13821437
13831438 network = _networkMgr .createGuestNetwork (networkOfferingId , name , displayText , gateway , cidr , vlanId , bypassVlanOverlapCheck , networkDomain , owner , sharedDomainId , pNtwk , zoneId ,
1384- aclType , subdomainAccess , vpcId , ip6Gateway , ip6Cidr , displayNetwork , isolatedPvlan , externalId );
1439+ aclType , subdomainAccess , vpcId , ip6Gateway , ip6Cidr , displayNetwork , isolatedPvlan , externalId , routerIp , routerIpv6 );
13851440 }
13861441
13871442 if (_accountMgr .isRootAdmin (caller .getId ()) && createVlan && network != null ) {
@@ -4363,7 +4418,7 @@ public Network doInTransaction(TransactionStatus status) throws ResourceAllocati
43634418 if (privateNetwork == null ) {
43644419 //create Guest network
43654420 privateNetwork = _networkMgr .createGuestNetwork (ntwkOffFinal .getId (), networkName , displayText , gateway , cidr , uriString , false , null , owner , null , pNtwk ,
4366- pNtwk .getDataCenterId (), ACLType .Account , null , vpcId , null , null , true , null , null );
4421+ pNtwk .getDataCenterId (), ACLType .Account , null , vpcId , null , null , true , null , null , null , null );
43674422 if (privateNetwork != null ) {
43684423 s_logger .debug ("Successfully created guest network " + privateNetwork );
43694424 } else {
0 commit comments