1717package com .cloud .network ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Arrays ;
2021import java .util .Date ;
2122import java .util .HashMap ;
2223import java .util .HashSet ;
2829
2930import javax .inject .Inject ;
3031
32+ import org .apache .log4j .Logger ;
33+
3134import org .apache .cloudstack .acl .ControlledEntity .ACLType ;
3235import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
3336import org .apache .cloudstack .context .CallContext ;
3942import org .apache .cloudstack .region .PortableIpDao ;
4043import org .apache .cloudstack .region .PortableIpVO ;
4144import org .apache .cloudstack .region .Region ;
42- import org .apache .log4j .Logger ;
4345
4446import com .cloud .agent .AgentManager ;
4547import com .cloud .alert .AlertManager ;
@@ -570,8 +572,9 @@ public boolean disassociatePublicIpAddress(long addrId, long userId, Account cal
570572
571573 IPAddressVO ip = markIpAsUnavailable (addrId );
572574
573- assert (ip != null ) : "Unable to mark the ip address id=" + addrId + " as unavailable." ;
574575 if (ip == null ) {
576+ String msg = "Unable to mark the ip address id=" + addrId + " as unavailable." ;
577+ s_logger .error (msg );
575578 return true ;
576579 }
577580
@@ -684,10 +687,10 @@ public IPAddressVO doInTransaction(TransactionStatus status) throws Insufficient
684687 if (dedicatedVlanDbIds != null && !dedicatedVlanDbIds .isEmpty ()) {
685688 fetchFromDedicatedRange = true ;
686689 sc .setParameters ("vlanId" , dedicatedVlanDbIds .toArray ());
687- errorMessage .append (", vlanId id=" + dedicatedVlanDbIds .toArray ());
690+ errorMessage .append (", vlanId id=" + Arrays . toString ( dedicatedVlanDbIds .toArray () ));
688691 } else if (nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds .isEmpty ()) {
689692 sc .setParameters ("vlanId" , nonDedicatedVlanDbIds .toArray ());
690- errorMessage .append (", vlanId id=" + nonDedicatedVlanDbIds .toArray ());
693+ errorMessage .append (", vlanId id=" + Arrays . toString ( nonDedicatedVlanDbIds .toArray () ));
691694 } else {
692695 if (podId != null ) {
693696 InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException ("Insufficient address capacity" , Pod .class , podId );
@@ -727,7 +730,7 @@ public IPAddressVO doInTransaction(TransactionStatus status) throws Insufficient
727730 if (useSystemIps && nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds .isEmpty ()) {
728731 fetchFromDedicatedRange = false ;
729732 sc .setParameters ("vlanId" , nonDedicatedVlanDbIds .toArray ());
730- errorMessage .append (", vlanId id=" + nonDedicatedVlanDbIds .toArray ());
733+ errorMessage .append (", vlanId id=" + Arrays . toString ( nonDedicatedVlanDbIds .toArray () ));
731734 addrs = _ipAddressDao .lockRows (sc , filter , true );
732735 }
733736 }
@@ -852,6 +855,11 @@ public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network gu
852855 public PublicIp assignDedicateIpAddress (Account owner , final Long guestNtwkId , final Long vpcId , final long dcId , final boolean isSourceNat )
853856 throws ConcurrentOperationException , InsufficientAddressCapacityException {
854857
858+ if (owner == null ) {
859+ s_logger .error ("No account to assign an ip to." );
860+ return null ;
861+ }
862+
855863 final long ownerId = owner .getId ();
856864
857865 PublicIp ip = null ;
@@ -883,13 +891,11 @@ public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAdd
883891
884892 return ip ;
885893 } finally {
886- if (owner != null ) {
887- if (s_logger .isDebugEnabled ()) {
888- s_logger .debug ("Releasing lock account " + ownerId );
889- }
890-
891- _accountDao .releaseFromLockTable (ownerId );
894+ if (s_logger .isDebugEnabled ()) {
895+ s_logger .debug ("Releasing lock account " + ownerId );
892896 }
897+ _accountDao .releaseFromLockTable (ownerId );
898+
893899 if (ip == null ) {
894900 s_logger .error ("Unable to get source nat ip address for account " + ownerId );
895901 }
@@ -1218,7 +1224,7 @@ public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean
12181224
12191225 s_logger .debug ("Associating ip " + ipToAssoc + " to network " + network );
12201226
1221- IPAddressVO ip = _ipAddressDao .findById (ipId );
1227+ IPAddressVO ip = ipToAssoc ; // _ipAddressDao.findById(ipId);
12221228 //update ip address with networkId
12231229 ip .setAssociatedWithNetworkId (networkId );
12241230 ip .setSourceNat (isSourceNat );
@@ -1235,18 +1241,16 @@ public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean
12351241 return ip ;
12361242 } finally {
12371243 if (!success && releaseOnFailure ) {
1238- if (ip != null ) {
1239- try {
1240- s_logger .warn ("Failed to associate ip address, so releasing ip from the database " + ip );
1241- _ipAddressDao .markAsUnavailable (ip .getId ());
1242- if (!applyIpAssociations (network , true )) {
1243- // if fail to apply ip assciations again, unassign ip address without updating resource
1244- // count and generating usage event as there is no need to keep it in the db
1245- _ipAddressDao .unassignIpAddress (ip .getId ());
1246- }
1247- } catch (Exception e ) {
1248- s_logger .warn ("Unable to disassociate ip address for recovery" , e );
1244+ try {
1245+ s_logger .warn ("Failed to associate ip address, so releasing ip from the database " + ip );
1246+ _ipAddressDao .markAsUnavailable (ip .getId ());
1247+ if (!applyIpAssociations (network , true )) {
1248+ // if fail to apply ip assciations again, unassign ip address without updating resource
1249+ // count and generating usage event as there is no need to keep it in the db
1250+ _ipAddressDao .unassignIpAddress (ip .getId ());
12491251 }
1252+ } catch (Exception e ) {
1253+ s_logger .warn ("Unable to disassociate ip address for recovery" , e );
12501254 }
12511255 }
12521256 }
@@ -1327,7 +1331,7 @@ public IPAddressVO disassociatePortableIPToGuestNetwork(long ipId, long networkI
13271331 PublicIp publicIp = PublicIp .createFromAddrAndVlan (ipToAssoc , _vlanDao .findById (ipToAssoc .getVlanId ()));
13281332 ipList .add (publicIp );
13291333 Map <PublicIpAddress , Set <Service >> ipToServices = _networkModel .getIpToServices (ipList , false , true );
1330- if (ipToServices != null & !ipToServices .isEmpty ()) {
1334+ if (ipToServices != null && !ipToServices .isEmpty ()) {
13311335 Set <Service > services = ipToServices .get (publicIp );
13321336 if (services != null && !services .isEmpty ()) {
13331337 throw new InvalidParameterValueException ("IP " + ipToAssoc + " has services and rules associated in the network " + networkId );
@@ -1368,7 +1372,7 @@ public boolean isPortableIpTransferableFromNetwork(long ipAddrId, long networkId
13681372 PublicIp publicIp = PublicIp .createFromAddrAndVlan (ip , _vlanDao .findById (ip .getVlanId ()));
13691373 ipList .add (publicIp );
13701374 Map <PublicIpAddress , Set <Service >> ipToServices = _networkModel .getIpToServices (ipList , false , true );
1371- if (ipToServices != null & !ipToServices .isEmpty ()) {
1375+ if (ipToServices != null && !ipToServices .isEmpty ()) {
13721376 Set <Service > ipServices = ipToServices .get (publicIp );
13731377 if (ipServices != null && !ipServices .isEmpty ()) {
13741378 return false ;
@@ -1671,14 +1675,14 @@ public String acquireGuestIpAddress(Network network, String requestedIp) {
16711675
16721676 @ Override
16731677 public boolean applyStaticNats (List <? extends StaticNat > staticNats , boolean continueOnError , boolean forRevoke ) throws ResourceUnavailableException {
1674- Network network = _networksDao .findById (staticNats .get (0 ).getNetworkId ());
1675- boolean success = true ;
1676-
16771678 if (staticNats == null || staticNats .size () == 0 ) {
16781679 s_logger .debug ("There are no static nat rules for the network elements" );
16791680 return true ;
16801681 }
16811682
1683+ Network network = _networksDao .findById (staticNats .get (0 ).getNetworkId ());
1684+ boolean success = true ;
1685+
16821686 // get the list of public ip's owned by the network
16831687 List <IPAddressVO > userIps = _ipAddressDao .listByAssociatedNetwork (network .getId (), null );
16841688 List <PublicIp > publicIps = new ArrayList <PublicIp >();
0 commit comments