5555import org .apache .cloudstack .api .response .AcquirePodIpCmdResponse ;
5656import org .apache .cloudstack .context .CallContext ;
5757import org .apache .cloudstack .engine .orchestration .service .NetworkOrchestrationService ;
58+ import org .apache .cloudstack .framework .config .ConfigKey ;
59+ import org .apache .cloudstack .framework .config .Configurable ;
5860import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
5961import org .apache .cloudstack .framework .messagebus .MessageBus ;
6062import org .apache .cloudstack .framework .messagebus .PublishScope ;
107109import com .cloud .network .dao .IPAddressDao ;
108110import com .cloud .network .dao .IPAddressVO ;
109111import com .cloud .network .dao .LoadBalancerDao ;
112+ import com .cloud .network .dao .NetworkAccountDao ;
110113import com .cloud .network .dao .NetworkDao ;
111114import com .cloud .network .dao .NetworkDetailVO ;
112115import com .cloud .network .dao .NetworkDetailsDao ;
200203/**
201204 * NetworkServiceImpl implements NetworkService.
202205 */
203- public class NetworkServiceImpl extends ManagerBase implements NetworkService {
206+ public class NetworkServiceImpl extends ManagerBase implements NetworkService , Configurable {
204207 private static final Logger s_logger = Logger .getLogger (NetworkServiceImpl .class );
205208
209+ private static final ConfigKey <Boolean > AllowDuplicateNetworkName = new ConfigKey <Boolean >("Advanced" , Boolean .class ,
210+ "allow.duplicate.networkname" , "true" , "Allow creating networks with same name in account" , true , ConfigKey .Scope .Account );
211+ private static final ConfigKey <Boolean > AllowEmptyStartEndIpAddress = new ConfigKey <Boolean >("Advanced" , Boolean .class ,
212+ "allow.empty.start.end.ipaddress" , "true" , "Allow creating network without mentioning start and end IP address" ,
213+ true , ConfigKey .Scope .Account );
206214 private static final long MIN_VLAN_ID = 0L ;
207215 private static final long MAX_VLAN_ID = 4095L ; // 2^12 - 1
208216 private static final long MIN_GRE_KEY = 0L ;
@@ -313,6 +321,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
313321 VpcOfferingDao _vpcOfferingDao ;
314322 @ Inject
315323 AccountService _accountService ;
324+ @ Inject
325+ NetworkAccountDao _networkAccountDao ;
316326
317327 int _cidrLimit ;
318328 boolean _allowSubdomainNetworkAccess ;
@@ -1164,6 +1174,18 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
11641174 throw new InvalidParameterValueException ("Parameter subDomainAccess can be specified only with aclType=Domain" );
11651175 }
11661176
1177+ if (aclType == ACLType .Domain ) {
1178+ owner = _accountDao .findById (Account .ACCOUNT_ID_SYSTEM );
1179+ }
1180+
1181+ // The network name is unique under the account
1182+ if (!AllowDuplicateNetworkName .valueIn (owner .getAccountId ())) {
1183+ List <NetworkVO > existingNetwork = _networksDao .listByAccountIdNetworkName (owner .getId (), name );
1184+ if (!existingNetwork .isEmpty ()) {
1185+ throw new InvalidParameterValueException ("Another network with same name already exists within account: " + owner .getAccountName ());
1186+ }
1187+ }
1188+
11671189 boolean ipv4 = true , ipv6 = false ;
11681190 if (startIP != null ) {
11691191 ipv4 = true ;
@@ -1188,6 +1210,15 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
11881210 }
11891211 }
11901212
1213+ // Start and end IP address are mandatory for shared networks.
1214+ if (ntwkOff .getGuestType () == GuestType .Shared && vpcId == null ) {
1215+ if (!AllowEmptyStartEndIpAddress .valueIn (owner .getAccountId ()) &&
1216+ (startIP == null && endIP == null ) &&
1217+ (startIPv6 == null && endIPv6 == null )) {
1218+ throw new InvalidParameterValueException ("Either IPv4 or IPv6 start and end address are mandatory" );
1219+ }
1220+ }
1221+
11911222 String cidr = null ;
11921223 if (ipv4 ) {
11931224 // if end ip is not specified, default it to startIp
@@ -4534,4 +4565,14 @@ public boolean releasePodIp(ReleasePodIpCmdByAdmin ip) throws CloudRuntimeExcept
45344565 return true ;
45354566 }
45364567
4568+ @ Override
4569+ public String getConfigComponentName () {
4570+ return NetworkService .class .getSimpleName ();
4571+ }
4572+
4573+ @ Override
4574+ public ConfigKey <?>[] getConfigKeys () {
4575+ return new ConfigKey <?>[] {AllowDuplicateNetworkName , AllowEmptyStartEndIpAddress };
4576+ }
4577+
45374578}
0 commit comments