@@ -192,14 +192,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
192192
193193 private Map <String , String > _configs ;
194194
195- @ DB
196- protected PublicIp fetchNewPublicIp (long dcId , VlanType vlanUse , Account owner , Long networkId , boolean sourceNat ) throws InsufficientAddressCapacityException {
195+ @ Override @ DB
196+ public PublicIp fetchNewPublicIp (long dcId , VlanType vlanUse , Account owner , Long networkId , boolean sourceNat ) throws InsufficientAddressCapacityException {
197197 Transaction txn = Transaction .currentTxn ();
198198 txn .start ();
199199 SearchCriteria <IPAddressVO > sc = AssignIpAddressSearch .create ();
200200 sc .setParameters ("dc" , dcId );
201- sc .setJoinParameters ("vlan" , "type" , vlanUse );
202201
202+ //for direct network take ip addresses only from the vlans belonging to the network
203+ if (vlanUse == VlanType .DirectAttached ) {
204+ sc .setJoinParameters ("vlan" , "networkId" , networkId );
205+ }
206+ sc .setJoinParameters ("vlan" , "type" , vlanUse );
207+
203208 Filter filter = new Filter (IPAddressVO .class , "vlanId" , true , 0l , 1l );
204209
205210 List <IPAddressVO > addrs = _ipAddressDao .lockRows (sc , filter , true );
@@ -214,7 +219,11 @@ protected PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner,
214219 addr .setAllocatedTime (new Date ());
215220 addr .setAllocatedInDomainId (owner .getDomainId ());
216221 addr .setAllocatedToAccountId (owner .getId ());
217- addr .setAssociatedNetworkId (networkId );
222+ addr .setState (IpAddress .State .Allocating );
223+
224+ if (vlanUse == VlanType .DirectAttached ) {
225+ addr .setState (IpAddress .State .Allocated );
226+ }
218227
219228 if (!_ipAddressDao .update (addr .getAddress (), addr )) {
220229 throw new CloudRuntimeException ("Found address to allocate but unable to update: " + addr );
@@ -445,6 +454,7 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
445454 for (IPAddressVO addr : userIps ) {
446455 if (addr .getState () == IpAddress .State .Allocating ) {
447456 addr .setState (IpAddress .State .Allocated );
457+ addr .setAssociatedNetworkId (network .getId ());
448458 _ipAddressDao .update (addr .getAddress (), addr );
449459 } else if (addr .getState () == IpAddress .State .Releasing ) {
450460 _ipAddressDao .unassignIpAddress (addr .getAddress ());
@@ -531,7 +541,6 @@ public IpAddress associateIP(AssociateIPAddrCmd cmd) throws ResourceAllocationEx
531541 _accountMgr .incrementResourceCount (ownerId , ResourceType .public_ip );
532542
533543 String ipAddress = ip .getAddress ();
534-
535544 event .setParameters ("address=" + ipAddress + "\n sourceNat=" + false + "\n dcId=" + zoneId );
536545 event .setDescription ("Assigned a public IP address: " + ipAddress );
537546 _eventDao .persist (event );
@@ -714,6 +723,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
714723 AssignIpAddressSearch .and ("allocated" , AssignIpAddressSearch .entity ().getAllocatedTime (), Op .NULL );
715724 AssignIpAddressSearch .join ("vlan" , vlanSearch , vlanSearch .entity ().getId (), AssignIpAddressSearch .entity ().getVlanId (), JoinType .INNER );
716725 vlanSearch .and ("type" , vlanSearch .entity ().getVlanType (), Op .EQ );
726+ vlanSearch .and ("networkId" , vlanSearch .entity ().getNetworkId (), Op .EQ );
717727 AssignIpAddressSearch .done ();
718728
719729 IpAddressSearch = _ipAddressDao .createSearchBuilder ();
@@ -773,17 +783,21 @@ public List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, N
773783 throw new ConcurrentOperationException ("Unable to acquire lock on " + owner );
774784 }
775785 try {
776- List < NetworkVO > configs = _networksDao . listBy ( owner . getId (), offering . getId (), plan . getDataCenterId ());
786+
777787 if (predefined == null || (predefined .getBroadcastUri () == null && predefined .getBroadcastDomainType () != BroadcastDomainType .Vlan )) {
788+ List <NetworkVO > configs = _networksDao .listBy (owner .getId (), offering .getId (), plan .getDataCenterId ());
778789 if (configs .size () > 0 ) {
779790 if (s_logger .isDebugEnabled ()) {
780791 s_logger .debug ("Found existing network configuration for offering " + offering + ": " + configs .get (0 ));
781792 }
782793 return configs ;
783794 }
795+ } else if (predefined != null && predefined .getBroadcastUri () != null ) {
796+ //don't allow duplicated vlans in the same zone
797+ List <NetworkVO > configs = _networksDao .listBy (owner .getId (), offering .getId (), plan .getDataCenterId ());
784798 }
785799
786- configs = new ArrayList <NetworkVO >();
800+ List < NetworkVO > configs = new ArrayList <NetworkVO >();
787801
788802 long related = -1 ;
789803
@@ -1047,16 +1061,17 @@ public void prepare(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, Dep
10471061 nic .setNetmask (profile .getNetmask ());
10481062 nic .setGateway (profile .getGateway ());
10491063 nic .setAddressFormat (profile .getFormat ());
1050- _nicDao .update (nic .getId (), nic );
1051- for (NetworkElement element : _networkElements ) {
1052- if (s_logger .isDebugEnabled ()) {
1053- s_logger .debug ("Asking " + element .getName () + " to prepare for " + nic );
1054- }
1055- element .prepare (config , profile , vmProfile , dest , context );
1056- }
1064+ _nicDao .update (nic .getId (), nic );
10571065 } else {
10581066 profile = new NicProfile (nic , config , nic .getBroadcastUri (), nic .getIsolationUri ());
10591067 }
1068+
1069+ for (NetworkElement element : _networkElements ) {
1070+ if (s_logger .isDebugEnabled ()) {
1071+ s_logger .debug ("Asking " + element .getName () + " to prepare for " + nic );
1072+ }
1073+ element .prepare (config , profile , vmProfile , dest , context );
1074+ }
10601075
10611076 vmProfile .addNic (profile );
10621077 }
@@ -1587,6 +1602,11 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InvalidParameterValueE
15871602 Boolean isShared = cmd .getIsShared ();
15881603 Account owner = null ;
15891604
1605+ //if end ip is not specified, default it to startIp
1606+ if (endIP == null && startIP != null ) {
1607+ endIP = startIP ;
1608+ }
1609+
15901610 //Check if network offering exists
15911611 NetworkOfferingVO networkOffering = _networkOfferingDao .findById (networkOfferingId );
15921612 if (networkOffering == null || networkOffering .isSystemOnly ()) {
@@ -1617,6 +1637,15 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InvalidParameterValueE
16171637 owner = ctxAccount ;
16181638 }
16191639
1640+ //Don't allow to create network with vlan that already exists in the system
1641+ if (networkOffering .getGuestIpType () == GuestIpType .Direct && vlanId != null ) {
1642+ String uri ="vlan://" + vlanId ;
1643+ List <NetworkVO > networks = _networksDao .listBy (zoneId , uri );
1644+ if ((networks != null && !networks .isEmpty ())) {
1645+ throw new InvalidParameterValueException ("Network with vlan " + vlanId + " already exists in zone " + zoneId );
1646+ }
1647+ }
1648+
16201649 //if VlanId is Direct untagged, verify if there is already network of this type in the zone
16211650 if (networkOffering .getGuestIpType () == GuestIpType .DirectPodBased && vlanId != null && vlanId .equalsIgnoreCase (Vlan .UNTAGGED )) {
16221651 SearchBuilder <NetworkVO > sb = _networksDao .createSearchBuilder ();
0 commit comments